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.
This commit is contained in:
parent
fd6003fb4e
commit
dfda8d5f3a
12 changed files with 77 additions and 60 deletions
|
@ -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>');
|
||||
?>
|
||||
```
|
||||
|
|
|
@ -20,7 +20,8 @@
|
|||
"psr-4": {
|
||||
"dW\\HTML5\\": [
|
||||
"lib/",
|
||||
"lib/DOM"
|
||||
"lib/DOM",
|
||||
"lib/DOM/traits"
|
||||
]
|
||||
},
|
||||
"classmap": ["lib/Token.php"]
|
||||
|
|
|
@ -3,5 +3,5 @@ declare(strict_types=1);
|
|||
namespace dW\HTML5;
|
||||
|
||||
class Comment extends \DOMComment {
|
||||
use Node;
|
||||
use Ancestor;
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -3,5 +3,5 @@ declare(strict_types=1);
|
|||
namespace dW\HTML5;
|
||||
|
||||
class DocumentFragment extends \DOMDocumentFragment {
|
||||
use Node;
|
||||
use Descendant;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,5 +3,5 @@ declare(strict_types=1);
|
|||
namespace dW\HTML5;
|
||||
|
||||
class ProcessingInstruction extends \DOMProcessingInstruction {
|
||||
use Node;
|
||||
use Ancestor;
|
||||
}
|
||||
|
|
|
@ -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
Normal file
27
lib/DOM/traits/Ancestor.php
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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
Normal file
37
lib/DOM/traits/Descendant.php
Normal file
|
@ -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,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…
Add table
Reference in a new issue