Skip to content

Commit

Permalink
Update to release v2.0.2
Browse files Browse the repository at this point in the history
  - Fix for QT3 test case 'K2-MatchesFunc-16a': pattern "[0-9-.]*/"
    is considered invalid for XSD 1.0 and valid for XSD 1.1.
  • Loading branch information
brunato committed Sep 3, 2020
1 parent 44a2eb0 commit 1b4f690
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
8 changes: 8 additions & 0 deletions elementpath/regex/patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from .character_classes import I_SHORTCUT_REPLACE, C_SHORTCUT_REPLACE, CharacterClass

HYPHENS_PATTERN = re.compile(r'(?<!\\)--')
INVALID_HYPHEN_PATTERN = re.compile(r'[^\\]-[^\\]-[^\\]')
DIGITS_PATTERN = re.compile(r'\d+')
QUANTIFIER_PATTERN = re.compile(r'{\d+(,(\d+)?)?}')
FORBIDDEN_ESCAPES_NOREF_PATTERN = re.compile(
Expand Down Expand Up @@ -73,6 +74,13 @@ def parse_character_class():
msg = "invalid character range '--' at position {}: {!r}"
raise RegexError(msg.format(pos, pattern))

if xsd_version == '1.0':
hyphen_match = INVALID_HYPHEN_PATTERN.search(char_class_pattern)
if hyphen_match is not None:
hyphen_pos = char_class_pos + hyphen_match.span()[1] - 2
msg = "unescaped character '-' at position {}: {!r}"
raise RegexError(msg.format(hyphen_pos, pattern))

char_class = CharacterClass(char_class_pattern, xsd_version)
if negative:
char_class.complement()
Expand Down
1 change: 0 additions & 1 deletion tests/execute_w3c_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@

# IMHO incorrect tests
'fn-resolve-uri__fn-resolve-uri-9', # URI scheme names are lowercase
'fn-matches__K2-MatchesFunc-16a', # "[0-9-.]*/" is valid or dozens of W3C XSD tests fail
]


Expand Down
10 changes: 10 additions & 0 deletions tests/test_regex.py
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,16 @@ def test_invalid_quantifiers(self):
translate_pattern('*')
self.assertIn("unexpected quantifier '*'", str(ctx.exception))

def test_invalid_hyphen(self):
with self.assertRaises(RegexError) as ctx:
translate_pattern('[a-b-c]')
self.assertIn("unescaped character '-' at position 4", str(ctx.exception))

regex = translate_pattern('[a-b-c]', xsd_version='1.1')
self.assertEqual(regex, '[\\-a-c]')
self.assertEqual(translate_pattern('[-a-bc]'), regex)
self.assertEqual(translate_pattern('[a-bc-]'), regex)

def test_invalid_pattern_groups(self):
with self.assertRaises(RegexError) as ctx:
translate_pattern('(?:.*)')
Expand Down

0 comments on commit 1b4f690

Please sign in to comment.