Browse Source

Added setAttributeNode, setAttributeNodeNS support for id attributes

split-manual
Dustin Wilson 3 years ago
parent
commit
4771a65239
  1. 6
      lib/DOM/DOMException.php
  2. 16
      lib/DOM/Element.php
  3. 2
      lib/DOM/traits/Ancestor.php
  4. 4
      lib/DOM/traits/Descendant.php
  5. 8
      lib/Exception.php

6
lib/DOM/DOMException.php

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace dW\HTML5;
class DOMException extends \DOMException {
class DOMException extends \Exception {
// From PHP's DOMException; keeping error codes consistent
const NO_MODIFICATION_ALLOWED = 7;
@ -19,11 +19,11 @@ class DOMException extends \DOMException {
];
public function __construct(int $code, ...$args) {
if (!isset(static::$messages[$code])) {
if (!isset(self::$messages[$code])) {
throw new Exception(Exception::INVALID_CODE);
}
$message = static::$messages[$code];
$message = self::$messages[$code];
$previous = null;
if ($args) {

16
lib/DOM/Element.php

@ -42,6 +42,22 @@ class Element extends \DOMElement {
}
}
public function setAttributeNode(\DOMAttr $attribute) {
parent::setAttributeNode($attribute);
if ($attribute->name === 'id') {
$this->setIdAttribute($attribute->name, true);
}
}
public function setAttributeNodeNS(\DOMAttr $attribute) {
parent::setAttributeNodeNS($attribute);
if ($attribute->name === 'id') {
$this->setIdAttribute($attribute->name, true);
}
}
public function __get(string $prop) {
switch ($prop) {
### DOM Parsing Specification ###

2
lib/DOM/traits/Ancestor.php

@ -16,7 +16,7 @@ trait Ancestor {
protected function ancestor($needle, bool $returnNode = true) {
$context = $this->parentNode;
do {
$result = static::compare($needle, $context);
$result = self::compare($needle, $context);
if (!is_null($result)) {
return ($returnNode === true) ? $result : true;
}

4
lib/DOM/traits/Descendant.php

@ -6,11 +6,11 @@ trait Descendant {
use Compare;
public function getDescendant($needle): \DOMNode {
return static::descendant($needle, true);
return self::descendant($needle, true);
}
public function hasDescendant($needle): bool {
return static::descendant($needle, false);
return self::descendant($needle, false);
}
protected function descendant($needle, bool $returnNode = true): \DOMNode {

8
lib/Exception.php

@ -48,11 +48,11 @@ class Exception extends \Exception {
];
public function __construct(int $code, ...$args) {
if (!isset(static::$messages[$code])) {
throw new Exception(self::INVALID_CODE);
if (!isset(self::$messages[$code])) {
throw new self(self::INVALID_CODE);
}
$message = static::$messages[$code];
$message = self::$messages[$code];
$previous = null;
if ($args) {
@ -70,7 +70,7 @@ class Exception extends \Exception {
// If the number of replacements don't match the arguments then oops.
if (count($args) !== $count) {
throw new Exception(self::INCORRECT_PARAMETERS_FOR_MESSAGE, $count);
throw new self(self::INCORRECT_PARAMETERS_FOR_MESSAGE, $count);
}
if ($count > 0) {

Loading…
Cancel
Save