public function handleError(\Dwoo\Exception $e) { $html = new \DOMDocument(); $html->loadHTMLFile('lib/resources/exception.html'); $message = $html->getElementById('message'); $template = $html->createDocumentFragment(); $template->appendXML($e->getMessage()); $message->appendChild($template); unset($template); $php_version = $html->getElementById('php-version'); $template = $html->createDocumentFragment(); $template->appendXML(phpversion()); $php_version->appendChild($template); unset($template); $dwoo_version = $html->getElementById('dwoo-version'); $template = $html->createDocumentFragment(); $template->appendXML(Core::VERSION); $dwoo_version->appendChild($template); unset($template); $exectime = $html->getElementById('exectime'); $template = $html->createDocumentFragment(); $template->appendXML(round(microtime(true) - $_SERVER['REQUEST_TIME'], 3)); $exectime->appendChild($template); unset($template); echo $html->saveHTML(); }
function boldCapitals() { // gets an instance of the object to display $CI =& get_instance(); // gets output of objects $page = $CI->output->get_output(); // this page's HTML DOM object $dom = new DOMDocument(); $dom->loadHTML($page); // gets element with 'p' tags $pList = $dom->getElementsByTagName('p'); // search for the following characters $search = array('/([A-Z]+[A-Za-z]*)/'); // repleaces characters we want to replace $replace = array('<strong>$1</strong>'); // loops through each 'p' tag on the page foreach ($pList as $p) { if ($p->getAttribute('class' == 'lead')) { $string = $p->nodeValue; // replace characters with a bold and capitalized characters $p->nodeValue = preg_replace($search, $replace, $string); // create a new tag object $frag = $dom->createDocumentFragment(); $frag->appendXML($string); // append new object to the element $p->nodeValue = ''; $p->appendChild($frag); } } //output the changes back to the screen echo $dom->saveHTML(); }
static function iqWrapper($xml = false, $to = false, $type = false, $id = false) { $session = \Sessionx::start(); $dom = new \DOMDocument('1.0', 'UTF-8'); $iq = $dom->createElementNS('jabber:client', 'iq'); $dom->appendChild($iq); if ($to != false) { $iq->setAttribute('to', $to); } if ($type != false) { $iq->setAttribute('type', $type); } global $language; if ($id == false) { $id = $session->id; } $iq->setAttribute('id', $id); if (isset($language)) { $iq->setAttribute('xml:lang', $language); } if (isset($session->user)) { $iq->setAttribute('from', $session->user . '@' . $session->host . '/' . $session->resource); } if ($xml != false) { if (is_string($xml)) { $f = $dom->createDocumentFragment(); $f->appendXML($xml); $iq->appendChild($f); } else { $xml = $dom->importNode($xml, true); $iq->appendChild($xml); } } return $dom->saveXML($dom->documentElement); }
/** * Append an XML snippet. * * @param DOMNode $parent_node Attach the XML below this parent. * @param string $xml The XML to append. * * @return DOMNode The new child node. */ public function appendXml($parent_node, $xml) { $node = $this->_xmldoc->createDocumentFragment(); $node->appendXML($xml); $parent_node->appendChild($node); return $node; }
/** * Get the child objects * and render them as document fragment * * @param \DOMDocument $dom DOMDocument * @return \DOMDocumentFragment */ public function getChildElements(\DOMDocument $dom) { $modelChildren = $this->model->getElements(); $documentFragment = NULL; foreach ($modelChildren as $key => $modelChild) { $child = $this->createChildElementFromModel($modelChild); if ($child) { if ($child->noWrap() === TRUE) { $childNode = $child->render(); } else { $childNode = $child->render('elementWrap'); if ($childNode) { $childNode->setAttribute('class', $child->getElementWraps()); } } if ($childNode) { $importedNode = $dom->importNode($childNode, TRUE); if (!$documentFragment) { $documentFragment = $dom->createDocumentFragment(); } $documentFragment->appendChild($importedNode); } } } return $documentFragment; }
/** * Our function called via Twig; it can do anything you want * * @return string */ public function lettering($text = null, $class = 'chars') { if (!$text || strlen($text) === 0 || !method_exists(craft()->lettering, $class)) { return $text; } $dom = new LetteringDom(); $dom->loadHTML(mb_convert_encoding('<div id="workingNode">' . $text . '</div>', 'HTML-ENTITIES', $this->encoding)); $workingNode = $dom->getElementById('workingNode'); $fragment = $dom->createDocumentFragment(); foreach ($workingNode->childNodes as $node) { if ($node->nodeType !== 1) { continue; } $value = $node->nodeValue; $result = craft()->lettering->{$class}($value, $class); $node->nodeValue = ''; $tempFragment = new LetteringDom(); $tempFragment->loadHTML(mb_convert_encoding($result[$class], 'HTML-ENTITIES', $this->encoding)); foreach ($tempFragment->getElementsByTagName('body')->item(0)->childNodes as $tempNode) { $tempNode = $node->ownerDocument->importNode($tempNode, true); $node->appendChild($tempNode); } $node->setAttribute('aria-label', trim(strip_tags($value))); $fragment->appendChild($node->cloneNode(true)); } $workingNode->parentNode->replaceChild($fragment, $workingNode); $result = TemplateHelper::getRaw(preg_replace('~<(?:!DOCTYPE|/?(?:html|head|body))[^>]*>\\s*~i', '', $dom->saveHTML())); if (strlen(trim($result)) === 0) { $result = craft()->lettering->{$class}($text); return $result ? $result[$class] : $text; } return $result; }
public function onRenderProductListBefore(FilterResponseEvent $event) { $app = $this->app; $request = $event->getRequest(); $response = $event->getResponse(); $id = $request->query->get('category_id'); // category_idがない場合、レンダリングを変更しない if (is_null($id)) { return; } $CategoryContent = $app['category_content.repository.category_content']->find($id); // 登録がない、もしくは空で登録されている場合、レンダリングを変更しない if (is_null($CategoryContent) || $CategoryContent->getContent() == '') { return; } // 書き換えhtmlの初期化 $html = $response->getContent(); libxml_use_internal_errors(true); $dom = new \DOMDocument(); $dom->loadHTML('<?xml encoding="UTF-8">' . $html); $dom->encoding = "UTF-8"; $dom->formatOutput = true; // 挿入対象を取得 $navElement = $dom->getElementById('page_navi_top'); if (!$navElement instanceof \DOMElement) { return; } $template = $dom->createDocumentFragment(); $template->appendXML(htmlspecialchars($CategoryContent->getContent())); $node = $dom->importNode($template, true); $navElement->insertBefore($node); $newHtml = html_entity_decode($dom->saveHTML(), ENT_NOQUOTES, 'UTF-8'); $response->setContent($newHtml); $event->setResponse($response); }
function foo() { $d = new DOMDocument(); $c = $d->createDocumentFragment(); $g = $d->createElement('fiz', 'buz'); $h = $d->createElement('red', 'xen'); $c->appendChild($g); $c->appendChild($h); return $c->childNodes; }
/** * @param \DOMNode $node * @param string $val * * @return bool */ protected final function appendXMLString(\DOMNode $node, $val) { if (strlen($val) > 0) { $frag = $this->dom->createDocumentFragment(); $frag->appendXML($val); $node->appendChild($frag); return true; } return false; }
/** * Loads a partial into the template * * @param string $source The path and file name to the partial * @param string $target The target XPath to place the partial in */ protected function load($source, $target) { $xpath = new DOMXPath($this->document); $items = $xpath->query($target); if ($items->length > 0) { $source = $this->parse($source); $fragment = $this->document->createDocumentFragment(); $fragment->appendXML($source); $items->item(0)->appendChild($fragment); } }
public function __toString() { $doc = new DOMDocument(); $doc->loadXML('<add/>'); foreach ($this->documents as $document) { $frag = $doc->createDocumentFragment(); $frag->appendXml((string) $document); $doc->documentElement->appendChild($frag); } return $doc->saveXml($doc->documentElement); }
/** * Converts HTML to the corresponding XLIFF representation. * * @return string * The source HTML converted to XLIFF. */ public function toXLIFF($pretty_print = FALSE) { $this->doc->formatOutput = $pretty_print; // Do not use getElementById to comply with older versions of libxml. // getElementById doesn't work properly on libxml 2.7.6 (CentOS) $xpath = new \DOMXPath($this->doc); $wrapper_div = $xpath->query("//*[@id='eggs-n-cereal-dont-ever-use-this-id']")->item(0); $out = $this->doc->createDocumentFragment(); $domNodeList = array(); for ($i = 0; $i < $wrapper_div->childNodes->length; ++$i) { $domNodeList[] = $wrapper_div->childNodes->item($i); } $this->sanitizeMixedDomNodeList($this->doc, $domNodeList); foreach ($domNodeList as $domNode) { if ($output = $this->convert($domNode)) { $out->appendChild($output); } } return $this->doc->saveXML($out); }
public function getRecommendationsDom($html = null) { if (!$html) { $html = $this->toHtml(); } $dom = new DOMDocument('1.0', 'utf8'); $dom->loadXml('<root/>'); $fragment = $dom->createDocumentFragment(); $fragment->appendXML($html); return $fragment; }
/** * onRenderProductListBefore. * * @param FilterResponseEvent $event */ public function onRenderProductListBefore(FilterResponseEvent $event) { log_info('CategoryContent eccube.event.render.product_list.before start'); $app = $this->app; $request = $event->getRequest(); $response = $event->getResponse(); $id = $request->query->get('category_id'); // category_idがない場合、レンダリングを変更しない if (!$id) { return; } $CategoryContent = $app['eccube.plugin.category_content.repository.category_content']->find($id); // 登録がない、もしくは空で登録されている場合、レンダリングを変更しない if (!$CategoryContent || !$CategoryContent->getContent()) { log_info('CategoryContent eccube.event.render.product_list.before not content end'); return; } // twigから挿入するhtmlを生成する $snipet = $this->app->renderView('CategoryContent/Resource/template/default/category_content.twig', array('PluginCategoryContent' => $CategoryContent)); // htmlの挿入処理 $html = $response->getContent(); $search = self::CATEGORY_CONTENT_TAG; if (strpos($html, $search)) { // タグの位置に挿入する場合 log_info('Render category content with ', array('CATEGORY_CONTENT_TAG' => $search)); $replace = $search . $snipet; $newHtml = str_replace($search, $replace, $html); $response->setContent($newHtml); } else { // Elementを探して挿入する場合 libxml_use_internal_errors(true); $dom = new \DOMDocument(); $dom->loadHTML('<?xml encoding="UTF-8">' . $html); $dom->encoding = 'UTF-8'; $dom->formatOutput = true; // 基準となるElementを取得 $navElement = $dom->getElementById('topicpath'); if (!$navElement instanceof \DOMElement) { log_info('CategoryContent eccube.event.render.product_list.before not have dom end'); return; } // 挿入するNodeを生成 $template = $dom->createDocumentFragment(); $template->appendXML(htmlspecialchars($snipet)); $node = $dom->importNode($template, true); // 基準となるElementの直後にNodeを挿入し、Responsを書き換え $navElement->parentNode->insertBefore($node, $navElement->nextSibling); $newHtml = html_entity_decode($dom->saveHTML(), ENT_NOQUOTES, 'UTF-8'); $response->setContent($newHtml); } $event->setResponse($response); log_info('CategoryContent eccube.event.render.product_list.before end'); }
function foo() { $html = '<b>Hello</b><i>World</i>'; $doc = new DOMDocument(); $element = $doc->createDocumentFragment(); $element->appendXML($html); foreach ($element->childNodes->getIterator() as $child) { $element = null; $doc = null; var_dump($child->nodeValue); } }
function rerender($html, $frag = false) { $doc = new DOMDocument(); if ($frag) { $body = $doc->createDocumentFragment(); $body->appendXML($html); } else { $doc->loadHTML($html); $body = $doc->documentElement; } return helper($body); }
/** * Creates and returns a new node to attach to a document * * @param \DOMDocument $doc The root document this node is being created for * @return \DOMElement Returns the created node */ public function buildNode(\DOMDocument $doc) { $parent = $doc->createDocumentFragment(); foreach ($this->children as $child) { $child = \r8\XMLBuilder::buildNode($child, $doc); if ($child instanceof \DOMNode) { $parent->appendChild($child); } } if (!$parent->hasChildNodes()) { return $doc->createTextNode(""); } return $parent; }
/** * Iterates over a set of data and builds it as XML * * @param \DOMDocument $doc The document being built * @param String $parent The tag name of the parent element * @param Array|\Traversable $data An array or a traversable object * @param Boolean $root Whether the data being parsed is at the root level * @return DOMNode Returns the built node */ protected function iterate(\DOMDocument $doc, $parent, &$data, $root = FALSE) { if (!$root && is_array($data) && \r8\ary\isList($data)) { $node = $doc->createDocumentFragment(); foreach ($data as $value) { $node->appendChild($this->build($doc, $parent, $value, FALSE)); } } else { $node = $this->createElement($doc, $parent); foreach ($data as $key => $value) { $node->appendChild($this->build($doc, $key, $value, FALSE)); } } return $node; }
/** * Recordset to KML * Accepts and ADODB recordset, converts it to KML, and returns the result. * * @param object $rs record set object * @return string $kml resulting kml */ function rs2kml($rs) { if (!$rs) { trigger_error("Caught Exception: bad recordset passed to rs2kml function.", E_USER_ERROR); return false; } $kml = ''; $domxml = new DOMDocument('1.0', 'utf-8'); $root = $domxml->appendChild($domxml->createElement('kml')); $root->setAttribute('xmlns', 'http://www.opengis.net/kml/2.2'); $document = $root->appendChild($domxml->createElement('Document')); $style = $document->appendChild($domxml->createElement('Style')); $style->setAttribute('id', 'stylish'); $line = $style->appendChild($domxml->createElement('LineStyle')); $lineColor = $line->appendChild($domxml->createElement('color', 'ff0086ff')); $lineWidth = $line->appendChild($domxml->createElement('width', '3')); $poly = $style->appendChild($domxml->createElement('PolyStyle')); $lineColor = $poly->appendChild($domxml->createElement('color', '9aefefef')); while ($line = $rs->fetch(PDO::FETCH_ASSOC)) { $placemark = $document->appendChild($domxml->createElement('Placemark')); $name = null; $kml_geom = $domxml->createDocumentFragment(); $kml_geom->appendXML($line['kml']); $extended_data = $domxml->createElement('ExtendedData'); foreach ($line as $col_key => $col_val) { if ($col_key !== "kml") { if (!isset($name)) { $name = $domxml->createElement('name'); $name->appendChild($domxml->createTextNode($col_val)); } $data = $extended_data->appendChild($domxml->createElement('Data')); $data->setAttribute('name', strtolower($col_key)); $value = $data->appendChild($domxml->createElement('value')); $value->appendChild($domxml->createTextNode(trim($col_val))); } } $placemark->appendChild($name); $placemark->appendChild($kml_geom); $placemark->appendChild($extended_data); $placemark->appendChild($domxml->createElement('styleUrl', '#stylish')); } $domxml->formatOutput = true; $kml = $domxml->saveXML(); $domxml = null; return $kml; }
public function createDocumentFragment($fragment, $attrs = null, $js = false, $fragment_wrap = 'div') { $wrap = $this->createElement($fragment_wrap, null, $attrs); if ($js) { $fragment = $this->removeJS($fragment); $javaScript = $fragment[1]; $fragment = $this->entityDecode($fragment[0]); } else { $fragment = $this->entityDecode($fragment); } $nodes = parent::createDocumentFragment(); $nodes->appendXML($fragment); $wrap->appendChild($nodes); if ($js) { $this->replaceJS($wrap, $javaScript); } return $wrap; }
/** * * @param array $tab * @param DOMElement $ul * @param DOMElement $div */ public function _addTabContainer($tab, $ul, $div) { if (!empty($tab['content']) && empty($this->_active)) { $class = 'active '; $this->_active = true; } else { $class = ''; } if (!empty($tab['id']) && !$this->view->access($tab['id'])) { return false; } $li = $this->_dom->createElement('li'); $a = $this->_dom->createElement('a'); $a->setAttribute('data-toggle', 'tab'); $a->setAttribute('href', '#' . $tab['ref']); $a->appendChild($this->_dom->createTextNode($tab['label'])); $classLi = $class; if (empty($tab['released'])) { $classLi .= 'disabled'; } $li->setAttribute('class', $classLi); $li->appendChild($a); $ul->appendChild($li); $divPane = $this->_dom->createElement('div'); $divPane->setAttribute('id', $tab['ref']); $divPane->setAttribute('class', $class . 'tab-pane'); $div->appendChild($divPane); if (empty($tab['content'])) { $a->setAttribute('data-href', $tab['url']); $a->setAttribute('class', 'ajax-tab'); } else { /* require_once APPLICATION_PATH . '/../library/HTMLPurifier/HTMLPurifier.auto.php'; $config = HTMLPurifier_Config::createDefault(); $purifier = new HTMLPurifier( $config ); * */ $fragment = $this->_dom->createDocumentFragment(); $fragment->appendXML($tab['content']); //$purifier->purify( $tab['content'] ) ); $divPane->appendChild($fragment); } }
/** * check if the template html is valid (enough) * if not: set an error flag & error message */ protected function check_template() { $error = 0; $errorMessage = ''; $dom = new DOMDocument(); $dom->formatOutput = true; if ($this->template != '') { try { $content = $dom->createDocumentFragment(); $content->appendXML($this->template); $dom->appendChild($content); } catch (Exception $e) { $error = 1; $errorMessage = $e->getMessage(); } } $this->error = $error; $this->error_message = $errorMessage; }
protected function updateDocument() { if ($this->dirty) { $this->dirty = false; $self = $this; $this->rowCounter = 1; $fragment = $this->document->createDocumentFragment(); $xml = implode('', array_map(function ($row) use($self) { return '<row r="' . $self->incrementRowCounter() . '">' . implode('', array_map(function ($column) use($self) { return $self->toXMLColumn($column); }, $row)) . '</row>'; }, $this->rows)); if (!$fragment->appendXML($xml)) { throw new \Exception('Parsing XML failed'); } $this->getSheetData()->parentNode->replaceChild($s = $this->getSheetData()->cloneNode(false), $this->getSheetData()); $this->sheetData = $s; $this->getSheetData()->appendChild($fragment); } }
/** * Convert a given content xml string into and array of nodes * * @param string $content * @param boolean $includeTextNodes * @param integer $limit * @return array */ protected function _getContentFragment($content, $includeTextNodes = TRUE, $limit = 0) { $result = array(); $fragment = $this->_document->createDocumentFragment(); if ($fragment->appendXML($content)) { for ($i = $fragment->childNodes->length - 1; $i >= 0; $i--) { $element = $fragment->childNodes->item($i); if ($element instanceof DOMElement || $includeTextNodes && $this->_isNode($element)) { array_unshift($result, $element); $element->parentNode->removeChild($element); } } if ($limit > 0 && count($result) >= $limit) { return array_slice($result, 0, $limit); } return $result; } else { throw new UnexpectedValueException('Invalid document fragment'); } }
/** * Structure and create the full text in a DOMDocumentFragment. * * @param \DOMDocument $document - The document where this element will be appended (optional). * * @return \DOMDocumentFragment */ public function textToDOMDocumentFragment($document = null) { if (!$document) { $document = new \DOMDocument(); } $fragment = $document->createDocumentFragment(); // Generate markup foreach ($this->textChildren as $content) { if (Type::is($content, Type::STRING)) { $text = $document->createTextNode($content); $fragment->appendChild($text); } else { $fragment->appendChild($content->toDOMElement($document)); } } if (!$fragment->hasChildNodes()) { $fragment->appendChild($document->createTextNode('')); } return $fragment; }
public static function arrayToXml(\SimpleXMLElement $object = null, $data = array()) { foreach ($data as $key => $value) { if (is_object($value)) { $value = ArrayUtilsVTT::objectToArray($value); } if ($load = simplexml_load_string($value)) { $keyXMl = null; $doc = new \DOMDocument(); $doc->loadXML($object->asXML()); $appendXml = $doc->createDocumentFragment(); $keyXMl = is_numeric($key) ? StringUtilsVTT::ToPluralString($load->children()->getName()) : $key; $stringXml = "<{$keyXMl}>"; foreach ($load as $item) { $stringXml .= $item->asXML(); } $stringXml .= "</{$keyXMl}>"; $appendXml->appendXML($stringXml); $doc->documentElement->appendChild($appendXml); $object = new \SimpleXmlElement($doc->saveXML()); continue; } if (is_array($value)) { if (!is_numeric($key)) { $new_object = $object->addChild($key); } else { $new_object = $object->addChild($object->getName()); } self::arrayToXml($new_object, $value, false); } else { if (!is_numeric($key)) { $object->addChild($key, $value); } else { $object->addChild($object->getName(), $value); } } } return $object; }
/** * Dodaj kod wewnątrz elementu * * @param string $content * @param int $where * * @throws \UnexpectedValueException * * @return HtmlElement */ public function insertHtml($content, $where = self::CHILD_APPEND) { if (!is_string($content)) { throw new \UnexpectedValueException('content is not valid code'); } $code = trim($content); if (!empty($code)) { $fragment = self::$DOM->createDocumentFragment(); libxml_use_internal_errors(true); $fragment->appendXML($code); libxml_clear_errors(); switch ($where) { case self::CHILD_APPEND: $this->element->appendChild($fragment); break; case self::CHILD_PREPEND: $this->element->insertBefore($fragment, $this->element->childNodes->item(0)); break; } } return $this; }
/** * Create HTML / DOM structure for the list of headers to be used in template * @return [string] */ private function process_header_tags() { $headers = $this->SiteChecklist->getHeaders(); if (!$headers) { return ''; } $dom = new \DOMDocument(); $dom->appendChild($dom->createElement('h3', 'List of headers')); $table = $dom->createElement('table'); $tr = $dom->createElement('tr'); $tr->appendChild($dom->createElement('th', 'Tag')); $tr->appendChild($dom->createElement('th', 'Tag Content')); $table->appendChild($tr); foreach ($headers as $tag => $header) { $tr = $dom->createElement('tr'); $tr->appendChild($dom->createElement('td', $tag)); $frag = $dom->createDocumentFragment(); $frag->appendXML('<td>' . implode('<br/>', $header) . '</td>'); $tr->appendChild($frag); $table->appendChild($tr); } $dom->appendChild($table); return $dom->saveHTML(); }
/** * Microsoft exchange emails often include HTML which, when passed through * html2text, results in lots of double line returns everywhere. * * To fix this any element with a className of `msoNormal` (the standard * classname in any Microsoft export or outlook for a paragraph that behaves * like a line return) is changed to a line with a break `<br>` afterwards. * * This cleaned up document can then be processed as normal through Html2Text. * * @param DOMDocument $doc the document to clean up * @return DOMDocument the modified document with less unnecessary paragraphs */ static function fixMSEncoding($doc) { $paras = $doc->getElementsByTagName('p'); for ($i = $paras->length - 1; $i >= 0; $i--) { $para = $paras->item($i); if ($para->getAttribute('class') == 'MsoNormal') { $fragment = $doc->createDocumentFragment(); $fragment->appendChild($doc->createTextNode($para->nodeValue)); $fragment->appendChild($doc->createElement('br')); $new_node = $para->parentNode->replaceChild($fragment, $para); } } $doc->loadHTML($doc->saveHTML()); return $doc; }
/** * Helper function to create a DOM fragment with given markup. * * @author Adam Schmalhofer * * @param DOMDocument $dom * @param String $markup * @return DOMNode fragment in a node. */ protected function createDomFragment($dom, $markup) { $node = $dom->createDocumentFragment(); $node->appendXML($markup); return $node; }