2025-08-06 13:59:43 +03:00
|
|
|
class_name SelectorUtils
|
|
|
|
|
extends RefCounted
|
|
|
|
|
|
|
|
|
|
static func match_element(selector: String, element: HTMLParser.HTMLElement) -> bool:
|
|
|
|
|
if not element:
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
|
|
var rule = CSSParser.CSSRule.new()
|
|
|
|
|
rule.init(selector)
|
|
|
|
|
|
|
|
|
|
var stylesheet = CSSParser.CSSStylesheet.new()
|
2025-08-07 14:05:41 +03:00
|
|
|
return stylesheet.selector_matches(rule, "", element)
|
2025-08-06 13:59:43 +03:00
|
|
|
|
|
|
|
|
static func find_all_matching(selector: String, elements: Array[HTMLParser.HTMLElement]) -> Array[HTMLParser.HTMLElement]:
|
|
|
|
|
var matches: Array[HTMLParser.HTMLElement] = []
|
|
|
|
|
|
|
|
|
|
for element in elements:
|
|
|
|
|
if match_element(selector, element):
|
|
|
|
|
matches.append(element)
|
|
|
|
|
|
|
|
|
|
return matches
|
|
|
|
|
|
|
|
|
|
static func find_first_matching(selector: String, elements: Array[HTMLParser.HTMLElement]) -> HTMLParser.HTMLElement:
|
|
|
|
|
for element in elements:
|
|
|
|
|
if match_element(selector, element):
|
|
|
|
|
return element
|
|
|
|
|
|
2025-08-09 13:28:24 +03:00
|
|
|
return null
|