Compare commits

...

2 Commits

  1. 28
      lib/DOM/Element.php
  2. 41
      tests/cases/TestDOM.php

28
lib/DOM/Element.php

@ -15,10 +15,15 @@ class Element extends \DOMElement {
// Newer versions of the DOM spec have getAttribute return an empty string only
// when the attribute exists and is empty, otherwise null. This fixes that.
$value = parent::getAttribute($name);
if ($value === '' && !$this->hasAttribute($name)) {
if ($value === '' && !parent::hasAttribute($name)) {
// the PHP DOM does not acknowledge the presence of XMLNS-namespace attributes
foreach ($this->attributes as $a) {
if ($a->nodeName === $name) {
return $a->value;
}
}
return null;
}
return $value;
}
@ -29,7 +34,6 @@ class Element extends \DOMElement {
if ($value === '' && !$this->hasAttributeNS($namespaceURI, $localName)) {
return null;
}
return $value;
}
@ -131,13 +135,10 @@ class Element extends \DOMElement {
return $this->getAttributeNode('class');
}
}
$result = parent::setAttributeNodeNS($attribute);
if ($fixId) {
$this->setIdAttribute($attribute->name, true);
}
return $result;
}
@ -151,12 +152,9 @@ class Element extends \DOMElement {
if ($this->_classList === null) {
$this->_classList = new TokenList($this, 'class');
}
return $this->_classList;
}
return null;
break;
return null; // @codeCoverageIgnore
### DOM Parsing Specification ###
# 2.3 The InnerHTML mixin
#
@ -165,8 +163,8 @@ class Element extends \DOMElement {
# might throw an exception instead of returning a string).
// DEVIATION: Parsing of XML documents will not be handled by this
// implementation, so there's no need for the well-formed flag.
case 'innerHTML': return $this->serialize($this);
break;
case 'innerHTML':
return $this->serialize($this);
### DOM Parsing Specification ###
# 2.4 Extensions to the Element interface
# outerHTML
@ -180,8 +178,10 @@ class Element extends \DOMElement {
// OPTIMIZATION: When following the instructions above the fragment serializing
// algorithm (Element::serialize) would invoke Element::__toString, so just
// doing that instead of multiple function calls.
case 'outerHTML': return $this->__toString();
break;
case 'outerHTML':
return $this->__toString();
default:
return null;
}
}

41
tests/cases/TestDOM.php

@ -288,20 +288,31 @@ class TestDOM extends \PHPUnit\Framework\TestCase {
$this->assertTrue($e->hasAttributeNS(Parser::XMLNS_NAMESPACE, "xlink"));
$this->assertTrue($e->hasAttributeNS("fake_ns", "eek"));
// perform retrival tests
//$this->assertNull($e->getAttribute("blah"));
//$this->assertNull($e->getAttribute("OOK"));
//$this->assertNull($e->getAttribute("eek"));
//$this->assertNull($e->getAttribute("ack"));
//$this->assertSame("eek", $e->getAttribute("ook"));
//$this->assertSame("http://example.com/", $e->getAttribute("xml:base"));
//$this->assertSame(Parser::XLINK_NAMESPACE, $e->getAttribute("xmlns:xlink"));
//$this->assertSame("ack", $e->getAttribute("ook:eek"));
//$this->assertNull($e->getAttributeNS(null, "blah"));
//$this->assertNull($e->getAttributeNS(null, "OOK"));
//$this->assertNull($e->getAttributeNS(null, "ack"));
//$this->assertSame("eek", $e->getAttributeNS(null, "ook"));
//$this->assertSame("http://example.com/", $e->getAttributeNS(Parser::XML_NAMESPACE, "base"));
//$this->assertSame(Parser::XLINK_NAMESPACE, $e->getAttributeNS(Parser::XMLNS_NAMESPACE, "xlink"));
//$this->assertSame("ack", $e->getAttributeNS("fake_ns", "eek"));
$this->assertNull($e->getAttribute("blah"));
$this->assertNull($e->getAttribute("OOK"));
$this->assertNull($e->getAttribute("eek"));
$this->assertNull($e->getAttribute("ack"));
$this->assertSame("eek", $e->getAttribute("ook"));
$this->assertSame("http://example.com/", $e->getAttribute("xml:base"));
$this->assertSame(Parser::XLINK_NAMESPACE, $e->getAttribute("xmlns:xlink"));
$this->assertSame("ack", $e->getAttribute("ook:eek"));
$this->assertNull($e->getAttributeNS(null, "blah"));
$this->assertNull($e->getAttributeNS(null, "OOK"));
$this->assertNull($e->getAttributeNS(null, "ack"));
$this->assertSame("eek", $e->getAttributeNS(null, "ook"));
$this->assertSame("http://example.com/", $e->getAttributeNS(Parser::XML_NAMESPACE, "base"));
$this->assertSame(Parser::XLINK_NAMESPACE, $e->getAttributeNS(Parser::XMLNS_NAMESPACE, "xlink"));
$this->assertSame("ack", $e->getAttributeNS("fake_ns", "eek"));
}
/** @covers \MensBeam\HTML\Element::__get */
public function testGetInnerAndOuterHtml(): void {
$d = new Document;
$d->appendChild($d->createElement("html"));
$d->documentElement->appendChild($d->createTextNode("OOK"));
$this->assertSame("OOK", $d->documentElement->innerHTML);
$this->assertSame("<html>OOK</html>", $d->documentElement->outerHTML);
$this->assertNull($d->documentElement->innerHtml);
$this->assertNull($d->documentElement->outerHtml);
}
}

Loading…
Cancel
Save