/** * Wrap this element in another element at the same place in the tree. * @param FluentHtmlElement $wrapper * @return $this */ protected function withWrapper(FluentHtmlElement $wrapper) { if ($this->hasParent()) { $parent = $this->getParentElement(); $this->setParent(null); } $wrapper->withAppendedContent($this); if (!empty($parent)) { $parent->html_contents->transform(function ($item) use($wrapper, $parent) { if ($this === $item) { $wrapper->setParent($parent); return $wrapper; } else { return $item; } }); } return $this; }