@@ -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 """
0 commit comments