@@ -28,29 +28,29 @@ def _plugin(config=mock_plugin_config, **kwargs):
2828@pytest .mark .parametrize (
2929 "attributes, prefix, string_to_match, match_group1, match_group3, should_match" ,
3030 [
31+ (
32+ ["data" ],
33+ "site:" ,
34+ "data= 'site:image.png'" ,
35+ "data" ,
36+ "image.png" ,
37+ True ,
38+ ), # valid1
3139 (
3240 ["href" , "src" ],
3341 "/docs/" ,
3442 'src = "/docs/image.png"' ,
3543 "src" ,
3644 "image.png" ,
3745 True ,
38- ), # valid1
46+ ), # valid2
3947 (
4048 ["href" , "src" ],
4149 "/" ,
4250 "href ='/docs/image.png'" ,
4351 "href" ,
4452 "docs/image.png" ,
4553 True ,
46- ), # valid2
47- (
48- ["data" ],
49- "site:" ,
50- "data= 'site:image.png'" ,
51- "data" ,
52- "image.png" ,
53- True ,
5454 ), # valid3
5555 (
5656 ["href" , "src" ],
@@ -117,38 +117,58 @@ def test_on_config_sets_regex(
117117
118118
119119@pytest .mark .parametrize (
120- "site_url" ,
120+ "prefix, site_url, expected_path " ,
121121 [
122- "https://example.com/docs/subpage/" ,
123- "https://example.com/docs/subpage" ,
122+ ("site:" , "https://example.com" , "/" ),
123+ ("site:" , "https://example.com/docs/subpage/" , "/docs/subpage/" ),
124+ ("site:" , "https://example.com/docs/subpage" , "/docs/subpage/" ),
125+ ("site:" , None , "/" ),
126+ ("site:" , "" , "/" ),
127+ ("prefix" , None , "/" ),
128+ ("/" , "https://example.com/docs/subpage/" , "/docs/subpage/" ),
129+ ],
130+ ids = [
131+ "basic" ,
132+ "path_trailing_slash" ,
133+ "path_no_trailing_slash" ,
134+ "none_siteurl" ,
135+ "empty_siteurl" ,
136+ "custom_prefix" ,
137+ "slash_as_prefix" ,
124138 ],
125- ids = ["trailing_slash" , "no_trailing_slash" ],
126139)
127- def test_on_page_content (create_plugin , site_url ):
140+ def test_on_page_content (create_plugin , prefix , site_url , expected_path ):
128141 """Test the on_page_content method of the SiteUrlsPlugin."""
129142 plugin = create_plugin (
130143 {
131- "attributes" : ["src" , "data" ],
132- "prefix" : " prefix" ,
144+ "attributes" : ["src" , "data" , "href" ],
145+ "prefix" : prefix ,
133146 }
134147 )
135148 page = MagicMock ()
136149 config = MagicMock ()
137- config .__getitem__ .side_effect = lambda key : site_url if key == "site_url" else None
150+ config .get .side_effect = (
151+ lambda key , default = None : site_url
152+ if key == "site_url" and site_url is not None
153+ else default
154+ )
138155 files = MagicMock ()
139- html = """
140- <img src ="prefixexample.png" alt="Image">
141- <img data = \' prefix/image.png\' >
142- <img data="site:docs/image.svg" class="example">
143- <img attr ="prefix/docs/image.png" >
156+
157+ html = f"""
158+ <img src ="{ prefix } example.png" alt="Image">
159+ <img data = \' { prefix } /image.png\' >
160+ <img data="{ prefix } docs/image.svg" class="example">
161+ <img attr ="{ prefix } /docs/image.png" title="this won't get touched, wrong attribute" >
162+ <img href = "NOT_A_PREFIX/foo/image.png" title="neither will this, wrong prefix">
144163 """
145164
146165 plugin .on_config (config )
147166 result = plugin .on_page_content (html , page , config , files )
148- expected_result = """
149- <img src="/docs/subpage/example.png" alt="Image">
150- <img data="/docs/subpage//image.png">
151- <img data="site:docs/image.svg" class="example">
152- <img attr ="prefix/docs/image.png" >
167+ expected_result = f"""
168+ <img src="{ expected_path } example.png" alt="Image">
169+ <img data="{ expected_path } /image.png">
170+ <img data="{ expected_path } docs/image.svg" class="example">
171+ <img attr ="{ prefix } /docs/image.png" title="this won't get touched, wrong attribute" >
172+ <img href = "NOT_A_PREFIX/foo/image.png" title="neither will this, wrong prefix">
153173 """
154174 assert result == expected_result
0 commit comments