Commit f4adb11f authored by di68kap's avatar di68kap
Browse files

- DHParser/ has_content: rules for matching content changed:...

- DHParser/ has_content: rules for matching content changed: regexp must not match the complete content, nur just the beginning
parent 28abee18
......@@ -411,7 +411,14 @@ def is_one_of(context: List[Node], tag_name_set: AbstractSet[str]) -> bool:
def has_content(context: List[Node], regexp: str) -> bool:
"""Checks a node's content against a regular expression."""
Checks a node's content against a regular expression.
In contrast to ``re.match`` the regular expression must match the complete
string and not just the beginning of the string to succeed!
if not regexp.endswith('$'):
regexp += "$"
return bool(re.match(regexp, context[-1].content))
......@@ -26,7 +26,7 @@ sys.path.extend(['../', './'])
from DHParser.syntaxtree import mock_syntax_tree
from DHParser.transform import traverse, reduce_single_child, remove_whitespace, \
traverse_locally, collapse, lstrip, rstrip
traverse_locally, collapse, lstrip, rstrip, remove_content
class TestRemoval:
......@@ -71,6 +71,14 @@ class TestRemoval:
assert cst.as_sxpr().find(":Whitespace") < 0, cst.as_sxpr()
def test_remove_content(self):
cst = mock_syntax_tree('(BelegLemma (:Series (:RegExp "#") (LAT_WORT (:RegExp "facitergula"))))')
remove_content([cst], '#')
assert cst.content == "#facitergula", str(cst.content)
remove_content([cst], '#')
assert cst.content == "facitergula"
class TestConditionalTransformations:
"""Tests conditional transformations."""
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment