diff --git a/README.md b/README.md index 464064d..3e8879d 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ or: ```php loadHTML('
Ook-ook? Oooook. Ook ook oook ook oooooook ook ooook ook.
Eek!
'); ?> ``` diff --git a/composer.json b/composer.json index 694ab12..7a9b202 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,8 @@ "psr-4": { "dW\\HTML5\\": [ "lib/", - "lib/DOM" + "lib/DOM", + "lib/DOM/traits" ] }, "classmap": ["lib/Token.php"] diff --git a/lib/DOM/Comment.php b/lib/DOM/Comment.php index 778ca19..b24561f 100644 --- a/lib/DOM/Comment.php +++ b/lib/DOM/Comment.php @@ -3,5 +3,5 @@ declare(strict_types=1); namespace dW\HTML5; class Comment extends \DOMComment { - use Node; + use Ancestor; } diff --git a/lib/DOM/Document.php b/lib/DOM/Document.php index db6c147..093c262 100644 --- a/lib/DOM/Document.php +++ b/lib/DOM/Document.php @@ -3,7 +3,7 @@ declare(strict_types=1); namespace dW\HTML5; class Document extends \DOMDocument { - use Printer; + use Descendant, Printing; public function __construct() { parent::__construct(); diff --git a/lib/DOM/DocumentFragment.php b/lib/DOM/DocumentFragment.php index 79dcc31..21178e2 100644 --- a/lib/DOM/DocumentFragment.php +++ b/lib/DOM/DocumentFragment.php @@ -3,5 +3,5 @@ declare(strict_types=1); namespace dW\HTML5; class DocumentFragment extends \DOMDocumentFragment { - use Node; + use Descendant; } diff --git a/lib/DOM/Element.php b/lib/DOM/Element.php index 8b20cb2..e822e4b 100644 --- a/lib/DOM/Element.php +++ b/lib/DOM/Element.php @@ -3,7 +3,9 @@ declare(strict_types=1); namespace dW\HTML5; class Element extends \DOMElement { - use Node; + use Ancestor, Descendant { + Ancestor::compare insteadof Descendant; + } // Used for template elements public $content = null; @@ -39,34 +41,4 @@ class Element extends \DOMElement { ) ); } - - public function getDescendant($needle): \DOMNode { - return static::descendant($needle, true); - } - - public function hasDescendant($needle): bool { - return static::descendant($needle, false); - } - - protected function descendant($needle, bool $returnNode = true): \DOMNode { - if ($this->hasChildNodes() === false) { - return ($returnNode === true) ? null : false; - } - - $context = $this->firstChild; - - do { - $result = $this->compare($needle, $context); - if (!is_null($result)) { - return ($returnNode === true) ? $result : true; - } - - $result = $this->descendant($needle, $context); - if (!is_null($result)) { - return ($returnNode === true) ? $result : true; - } - } while ($context = $context->nextSibling); - - return ($returnNode === true) ? null : false; - } } diff --git a/lib/DOM/ProcessingInstruction.php b/lib/DOM/ProcessingInstruction.php index 90ee98d..9eb91e7 100644 --- a/lib/DOM/ProcessingInstruction.php +++ b/lib/DOM/ProcessingInstruction.php @@ -3,5 +3,5 @@ declare(strict_types=1); namespace dW\HTML5; class ProcessingInstruction extends \DOMProcessingInstruction { - use Node; + use Ancestor; } diff --git a/lib/DOM/Text.php b/lib/DOM/Text.php index d14146c..422ceef 100644 --- a/lib/DOM/Text.php +++ b/lib/DOM/Text.php @@ -3,5 +3,5 @@ declare(strict_types=1); namespace dW\HTML5; class Text extends \DOMText { - use Node; + use Ancestor; } diff --git a/lib/DOM/traits/Ancestor.php b/lib/DOM/traits/Ancestor.php new file mode 100644 index 0000000..87a5b91 --- /dev/null +++ b/lib/DOM/traits/Ancestor.php @@ -0,0 +1,27 @@ +ancestor($needle, true); + } + + public static function hasAncestor($needle): bool { + return $this->ancestor($needle, false); + } + + protected function ancestor($needle, bool $returnNode = true) { + $context = $this->parentNode; + do { + $result = static::compare($needle, $context); + if (!is_null($result)) { + return ($returnNode === true) ? $result : true; + } + } while ($context = $context->parentNode); + + return ($returnNode === true) ? null : false; + } +} \ No newline at end of file diff --git a/lib/DOM/Node.php b/lib/DOM/traits/Compare.php similarity index 52% rename from lib/DOM/Node.php rename to lib/DOM/traits/Compare.php index 4c7c583..852bc83 100644 --- a/lib/DOM/Node.php +++ b/lib/DOM/traits/Compare.php @@ -2,27 +2,7 @@ declare(strict_types=1); namespace dW\HTML5; -trait Node { - public function getAncestor($needle): Element { - return $this->ancestor($needle, true); - } - - public static function hasAncestor($needle): bool { - return $this->ancestor($needle, false); - } - - protected function ancestor($needle, bool $returnNode = true) { - $context = $this->parentNode; - do { - $result = static::compare($needle, $context); - if (!is_null($result)) { - return ($returnNode === true) ? $result : true; - } - } while ($context = $context->parentNode); - - return ($returnNode === true) ? null : false; - } - +trait Compare { protected function compare($needle, Element $context): \DOMNode { if (is_string($needle)) { if ($context->nodeName == $needle) { diff --git a/lib/DOM/traits/Descendant.php b/lib/DOM/traits/Descendant.php new file mode 100644 index 0000000..480a2e2 --- /dev/null +++ b/lib/DOM/traits/Descendant.php @@ -0,0 +1,37 @@ +hasChildNodes() === false) { + return ($returnNode === true) ? null : false; + } + + $context = $this->firstChild; + + do { + $result = $this->compare($needle, $context); + if (!is_null($result)) { + return ($returnNode === true) ? $result : true; + } + + $result = $this->descendant($needle, $context); + if (!is_null($result)) { + return ($returnNode === true) ? $result : true; + } + } while ($context = $context->nextSibling); + + return ($returnNode === true) ? null : false; + } +} \ No newline at end of file diff --git a/lib/DOM/Printer.php b/lib/DOM/traits/Printing.php similarity index 99% rename from lib/DOM/Printer.php rename to lib/DOM/traits/Printing.php index 4a7f8ed..c1e6091 100644 --- a/lib/DOM/Printer.php +++ b/lib/DOM/traits/Printing.php @@ -2,7 +2,7 @@ declare(strict_types=1); namespace dW\HTML5; -trait Printer { +trait Printing { protected $selfClosingElements = ['area', 'base', 'basefont', 'bgsound', 'br', 'col', 'embed', 'frame', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr']; public function saveHTML(\DOMNode $node = null): string {