Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ DomQuery::create('<a title="hello"></a>')->attr('title') // hello
- `.wrap( [content] )`
- `.wrapAll( [content] )`
- `.wrapInner( [content] )`
- `.unwrap( )`
- `.remove( [selector] )`

<sub>\* __[content]__ can be html or an instance of DomQuery|DOMNodeList|DOMNode</sub>
Expand Down
25 changes: 18 additions & 7 deletions src/Rct567/DomQuery/DomQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,9 @@ private function importNodes($content, callable $import_function)
foreach ($this->nodes as $node) {
foreach ($content->getNodes() as $content_node) {
if ($content_node->ownerDocument === $node->ownerDocument) {
$imported_node = $content_node->cloneNode(true);
// No clone so that the element used with replaceWith()
// gives a connected dom element.
$imported_node = $content_node;
} else {
$imported_node = $this->document->importNode($content_node, true);
}
Expand Down Expand Up @@ -1113,12 +1115,6 @@ public function replaceWith()
$node->parentNode->removeChild($node);
});

foreach (\func_get_args() as $new_content) {
if (!\is_string($new_content)) {
self::create($new_content)->remove();
}
}

return $removed_nodes;
}

Expand Down Expand Up @@ -1202,6 +1198,21 @@ public function wrapInner()
return $this;
}

/**
* Unwrap all the the matched elements.
*
* @param string|self|callable|\DOMNodeList|\DOMNode|false|null $selector expression that filters the set of matched elements
*
* @return self
*/
public function unwrap($selector=null)
{
foreach ($this->parent($selector) as $parent) {
$parent->replaceWith($this->nodes);
}
return $this;
}

/**
* Check if property exist for this instance
*
Expand Down
30 changes: 30 additions & 0 deletions tests/Rct567/DomQuery/Tests/DomQueryManipulationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,36 @@ public function testWrapWithMultipleElementsWrapper()
$this->assertEquals("<p><div><x><a>Hello</a></x></div></p>", (string) $dom);
}

/*
* Test unwrap.
*/
public function testUnwrap()
{
$expected = '<html><body><a href="http://example.org/">this is a test</a></body></html>';
$doc = DomQuery::create('<html><body><article><a href="http://example.org/">this is a test</a></article></body></html>');
$doc->find('article > a')->first()->unwrap();
$this->assertEquals($expected, (string) $dom);
}

/*
* Test wraping and unwrapping.
*/
public function testWrapAllandUnwrap()
{
$input = '<div>' . PHP_EOL .
' <div id="target" class="my-class">' . PHP_EOL .
' <div class="nested-class">' . PHP_EOL .
' <span class="my-span"></span>' . PHP_EOL .
' </div>' . PHP_EOL .
' <div class="nested-class">' . PHP_EOL .
' <span class="my-span"></span>' . PHP_EOL .
' </div>' . PHP_EOL .
' </div>' . PHP_EOL .
'</div>';
$dom->find('#target')->wrapAll('<div id="extra-wrap"></div>')->parent()->children()->unwrap();
$this->assertEquals($input, (string) $dom);
}

/*
* Test remove
*/
Expand Down