Skip to content

Commit af5789d

Browse files
committed
adblocker: improve coverage.
This patch fixes #422. Indeed, before this patch, the following case wasn't handled properly: example.com##a[href=https://example.org][target=_blank] Contributors: * @ryanbr
1 parent b0a32e3 commit af5789d

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

PyFunceble/converter/adblock_input_line2subject.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ def extract_base(subject: Union[str, List[str]]) -> Union[str, List[str]]:
123123
Giving :code:`"hello.world/?is=beautiful"` returns :code:`"hello.world"`
124124
"""
125125

126-
subject = subject.replace("*", "").replace("~", "")
126+
subject = (
127+
subject.replace("*", "").replace("~", "").replace('"', "").replace("'", "")
128+
)
127129

128130
try:
129131
return Url2Netloc(subject).get_converted()
@@ -175,11 +177,12 @@ def _decode_options(self, decoded_options: List[str]) -> Set[str]:
175177

176178
if "href" in rule:
177179
matched = self._regex_helper.set_regex(
178-
r"((?:\"|\')(.*)(?:\"|\'))"
179-
).match(rule, return_match=True, rematch=True, group=1)
180+
r"((?:\"|\')(.*?)(?:\"|\'))"
181+
).match(rule, return_match=True, rematch=True)
180182

181183
if matched:
182-
result.add(self.extract_base(matched))
184+
result.update(self.extract_base(x) for x in matched)
185+
183186
continue
184187

185188
return result

tests/converter/test_adblock_input_line2subject.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,18 @@ class TestAdblockInputLine2Subject(unittest.TestCase):
274274
"aggressive": ["example.com", "example.net", "example.org"],
275275
},
276276
},
277+
{
278+
"subject": 'example.com##a[href="https://example.org"][target="_blank"]',
279+
"expected": {"aggressive": ["example.com", "example.org"], "standard": []},
280+
},
281+
{
282+
# unlikely, but we should be able to handle such weird cases.
283+
"subject": 'example.com##a[href="https://example.org"][target="https://example.net"]',
284+
"expected": {
285+
"aggressive": ["example.com", "example.net", "example.org"],
286+
"standard": [],
287+
},
288+
},
277289
]
278290

279291
def setUp(self) -> None:

0 commit comments

Comments
 (0)