Skip to content

Commit b7bc7d7

Browse files
authored
Inherit the direction of non-HTML elements in :dir() (#346)
* Inherit the direction of non-HTML elements in :dir() * Remove unreachable check in :dir() resolution
1 parent 33f9057 commit b7bc7d7

3 files changed

Lines changed: 44 additions & 8 deletions

File tree

‎docs/src/markdown/about/changelog.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ icon: lucide/scroll-text
1818
- **FIX**: Match the values of every attribute that the HTML specification lists as ASCII case insensitive (`rel`,
1919
`method`, `lang`, `dir`, ...) without ASCII case sensitivity on HTML elements in HTML documents, not just `type`.
2020
Non-HTML elements are matched case sensitively.
21+
- **FIX**: `:dir()` now matches non-HTML elements, such as SVG and MathML, by the direction they inherit from their
22+
parent (the `dir` attribute only applies to HTML elements), and HTML elements inside them, such as in an SVG
23+
`foreignObject`, by the direction of their ancestors.
2124
- **FIX**: Fix `:dir()` ignoring the value of an `input` with `dir="auto"` and a missing or empty `type`.
2225
- **FIX**: Fix `:lang()` treating an element with an empty `lang` or `xml:lang` attribute as having the language of
2326
a tagged ancestor.

‎soupsieve/css_match.py‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,7 +1445,7 @@ def match_lang(self, el: bs4.Tag, langs: tuple[ct.SelectorLang, ...]) -> bool:
14451445

14461446
return match
14471447

1448-
def match_dir(self, el: bs4.Tag | None, directionality: int) -> bool:
1448+
def match_dir(self, el: bs4.Tag, directionality: int) -> bool:
14491449
"""Check directionality."""
14501450

14511451
# If we have to match both left and right, we can't match either.
@@ -1461,18 +1461,21 @@ def match_dir(self, el: bs4.Tag | None, directionality: int) -> bool:
14611461
self.dir_cache[key] = direction
14621462
return direction == directionality
14631463

1464-
def resolve_dir(self, el: bs4.Tag | None, visited: list[int]) -> int:
1465-
"""Resolve the direction of an element, returning `0` if it cannot be resolved."""
1464+
def resolve_dir(self, el: bs4.Tag, visited: list[int]) -> int:
1465+
"""Resolve the direction of an element."""
14661466

14671467
while True:
1468-
if el is None or not self.is_html_tag(el):
1469-
return 0
1470-
14711468
key = id(el)
14721469
if key in self.dir_cache:
14731470
return self.dir_cache[key]
14741471
visited.append(key)
14751472

1473+
# `dir` is only defined for HTML elements, so other elements (SVG, MathML, etc.)
1474+
# inherit the direction of their parent. They always have one, as the root is an HTML element.
1475+
if not self.is_html_tag(el):
1476+
el = cast('bs4.Tag', self.get_parent(el, no_iframe=True))
1477+
continue
1478+
14761479
# Element has defined direction of left to right or right to left
14771480
direction = DIR_MAP.get(util.lower(self.get_attribute_by_name(el, 'dir', '')), None)
14781481
if direction not in (None, 0):
@@ -1526,8 +1529,8 @@ def resolve_dir(self, el: bs4.Tag | None, visited: list[int]) -> int:
15261529
# Auto directionality could not be determined, assume left to right
15271530
return ct.SEL_DIR_LTR
15281531

1529-
# Match parents direction
1530-
el = self.get_parent(el, no_iframe=True)
1532+
# Match parents direction (the root always resolves above, so there is always a parent)
1533+
el = cast('bs4.Tag', self.get_parent(el, no_iframe=True))
15311534

15321535
def match_range(self, el: bs4.Tag, condition: int) -> bool:
15331536
"""

‎tests/test_level4/test_dir.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,33 @@ def test_xml_in_html(self):
283283
[],
284284
flags=util.HTML5
285285
)
286+
287+
def test_non_html_elements_inherit_direction(self):
288+
"""Test that non-HTML elements, which have no `dir` attribute, inherit the direction of their parent."""
289+
290+
markup = """
291+
<html>
292+
<head></head>
293+
<body>
294+
<div dir="rtl">
295+
<svg id="1"><g id="2"></g><foreignObject id="3"><p id="4">text</p></foreignObject></svg>
296+
<math id="5"><mi id="6">x</mi></math>
297+
</div>
298+
<svg id="7" dir="rtl"><g id="8"></g></svg>
299+
</body>
300+
</html>
301+
"""
302+
303+
self.assert_selector(
304+
markup,
305+
"[id]:dir(rtl)",
306+
['1', '2', '3', '4', '5', '6'],
307+
flags=util.HTML5
308+
)
309+
310+
self.assert_selector(
311+
markup,
312+
"[id]:dir(ltr)",
313+
['7', '8'],
314+
flags=util.HTML5
315+
)

0 commit comments

Comments
 (0)