Browse Source

Test template and attribute creation

split-manual
J. King 3 years ago
parent
commit
2bfb77c081
  1. 38
      lib/DOM/Document.php
  2. 61
      tests/cases/TestDOM.php

38
lib/DOM/Document.php

@ -36,19 +36,7 @@ class Document extends \DOMDocument {
}
public function createAttribute($name) {
// Normalize the attribute name per modern DOM specifications.
$name = strtolower(trim($name));
try {
return parent::createAttribute($name);
} catch (\DOMException $e) {
// The element name is invalid for XML
// Replace any offending characters with "UHHHHHH" where H are the
// uppercase hexadecimal digits of the character's code point
$this->mangledAttributes = true;
$name = $this->coerceName($name);
return parent::createAttribute($name);
}
return $this->createAttributeNS(null, $name);
}
public function createAttributeNS($namespaceURI, $qualifiedName) {
@ -66,32 +54,12 @@ class Document extends \DOMDocument {
// uppercase hexadecimal digits of the character's code point
$this->mangledAttributes = true;
$qualifiedName = $this->coerceName($qualifiedName);
return parent::createAttributeNS($namespaceURI, $qualifiedName);
return $this->createAttributeNS($namespaceURI, $qualifiedName);
}
}
public function createElement($name, $value = "") {
// Normalize the element name per modern DOM specifications.
$name = strtolower(trim($name));
try {
if ($name !== 'template') {
$e = parent::createElementNS(null, $name, $value);
} else {
$e = new TemplateElement($this, $name, $value);
$this->templateElements[] = $e;
$e->content = $this->createDocumentFragment();
}
return $e;
} catch (\DOMException $e) {
// The element name is invalid for XML
// Replace any offending characters with "UHHHHHH" where H is the
// uppercase hexadecimal digits of the character's code point
$this->mangledElements = true;
$name = $this->coerceName($name);
return parent::createElementNS(null, $name, $value);
}
return $this->createElementNS(null, $name, $value);
}
public function createElementNS($namespaceURI, $qualifiedName, $value = "") {

61
tests/cases/TestDOM.php

@ -7,6 +7,7 @@ declare(strict_types=1);
namespace MensBeam\HTML\TestCase;
use MensBeam\HTML\Document;
use MensBeam\HTML\TemplateElement;
/**
* @covers \MensBeam\HTML\Document
@ -61,4 +62,64 @@ class TestDOM extends \PHPUnit\Framework\TestCase {
["TEST", "test"],
];
}
public function testCreateTemplateElements(): void {
$d = new Document;
$t = $d->createElement("template");
$this->assertInstanceOf(TemplateElement::class, $t);
$this->assertNotNull($t->ownerDocument);
$t = $d->createElement("TEMPLATE");
$this->assertInstanceOf(TemplateElement::class, $t);
$this->assertNotNull($t->ownerDocument);
$t = $d->createElementNS(null, "template");
$this->assertInstanceOf(TemplateElement::class, $t);
$this->assertNotNull($t->ownerDocument);
$t = $d->createElementNS(null, "TEMPLATE");
$this->assertInstanceOf(TemplateElement::class, $t);
$this->assertNotNull($t->ownerDocument);
$t = $d->createElementNS("http://www.w3.org/1999/xhtml", "template");
$this->assertInstanceOf(TemplateElement::class, $t);
$this->assertNotNull($t->ownerDocument);
$t = $d->createElementNS("http://www.w3.org/1999/xhtml", "TEMPLATE");
$this->assertInstanceOf(TemplateElement::class, $t);
$this->assertNotNull($t->ownerDocument);
}
/** @dataProvider provideNamespacedAttributeCreations */
public function testCreateNamespacedAttributes(?string $nsIn, string $nameIn, string $local, string $prefix): void {
$d = new Document;
$d->appendChild($d->createElement("html"));
$a = $d->createAttributeNS($nsIn, $nameIn);
$this->assertSame($local, $a->localName);
$this->assertSame($nsIn, $a->namespaceURI);
$this->assertSame($prefix, $a->prefix);
}
public function provideNamespacedAttributeCreations(): iterable {
return [
[null, "test", "test", ""],
[null, "test:test", "testU00003Atest", ""],
[null, "test", "test", ""],
[null, "TEST:TEST", "TESTU00003ATEST", ""],
["fake_ns", "test", "test", ""],
];
}
/** @dataProvider provideBareAttributeCreations */
public function testCreateBareAttributes(string $nameIn, string $nameOut): void {
$d = new Document;
$d->appendChild($d->createElement("html"));
$a = $d->createAttribute($nameIn);
$this->assertSame($nameOut, $a->name);
$this->assertNull($a->namespaceURI);
}
public function provideBareAttributeCreations(): iterable {
return [
["test", "test"],
["test:test", "testU00003Atest"],
["TEST", "TEST"],
["TEST:TEST", "TESTU00003ATEST"],
];
}
}

Loading…
Cancel
Save