kgcss 1.2.0__tar.gz → 1.2.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (122) hide show
  1. {kgcss-1.2.0/src/kgcss.egg-info → kgcss-1.2.1}/PKG-INFO +1 -1
  2. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/__init__.py +2 -0
  3. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/_version.py +1 -1
  4. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/compat.py +25 -1
  5. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/cssom/__init__.py +2 -0
  6. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/cssom/rules.py +12 -1
  7. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/cssom/selector_list.py +21 -6
  8. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/cssom/values.py +19 -1
  9. {kgcss-1.2.0 → kgcss-1.2.1/src/kgcss.egg-info}/PKG-INFO +1 -1
  10. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_compat.py +57 -0
  11. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_cssom_mutation.py +11 -1
  12. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_cssom_values.py +25 -0
  13. {kgcss-1.2.0 → kgcss-1.2.1}/LICENSE +0 -0
  14. {kgcss-1.2.0 → kgcss-1.2.1}/MANIFEST.in +0 -0
  15. {kgcss-1.2.0 → kgcss-1.2.1}/README.md +0 -0
  16. {kgcss-1.2.0 → kgcss-1.2.1}/csrc/color.c +0 -0
  17. {kgcss-1.2.0 → kgcss-1.2.1}/csrc/kgcss.h +0 -0
  18. {kgcss-1.2.0 → kgcss-1.2.1}/csrc/module.c +0 -0
  19. {kgcss-1.2.0 → kgcss-1.2.1}/csrc/objects.h +0 -0
  20. {kgcss-1.2.0 → kgcss-1.2.1}/csrc/properties.c +0 -0
  21. {kgcss-1.2.0 → kgcss-1.2.1}/csrc/select.c +0 -0
  22. {kgcss-1.2.0 → kgcss-1.2.1}/csrc/selectors.c +0 -0
  23. {kgcss-1.2.0 → kgcss-1.2.1}/csrc/selectors.h +0 -0
  24. {kgcss-1.2.0 → kgcss-1.2.1}/csrc/serialize.c +0 -0
  25. {kgcss-1.2.0 → kgcss-1.2.1}/csrc/syntax.c +0 -0
  26. {kgcss-1.2.0 → kgcss-1.2.1}/csrc/tokenizer.c +0 -0
  27. {kgcss-1.2.0 → kgcss-1.2.1}/csrc/tokenizer_impl.h +0 -0
  28. {kgcss-1.2.0 → kgcss-1.2.1}/csrc/tokens.h +0 -0
  29. {kgcss-1.2.0 → kgcss-1.2.1}/csrc/types.c +0 -0
  30. {kgcss-1.2.0 → kgcss-1.2.1}/csrc/urls.c +0 -0
  31. {kgcss-1.2.0 → kgcss-1.2.1}/csrc/values.c +0 -0
  32. {kgcss-1.2.0 → kgcss-1.2.1}/csrc/writer.c +0 -0
  33. {kgcss-1.2.0 → kgcss-1.2.1}/pyproject.toml +0 -0
  34. {kgcss-1.2.0 → kgcss-1.2.1}/setup.cfg +0 -0
  35. {kgcss-1.2.0 → kgcss-1.2.1}/setup.py +0 -0
  36. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/_encodings.py +0 -0
  37. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/_kgcss.pyi +0 -0
  38. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/colors.py +0 -0
  39. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/cssom/build.py +0 -0
  40. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/cssom/declarations.py +0 -0
  41. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/cssom/exceptions.py +0 -0
  42. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/cssom/imports.py +0 -0
  43. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/cssom/serializer.py +0 -0
  44. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/cssom/state.py +0 -0
  45. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/cssom/stylesheet.py +0 -0
  46. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/cssom/urls.py +0 -0
  47. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/decode.py +0 -0
  48. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/enums.py +0 -0
  49. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/escaping.py +0 -0
  50. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/fonts.py +0 -0
  51. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/grammar.py +0 -0
  52. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/matching.py +0 -0
  53. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/media.py +0 -0
  54. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/py.typed +0 -0
  55. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/selectors.py +0 -0
  56. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/shorthands.py +0 -0
  57. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/supports.py +0 -0
  58. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/syntax.py +0 -0
  59. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss/tokenizer.py +0 -0
  60. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss.egg-info/SOURCES.txt +0 -0
  61. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss.egg-info/dependency_links.txt +0 -0
  62. {kgcss-1.2.0 → kgcss-1.2.1}/src/kgcss.egg-info/top_level.txt +0 -0
  63. {kgcss-1.2.0 → kgcss-1.2.1}/tests/__init__.py +0 -0
  64. {kgcss-1.2.0 → kgcss-1.2.1}/tests/conftest.py +0 -0
  65. {kgcss-1.2.0 → kgcss-1.2.1}/tests/css-parsing-tests/An+B.json +0 -0
  66. {kgcss-1.2.0 → kgcss-1.2.1}/tests/css-parsing-tests/LICENSE +0 -0
  67. {kgcss-1.2.0 → kgcss-1.2.1}/tests/css-parsing-tests/README.rst +0 -0
  68. {kgcss-1.2.0 → kgcss-1.2.1}/tests/css-parsing-tests/VENDORED +0 -0
  69. {kgcss-1.2.0 → kgcss-1.2.1}/tests/css-parsing-tests/blocks_contents.json +0 -0
  70. {kgcss-1.2.0 → kgcss-1.2.1}/tests/css-parsing-tests/color_function_4.json +0 -0
  71. {kgcss-1.2.0 → kgcss-1.2.1}/tests/css-parsing-tests/color_functions_5.json +0 -0
  72. {kgcss-1.2.0 → kgcss-1.2.1}/tests/css-parsing-tests/color_hexadecimal_3.json +0 -0
  73. {kgcss-1.2.0 → kgcss-1.2.1}/tests/css-parsing-tests/color_hexadecimal_4.json +0 -0
  74. {kgcss-1.2.0 → kgcss-1.2.1}/tests/css-parsing-tests/color_hsl_3.json +0 -0
  75. {kgcss-1.2.0 → kgcss-1.2.1}/tests/css-parsing-tests/color_hsl_4.json +0 -0
  76. {kgcss-1.2.0 → kgcss-1.2.1}/tests/css-parsing-tests/color_hwb_4.json +0 -0
  77. {kgcss-1.2.0 → kgcss-1.2.1}/tests/css-parsing-tests/color_keywords_3.json +0 -0
  78. {kgcss-1.2.0 → kgcss-1.2.1}/tests/css-parsing-tests/color_keywords_4.json +0 -0
  79. {kgcss-1.2.0 → kgcss-1.2.1}/tests/css-parsing-tests/color_lab_4.json +0 -0
  80. {kgcss-1.2.0 → kgcss-1.2.1}/tests/css-parsing-tests/color_lch_4.json +0 -0
  81. {kgcss-1.2.0 → kgcss-1.2.1}/tests/css-parsing-tests/color_oklab_4.json +0 -0
  82. {kgcss-1.2.0 → kgcss-1.2.1}/tests/css-parsing-tests/color_oklch_4.json +0 -0
  83. {kgcss-1.2.0 → kgcss-1.2.1}/tests/css-parsing-tests/component_value_list.json +0 -0
  84. {kgcss-1.2.0 → kgcss-1.2.1}/tests/css-parsing-tests/declaration_list.json +0 -0
  85. {kgcss-1.2.0 → kgcss-1.2.1}/tests/css-parsing-tests/one_component_value.json +0 -0
  86. {kgcss-1.2.0 → kgcss-1.2.1}/tests/css-parsing-tests/one_declaration.json +0 -0
  87. {kgcss-1.2.0 → kgcss-1.2.1}/tests/css-parsing-tests/one_rule.json +0 -0
  88. {kgcss-1.2.0 → kgcss-1.2.1}/tests/css-parsing-tests/rule_list.json +0 -0
  89. {kgcss-1.2.0 → kgcss-1.2.1}/tests/css-parsing-tests/stylesheet.json +0 -0
  90. {kgcss-1.2.0 → kgcss-1.2.1}/tests/css-parsing-tests/stylesheet_bytes.json +0 -0
  91. {kgcss-1.2.0 → kgcss-1.2.1}/tests/data/css_selectors_shakespeare.html +0 -0
  92. {kgcss-1.2.0 → kgcss-1.2.1}/tests/selector_oracle.py +0 -0
  93. {kgcss-1.2.0 → kgcss-1.2.1}/tests/strategies.py +0 -0
  94. {kgcss-1.2.0 → kgcss-1.2.1}/tests/suite.py +0 -0
  95. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_api.py +0 -0
  96. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_colors.py +0 -0
  97. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_css_parsing_suite.py +0 -0
  98. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_cssom_declarations.py +0 -0
  99. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_cssom_imports.py +0 -0
  100. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_cssom_properties.py +0 -0
  101. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_cssom_rules.py +0 -0
  102. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_cssom_serialize.py +0 -0
  103. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_cssom_urls.py +0 -0
  104. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_decode.py +0 -0
  105. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_fonts.py +0 -0
  106. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_grammar.py +0 -0
  107. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_media.py +0 -0
  108. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_native.py +0 -0
  109. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_properties.py +0 -0
  110. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_readme.py +0 -0
  111. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_safety.py +0 -0
  112. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_select.py +0 -0
  113. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_select_vs_css_selectors.py +0 -0
  114. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_selectors.py +0 -0
  115. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_serialize.py +0 -0
  116. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_shorthands.py +0 -0
  117. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_supports.py +0 -0
  118. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_syntax.py +0 -0
  119. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_threads.py +0 -0
  120. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_tokenizer.py +0 -0
  121. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_values.py +0 -0
  122. {kgcss-1.2.0 → kgcss-1.2.1}/tests/test_writer.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: kgcss
