Modern DOM library written in PHP for HTML documents
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.5 KiB

<?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\Parser;
# An HTMLCollection object is a collection of elements.
class HTMLCollection extends Collection {
public function current(): ?Element {
return parent::current();
}
public function item(int $index): ?Element {
return parent::item($index);
}
public function namedItem(string $name): ?Element {
# The namedItem(key) method steps are:
// The interface says to use "name" as the argument while the steps below show
// to use "key" *shrug*.
# 1. If key is the empty string, return null.
if ($name === '') {
return null;
}
# 2. Return the first element in the collection for which at least one of the following is true:
# • it has an ID which is key;
# • it is in the HTML namespace and has a name attribute whose value is key;
# or null if there is no such element.
foreach ($this->innerCollection as $element) {
if ($element->getAttribute('id') === $name || $element->namespaceURI === null && $element->getAttribute('name') === $name) {
return $this->innerDocument->getWrapperNode($element);
}
}
return null;
}
public function offsetGet($offset): ?Element {
return (is_int($offset)) ? $this->item($offset) : $this->namedItem($offset);
}
}