Example #1
0
 public function css_in_head()
 {
     $xPath = new \DOMXPath($this->_document);
     $nodeList = $xPath->query(Query::css2xpath('styles , link[rel="stylesheet"]'));
     $nodeListHead = $xPath->query(Query::css2xpath('head'));
     $head = $nodeListHead->item(0);
     $i = 0;
     while ($i < $nodeList->length) {
         $element = $nodeList->item($i);
         $head->appendChild($element);
         $i++;
     }
 }
Example #2
0
 /**
  * 
  * @global type $renderedPage
  * @param type $name
  * @param type $value
  * @param type $namespaceURI
  * @param DOMDocument $parentDocument
  * @param type $children
  * @param type $attributes
  * @param type $classes
  * @param string $styles
  * @return \render_Element
  */
 public function __construct($name, $value = null, $namespaceURI = null, $parentDocument = null, $children = null, $attributes = null, $classes = null, $styles = null)
 {
     // se non c'è un elemento padre, creo il documento e lo associo all'elemento
     // altriment il lo prelevo
     // parentDocument null | DomElement | DomDocument | render_Element | String | DomNodeList
     // se name è un domelement, creo una classe con già l'elelmento e i parametri fondamentali e mi fermo.
     // equivale ad una new render_Element con tutti i parametri
     if ($name instanceof self) {
         $element = $name->getElement();
         $name = $element;
     }
     if ($name instanceof \DOMElement) {
         $this->setElement($name);
         $this->setDocument($name->ownerDocument);
         $this->setParent($name->parentNode);
         return $this;
     }
     global $renderedPage;
     if (!is_null($parentDocument)) {
         if ($parentDocument instanceof \DOMElement) {
             // se è un domelement, il doc diventa il documento dell'elemento inviato
             // il parent diventa il padre
             $this->_doc = $parentDocument->ownerDocument;
             $this->_parentElement = $parentDocument;
             $this->_elementObject = $this->getDocument()->createElement($name, $value);
             $this->_parentElement->appendChild($this->_elementObject);
         }
         if ($parentDocument instanceof \DOMDocument) {
             $this->_doc = $parentDocument;
             $this->_elementObject = $this->getDocument()->createElement($name, $value);
             $this->_doc->appendChild($this->_elementObject);
         }
         if ($parentDocument instanceof self) {
             $this->_doc = $parentDocument->getDocument();
             $this->_parentElement = $parentDocument->getElement();
             $this->_elementObject = $this->getDocument()->createElement($name, $value);
             $this->_parentElement->appendChild($this->_elementObject);
         }
         if (is_string($parentDocument)) {
             // se è una stringa, la trasformo in xpath per il documento generale
             $csstoxpath = Query::css2xpath($parentDocument);
             $xPathClass = new \DOMXPath($renderedPage);
             $nodeList = $xPathClass->query($csstoxpath);
             if ($nodeList->length == 0) {
                 return;
                 // capire cosa fare
             }
             $element = $nodeList->item(0);
             // solo al primo elemento
             //$selfClass = new render_Element($name, $value, $namespaceURI, $element, $children, $attributes, $classes, $styles);
             $this->_doc = $element->ownerDocument;
             $this->_parentElement = $element;
             if (!is_null($namespaceURI)) {
                 $this->_elementObject = $this->getDocument()->createElementNS($namespaceURI, $name, $value);
                 $this->_parentElement->appendChild($this->_elementObject);
             } else {
                 $this->_elementObject = $this->getDocument()->createElement($name, $value);
                 $this->_parentElement->appendChild($this->_elementObject);
             }
         }
     } else {
         //$docImplementation = new DOMImplementation();
         //$dtd = $docImplementation->createDocumentType('html'); // html5
         //$doc = $docImplementation->createDocument(null, 'html', $dtd); // html 5
         //$this->_doc = $doc;
         $this->_doc = $renderedPage;
         $this->_parentElement = $renderedPage->documentElement;
         // se il parent non esiste, gli do il primo elemento del documento creato
         $this->_elementObject = $this->getDocument()->createElement($name, $value);
         $this->_parentElement->appendChild($this->_elementObject);
     }
     $this->setValue($value);
     if (!is_null($children)) {
         if (is_array($children)) {
             foreach ($children as $child) {
                 $this->addChildren($child);
             }
         } else {
             $this->addChildren($children);
         }
     }
     // aggiungo gli attributi
     if (is_array($attributes)) {
         foreach ($attributes as $attrName => $attrValue) {
             $this->setAttribute($attrName, $attrValue);
         }
     }
     // aggiungo le classi
     if (!is_null($classes)) {
         if (is_array($classes)) {
             $class = implode(' ', $classes);
         } else {
             $class = $classes;
         }
         $this->setAttribute('class', $class);
     }
     // aggiungo gli stili
     if (!is_null($styles)) {
         if (is_array($styles)) {
             $_styles = array();
             foreach ($styles as $styleName => $styleValue) {
                 $styles[] = $styleName . ': ' . $styleValue;
             }
             $styles = implode(';', $styles);
             $this->setAttribute('styles', $styles);
         } else {
             $this->setAttribute('styles', $styles);
         }
     }
     return $this;
 }