Browse Source

Organization

• Moved the ancestor and descendant methods into their own traits along with the compare method which they share.
• Made DocumentFragment use only the descendant methods and not the ancestor ones.
• Fixed error in README.
ns
Dustin Wilson 6 years ago
parent
commit
dfda8d5f3a
  1. 2
      README.md
  2. 3
      composer.json
  3. 2
      lib/DOM/Comment.php
  4. 2
      lib/DOM/Document.php
  5. 2
      lib/DOM/DocumentFragment.php
  6. 34
      lib/DOM/Element.php
  7. 2
      lib/DOM/ProcessingInstruction.php
  8. 2
      lib/DOM/Text.php
  9. 27
      lib/DOM/traits/Ancestor.php
  10. 22
      lib/DOM/traits/Compare.php
  11. 37
      lib/DOM/traits/Descendant.php
  12. 2
      lib/DOM/traits/Printing.php

2
README.md

@ -12,7 +12,7 @@ or:
```php
<?php
$dom = new dW\HTML\Document;
$dom = new dW\HTML5\Document;
$dom->loadHTML('<!DOCTYPE html><html lang="en" charset="utf-8"><head><title>Ook!</title></head><body><h1>Ook!</h1><p>Ook-ook? Oooook. Ook ook oook ook oooooook ook ooook ook.</p><p>Eek!</p></body></html>');
?>
```

3
composer.json

@ -20,7 +20,8 @@
"psr-4": {
"dW\\HTML5\\": [
"lib/",
"lib/DOM"
"lib/DOM",
"lib/DOM/traits"
]
},
"classmap": ["lib/Token.php"]

2
lib/DOM/Comment.php

@ -3,5 +3,5 @@ declare(strict_types=1);
namespace dW\HTML5;
class Comment extends \DOMComment {
use Node;
use Ancestor;
}

2
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();

2
lib/DOM/DocumentFragment.php

@ -3,5 +3,5 @@ declare(strict_types=1);
namespace dW\HTML5;
class DocumentFragment extends \DOMDocumentFragment {
use Node;
use Descendant;
}

34
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;
}
}

2
lib/DOM/ProcessingInstruction.php

@ -3,5 +3,5 @@ declare(strict_types=1);
namespace dW\HTML5;
class ProcessingInstruction extends \DOMProcessingInstruction {
use Node;
use Ancestor;
}

2
lib/DOM/Text.php

@ -3,5 +3,5 @@ declare(strict_types=1);
namespace dW\HTML5;
class Text extends \DOMText {
use Node;
use Ancestor;
}

27
lib/DOM/traits/Ancestor.php

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace dW\HTML5;
trait Ancestor {
use Compare;
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;
}
}

22
lib/DOM/Node.php → 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) {

37
lib/DOM/traits/Descendant.php

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace dW\HTML5;
trait Descendant {
use Compare;
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;
}
}

2
lib/DOM/Printer.php → 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 {
Loading…
Cancel
Save