Skip to content
This repository has been archived by the owner on Mar 19, 2020. It is now read-only.

Commit

Permalink
Added wrapInner() and some more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyBogdanov committed Apr 1, 2018
1 parent 5af1dcc commit 60a2fcd
Show file tree
Hide file tree
Showing 40 changed files with 2,242 additions and 1,497 deletions.
27 changes: 24 additions & 3 deletions classes/Dom.php
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,24 @@ public function wrap($content): Dom
return $this;
}

/**
* Wrap a clone of the supplied content around each child node (not only element nodes) of nodes in the collection.
*
* This function works exactly like wrap() except it wraps the children of the nodes in the collection instead.
*
* Return the original collection for chaining.
*
* @param $content
* @return Dom
*/
public function wrapInner($content): Dom
{
foreach ($this->children() as $child) {
$child->wrap($content);
}
return $this;
}

/**
* Return a new Dom collection of all the descendants of each Element node in the current collection,
* filtered by the specified CSS selector.
Expand Down Expand Up @@ -662,13 +680,15 @@ public function find(string $selector): Dom
*/
public function addClass(string $className): Dom
{
$addClasses = preg_split('/\s+/', $className);
$className = trim($className);

// bail if no classes to add
if (0 === count($addClasses)) {
if ('' === $className) {
return $this;
}

$addClasses = preg_split('/\s+/', $className);

/** @var NodeInterface $node */
foreach ($this->nodes as $node) {
if (!$node instanceof Element) {
Expand All @@ -677,7 +697,8 @@ public function addClass(string $className): Dom

// if the node already has a "class" attribute, merge all classes & make sure the result is unique
if ($node->hasAttribute('class')) {
$currentClasses = preg_split('/\s+/', $node->getAttribute('class'));
$currentClassName = trim($node->getAttribute('class'));
$currentClasses = '' === $currentClassName ? [] : preg_split('/\s+/', $currentClassName);
$node->setAttribute('class', implode(' ', array_unique(array_merge($currentClasses, $addClasses))));
}

Expand Down
9 changes: 9 additions & 0 deletions classes/SelectorMatcher/AttributeNodeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SDom\SelectorMatcher;

use SDom\Node\Element;
use SDom\Node\NodeInterface;
use SDom\SelectorMatcher;
use Symfony\Component\CssSelector\Node\AttributeNode;

Expand Down Expand Up @@ -114,4 +115,12 @@ protected function matchAttributeNode(AttributeNode $token, Element $node, Eleme

return $this->match($token->getSelector(), $node, $effectiveRoot);
}

/**
* @param NodeInterface $token
* @param Element $node
* @param Element|null $effectiveRoot
* @return bool
*/
abstract public function match(NodeInterface $token, Element $node, Element $effectiveRoot = null): bool;
}
9 changes: 9 additions & 0 deletions classes/SelectorMatcher/ClassNodeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SDom\SelectorMatcher;

use SDom\Node\Element;
use SDom\Node\NodeInterface;
use SDom\SelectorMatcher;
use Symfony\Component\CssSelector\Node\ClassNode;

Expand Down Expand Up @@ -36,4 +37,12 @@ protected function matchClassNode(ClassNode $token, Element $node, Element $effe

return $this->match($token->getSelector(), $node, $effectiveRoot);
}

/**
* @param NodeInterface $token
* @param Element $node
* @param Element|null $effectiveRoot
* @return bool
*/
abstract public function match(NodeInterface $token, Element $node, Element $effectiveRoot = null): bool;
}
9 changes: 9 additions & 0 deletions classes/SelectorMatcher/CombinedSelectorNodeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SDom\SelectorMatcher;

use SDom\Node\Element;
use SDom\Node\NodeInterface;
use Symfony\Component\CssSelector\Node\CombinedSelectorNode;

/**
Expand Down Expand Up @@ -211,4 +212,12 @@ protected function matchCombinedSelectorNode(
));
}
}

/**
* @param NodeInterface $token
* @param Element $node
* @param Element|null $effectiveRoot
* @return bool
*/
abstract public function match(NodeInterface $token, Element $node, Element $effectiveRoot = null): bool;
}
9 changes: 9 additions & 0 deletions classes/SelectorMatcher/ElementNodeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SDom\SelectorMatcher;

use SDom\Node\Element;
use SDom\Node\NodeInterface;
use Symfony\Component\CssSelector\Node\ElementNode;

/**
Expand Down Expand Up @@ -34,4 +35,12 @@ protected function matchElementNode(ElementNode $token, Element $node): bool
// node tag name must match
return $node->getTag() === $token->getElement();
}

/**
* @param NodeInterface $token
* @param Element $node
* @param Element|null $effectiveRoot
* @return bool
*/
abstract public function match(NodeInterface $token, Element $node, Element $effectiveRoot = null): bool;
}
9 changes: 9 additions & 0 deletions classes/SelectorMatcher/HashNodeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SDom\SelectorMatcher;

use SDom\Node\Element;
use SDom\Node\NodeInterface;
use Symfony\Component\CssSelector\Node\HashNode;

/**
Expand Down Expand Up @@ -35,4 +36,12 @@ protected function matchHashNode(HashNode $token, Element $node, Element $effect

return $this->match($token->getSelector(), $node, $effectiveRoot);
}

/**
* @param NodeInterface $token
* @param Element $node
* @param Element|null $effectiveRoot
* @return bool
*/
abstract public function match(NodeInterface $token, Element $node, Element $effectiveRoot = null): bool;
}
Loading

0 comments on commit 60a2fcd

Please sign in to comment.