Пример #1
0
 private function render(\DOMDocument $doc, Element $element, $parent = null)
 {
     if ($element instanceof \tarcisio\svg\SVG) {
         $svgs = $doc->getElementsByTagName('svg');
         $svg = $svgs->item(0);
         $svg->setAttribute('width', $element->width);
         $svg->setAttribute('height', $element->height);
         $svg->setAttribute('viewBox', "0 0 {$element->width} {$element->height}");
         $gs = $doc->getElementsByTagName('g');
         $g = $gs->item(0);
         foreach ($element->getChildren() as $ch) {
             $this->render($doc, $ch, $g);
         }
         return $doc;
     }
     if (!is_null($element->getTitle())) {
         $element->appendChild(new Title('', $element->getTitle()));
     }
     $e = $doc->createElement($element->getElementName());
     $parent->appendChild($e);
     foreach ($element->getAttributes() as $k => $v) {
         if (!is_null($v)) {
             $e->setAttribute($k, $v);
         }
     }
     if (!is_null($element->getValue())) {
         $e->nodeValue = $element->getValue();
     }
     foreach ($element->getChildren() as $ch) {
         $this->render($doc, $ch, $e);
     }
     return $doc;
 }
Пример #2
0
 /** Tests for {@link Element::append}. */
 public function testAppendMultipleNodes()
 {
     $e = new Element('html');
     $e->append(new Element('head'), new Element('body'));
     $this->assertSame(2, count($e->getChildren()), 'Appended elements missing from children.');
     $e = new Element('div');
     $e->append('Some', 'text');
     $this->assertSame(2, count($e->getChildren()), 'Appended texts missing from children.');
     $this->assertTrue($e->getChildren()[0] instanceof TextNode, 'First text not appended as TextNode child.');
     $this->assertTrue($e->getChildren()[1] instanceof TextNode, 'Second text not appended as TextNode child.');
     $e = new Element('li');
     $e->append('Visit', new Element('a'), 'now');
     $this->assertSame(3, count($e->getChildren()), 'Appended mixed nodes missing from children.');
     $this->assertTrue($e->getChildren()[0] instanceof TextNode, 'First text not appended as TextNode child.');
     $this->assertTrue($e->getChildren()[1] instanceof Element, 'Second appended node has incorrect type.');
     $this->assertTrue($e->getChildren()[2] instanceof TextNode, 'Third text not appended as TextNode child.');
 }
Пример #3
0
 /** Returns a HTML representation of an element and its child nodes.
  * @param   Element                $element            the element to generate as HTML.
  * @return  string                                     the HTML. */
 public function createHtml($element)
 {
     $data = array_reduce($element->getChildren(), function ($carry, $child) {
         return $carry . $child->toHtml();
     }, '');
     $allAttributes = [];
     foreach ($element->getAttributes() as $name => $value) {
         $allAttributes[] = sprintf(static::ATTRIBUTE_TEMPLATE, htmlspecialchars($name, ENT_COMPAT), htmlspecialchars($value, ENT_COMPAT));
     }
     if (0 === count($allAttributes)) {
         $attributes = '';
     } else {
         $attributes = ' ' . implode(' ', $allAttributes);
     }
     if (in_array($element->getName(), static::VOID_ELEMENTS)) {
         $html = sprintf(static::VOID_ELEMENT_TEMPLATE, $element->getName(), $attributes);
     } else {
         $html = sprintf(static::ELEMENT_TEMPLATE, $element->getName(), $attributes, $data);
     }
     return $html;
 }
Пример #4
0
 protected function writeElement(Element $element, $level)
 {
     $children = $element->getChildren();
     $childCount = count($children);
     $hasChildren = $childCount > 0;
     $newLine = $this->_pretty ? $this->_newLine : '';
     $indent = $this->_pretty ? str_repeat($this->_tabString, $level) : '';
     $str = $indent;
     $str .= $this->writeOpenTag($element->getTag(), $element->getAttributes(), $hasChildren);
     $writeCloseIndent = false;
     if ($hasChildren) {
         if ($childCount === 1 && $children[0] instanceof Text && mb_strlen($children[0]->getText(), 'utf-8') < $this->_textWrap) {
             $str .= $children[0]->getText();
         } else {
             $str .= $newLine;
             for ($i = 0; $i < $childCount; $i++) {
                 $child = $children[$i];
                 $str .= $this->writeLeaf($child, $level + 1);
                 $str .= $newLine;
             }
             $writeCloseIndent = true;
         }
     }
     if ($hasChildren || !empty($this->_selfClosingTags) && !in_array($element->getTag(), $this->_selfClosingTags)) {
         if ($hasChildren && $writeCloseIndent) {
             $str .= $indent;
         }
         $str .= $this->writeCloseTag($element->getTag());
     }
     return $str;
 }