protected function get_permalink_as_dom($path)
 {
     ob_start();
     $this->go_to($path);
     $this->load_template();
     $output = ob_get_clean();
     $html5 = new HTML5();
     $dom = $html5->loadHTML($output);
     return $dom;
 }
Пример #2
0
 /**
  * Stores a view output in the elements.
  */
 function storeViewPreview($output)
 {
     $html5 = new HTML5();
     $htmlDom = $html5->loadHTML('<html><body>' . $output . '</body></html>');
     if ($htmlDom) {
         // It's much easier to work with simplexml than DOM, luckily enough
         // we can just simply import our DOM tree.
         $this->elements = simplexml_import_dom($htmlDom);
     }
 }
Пример #3
0
 public function process($content)
 {
     if (strlen($content) == 0) {
         return '';
     }
     $fragment = $this->html5->loadHTMLFragment($content);
     $fragment = $this->processModifiers($fragment);
     return $this->html5->saveHTML($fragment);
 }
Пример #4
0
 /**
  * Parse HTML5 and convert to Dom document
  *
  * @param $string
  *
  * @return false|\DomDocument
  */
 public function parseFromString($string)
 {
     $dom = $this->html5->loadHTML($string);
     if (is_a($dom, 'DOMDocument')) {
         return $dom;
     } else {
         return false;
     }
 }
Пример #5
0
 /**
  * Fix markup
  *
  * @param string $markup
  * @param int    $topLevel
  * @param int    $depth
  * @return string Markup with added IDs
  * @throws RuntimeException
  */
 public function fix($markup, $topLevel = 1, $depth = 6)
 {
     if (!$this->isFullHtmlDocument($markup)) {
         $partialID = 'toc_generator_' . mt_rand(1000, 4000);
         $markup = sprintf("<body id='%s'>%s</body>", $partialID, $markup);
     }
     $domDocument = $this->htmlParser->loadHTML($markup);
     $domDocument->preserveWhiteSpace = true;
     // do not clobber whitespace
     $sluggifier = new UniqueSluggifier();
     /** @var \DOMElement $node */
     foreach ($this->traverseHeaderTags($domDocument, $topLevel, $depth) as $node) {
         if ($node->getAttribute('id')) {
             continue;
         }
         $node->setAttribute('id', $sluggifier->slugify($node->getAttribute('title') ?: $node->textContent));
     }
     return $this->htmlParser->saveHTML(isset($partialID) ? $domDocument->getElementById($partialID)->childNodes : $domDocument);
 }
Пример #6
0
 /**
  * Get Menu
  *
  * Returns a KNP Menu object, which can be traversed or rendered
  *
  * @param string  $markup    Content to get items fro $this->getItems($markup, $topLevel, $depth)m
  * @param int     $topLevel  Top Header (1 through 6)
  * @param int     $depth     Depth (1 through 6)
  * @return ItemInterface     KNP Menu
  */
 public function getMenu($markup, $topLevel = 1, $depth = 6)
 {
     // Setup an empty menu object
     $menu = $this->menuFactory->createItem('TOC');
     // Empty?  Do nothing.
     if (trim($markup) == '') {
         return [];
     }
     // Parse HTML
     $tagsToMatch = $this->determineHeaderTags($topLevel, $depth);
     // Initial settings
     $lastElem = $menu;
     // Do it...
     $domDocument = $this->domParser->loadHTML($markup);
     foreach ($this->traverseHeaderTags($domDocument, $topLevel, $depth) as $node) {
         // Skip items without IDs
         if (!$node->hasAttribute('id')) {
             continue;
         }
         // Get the TagName and the level
         $tagName = $node->tagName;
         $level = array_search(strtolower($tagName), $tagsToMatch) + 1;
         // Determine parent item which to add child
         if ($level == 1) {
             $parent = $menu;
         } elseif ($level == $lastElem->getLevel()) {
             $parent = $lastElem->getParent();
         } elseif ($level > $lastElem->getLevel()) {
             $parent = $lastElem;
             for ($i = $lastElem->getLevel(); $i < $level - 1; $i++) {
                 $parent = $parent->addChild('');
             }
         } else {
             //if ($level < $lastElem->getLevel())
             $parent = $lastElem->getParent();
             while ($parent->getLevel() > $level - 1) {
                 $parent = $parent->getParent();
             }
         }
         $lastElem = $parent->addChild($node->getAttribute('title') ?: $node->textContent, ['uri' => '#' . $node->getAttribute('id')]);
     }
     return $menu;
 }
