Dustin Wilson
3 years ago
12 changed files with 464 additions and 411 deletions
@ -0,0 +1,109 @@ |
|||
<?php |
|||
/** |
|||
* @license MIT |
|||
* Copyright 2017 Dustin Wilson, J. King, et al. |
|||
* See LICENSE and AUTHORS files for details |
|||
*/ |
|||
|
|||
declare(strict_types=1); |
|||
namespace MensBeam\HTML\DOM; |
|||
use MensBeam\Framework\MagicProperties, |
|||
MensBeam\HTML\DOM\InnerNode\Document as InnerDocument; |
|||
|
|||
|
|||
|
|||
# A collection is an object that represents a list of nodes. A collection can be |
|||
# either live or static. Unless otherwise stated, a collection must be live. |
|||
# |
|||
# If a collection is live, then the attributes and methods on that object must |
|||
# operate on the actual underlying data, not a snapshot of the data. |
|||
# |
|||
# When a collection is created, a filter and a root are associated with it. |
|||
# |
|||
# The collection then represents a view of the subtree rooted at the |
|||
# collection’s root, containing only nodes that match the given filter. The view |
|||
# is linear. In the absence of specific requirements to the contrary, the nodes |
|||
# within the collection must be sorted in tree order. |
|||
/** |
|||
* Not in standard except as an abstract description of HTMLCollection and |
|||
* NodeList. Exists to eliminate code duplication between HTMLCollection and |
|||
* NodeList. |
|||
*/ |
|||
abstract class Collection implements \ArrayAccess, \Countable, \Iterator { |
|||
use MagicProperties; |
|||
|
|||
protected InnerDocument $innerDocument; |
|||
protected \DOMNodeList|\DOMNamedNodeMap $innerCollection; |
|||
protected ?\Closure $filter = null; |
|||
protected int $_length = 0; |
|||
protected ?array $nodeArray = null; |
|||
protected int $position = 0; |
|||
|
|||
|
|||
protected function __get_length(): int { |
|||
# The length attribute must return the number of nodes represented by the |
|||
# collection. |
|||
return $this->count(); |
|||
} |
|||
|
|||
|
|||
protected function __construct(InnerDocument $innerDocument, \DOMNodeList $nodeList) { |
|||
$this->innerDocument = $innerDocument; |
|||
$this->innerCollection = $nodeList; |
|||
} |
|||
|
|||
|
|||
public function count(): int { |
|||
return $this->innerCollection->length; |
|||
} |
|||
|
|||
public function current(): ?Node { |
|||
return $this->item($this->position); |
|||
} |
|||
|
|||
public function item(int $index): ?Node { |
|||
# The item(index) method must return the indexth node in the collection. If |
|||
# there is no indexth node in the collection, then the method must return null. |
|||
// PHP's DOM does this okay already |
|||
$node = $this->innerCollection->item($index); |
|||
if ($node === null) { |
|||
return null; |
|||
} |
|||
|
|||
return $this->innerDocument->getWrapperNode($node); |
|||
} |
|||
|
|||
public function key(): int { |
|||
return $this->position; |
|||
} |
|||
|
|||
public function next(): void { |
|||
$this->position++; |
|||
} |
|||
|
|||
public function rewind(): void { |
|||
$this->position = 0; |
|||
} |
|||
|
|||
public function offsetExists($offset): bool { |
|||
return ($this->innerCollection->item($offset) !== null); |
|||
} |
|||
|
|||
public function offsetGet($offset): ?Node { |
|||
return $this->item($offset); |
|||
} |
|||
|
|||
public function offsetSet($offset, $value): void { |
|||
// Collections are immutable; the spec is ambiguous as to what to do here. |
|||
// Browsers silently fail here, so that's what we're going to do. |
|||
} |
|||
|
|||
public function offsetUnset($offset): void { |
|||
// Collections are immutable; the spec is ambiguous as to what to do here. |
|||
// Browsers silently fail here, so that's what we're going to do. |
|||
} |
|||
|
|||
public function valid(): bool { |
|||
return $this->offsetExists($this->position); |
|||
} |
|||
} |
@ -0,0 +1,76 @@ |
|||
<?php |
|||
/** |
|||
* @license MIT |
|||
* Copyright 2017 Dustin Wilson, J. King, et al. |
|||
* See LICENSE and AUTHORS files for details |
|||
*/ |
|||
|
|||
declare(strict_types=1); |
|||
namespace MensBeam\HTML\DOM; |
|||
use MensBeam\HTML\DOM\InnerNode\Document as InnerDocument; |
|||
|
|||
|
|||
class NamedNodeMap extends Collection { |
|||
# A NamedNodeMap has an associated element (an element). |
|||
protected Element $element; |
|||
|
|||
|
|||
protected function __construct(Element $element, InnerDocument $innerDocument, \DOMNamedNodeMap $namedNodeMap) { |
|||
$this->element = $element; |
|||
$this->innerDocument = $innerDocument; |
|||
$this->innerCollection = $namedNodeMap; |
|||
} |
|||
|
|||
|
|||
public function current(): ?Attr { |
|||
return parent::current(); |
|||
} |
|||
|
|||
public function getNamedItem(string $qualifiedName): ?Attr { |
|||
# The getNamedItem(qualifiedName) method steps are to return the result of |
|||
# getting an attribute given qualifiedName and element. |
|||
return $this->element->getAttributeNode($qualifiedName); |
|||
} |
|||
|
|||
public function getNamedItemNS(?string $namespace, string $localName): ?Attr { |
|||
# The getNamedItemNS(namespace, localName) method steps are to return the result |
|||
# of getting an attribute given namespace, localName, and element. |
|||
return $this->element->getAttributeNodeNS($namespace, $localName); |
|||
} |
|||
|
|||
public function item(int $index): ?Attr { |
|||
return parent::item($index); |
|||
} |
|||
|
|||
public function removeNamedItem(string $qualifiedName): ?Attr { |
|||
return $this->removeNamedItemNS(null, $qualifiedName); |
|||
} |
|||
|
|||
public function removeNamedItemNS(?string $namespace, string $localName): ?Attr { |
|||
# The removeNamedItem(qualifiedName) method steps are: |
|||
# |
|||
# 1. Let attr be the result of removing an attribute given namespace, localName, |
|||
# and element. |
|||
$attr = $this->element->removeAttributeNode($namespace, $localName); |
|||
|
|||
# 2. If attr is null, then throw a "NotFoundError" DOMException. |
|||
if ($attr === null) { |
|||
throw new DOMException(DOMException::NOT_FOUND); |
|||
} |
|||
|
|||
# 3. Return attr. |
|||
return $attr; |
|||
} |
|||
|
|||
public function setNamedItem(string $attr): ?Attr { |
|||
# The setNamedItem(attr) and setNamedItemNS(attr) method steps are to return the |
|||
# result of setting an attribute given attr and element. |
|||
return $this->element->setAttributeNode($attr); |
|||
} |
|||
|
|||
public function setNamedItemNS(string $attr): ?Attr { |
|||
# The setNamedItem(attr) and setNamedItemNS(attr) method steps are to return the |
|||
# result of setting an attribute given attr and element. |
|||
return $this->element->setAttributeNode($attr); |
|||
} |
|||
} |
@ -0,0 +1,52 @@ |
|||
<?php |
|||
/** |
|||
* @license MIT |
|||
* Copyright 2017 Dustin Wilson, J. King, et al. |
|||
* See LICENSE and AUTHORS files for details |
|||
*/ |
|||
|
|||
declare(strict_types=1); |
|||
namespace MensBeam\HTML\DOM\TestCase; |
|||
|
|||
use MensBeam\HTML\DOM\{ |
|||
Document, |
|||
Node, |
|||
XMLDocument |
|||
}; |
|||
use MensBeam\HTML\Parser; |
|||
|
|||
|
|||
/** @covers \MensBeam\HTML\DOM\Document */ |
|||
class TestDocument extends \PHPUnit\Framework\TestCase { |
|||
/** |
|||
* @covers \MensBeam\HTML\DOM\Document::__get_body |
|||
* |
|||
* @covers \MensBeam\HTML\DOM\Document::__construct |
|||
* @covers \MensBeam\HTML\DOM\Document::createElement |
|||
* @covers \MensBeam\HTML\DOM\Element::__construct |
|||
* @covers \MensBeam\HTML\DOM\Node::__construct |
|||
* @covers \MensBeam\HTML\DOM\Node::appendChild |
|||
* @covers \MensBeam\HTML\DOM\Node::preInsertionValidity |
|||
* @covers \MensBeam\HTML\DOM\InnerNode\Document::__construct |
|||
* @covers \MensBeam\HTML\DOM\InnerNode\Document::getWrapperNode |
|||
* @covers \MensBeam\HTML\DOM\InnerNode\Document::__get_wrapperNode |
|||
* @covers \MensBeam\HTML\DOM\InnerNode\Reflection::createFromProtectedConstructor |
|||
* @covers \MensBeam\HTML\DOM\InnerNode\Reflection::getProtectedProperty |
|||
* @covers \MensBeam\HTML\DOM\InnerNode\NodeMap::get |
|||
* @covers \MensBeam\HTML\DOM\InnerNode\NodeMap::has |
|||
* @covers \MensBeam\HTML\DOM\InnerNode\NodeMap::key |
|||
* @covers \MensBeam\HTML\DOM\InnerNode\NodeMap::set |
|||
*/ |
|||
public function testProperty_body() { |
|||
$d = new Document(); |
|||
$d->appendChild($d->createElement('html')); |
|||
|
|||
// Node::body without body |
|||
$this->assertNull($d->body); |
|||
|
|||
$body = $d->documentElement->appendChild($d->createElement('body')); |
|||
|
|||
// Node::body with body |
|||
$this->assertSame($body, $d->body); |
|||
} |
|||
} |
Loading…
Reference in new issue