3
- Version: 1.2.0
3
+ Version: 1.2.1
4
4
  Summary: Fast and comprehensive CSS handling: tokenizing, parsing, a mutable object model, serialization, selector matching, media queries, colors, fonts and shorthands, with a C99 backend
5
5
  Author-email: Kovid Goyal <kovid@kovidgoyal.net>
6
6
  License-Expression: BSD-3-Clause
@@ -39,6 +39,7 @@ from kgcss.cssom import (
39
39
  Property,
40
40
  PropertyValue,
41
41
  Rule,
42
+ RuleContainer,
42
43
  RuleList,
43
44
  RuleType,
44
45
  ScopeRule,
@@ -121,6 +122,7 @@ __all__ = [
121
122
  'Property',
122
123
  'PropertyValue',
123
124
  'Rule',
125
+ 'RuleContainer',
124
126
  'RuleList',
125
127
  'RuleType',
126
128
  'ScopeRule',
@@ -1,3 +1,3 @@
1
1
  # License: BSD-3-Clause Copyright: 2026, Kovid Goyal <kovid at kovidgoyal.net>
2
2
 
3
- __version__ = '1.2.0'
3
+ __version__ = '1.2.1'
@@ -832,6 +832,28 @@ class CSSStyleDeclaration(_View[DeclarationBlock]):
832
832
  @overload
833
833
  def wrap(obj: Stylesheet) -> CSSStyleSheet: ...
834
834
  @overload
835
+ def wrap(obj: StyleRule) -> CSSStyleRule: ...
836
+ @overload
837
+ def wrap(obj: MediaRule) -> CSSMediaRule: ...
838
+ @overload
839
+ def wrap(obj: PageRule) -> CSSPageRule: ...
840
+ @overload
841
+ def wrap(obj: ImportRule) -> CSSImportRule: ...
842
+ @overload
843
+ def wrap(obj: FontFaceRule) -> CSSFontFaceRule: ...
844
+ @overload
845
+ def wrap(obj: MarginRule) -> CSSMarginRule: ...
846
+ @overload
847
+ def wrap(obj: CharsetRule) -> CSSCharsetRule: ...
848
+ @overload
849
+ def wrap(obj: NamespaceRule) -> CSSNamespaceRule: ...
850
+ @overload
851
+ def wrap(obj: Comment) -> CSSComment: ...
852
+ @overload
853
+ def wrap(obj: UnknownAtRule) -> CSSUnknownRule: ...
854
+ @overload
855
+ def wrap(obj: RuleContainer) -> CSSGroupingRule: ...
856
+ @overload
835
857
  def wrap(obj: Rule) -> CSSRule: ...
836
858
  @overload
837
859
  def wrap(obj: DeclarationBlock) -> CSSStyleDeclaration: ...
@@ -840,7 +862,9 @@ def wrap(obj: KProperty) -> Property: ...
840
862
  def wrap(
841
863
  obj: Stylesheet | Rule | DeclarationBlock | KProperty,
842
864
  ) -> CSSStyleSheet | CSSRule | CSSStyleDeclaration | Property:
843
- """The css_parser-style view of a kgcss object."""
865
+ """The css_parser-style view of a kgcss object: a CSSStyleSheet for a Stylesheet, a CSSStyleDeclaration
866
+ for a DeclarationBlock, a Property for a Property, and for a rule the view of its class (a CSSStyleRule for
867
+ a StyleRule, ...). The overloads give these types to type checkers too."""
844
868
  match obj:
845
869
  case Stylesheet():
846
870
  return CSSStyleSheet(obj)
@@ -32,6 +32,7 @@ from kgcss.cssom.rules import (
32
32
  NestedDeclarationsRule,
33
33
  PageRule,
34
34
  Rule,
35
+ RuleContainer,
35
36
  RuleList,
36
37
  RuleType,
37
38
  ScopeRule,
@@ -75,6 +76,7 @@ __all__ = [
75
76
  'Property',
76
77
  'PropertyValue',
77
78
  'Rule',
79
+ 'RuleContainer',
78
80
  'RuleList',
79
81
  'RuleType',
80
82
  'ScopeRule',
@@ -415,11 +415,22 @@ def delete_from(rules: RuleList, rule_or_index: Rule | int) -> None:
415
415
 
416
416
 
417
417
  class RuleContainer(Rule):
418
- """Base of rules that contain rules."""
418
+ """Base of rules that contain rules: style rules (which can have nested rules), @page and the grouping
419
+ rules. The parent_rule of a rule in a list of rules is one of these, or None at the top level of a
420
+ sheet, so this removes such a rule from whatever contains it::
421
+
422
+ parent = rule.parent_rule or rule.parent_style_sheet
423
+ if isinstance(parent, RuleContainer | Stylesheet):
424
+ parent.delete_rule(rule)
425
+
426
+ (A comment in a declaration block has the rule of the block as its parent_rule, but it is not
427
+ one of that rule's child_rules(): remove it from the block.)
428
+ """
419
429
 
420
430
  __slots__ = ()
421
431
 
422
432
  def child_rules(self) -> RuleList:
433
+ """The rules this rule contains."""
423
434
  raise NotImplementedError
424
435
 
425
436
  def insert_rule(self, rule: Rule | str, index: int | None = None) -> Rule:
@@ -72,23 +72,26 @@ def validate_selector_list(text: str, nested: bool) -> list[str]:
72
72
  class Selector:
73
73
  """One selector of a selector list.
74
74
 
75
- Callers can attach their own attributes (for example calibre's
76
- selector.calibre_used), which are kept by copy(). The fixed fields are
77
- slots and __dict__ is one too, so it is only created when a caller sets
78
- an attribute: there can be tens of thousands of selectors.
75
+ Callers can keep their own data in the dict data (for example
76
+ selector.data['used'] = True), which copy() copies. Selectors compare and
77
+ hash by identity, so they can also be the keys of a dict of the caller's.
78
+ Attributes of the caller's own can be set too (they are kept by copy()),
79
+ but type checkers reject them. data and __dict__ are only created when
80
+ they are first used: there can be tens of thousands of selectors.
79
81
 
80
82
  The text is parsed when the tree (parsed), specificity or pseudo_element
81
83
  are first needed. A selector of a parsed stylesheet that is not valid
82
84
  raises SelectorSyntaxError then; text that is assigned is checked at once.
83
85
  """
84
86
 
85
- __slots__ = ('__dict__', '_compiled', '_owner', '_text', '_tree')
87
+ __slots__ = ('__dict__', '_compiled', '_data', '_owner', '_text', '_tree')
86
88
 
87
89
  def __init__(self, text: str) -> None:
88
90
  parts = validate_selector_list(text, True)
89
91
  if len(parts) != 1:
90
92
  raise SelectorSyntaxError(f'Not a single selector: {text!r}', text, 0)
91
93
  self._text = parts[0]
94
+ self._data: dict[str, object] | None = None
92
95
  self._owner: SelectorList | None = None
93
96
  self._compiled: tuple[bool, CompiledSelector] | None = None
94
97
  self._tree: tuple[CompiledSelector, ComplexSelector] | None = None
@@ -97,6 +100,7 @@ class Selector:
97
100
  def _from_normalized(cls, text: str, compiled: CompiledSelector | None = None, nested: bool = True) -> Selector:
98
101
  s = cls.__new__(cls)
99
102
  s._text = text
103
+ s._data = None
100
104
  s._owner = None
101
105
  s._compiled = None if compiled is None else (nested, compiled)
102
106
  s._tree = None
@@ -106,6 +110,15 @@ class Selector:
106
110
  def text(self) -> str:
107
111
  return self._text
108
112
 
113
+ @property
114
+ def data(self) -> dict[str, object]:
115
+ """A dict for the caller's own data about this selector, created when first used. copy() copies it
116
+ (a shallow copy). kgcss never reads or writes it."""
117
+ data = self._data
118
+ if data is None:
119
+ self._data = data = {}
120
+ return data
121
+
109
122
  @text.setter
110
123
  def text(self, value: str) -> None:
111
124
  parts = validate_selector_list(value, self._is_nested())
@@ -251,9 +264,11 @@ class Selector:
251
264
  return self.parsed.walk()
252
265
 
253
266
  def copy(self) -> Selector:
254
- """An unattached copy, with any attributes attached by callers."""
267
+ """An unattached copy, with a copy of data and any attributes attached by callers."""
255
268
  s = Selector._from_normalized(self._text)
256
269
  s._compiled, s._tree = self._compiled, self._tree
270
+ if self._data:
271
+ s._data = self._data.copy()
257
272
  if self.__dict__:
258
273
  s.__dict__.update(self.__dict__)
259
274
  return s
@@ -24,17 +24,31 @@ from kgcss.tokenizer import TokenType
24
24
 
25
25
  @dataclass(frozen=True, slots=True)
26
26
  class Component:
27
- """Base of all value components. css_text is the exact source text."""
27
+ """Base of all value components. css_text is the exact source text.
28
+
29
+ Each subclass has its own typed value. string_value is the value of an
30
+ Ident or a String (the keyword, or the unquoted string) and None for any
31
+ other component, so that code can read it without checking the class.
32
+ """
28
33
 
29
34
  css_text: str
30
35
  type: ClassVar[str] = ''
31
36
 
37
+ @property
38
+ def string_value(self) -> str | None:
39
+ """The keyword of an Ident or the unquoted, unescaped text of a String, else None."""
40
+ return None
41
+
32
42
 
33
43
  @dataclass(frozen=True, slots=True)
34
44
  class Ident(Component):
35
45
  value: str
36
46
  type: ClassVar[str] = 'ident'
37
47
 
48
+ @property
49
+ def string_value(self) -> str:
50
+ return self.value
51
+
38
52
 
39
53
  @dataclass(frozen=True, slots=True)
40
54
  class String(Component):
@@ -43,6 +57,10 @@ class String(Component):
43
57
  value: str
44
58
  type: ClassVar[str] = 'string'
45
59
 
60
+ @property
61
+ def string_value(self) -> str:
62
+ return self.value
63
+
46
64
 
47
65
  @dataclass(frozen=True, slots=True)
48
66
  class Number(Component):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: kgcss
3
- Version: 1.2.0
3
+ Version: 1.2.1
4
4
  Summary: Fast and comprehensive CSS handling: tokenizing, parsing, a mutable object model, serialization, selector matching, media queries, colors, fonts and shorthands, with a C99 backend
5
5
  Author-email: Kovid Goyal <kovid@kovidgoyal.net>
6
6
  License-Expression: BSD-3-Clause
@@ -185,6 +185,63 @@ def test_views_are_live() -> None:
185
185
  wrap('a') # ty: ignore[no-matching-overload]
186
186
 
187
187
 
188
+ def test_wrap_types_rules_by_class() -> None:
189
+ sheet = kgcss.parse_stylesheet(
190
+ '@charset "utf-8"; @import "x.css"; @namespace svg url(http://www.w3.org/2000/svg); a {} @media print {}'
191
+ ' @page { @top-left {} } @font-face {} @supports (a: b) {} @unknown; /* c */'
192
+ )
193
+ rules = sheet.rules
194
+ charset, imp, ns, style, media, page, font_face, supports, unknown, comment = rules
195
+ assert isinstance(charset, kgcss.CharsetRule)
196
+ assert isinstance(imp, kgcss.ImportRule)
197
+ assert isinstance(ns, kgcss.NamespaceRule)
198
+ assert isinstance(style, kgcss.StyleRule)
199
+ assert isinstance(media, kgcss.MediaRule)
200
+ assert isinstance(page, kgcss.PageRule)
201
+ assert isinstance(font_face, kgcss.FontFaceRule)
202
+ assert isinstance(supports, kgcss.RuleContainer)
203
+ assert isinstance(unknown, kgcss.UnknownAtRule)
204
+ assert isinstance(comment, kgcss.Comment)
205
+ margin = page.rules[0]
206
+ assert isinstance(margin, kgcss.MarginRule)
207
+ # The overloads type the views by class, so that no isinstance() is needed: these
208
+ # annotations are checked by ty, the asserts at run time
209
+ views: tuple[
210
+ kgcss.compat.CSSCharsetRule,
211
+ kgcss.compat.CSSImportRule,
212
+ kgcss.compat.CSSNamespaceRule,
213
+ kgcss.compat.CSSStyleRule,
214
+ kgcss.compat.CSSMediaRule,
215
+ kgcss.compat.CSSPageRule,
216
+ kgcss.compat.CSSFontFaceRule,
217
+ kgcss.compat.CSSGroupingRule,
218
+ kgcss.compat.CSSUnknownRule,
219
+ kgcss.compat.CSSComment,
220
+ kgcss.compat.CSSMarginRule,
221
+ ] = (
222
+ wrap(charset),
223
+ wrap(imp),
224
+ wrap(ns),
225
+ wrap(style),
226
+ wrap(media),
227
+ wrap(page),
228
+ wrap(font_face),
229
+ wrap(supports),
230
+ wrap(unknown),
231
+ wrap(comment),
232
+ wrap(margin),
233
+ )
234
+ expected = (
235
+ 'CSSCharsetRule', 'CSSImportRule', 'CSSNamespaceRule', 'CSSStyleRule', 'CSSMediaRule', 'CSSPageRule',
236
+ 'CSSFontFaceRule', 'CSSGroupingRule', 'CSSUnknownRule', 'CSSComment', 'CSSMarginRule',
237
+ ) # fmt: skip
238
+ assert [type(v).__name__ for v in views] == list(expected)
239
+ unwrapped: kgcss.StyleRule = unwrap(views[3])
240
+ assert unwrapped is style
241
+ assert views[3].selectorText == 'a'
242
+ assert views[4].media.mediaText == 'print'
243
+
244
+
188
245
  def test_sheet_api() -> None:
189
246
  sheet = parseString(b'@charset "latin1"; a { content: "\xe9" }')
190
247
  assert sheet.encoding == 'windows-1252' # the WHATWG encoding for the label latin1
@@ -297,8 +297,10 @@ def test_selectors() -> None:
297
297
  assert selectors is rule.selectors
298
298
  assert [s.text for s in selectors] == ['a', 'b.c']
299
299
  assert selectors.text == 'a, b.c'
300
- # Callers can attach attributes to selectors (calibre's selector.calibre_used)
300
+ # Callers can keep data on selectors, or attach attributes (calibre's selector.calibre_used)
301
301
  selectors[1].calibre_used = True # ty: ignore[missing-attribute]
302
+ assert not selectors[1].data
303
+ selectors[1].data['used'] = True
302
304
  del selectors[0]
303
305
  assert rule.selector_text == 'b.c'
304
306
  # Appending a selector from another rule appends a copy, with its attributes
@@ -307,6 +309,14 @@ def test_selectors() -> None:
307
309
  copied = other.selectors[1]
308
310
  assert copied is not selectors[0]
309
311
  assert copied.calibre_used # ty: ignore[missing-attribute]
312
+ assert copied.data == {'used': True}
313
+ assert copied.data is not selectors[0].data
314
+ copied.data['used'] = False
315
+ assert selectors[0].data['used'] is True
316
+ # Selectors hash by identity, so they can be the keys of a caller's dict
317
+ used = {selectors[0]: True}
318
+ assert copied not in used
319
+ assert selectors[0] in used
310
320
  selectors.append(Selector('e > f'))
311
321
  selectors.sort(reverse=True)
312
322
  assert rule.selector_text == 'e > f, b.c'
@@ -79,6 +79,31 @@ def test_component_types() -> None:
79
79
  assert other.token_type is TokenType.AT_KEYWORD
80
80
 
81
81
 
82
+ @pytest.mark.parametrize(
83
+ ('text', 'expected'),
84
+ [
85
+ ('bold', 'bold'),
86
+ ('"My \\46 ont"', 'My Font'),
87
+ ("'a'", 'a'),
88
+ ('12px', None),
89
+ ('700', None),
90
+ ('50%', None),
91
+ ('#fff', None),
92
+ ('url(x.png)', None),
93
+ ('rgb(1 2 3)', None),
94
+ ('[a]', None),
95
+ ('+', None),
96
+ ('@x', None),
97
+ ],
98
+ )
99
+ def test_string_value(text: str, expected: str | None) -> None:
100
+ component = parse_value(text)[0]
101
+ assert component.string_value == expected
102
+ if expected is not None:
103
+ assert isinstance(component, Ident | String)
104
+ assert component.string_value == component.value
105
+
106
+
82
107
  def test_functions_and_blocks() -> None:
83
108
  # requirements V3 and V4: urls inside functions, function names and arguments
84
109
  v = parse_value('image-set(url(a.png) 1x, url("b.png") 2x) local(My\\ Font) calc(1px + (2 * 3)) [a b]')
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes