diff --git a/README.md b/README.md index 7ca49fe..273aea7 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,7 @@ DomQuery::create('')->attr('title') // hello - `.wrap( [content] )` - `.wrapAll( [content] )` - `.wrapInner( [content] )` +- `.unwrap( )` - `.remove( [selector] )` \* __[content]__ can be html or an instance of DomQuery|DOMNodeList|DOMNode diff --git a/src/Rct567/DomQuery/DomQuery.php b/src/Rct567/DomQuery/DomQuery.php index 9198519..29ac34c 100644 --- a/src/Rct567/DomQuery/DomQuery.php +++ b/src/Rct567/DomQuery/DomQuery.php @@ -1283,6 +1283,26 @@ public function addBack() return $this; } + /** + * Unwrap all the the matched elements. + * + * @return $this + */ + public function unwrap() + { + foreach ($this as $node) { + $parent = $node->parent(); + // Replace parent node (the one we're unwrapping) with it's children. + foreach ($parent->contents() as $childNode) { + $oldChildNode = $childNode->clone(); + $childNode->remove(); + $parent->before($oldChildNode); + } + $parent->remove(); + } + return $this; + } + /** * Check if property exist for this instance * diff --git a/tests/Rct567/DomQuery/Tests/DomQueryManipulationTest.php b/tests/Rct567/DomQuery/Tests/DomQueryManipulationTest.php index 74ad91f..7b8d0a2 100644 --- a/tests/Rct567/DomQuery/Tests/DomQueryManipulationTest.php +++ b/tests/Rct567/DomQuery/Tests/DomQueryManipulationTest.php @@ -105,6 +105,36 @@ public function testWrapWithMultipleElementsWrapper() $this->assertEquals("
", (string) $dom); } + /* + * Test unwrap. + */ + public function testUnwrap() + { + $expected = 'this is a test'; + $doc = DomQuery::create('