Пример #7
0
 /**
  * Load a HTML5 file and copy it into a FluentDOM\Document
  *
  * @codeCoverageIgnore
  *
  * @see Loadable::load
  * @param string $source
  * @param string $contentType
  * @param array $options
  * @return Document|NULL
  */
 public function load($source, $contentType, array $options = [])
 {
     if ($this->supports($contentType)) {
         $html5 = new HTML5Support();
         if ($this->startsWith($source, '<')) {
             $dom = $html5->loadHTML($source);
         } else {
             $dom = $html5->loadHTMLFile($source);
         }
         if (!$dom instanceof Document) {
             $fd = new Document();
             if ($dom->documentElement instanceof \DOMElement) {
                 $fd->appendChild($fd->importNode($dom->documentElement, TRUE));
             }
             $dom = $fd;
         }
         $dom->registerNamespace('html', 'http://www.w3.org/1999/xhtml');
         return $dom;
     }
     return NULL;
 }
 /**
  * {@inheritdoc}
  */
 public function toHtml(\DOMDocument $document, $encoding = 'UTF-8')
 {
     return $this->html5Parser->saveHTML($document);
 }
Пример #9
0
<?php

require "vendor/autoload.php";
use Masterminds\HTML5;
$html = <<<'HERE'
  <html>
  <head>
  <title>TEST</title>
  <script language="javascript">
  if (2 > 1) { alert("Math wins."); }
  </script>
  </head>
  <body id='foo'>
  <!-- This space intentionally left blank. -->
  <section class="section-a pretty" id="bar1">
  <h1>Hello World</h1><p>This is a test of the HTML5 parser.</p>
  <hr>
  &amp; Nobody nowhere.
  </section>
  <test xmlns:foo="http://example.com/foo">TEST</test>
  <![CDATA[Because we can.]]>
  &copy;
  </body></html>
HERE;
$html5 = new HTML5();
$dom = $html5->loadHTML($html);
print "Converting to HTML 5\n";
$html5->save($dom, fopen("php://stdin", 'w'));
 /**
  * Write the document to HTML5.
  *
  * This works the same as the other write* functions, but it encodes the output
  * as HTML5 with UTF-8.
  * @see html5()
  * @see innerHTML5()
  * @throws Exception
  *  In the event that a file cannot be written, an Exception will be thrown.
  */
 public function writeHTML5($path = NULL)
 {
     $html5 = new HTML5();
     if ($path == NULL) {
         // Print the document to stdout.
         print $html5->saveHTML($this->document);
         return;
     }
     $html5->save($this->document, $path);
 }
 /**
  * Parse HTML5 documents.
  *
  * This uses HTML5-PHP to parse the document. In actuality, this parser does
  * a fine job with pre-HTML5 documents in most cases, though really old HTML
  * (like 2.0) may have some substantial quirks.
  *
  * <b>Supported Options</b>
  * Any options supported by HTML5-PHP are allowed here. Additionally, the
  * following options have meaning to QueryPath.
  * - QueryPath_class
  *
  *
  * @param mixed $source
  *  A document as an HTML string, or a path/URL. For compatibility with
  *  existing functions, a DOMDocument, SimpleXMLElement, DOMNode or array
  *  of DOMNodes will be passed through as well. However, these types are not
  *  validated in any way.
  *
  * @param string $selector
  *  A CSS3 selector.
  *
  * @param array $options
  *   An associative array of options, which is passed on into HTML5-PHP. Note
  *   that the standard QueryPath options may be ignored for this function,
  *   since it uses a different parser.
  *
  * @return QueryPath
  */
 public static function withHTML5($source = NULL, $selector = NULL, $options = array())
 {
     $qpClass = isset($options['QueryPath_class']) ? $options['QueryPath_class'] : '\\QueryPath\\DOMQuery';
     if (is_string($source)) {
         $html5 = new HTML5();
         if (strpos($source, '<') !== FALSE && strpos($source, '>') !== FALSE) {
             $source = $html5->loadHTML($source);
         } else {
             $source = $html5->load($source);
         }
     }
     $qp = new $qpClass($source, $selector, $options);
     return $qp;
 }
