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 run($str = NULL) { if ($str == NULL) { return $this; } $dom = $this->node->dom()->get(); $this->node->_lastDom = $dom->lastdom . $str; $rec = new DomDocument(); $rec->loadHtml($str); if ($rec->doctype) { $rec->removeChild($rec->doctype); } $document = jqm_use($this->node->_parentElement); $dom = $document->_DOM; if ($dom->doctype) { $dom->removeChild($dom->doctype); } $body = $rec->childNodes->item(0)->firstChild; $frag = $dom->importNode($body, true); $xpath = new DomXpath($dom); $find = $this->node->getPathByID($dom); $find = $xpath->query($find); if ($find->length > 0) { $save = $find->item(0)->parentNode->insertBefore($frag->firstChild, $find->item(0)->nextSibling); $this->node->_path = $save->previousSibling->getNodePath(); $document->_DOM = $dom; } return $this; }
public function truncatehtml($html, $minimum) { $oldDocument = new \DomDocument(); $html = mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'); $oldDocument->loadHTML('<div>' . $html . '</div>'); // remove DOCTYPE, HTML and BODY tags $oldDocument->removeChild($oldDocument->firstChild); $oldDocument->replaceChild($oldDocument->firstChild->firstChild->firstChild, $oldDocument->firstChild); $currentLength = 0; // displayed text length (without markup) $newDocument = new \DomDocument(); foreach ($oldDocument->documentElement->childNodes as $node) { if ($node->nodeType != 3) { // not text node $imported = $newDocument->importNode($node, true); $newDocument->appendChild($imported); // copy original node to output document $currentLength += strlen(html_entity_decode($imported->nodeValue)); if ($currentLength >= $minimum) { // check if the minimum is reached break; } } } $output = $newDocument->saveHTML(); return html_entity_decode($output); }
if (!$buf) { die($xml); } $mime_type = NULL; if (isset($_REQUEST['filter_extension'])) { require_once dirname(__FILE__) . '/includes/mime.inc.php'; $mime_type = getMimeTypefromExtension($_REQUEST['filter_extension']); } else { if (isset($_REQUEST['filter_mime'])) { $mime_type = $_REQUEST['filter_mime']; } } if (!is_null($mime_type)) { $user_node = $dom->documentElement; $user_node_new = $user_node->cloneNode(false); $applications_nodes = $user_node->getElementsByTagName('application'); foreach ($applications_nodes as $application_node) { $found_mime = false; $mime_nodes = $application_node->getElementsByTagName('mime'); foreach ($mime_nodes as $mime_node) { if ($mime_node->getAttribute('type') == $mime_type) { $user_node_new->appendChild($application_node->cloneNode(true)); break; } } } $dom->removeChild($user_node); $dom->appendChild($user_node_new); } header('Content-Type: text/xml'); echo $dom->saveXML();
public static function html($var, $array = null, $default = '') { $var = (string) self::getVal($var, $array, $default); //$var = Filter::utf8($var); if (!preg_match('/\\S/', $var)) { return $default; } // corrige les </li> $var = preg_replace('#>[\\r\\n]+#m', '>', $var); //ignore error libxml_use_internal_errors(true); $dom = new \DomDocument('1.0', 'UTF-8'); $dom->recover = true; // this is the trick $var = '<div>' . $var . '</div>'; try { // hack to preserve UTF-8 characters //http://stackoverflow.com/questions/3548880/php-dom-utf-8-problem $dom->loadHTML('<?xml encoding="UTF-8">' . $var); // hack fix utf-8 $dom->encoding = 'UTF-8'; } catch (\Exception $e) { } if (!function_exists('filterHtmlCleanChild')) { function filterHtmlCleanChild(DOMElement $p, $removeTag, $removeAttr) { if ($p->hasChildNodes()) { foreach ($p->childNodes as $c) { // on delete ce fils if (in_array($c->nodeName, $removeTag)) { $p->removeChild($c); } elseif ($c->nodeType == XML_ELEMENT_NODE) { filterHtmlCleanChild($c, $removeTag, $removeAttr); } } } foreach ($removeAttr as $ra) { $p->removeAttribute($ra); /** test if ( $ra == 'onkeydown' ) { if ( $va = $p->getAttribute('onkeydown') ) { $p->removeAttribute($ra); $rgx = '#*media\{id:([0-9]*)\}*#'; preg_match($rgx, $va, $m); $r = array(); if ( isset($m[1]) ) { $p->setAttribute($ra,"media{id:".$m[1]."}"); } } */ } return $p; } } $removeAttr = array('onclick', 'ondblclick', 'onkeydown', 'onkeypress', 'onkeyup', 'onload', 'onmousedown', 'onmousemove', 'onmouseout', 'onmouseover', 'onmouseup', 'onunload'); $removeTag = array('script'); foreach ($dom->childNodes as $p) { if ($p->nodeType == XML_PI_NODE) { $dom->removeChild($p); } elseif ($p->hasChildNodes()) { filterHtmlCleanChild($p, $removeTag, $removeAttr); } } $dom->normalizeDocument(); $r = $dom->saveXML($dom->childNodes->item(1)->childNodes->item(0)->childNodes->item(0), LIBXML_NOEMPTYTAG); $r = preg_replace('#^<div>#', '', $r); $var = preg_replace('#</div>$#', '', $r); // fix certain navigateur compte 2 <br> pour <br></br>, et n'accepte pas les <iframe /> $var = preg_replace('#<br></br>#', '<br />', $var); return self::setVal($var, $default); }
private function parseFull($xml, $encoding = null) { $dom = new \DomDocument(); if ($encoding) { $xml = '<?xml encoding="' . $encoding . '">' . $xml; } libxml_disable_entity_loader(); // prevents XXE attacks $prevErrorSetting = libxml_use_internal_errors(true); if ($dom->loadXML($xml)) { if ($encoding) { foreach ($dom->childNodes as $item) { if ($item->nodeType == XML_PI_NODE) { $dom->removeChild($item); break; } } $dom->encoding = $encoding; } libxml_use_internal_errors($prevErrorSetting); return new Proxy(simplexml_import_dom($dom), $this); } $errors = libxml_get_errors(); libxml_clear_errors(); libxml_use_internal_errors($prevErrorSetting); $message = 'Incorrect xml passed.'; foreach ($errors as $error) { $message .= '\\nline: ' . $error->line . '; column: ' . $error->column . '; ' . $error->message; } throw new \arc\UnknownError($message, \arc\exceptions::ILLEGAL_ARGUMENT); }