public function saveHtml($refresh = true) { if (extension_loaded('xml') and class_exists('DomXpath')) { if (get_parent_class($this) === __CLASS__) { $node = $this->node; } elseif (get_parent_class($this) === false) { $node = $this; } else { return false; } if (!is_a($node, jqmel)) { return false; } if ($node->_length === false) { return false; } $docm = jqm_use($node->_parentElement); //$docm->__documentRaw = preg_replace('/\s+/',' ',$docm->__documentRaw); $doc = $docm->_DOM; if ($doc->doctype) { $doc->removeChild($doc->doctype); } $k = key($this->_domId); if ($refresh == true) { $this->refresh(); $node->_attributes['data-dom-id'] = $k; } else { if ($this->_domId[$k] === NULL) { $attr = $node->_attributes; unset($attr['data-dom-id']); $node->_attributes = $attr; } else { $node->_attributes['data-dom-id'] = $this->_domId[$k]; } } $newdoc = new domDocument(); $dom = $node->dom()->get(); $newdoc->loadhtml(mb_convert_encoding($dom->lastdom, 'HTML-ENTITIES', 'UTF-8')); $newdoc->removeChild($newdoc->doctype); $newdoc_get = $newdoc->getElementsByTagName($node->_name); //Finder $xpath = new DomXpath($doc); $find = $xpath->query($node->_path); if ($find->length > 0) { $frag = $doc->importNode($newdoc_get->item(0), true); if ($find->item(0)->parentNode) { $find->item(0)->parentNode->replaceChild($frag, $find->item(0)); } /* $output = $doc->saveHTML(); $hasHTML = (strip_tags($docm->__documentRaw,'<html>') !==$docm->__documentRaw); if($hasHTML===false){ //Return just a partial html $docm->__documentRaw = str_ireplace(array('<html>','<body>','</html>','</body>','<head>','</head>'),'',$output); }else{ $docm->__documentRaw = $output; } */ $node->_attributes['data-dom-id'] = $this->_domId[$k]; return $frag->getNodepath(); } } }
/** * * @get text between tags * @param string $tag The tag name * @param string $html The XML or XHTML string * @param int $strict Whether to use strict mode * @param string $encoding * @return array */ private function getTextBetweenTags($tag, $html, $strict = 0, $encoding = "UTF-8") { global $PAGE, $CFG; if (!isset($CFG->filter_jsxgraph_divid)) { set_config('filter_jsxgraph_divid', 'box'); } if (!isset($CFG->filter_jsxgraph_boardvar)) { set_config('filter_jsxgraph_boardvar', 'board'); } if (!isset($CFG->filter_jsxgraph_width)) { set_config('filter_jsxgraph_width', '500'); } if (!isset($CFG->filter_jsxgraph_height)) { set_config('filter_jsxgraph_height', '400'); } // a new dom object $dom = new domDocument(); $dom->formatOutput = true; // load the html into the object if ($strict == 1) { $dom->loadXML($html); } else { libxml_use_internal_errors(true); $htmlutf8 = mb_convert_encoding($html, 'HTML-ENTITIES', $encoding); $dom->loadHTML($htmlutf8); libxml_use_internal_errors(false); } // discard white space $dom->preserveWhiteSpace = false; $dom->strictErrorChecking = false; $dom->recover = true; // the tag by its tag name $content = $dom->getElementsByTagname($tag); if (count($content) > 0) { $PAGE->requires->js(new moodle_url($CFG->wwwroot . '/filter/jsxgraph/jsxgraphcore.js')); } // Iterate backwards through the jsxgraph tags $i = $content->length - 1; while ($i > -1) { $item = $content->item($i); // Read attributes $w = $item->getAttribute('width'); if ($w == "") { $w = $CFG->filter_jsxgraph_width; } $h = $item->getAttribute('height'); if ($h == "") { $h = $CFG->filter_jsxgraph_height; } $b = $item->getAttribute('box'); if ($b == "") { $b = $CFG->filter_jsxgraph_divid . $i; } $brd = $item->getAttribute('board'); if ($brd == "") { $brd = $CFG->filter_jsxgraph_boardvar . $i; } /* Create new div element containing JSXGraph */ $out = $dom->createElement('div'); $a = $dom->createAttribute('id'); $a->value = $b; $out->appendChild($a); $a = $dom->createAttribute('class'); $a->value = "jxgbox"; $out->appendChild($a); $a = $dom->createAttribute('style'); $a->value = "width:" . $w . "px; height:" . $h . "px; "; $out->appendChild($a); $t = $dom->createTextNode(""); $out->appendChild($t); $out = $dom->appendChild($out); // Replace <jsxgraph> by <div> $item->parentNode->replaceChild($out, $item); $code = ""; $needGXT = false; $url = $item->getAttribute('file'); if ($url != "") { $code = "var " . $brd . " = JXG.JSXGraph.loadBoardFromFile('" . $b . "', '" . $url . "', 'Geonext');"; $needGXT = true; } else { $url = $item->getAttribute('filestring'); if ($url != "") { $code = "var " . $brd . " = JXG.JSXGraph.loadBoardFromString('" . $b . "', '" . $url . "', 'Geonext');"; $needGXT = true; } else { // Plain JavaScript code $code = $item->nodeValue; } } // Place JavaScript code at the end of the page. $PAGE->requires->js_init_call($code); if ($needGXT) { $PAGE->requires->js(new moodle_url($CFG->wwwroot . '/filter/jsxgraph/GeonextReader.js')); } --$i; } // remove DOCTYPE $dom->removeChild($dom->firstChild); // remove <html><body></body></html> //$dom->replaceChild($dom->firstChild->firstChild->firstChild, $dom->firstChild); //return $dom->saveXML(); $str = $dom->saveHTML(); $str = str_replace("<body>", "", $str); $str = str_replace("</body>", "", $str); $str = str_replace("<html>", "", $str); $str = str_replace("</html>", "", $str); return $str; }
public function run($str = nULL) { if (!is_null($str)) { $regex = '/<([a-z0-9\\-]+)(.*?)>(?:(.*?)(<\\/\\1>))?/ix'; if (is_a($str, jqmel)) { $str = $str->dom()->get()->lastdom; } elseif (is_callable($str)) { $str = call_user_func($str, array()); } $str = preg_replace('/\\s+/', ' ', $str); if (preg_match($regex, $str, $match)) { $match[2] = rtrim($match[2], '/'); } $document = jqm_use($this->node->_parentElement); $np = $this->node->next()->get(); $npp = $this->node->prev()->get(); $par = $this->node->parent(); $wrap = $str; if (!is_a($par->get(), jqmel)) { return; } $rec = $document->_DOM; if ($rec->doctype) { $rec->removeChild($rec->doctype); } $xpath = new domxpath($rec); $find = $xpath->query($this->node->_path); if ($find->length > 0) { $child = new domDocument(); $child->loadHtml($wrap); if ($child->doctype) { $child->removeChild($child->doctype); } $chf = $child->getElementsByTagName($match[1]); $node = $this->node->__toDomElement(); $frag = $child->importNode($node, true); $depth = $this->getDeepest($chf->item(0)); $save = $depth->appendChild($frag); //Import to document $frag = $rec->importNode($chf->item(0), true); $newsave = $find->item(0)->parentNode->replaceChild($frag, $find->item(0)); $new_path = explode('/', $frag->getNodePath()); $p_path = array_filter(explode('/', $save->getNodePath())); //$p_path = array_pop($p_path); $htm = array_search('html', $p_path); if ($htm) { unset($p_path[$htm]); unset($p_path[$htm + 1]); } $new_path = array_merge($new_path, $p_path); $sh = array_pop($new_path); array_pop($new_path); $ele_path = ltrim($newsave->getNodePath(), '/'); $this->node->_parent_path = implode('/', $new_path); $this->node->_path = $this->node->_parent_path . '/' . $sh; $wrap = $rec->saveHtml($frag->parentNode); $wrap = substr($wrap, strpos($wrap, '>') + 1); if (!empty($par->get()->_localName)) { $wrap = substr($wrap, 0, strripos($wrap, $par->get()->_localName)); $par->get()->html($wrap); } $find = $this->node->getPathById(); if ($find) { $this->node->_path = $find; } if ($this->node->trace) { $key = $this->node->trace->key(); $this->node->trace->next(); $n = $this->node->trace->current(); if ($key > 0) { $this->node->trace->seek($key - 1); $p = $this->node->trace->current(); } if ($p and is_a($p, jqmel)) { $find = $p->getPathById(); if ($find) { $p->_path = $find; //var_dump($key,$p); } } if ($n and is_a($n, jqmel)) { $find = $n->getPathById(); if ($find) { $n->_path = $find; } } unset($p); unset($n); } } } }