瀏覽代碼

use less restrictive rules for XML tag names

Falko Schindler 1 年之前
父節點
當前提交
613925b0e0
共有 2 個文件被更改,包括 7 次插入4 次删除
  1. 4 1
      nicegui/element.py
  2. 3 3
      tests/test_element.py

+ 4 - 1
nicegui/element.py

@@ -46,7 +46,10 @@ PROPS_PATTERN = re.compile(r'''
 (?:$|\s)            # Match end of string or whitespace
 ''', re.VERBOSE)
 
-TAG_PATTERN = re.compile(r'^[a-z][a-z0-9\-]*$', re.IGNORECASE)
+# https://www.w3.org/TR/xml/#sec-common-syn
+TAG_START_CHAR = r':|[A-Z]|_|[a-z]|[\u00C0-\u00D6]|[\u00D8-\u00F6]|[\u00F8-\u02FF]|[\u0370-\u037D]|[\u037F-\u1FFF]|[\u200C-\u200D]|[\u2070-\u218F]|[\u2C00-\u2FEF]|[\u3001-\uD7FF]|[\uF900-\uFDCF]|[\uFDF0-\uFFFD]|[\U00010000-\U000EFFFF]'
+TAG_CHAR = TAG_START_CHAR + r'|-|\.|[0-9]|\u00B7|[\u0300-\u036F]|[\u203F-\u2040]'
+TAG_PATTERN = re.compile(fr'^({TAG_START_CHAR})({TAG_CHAR})*$')
 
 
 class Element(Visibility):

+ 3 - 3
tests/test_element.py

@@ -266,12 +266,12 @@ def test_default_style():
 
 
 def test_invalid_tags(screen: Screen):
-    good_tags = ['div', 'span', 'a', 'div1', 'div-', 'DIV']
-    bad_tags = ['1div', 'di v', '-div', '_div']
+    good_tags = ['div', 'div-1', 'DIV', 'däv', 'div_x', '🙂']
+    bad_tags = ['<div>', 'hi hi', 'hi/ho', 'foo$bar']
     for tag in good_tags:
         ui.element(tag)
     for tag in bad_tags:
-        with pytest.raises(ValueError, match=f'Invalid HTML tag: {tag}'):
+        with pytest.raises(ValueError):
             ui.element(tag)
 
     screen.open('/')