Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions abp/filters/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ def _line_type(name, field_names, format_string):
Header = _line_type("Header", "version", "[{.version}]")
EmptyLine = _line_type("EmptyLine", "", "")
Comment = _line_type("Comment", "text", "! {.text}")
Preprocessor = _line_type("Preprocessor", "text", "!#{.text}")
Metadata = _line_type("Metadata", "key value", "! {0.key}: {0.value}")
Filter = _line_type("Filter", "text selector action options", "{.text}")
Include = _line_type("Include", "target", "%include {0.target}%")
Expand Down Expand Up @@ -414,6 +415,11 @@ def parse_line(line, position="body"):
if match:
return Header(match.group(1))

if stripped.startswith("!#"):
directive = stripped[2:]
if directive and not directive[:1].isspace():
return Preprocessor(directive)

if stripped.startswith("!"):
match = METADATA_REGEXP.match(line)
if match:
Expand Down
24 changes: 24 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,30 @@ def test_parse_filters(filter_text, expected):
assert getattr(parsed, attribute) == expected_value


@pytest.mark.parametrize("text", [
"!#if (foo || bar)",
"!#else",
"!#endif",
"!#include my_filter.txt",
])
def test_parse_preprocessor_directive(text):
line = parse_line(text)
assert line.type == "preprocessor"
assert line.to_string() == text


def test_parse_bad_preprocessor_directive():
line = parse_line("! #if bar")
assert line.type == "comment"
assert line.to_string() == "! #if bar"


def test_parse_bare_directive_as_comment():
line = parse_line("!#")
assert line.type == "comment"
assert line.to_string() == "! #"


def test_parse_comment():
line = parse_line("! Block foo")
assert line.type == "comment"
Expand Down
9 changes: 9 additions & 0 deletions tests/test_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,12 @@ def test_remove_checksum(head):
src = MockSource(fl="[Adblock]\n! Comment\n! Checksum: foo")
got = render_str("fl", {}, src)
assert got == head + "! Comment"


def test_preprocessor_directives(head):
src = MockSource(
fl="[Adblock]\n%include fragment%",
fragment="!#if foo\n||example.com^\n!#endif",
)
got = render_str("fl", {}, src)
assert "!#if foo\n||example.com^\n!#endif" in got