xpath->query($query."[contains(concat(' ', normalize-space(@class), ' '), ' $class ')][1]", $context ?? $this->subject); assert($el !== false, "Invalid XPath query: \"$query\""); return $el->length ? $el->item(0) : null; } public function getLang(): ?string { // walk up the tree looking for the nearest language tag, preferring XML over HTML when appropriate $el = $this->subject; $out = null; $xhtml = (bool) $el->ownerDocument->documentElement->namespaceURI; do { if ($xhtml) { $out = trim($el->getAttributeNS("http://www.w3.org/XML/1998/namespace", "lang") ?? ""); $out = strlen($out) ? $out : null; } if (is_null($out)) { $out = trim($el->getAttribute("lang") ?? ""); $out = strlen($out) ? $out : null; } $el = $el->parentNode; } while (is_null($out) && $el); return $out; } }