public function run($str = NULL) { if ($str == NULL) { return $this; } $document = jqm_use($this->node->_parentElement); $dom = $document->_DOM; if ($dom->doctype) { $dom->removeChild($dom->doctype); } $find = $this->node->getPathById($dom); if (!$find) { $find = $this->node->_path; } $xpath = new DomXpath($dom); $find = $xpath->query($find); if ($find->length > 0) { $child = new DomDocument(); $child->loadHtml($str); if ($child->doctype) { $child->removeChild($child->doctype); } $child->normalize(); $frag = $dom->importNode($child->firstChild->firstChild->firstChild, true); $save = $find->item(0)->parentNode->insertBefore($frag, $find->item(0)); $this->node->_path = $save->nextSibling->getNodePath(); $document->_DOM = $dom; } return $this; }
public function getPreview($elements) { if (!isset($this->preview)) { if (!isset($elements)) { $elements = 2; } // Get just the text (no markup) from a node using $node->textContent. // Compare the textContent value to the one returned by $node->nodeValue. libxml_use_internal_errors(true); $dom = new DomDocument(); $dom->preserveWhiteSpace = false; $dom->loadHTML('<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>' . $this->Body . '</body></html>'); $dom->normalize(); $nodes = $dom->getElementsByTagName("body")->item(0)->childNodes; $elementCount = 0; $this->preview = ''; foreach ($nodes as $node) { if ($node->nodeType === XML_ELEMENT_NODE) { $this->preview .= $dom->saveXML($node); $elementCount++; if ($elementCount === $elements) { break; } } } // Carriage returns in the XML prevent the markup from validating. -- cwells $this->preview = str_replace(' ', '', $this->preview); } return $this->preview; }