Browse Source

Use createElementNS when creating bare elements

This forces coercion in more situations, but has the advantage of being
consistent with creating an element in the HTML namespace
split-manual
J. King 3 years ago
parent
commit
f660ecaad4
  1. 4
      lib/DOM/Document.php
  2. 17
      tests/cases/TestDOM.php

4
lib/DOM/Document.php

@ -76,7 +76,7 @@ class Document extends \DOMDocument {
try {
if ($name !== 'template') {
$e = parent::createElement($name, $value);
$e = parent::createElementNS(null, $name, $value);
} else {
$e = new TemplateElement($this, $name, $value);
$this->templateElements[] = $e;
@ -90,7 +90,7 @@ class Document extends \DOMDocument {
// uppercase hexadecimal digits of the character's code point
$this->mangledElements = true;
$name = $this->coerceName($name);
return parent::createElement($name, $value);
return parent::createElementNS(null, $name, $value);
}
}

17
tests/cases/TestDOM.php

@ -44,4 +44,21 @@ class TestDOM extends \PHPUnit\Framework\TestCase {
["http://www.w3.org/1998/Math/MathML", "TEST", "http://www.w3.org/1998/Math/MathML", "TEST", ""],
];
}
/** @dataProvider provideBareElements */
public function testCreateBareElements(string $nameIn, $nameOut): void {
$d = new Document;
$e = $d->createElement($nameIn);
$this->assertNull($e->namespaceURI);
$this->assertSame("", $e->prefix);
$this->assertSame($nameOut, $e->localName);
}
public function provideBareElements(): iterable {
return [
["test", "test"],
["test:test", "testU00003Atest"],
["9", "U000039"],
["TEST", "test"],
];
}
}

Loading…
Cancel
Save