Пример #12
0
 /**
  * Return body as string
  *
  * @param \DOMDocument $dom
  *
  * @return mixed
  */
 public static function to_string(\DOMDocument $dom)
 {
     $html5 = new HTML5();
     preg_match('/<body>(.*)<\\/body>/s', $html5->saveHTML($dom), $match);
     return $match[1];
 }
Пример #13
0
 public function asString()
 {
     $html5 = new HTML5Support($this->_options);
     return (string) $html5->saveHTML($this->_document);
 }
Пример #14
0
foreach ($contentsNodes->getElementsByTagName('span') as $span) {
    if ($span->getAttribute('class') == 'page-name') {
        $span->appendChild($contentsNodes->createTextNode($requestedPageName));
    }
}
// script tags may appear above the script block - can't have that!
$scriptTags = [];
foreach ($contentsNodes->getElementsByTagName('script') as $script) {
    $scriptTags[] = $script->cloneNode(true);
    $script->parentNode->removeChild($script);
}
/*************************/
/*  Load the outer page  */
/*************************/
$index = file_get_contents("../index.html");
$html5Parser = new HTML5();
$completePage = $html5Parser->loadHTML($index);
/*************************/
/*         Merge         */
/*************************/
// find the destination
$destination = $completePage->getElementById('cms-less-destination');
$destination->setAttribute("data-cms-less-preloaded", $originalPageName);
// empty the destination
foreach ($destination->childNodes as $node) {
    $destination->removeChild($node);
}
// load the page content
$childNodes = $contentsNodes->childNodes;
foreach ($childNodes as $node) {
    $importedNode = $completePage->importNode($node, true);
 /**
  * Test alternate constructors.
  * @group basic
  */
 public function testDOMQueryHtmlConstructors()
 {
     $qp = htmlqp(\QueryPath::HTML_STUB);
     $this->assertEquals(1, count($qp->get()));
     $this->assertTrue($qp->get(0) instanceof \DOMNode);
     // Bad BR tag.
     $borken = '<html><head></head><body><br></body></html>';
     $qp = htmlqp($borken);
     $this->assertEquals(1, count($qp->get()));
     $this->assertTrue($qp->get(0) instanceof \DOMNode);
     // XHTML Faker
     $borken = '<?xml version="1.0"?><html><head></head><body><br></body></html>';
     $qp = htmlqp($borken);
     $this->assertEquals(1, count($qp->get()));
     $this->assertTrue($qp->get(0) instanceof \DOMNode);
     // HTML in a file that looks like XML.
     $qp = htmlqp(HTML_IN_XML_FILE);
     $this->assertEquals(1, count($qp->get()));
     $this->assertTrue($qp->get(0) instanceof \DOMNode);
     // HTML5
     $html5 = new \Masterminds\HTML5();
     $dom = $html5->loadHTML(\QueryPath::HTML_STUB);
     qp($dom, 'html');
     // Stripping #13 (CR) from HTML.
     $borken = '<html><head></head><body><p>' . chr(13) . '</p><div id="after"/></body></html>';
     $this->assertFalse(strpos(htmlqp($borken)->html(), '&#13;'), 'Test that CRs are not encoded.');
     // Regression for #58: Make sure we aren't getting &#10; encoded.
     $borken = '<html><head><style>
     .BlueText {
       color:red;
     }</style><body></body></html>';
     $this->assertFalse(strpos(htmlqp($borken)->html(), '&#10;'), 'Test that LF is not encoded.');
     // Low ASCII in a file
     $borken = '<html><head></head><body><p>' . chr(27) . '</p><div id="after"/></body></html>';
     $this->assertEquals(1, htmlqp($borken, '#after')->size());
 }