Browse Source
• 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
12 changed files with 77 additions and 60 deletions
@ -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; |
|||
} |
|||
} |
@ -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; |
|||
} |
|||
} |
Loading…
Reference in new issue