Browse Source

Handle xmlns on HTML elements correctly

The test for when to produce a parse error was not correct previously
domparser 1.2.7
J. King 1 year ago
parent
commit
82b76b192c
  1. 6
      lib/Parser/TreeConstructor.php
  2. 2
      tests/cases/TestTreeConstructor.php

6
lib/Parser/TreeConstructor.php

@ -3541,7 +3541,7 @@ class TreeConstructor {
# corresponding cell in the fourth column. (This fixes the use of namespaced
# attributes, in particular lang attributes in the XML namespace.)
// DOMElement::setAttributeNS requires the prefix and local name be in one
// string, so there is no need to separate the prefix and the local name here.
// string, so there is no need to separate the prefix and the local name here.
$a->namespace = self::FOREIGN_ATTRIBUTE_NAMESPACE_MAP[$a->name] ?? null;
}
# Insert a foreign element for the token, in the same namespace as the adjusted
@ -4236,9 +4236,9 @@ class TreeConstructor {
// NOTE: The specification is silent as to how to handle these
// attributes. We assume these bad attributes should be dropped,
// since they break the DOM when added
if ($attr->name === "xmlns" && $namespace !== $this->htmlNamespace && $attr->value !== $namespace) {
if ($attr->name === "xmlns" && $attr->namespace === Parser::XMLNS_NAMESPACE && $attr->value !== ($namespace ?? Parser::HTML_NAMESPACE)) {
$this->error(ParseError::INVALID_NAMESPACE_ATTRIBUTE_VALUE, "xmlns", $namespace);
} elseif ($attr->name === "xmlns:xlink" && $namespace !== $this->htmlNamespace && $attr->value !== Parser::XLINK_NAMESPACE) {
} elseif ($attr->name === "xmlns:xlink" && $attr->namespace === Parser::XMLNS_NAMESPACE && $attr->value !== Parser::XLINK_NAMESPACE) {
$this->error(ParseError::INVALID_NAMESPACE_ATTRIBUTE_VALUE, "xmlns:xlink", Parser::XLINK_NAMESPACE);
} else {
$this->elementSetAttribute($element, $attr->namespace, $attr->name, $attr->value);

2
tests/cases/TestTreeConstructor.php

@ -135,7 +135,7 @@ class TestTreeConstructor extends \PHPUnit\Framework\TestCase {
}
protected function patchTest(string $data, $fragment, array $errors, array $exp): array {
// When using the HTML namespace, xmlns attributes lose their namespace due to a PHP limitation
// When using the HTML namespace, xmlns attributes in the xmlns namespace lose their namespace URI due to a PHP limitation
if ($this->ns) {
for ($a = 0; $a < sizeof($exp); $a++) {
if (preg_match('/^\|\s+xmlns xmlns=/', $exp[$a])) {

Loading…
Cancel
Save