protected function _build_tree_r(DomNode $node)
 {
     $frame = new Frame($node);
     $id = $frame->get_id();
     $this->_registry[$id] = $frame;
     if (!$node->hasChildNodes()) {
         return $frame;
     }
     $children = array();
     for ($i = 0; $i < $node->childNodes->length; $i++) {
         $children[] = $node->childNodes->item($i);
     }
     foreach ($children as $child) {
         $node_name = mb_strtolower($child->nodeName);
         if (in_array($node_name, self::$_HIDDEN_TAGS)) {
             if ($node_name !== "head" && $node_name !== "style") {
                 $child->parentNode->removeChild($child);
             }
             continue;
         }
         if ($node_name === "#text" && $child->nodeValue == "") {
             $child->parentNode->removeChild($child);
             continue;
         }
         if ($node_name === "img" && $child->getAttribute("src") == "") {
             $child->parentNode->removeChild($child);
             continue;
         }
         $frame->append_child($this->_build_tree_r($child), false);
     }
     return $frame;
 }
 /**
  * Converts DomDocument to array with all attributes in it
  *
  * @param  DOMDocument|DOMElement|DomNode $root
  * @return array
  */
 function convertDomToArray($root)
 {
     $result = [];
     if ($root->hasAttributes()) {
         $attrs = $root->attributes;
         foreach ($attrs as $attr) {
             $result['@attributes'][$attr->name] = $attr->value;
         }
     }
     if ($root->hasChildNodes()) {
         $children = $root->childNodes;
         if ($children->length == 1) {
             $child = $children->item(0);
             if ($child->nodeType == XML_TEXT_NODE) {
                 $result['_value'] = $child->nodeValue;
                 return count($result) == 1 ? $result['_value'] : $result;
             }
         }
         $groups = [];
         foreach ($children as $child) {
             if (!isset($result[$child->nodeName])) {
                 $result[$child->nodeName] = $this->convertDomToArray($child);
             } else {
                 if (!isset($groups[$child->nodeName])) {
                     $result[$child->nodeName] = array($result[$child->nodeName]);
                     $groups[$child->nodeName] = 1;
                 }
                 $result[$child->nodeName][] = $this->convertDomToArray($child);
             }
         }
     }
     return $result;
 }
Beispiel #3
0
 /**
  * Add `AdSystem` element to `Ad' element
  *
  * @param string $adSystem
  * @return \Sokil\Vast\Ad\InLine|\Sokil\Vast\Ad\Wrapper
  */
 public function setAdSystem($adSystem)
 {
     $adSystemDomElement = $this->domElement->getElementsByTagName('AdSystem')->item(0);
     if ($adSystemDomElement) {
         $adSystemDomElement->nodeValue = $adSystem;
     } else {
         $adSystemDomElement = $this->domElement->ownerDocument->createElement('AdSystem', $adSystem);
         $this->domElement->firstChild->appendChild($adSystemDomElement);
     }
     return $this;
 }
Beispiel #4
0
 protected function import(DOMDocument $document, DomNode $node, array $statements)
 {
     /** @var Node $statement */
     foreach ($statements as $statement) {
         $newNode = $statement->getNode()->cloneNode(false);
         $newNode = $document->importNode($newNode);
         $newNode = $node->appendChild($newNode);
         if ($statement->hasChildren()) {
             $this->import($document, $newNode, $statement->getChildren());
         }
     }
 }
Beispiel #5
0
 /**
  * Move the children of this node into this node's parent, just before this node in the DOM tree
  */
 function promote_children()
 {
     while ($this->node->hasChildNodes()) {
         $child = $this->node->firstChild;
         $this->node->removeChild($child);
         $this->node->parentNode->insertBefore($child, $this->node);
     }
 }
Beispiel #6
0
 /**
  * Returns an array with hashes from a file-node
  *
  * @param DomNode $node
  * @return array
  */
 public function getHashesFromFileNode(DomNode $node)
 {
     $hashes = array();
     if ($node->hasChildNodes()) {
         $children = $node->childNodes;
         /* @var $children DOMNodeList */
         foreach ($children as $childnode) {
             /* @var $childnode DOMNode */
             if ($childnode->nodeName === 'hash') {
                 $attributes = $childnode->attributes;
                 /* @var $attributes DOMNamedNodeMap */
                 $hash = $attributes->getNamedItem('hash');
                 /* @var $hash DOMAttr */
                 $hashes[$hash->value] = $childnode->nodeValue;
             }
         }
     }
     return $hashes;
 }
 /**
  * @param \DomDocument $dom
  * @param \DomNode $parent
  * @param string $identifier
  * @return void
  */
 protected function createXlfLanguageNode(\DomDocument $dom, \DomNode $parent, $identifier)
 {
     $labelNode = $dom->createElement('trans-unit');
     $idAttribute = $dom->createAttribute('id');
     $idAttribute->nodeValue = $identifier;
     $spaceAttribute = $dom->createAttribute('xml:space');
     $spaceAttribute->nodeValue = 'preserve';
     $sourceNode = $dom->createElement('source');
     $sourceNode->nodeValue = $identifier;
     $labelNode->appendChild($idAttribute);
     $labelNode->appendChild($spaceAttribute);
     $labelNode->appendChild($sourceNode);
     $parent->appendChild($labelNode);
 }
Beispiel #8
0
 static function convertDomNodeToDataArray(DomNode $node)
 {
     $data = array();
     $values = array();
     $has_value = false;
     if ($node->hasChildNodes()) {
         foreach ($node->childNodes as $child_node) {
             /*
              * A html dom node always contains one dom document type child
              * node with no content (DOMDocumentType#internalSubset is for
              * example <!DOCTYPE html>). Ignore it!
              */
             if ($child_node instanceof DOMDocumentType) {
                 continue;
             }
             if ($child_node->nodeType === XML_TEXT_NODE) {
                 $has_value = true;
                 $values[] = $child_node->nodeValue;
             } else {
                 $key = $child_node->nodeName;
                 if (isset($data[$key])) {
                     if (!is_array($data[$key]) || !isset($data[$key][0])) {
                         $data[$key] = array($data[$key]);
                     }
                     $data[$key][] = self::convertDomNodeToDataArray($child_node);
                 } else {
                     $data[$key] = self::convertDomNodeToDataArray($child_node);
                 }
             }
         }
     }
     if ($node->hasAttributes()) {
         foreach ($node->attributes as $attribute_node) {
             $key = '@' . $attribute_node->nodeName;
             $data[$key] = $attribute_node->nodeValue;
         }
     }
     if ($has_value) {
         $value = implode('', $values);
         if (trim($value) != '') {
             if (empty($data)) {
                 $data = $value;
             } else {
                 $data['@'] = $value;
             }
         }
     }
     return $data;
 }
Beispiel #9
0
 /**
  * 
  * @param type $id
  * @return \Sokil\Vast\Ad
  */
 public function setId($id)
 {
     $this->_domElement->setAttribute('id', $id);
     return $this;
 }
Beispiel #10
0
/**
 * Recursive function to create schema from import schema
 * and also used to resove wsdl import problem
 * @param DomNode $parent parent dom node
 * @param DomNode $child dom node of import schema
 * @param DomDocument $doc DomDocument of parent DomNode
 */
function wsf_wsdl_append_node($parent, $child, $doc)
{
    if ($child == NULL) {
        return;
    }
    $imported_node = $doc->importNode($child, TRUE);
    if ($imported_node) {
        $parent->appendChild($imported_node);
    }
}
Beispiel #11
0
 /**
  * Returns an associative array containing the possible values of a
  * <configspecial> tag as used inside of enum configurations.
  *
  * @param DomNode $node  The DomNode representation of the <configspecial>
  *                       tag.
  *
  * @return array  An associative array with the possible values.
  */
 protected function _handleSpecials($node)
 {
     $app = $node->getAttribute('application');
     try {
         if (!in_array($app, $GLOBALS['registry']->listApps())) {
             $app = $GLOBALS['registry']->hasInterface($app);
         }
     } catch (Horde_Exception $e) {
         return array();
     }
     if (!$app) {
         return array();
     }
     try {
         return $GLOBALS['registry']->callAppMethod($app, 'configSpecialValues', array('args' => array($node->getAttribute('name')), 'noperms' => true));
     } catch (Horde_Exception $e) {
         return array();
     }
 }
Beispiel #12
0
/**
* Extract the sig node looking at the user object type
* @param array $sig_model as a DomNode
* @param DomNode $parent_node - The parent node to add the content 
* @param DomNode $root_node - The top most of parent
* @param mixed $user_obj - class object to pass
* @param $prefix_i - next available prefix index 
* @param $namespace_map - Just make sure the unique namespace is used.
   Newly added (passed by reference)
*/
function wsf_infer_sig_node_from_user_obj($sig_node, $parent_node, $root_node, $user_obj, $classmap, &$prefix_i, array &$namespace_map)
{
    ws_log_write(__FILE__, __LINE__, WSF_LOG_DEBUG, "Calling infer sig mode from user obj");
    // first loop the through sig child whther there is is inheriting-types in there
    $inheriting_type_sigs = array();
    $inheriting_type_namespaces = array();
    $sig_child_nodes = $sig_node->childNodes;
    foreach ($sig_child_nodes as $sig_child_node) {
        if ($sig_child_node->localName == WSF_INHERITING_TYPE) {
            $sig_child_attris = $sig_child_node->attributes;
            $type_name = $type_ns = "";
            if ($sig_child_attris->getNamedItem(WSF_XSI_TYPE)) {
                $type_name = $sig_child_attris->getNamedItem(WSF_XSI_TYPE)->value;
            }
            if ($sig_child_attris->getNamedItem(WSF_XSI_TYPE_NS)) {
                $type_ns = $sig_child_attris->getNamedItem(WSF_XSI_TYPE_NS)->value;
            }
            ws_log_write(__FILE__, __LINE__, WSF_LOG_DEBUG, "type name {$type_name} ; {$type_ns} ");
            $inheriting_type_sigs[$type_name] = $sig_child_node;
            $inheriting_type_namespaces[$type_name] = $type_ns;
        }
    }
    ws_log_write(__FILE__, __LINE__, WSF_LOG_DEBUG, print_r($inheriting_type_namespaces, TRUE));
    // if not inheriting types just let the sig_node to be the sig node
    if (count($inheriting_type_sigs) == 0) {
        return $sig_node;
    }
    $reflex_obj = new ReflectionObject($user_obj);
    if (!$reflex_obj) {
        return $sig_node;
    }
    $class_name = $reflex_obj->getName();
    // find the type name
    $type_name = $class_name;
    // if the classmap is present we need to check the type name from the map
    if ($classmap && is_array($classmap)) {
        foreach ($classmap as $type_name_key => $class_name_value) {
            if ($class_name_value == $class_name) {
                $type_name = $type_name_key;
                break;
            }
        }
    }
    // so we found the type, check with the collected inherited_types
    $the_sig_node = NULL;
    $the_type_name = NULL;
    $the_type_namespace = NULL;
    if (array_key_exists($type_name, $inheriting_type_sigs)) {
        $the_type_name = $type_name;
        $the_type_namespace = $inheriting_type_namespaces[$type_name];
        $the_sig_node = $inheriting_type_sigs[$type_name];
    } else {
        // not in inherited map, so should be the same sig,
        return $sig_node;
    }
    //now retrieve the namespace or declare it if it is not present
    $the_type_ns_prefix = NULL;
    if (array_key_exists($the_type_namespace, $namespace_map)) {
        $the_type_ns_prefix = $namespace_map[$the_type_namespace];
    } else {
        $the_type_ns_prefix = "ns" . $prefix_i;
        $prefix_i++;
        $root_node->setAttribute("xmlns:" . $the_type_ns_prefix, $the_type_namespace);
        $namespace_map[$the_type_namespace] = $the_type_ns_prefix;
    }
    $xsi_namespace_prefix = NULL;
    if (array_key_exists(WSF_XSI_NAMESPACE, $namespace_map)) {
        $xsi_namespace_prefix = $namespace_map[WSF_XSI_NAMESPACE];
    } else {
        $xsi_namespace_prefix = "xsi";
        $root_node->setAttribute("xmlns:" . $xsi_namespace_prefix, WSF_XSI_NAMESPACE);
        $namespace_map[WSF_XSI_NAMESPACE] = $xsi_namespace_prefix;
    }
    $attribute_name = $xsi_namespace_prefix . ":" . "type";
    $attribute_value = $the_type_ns_prefix . ":" . $the_type_name;
    $parent_node->setAttribute($attribute_name, $attribute_value);
    ws_log_write(__FILE__, __LINE__, WSF_LOG_DEBUG, wsf_test_serialize_node($the_sig_node));
    return $the_sig_node;
}
 /**
  * Test if this node is a match based on the node type and URL format.
  *
  * @param DomNode $node
  * @param string $pattern
  * @return boolean
  * @static
  * @access public
  */
 public static function is_embed_web_video($node, $pattern)
 {
     return 'p' == $node->nodeName && preg_match($pattern, trim($node->nodeValue)) || 'iframe' == $node->nodeName && preg_match($pattern, trim($node->getAttribute('src')));
 }
Beispiel #14
0
 /**
  * Performs a css select query on the root node
  * @see DomNode::select()
  * @return array
  */
 function select($query = '*', $index = false, $recursive = true, $check_self = false)
 {
     return $this->root->select($query, $index, $recursive, $check_self);
 }
 /**
  * create a new element in the channels dom
  * @param DomNode $target
  * @param string $tag
  * @param array $attributes
  * @param string $content
  * @param bool $cdata
  * @return DomNode 
  */
 protected function createElementOn(&$target, $tag, $attributes = false, $content = false, $cdata = false)
 {
     $_tag = $this->dom->createElement($tag);
     if (is_array($attributes)) {
         foreach ($attributes as $key => $value) {
             $attr = $this->dom->createAttribute($key);
             $attr_val = $this->dom->createTextNode($value);
             $attr->appendChild($attr_val);
             $_tag->appendChild($attr);
         }
     }
     if ($content) {
         if ($cdata) {
             $content = $this->dom->createCDATASection($content);
         } else {
             $content = $this->dom->createTextNode($content);
         }
         $_tag->appendChild($content);
     }
     $target->appendChild($_tag);
     return $_tag;
 }
 /**
  * Recursively adds {@link Frame} objects to the tree
  *
  * Recursively build a tree of Frame objects based on a dom tree.
  * No layout information is calculated at this time, although the
  * tree may be adjusted (i.e. nodes and frames for generated content
  * and images may be created).
  *
  * @param DomNode $node the current DomNode being considered
  * @return Frame
  */
 protected function _build_tree_r(DomNode $node)
 {
     $frame = new Frame($node);
     $frame->set_id($id = uniqid(rand()));
     $this->_registry[$id] = $frame;
     if (!$node->hasChildNodes()) {
         return $frame;
     }
     foreach ($node->childNodes as $child) {
         // Skip non-displaying nodes
         if (in_array($child->nodeName, self::$_HIDDEN_TAGS)) {
             continue;
         }
         // Skip empty #text nodes
         if ($child->nodeName == "#text" && $child->nodeValue == "") {
             continue;
         }
         // Add a container frame for images
         if ($child->nodeName == "img") {
             $img_node = $child->ownerDocument->createElement("img_inner");
             // Move attributes to inner node
             foreach ($child->attributes as $attr => $attr_node) {
                 // Skip style, but move all other attributes
                 if ($attr == "style") {
                     continue;
                 }
                 $img_node->setAttribute($attr, $attr_node->value);
             }
             foreach ($child->attributes as $attr => $node) {
                 if ($attr == "style") {
                     continue;
                 }
                 $child->removeAttribute($attr);
             }
             $child->appendChild($img_node);
         }
         $frame->append_child($this->_build_tree_r($child), false);
     }
     return $frame;
 }
Beispiel #17
0
 /**
  * Transforms the APF DOM tree of the current page. Returns the content of the transformed document.
  *
  * @return string The content of the transformed page
  *
  * @author Christian Achatz
  * @version
  * Version 0.1, 28.12.2006<br />
  * Version 0.2, 03.01.2007 (Introduced URL rewriting)<br />
  * Version 0.3, 08.06.2007 (Moved the URL rewriting into a filter)<br />
  * Version 0.4, 11.12.2008 (Switched to the new input filter concept)<br />
  */
 public function transform()
 {
     return $this->document->transform();
 }
Beispiel #18
0
 /**
  * @param DomDocument $dom The DomNode of the form
  * @param PGPage $page The parent PGPage object
  * @return PGForm
  */
 function __construct($dom, $page)
 {
     $this->page = $page;
     $this->browser = $this->page->browser;
     $this->dom = $dom;
     $this->method = strtolower($this->dom->getAttribute('method'));
     if (empty($this->method)) {
         $this->method = 'get';
     }
     $this->enctype = strtolower($this->dom->getAttribute('enctype'));
     if (empty($this->enctype)) {
         $this->enctype = '';
     }
     $this->action = phpUri::parse($this->page->url)->join($this->dom->getAttribute('action'));
     $this->initFields();
 }
Beispiel #19
0
 public function unwrap()
 {
     $this->parent->detach(true);
     return $this;
 }
 /**
  * addArray recursive function 
  *
  * @param DomDocument $doc 
  * @param DomNode $n 
  * @param array $arr 
  * @param string $name 
  * @param string $debug 
  * @return void
  * @author Andy Bennett
  */
 public static function array_to_xml(&$doc, &$n, $arr, $name = "", $debug = 0)
 {
     if (count($arr) == 0) {
         return;
     }
     if (!is_array($arr) && !is_object($arr)) {
         return;
     }
     foreach ($arr as $key => $val) {
         if (is_int($key)) {
             if (strlen($name) > 1) {
                 $newKey = substr($name, 0, strlen($name) - 1);
             } else {
                 $newKey = "item";
             }
         } else {
             $newKey = $key;
         }
         $node = $doc->createElement($newKey);
         if (is_array($val) || is_object($val)) {
             $ak = is_object($arr) ? $arr->{$key} : $arr[$key];
             self::array_to_xml($doc, $node, $ak, $key, $debug);
         } else {
             $nodeText = $doc->createTextNode($val);
             $node->appendChild($nodeText);
         }
         $n->appendChild($node);
     }
 }
Beispiel #21
0
 public function getChildNodes(\DomNode $node, array $types = array())
 {
     if (!$node->hasChildNodes()) {
         return FALSE;
     }
     $childs = $node->childNodes;
     $return = array();
     foreach ($childs as $child) {
         $nodeName = $child->nodeName;
         if (is_a($child, "DomElement")) {
             if ($hasChilds = $this->getChildNodes($child, $types)) {
                 foreach ($hasChilds as $childChild) {
                     $return[] = $childChild;
                     continue;
                 }
             }
             if (sizeof($types)) {
                 if (in_array($nodeName, $types)) {
                     $return[] = $child;
                 }
             } else {
                 $return[] = $child;
             }
         }
     }
     return $return;
 }
Beispiel #22
0
/**
 * Extract the sig node looking at the incomming xml type
 * @param array $sig_model as a DomNode
 * @param DomNode $parent_node - The parent node to check for the content 
 * @param $object mixed relevent object instance(passed by reference)
 * @param $original_type the type in the first place
 * @param $classmap - the type name to class map
 */
function wsf_infer_sig_node_from_xml($sig_node, $parent_node, &$object, $original_type, $classmap)
{
    ws_log_write(__FILE__, __LINE__, WSF_LOG_DEBUG, "Calling infer sig mode from xml");
    // first it is to be safte to keep the element type as the original type and the sig node too
    $the_element_type = $original_type;
    $the_sig_node = $sig_node;
    // second we have to find whether there is xsi:type in the xml.
    $xml_type = NULL;
    $xsi_type_attri_node = $parent_node->getAttributeNodeNS(WSF_XSI_NAMESPACE, "type");
    if ($xsi_type_attri_node) {
        $xml_type = $xsi_type_attri_node->value;
    }
    ws_log_write(__FILE__, __LINE__, WSF_LOG_DEBUG, "xml type {$xml_type}");
    if ($xml_type) {
        //looks like there is a type declared in the xml, so find possible types..
        $inheriting_type_found = FALSE;
        $xml_type_local_value = $xml_type;
        $pos = strpos($xml_type, ":");
        if ($pos !== FALSE) {
            $xml_type_local_value = substr($xml_type, $pos + 1);
        }
        $sig_child_nodes = $sig_node->childNodes;
        foreach ($sig_child_nodes as $sig_child_node) {
            if ($sig_child_node->localName == WSF_INHERITING_TYPE) {
                $sig_child_attris = $sig_child_node->attributes;
                $type_name = $type_ns = "";
                if ($sig_child_attris->getNamedItem(WSF_XSI_TYPE)) {
                    $type_name = $sig_child_attris->getNamedItem(WSF_XSI_TYPE)->value;
                }
                if ($xml_type_local_value == $type_name) {
                    ws_log_write(__FILE__, __LINE__, WSF_LOG_DEBUG, "type name {$type_name} found ");
                    $the_element_type = $type_name;
                    $the_sig_node = $sig_child_node;
                    $inheriting_type_found = TRUE;
                    break;
                }
            }
        }
    }
    if (is_array($classmap) && array_key_exists($the_element_type, $classmap)) {
        $class_name = $classmap[$the_element_type];
    }
    if (!isset($class_name) || $class_name == NULL) {
        $class_name = $the_element_type;
    }
    try {
        $ref_class = new ReflectionClass($class_name);
        if ($ref_class->isInstantiable()) {
            $object = $ref_class->newInstance();
        }
    } catch (Exception $e) {
        $object = new WSFUnknownSchemaConstruct();
    }
    return $the_sig_node;
}
Beispiel #23
0
 /**
  * Evaluate root node using custom callback
  * @param array $conditions {@link parse_conditions()}
  * @param bool|int $recursive
  * @param bool $check_root
  * @return array
  * @access private
  */
 protected function parse_callback($conditions, $recursive = true, $check_root = false)
 {
     return $this->result = $this->root->getChildrenByMatch($conditions, $recursive, $check_root, $this->custom_filter_map);
 }
 private function hasParentbyTagName(DomNode $node, $tagName)
 {
     return strpos($node->getNodePath(), $tagName) === false || $node->nodeName === $tagName ? 0 : 1;
 }
Beispiel #25
0
 /**
  *
  * @param DomNode $node
  * @param array $error
  */
 public function displayXmlError($node, $error)
 {
     $a = $this->errors->createAttribute("line");
     $t = $this->errors->createAttribute("code");
     $m = $this->errors->createTextNode(trim($error->message));
     $node->appendChild($a);
     $node->appendChild($t);
     $node->appendChild($m);
     $node->setAttribute("line", $error->line);
     switch ($error->level) {
         case LIBXML_ERR_WARNING:
             $node->setAttribute("code", $error->code);
             break;
         case LIBXML_ERR_ERROR:
             $node->setAttribute("code", $error->code);
             break;
         case LIBXML_ERR_FATAL:
             $node->setAttribute("code", $error->code);
             break;
     }
 }
Beispiel #26
0
 /**
  * Minifies javascript using JSMin+
  * @param DomNode $root
  * @param string $indent_string
  * @param bool $wrap_comment Wrap javascript in HTML comments (<!-- ~text~ //-->)
  * @param bool $recursive
  * @return bool|array Array of errors on failure, true on succes
  */
 static function minify_javascript(&$root, $indent_string = ' ', $wrap_comment = true, $recursive = true)
 {
     #php4 JSMin+ doesn't support PHP4
     #return true;
     #php4e
     #php5
     include_once 'third party/jsminplus.php';
     $errors = array();
     foreach ($root->select('script:not-empty > "~text~"', false, $recursive, true) as $c) {
         try {
             $text = $c->text;
             while ($text) {
                 $text = trim($text);
                 //Remove comment/CDATA tags at begin and end
                 if (substr($text, 0, 4) === '<!--') {
                     $text = substr($text, 5);
                     continue;
                 } elseif (strtolower(substr($text, 0, 9)) === '<![cdata[') {
                     $text = substr($text, 10);
                     continue;
                 }
                 if (($end = substr($text, -3)) && ($end === '-->' || $end === ']]>')) {
                     $text = substr($text, 0, -3);
                     continue;
                 }
                 break;
             }
             if (trim($text)) {
                 $text = \JSMinPlus::minify($text);
                 if ($wrap_comment) {
                     $text = "<!--\n" . $text . "\n//-->";
                 }
                 if ($indent_string && ($wrap_comment || strpos($text, "\n") !== false)) {
                     $text = indent_text("\n" . $text, $c->indent(), $indent_string);
                 }
             }
             $c->text = $text;
         } catch (\Exception $e) {
             $errors[] = array($e, $c->parent->dumpLocation());
         }
     }
     return $errors ? $errors : true;
     #php5e
 }
Beispiel #27
0
 /**
  * Recursively adds {@link Frame} objects to the tree
  *
  * Recursively build a tree of Frame objects based on a dom tree.
  * No layout information is calculated at this time, although the
  * tree may be adjusted (i.e. nodes and frames for generated content
  * and images may be created).
  *
  * @param DomNode $node the current DomNode being considered
  * @return Frame
  */
 protected function _build_tree_r(DomNode $node)
 {
     $frame = new Frame($node);
     $id = $frame->get_id();
     $this->_registry[$id] = $frame;
     if (!$node->hasChildNodes()) {
         return $frame;
     }
     // Fixes 'cannot access undefined property for object with
     // overloaded access', fix by Stefan radulian
     // <*****@*****.**>
     //foreach ($node->childNodes as $child) {
     // Store the children in an array so that the tree can be modified
     $children = array();
     for ($i = 0; $i < $node->childNodes->length; $i++) {
         $children[] = $node->childNodes->item($i);
     }
     foreach ($children as $child) {
         $node_name = mb_strtolower($child->nodeName);
         // Skip non-displaying nodes
         if (in_array($node_name, self::$_HIDDEN_TAGS)) {
             if ($node_name !== "head" && $node_name !== "style") {
                 $child->parentNode->removeChild($child);
             }
             continue;
         }
         // Skip empty text nodes
         if ($node_name === "#text" && $child->nodeValue == "") {
             $child->parentNode->removeChild($child);
             continue;
         }
         // Skip empty image nodes
         if ($node_name === "img" && $child->getAttribute("src") == "") {
             $child->parentNode->removeChild($child);
             continue;
         }
         $frame->append_child($this->_build_tree_r($child), false);
     }
     return $frame;
 }
Beispiel #28
0
 /**
  * store classes
  */
 private function store()
 {
     $content = trim(implode(" ", $this->classes));
     if ($this->node instanceof \DOMElement) {
         if ($content) {
             $this->node->setAttribute('class', $content);
         } else {
             $this->node->removeAttribute('class');
         }
     }
 }
Beispiel #29
0
 protected function parseHtmlNode(DomNode $node, $output)
 {
     $tag = $node->tagName;
     switch ($tag) {
         case 'br':
             return "\n";
         case 'b':
         case 'strong':
             return "[b]{$output}[/b]";
         case 'i':
         case 'em':
             return "[i]{$output}[/i]";
         case 'u':
             return "[u]{$output}[/u]";
         case 'span':
             $style = $node->getAttribute('style');
             if ($style == 'text-decoration: underline;') {
                 $output = "[u]{$output}[/u]";
             }
             return $output;
         case 'a':
             return "[url={$node->getAttribute('href')}]{$output}[/url]";
         case 'img':
             return "[img]{$node->getAttribute('src')}[/img]";
         case 'address':
             return "{$output}";
         case 'h1':
             return "\n[size=6]{$output}[/size]";
         case 'h2':
             return "\n[size=5]{$output}[/size]";
         case 'h3':
             return "\n[size=4]{$output}[/size]";
         case 'h4':
             return "\n[size=3]{$output}[/size]";
         case 'h5':
             return "\n[size=2]{$output}[/size]";
         case 'h6':
             return "\n[size=1]{$output}[/size]";
         case 'pre':
         case 'li':
         case 'ul':
         case 'ol':
             return "\n[{$tag}]{$output}[/{$tag}]";
         case 'hr':
             return "\n[hr]";
         case 'p':
             $output = trim($output);
             if (!$output) {
                 return;
             }
             $style = $node->getAttribute('style');
             if ($node->getAttribute('class' == 'caption')) {
                 $output = "[center]{$output}[/center]";
             } elseif ($style == 'text-align: left;') {
                 $output = "[left]{$output}[/left]";
             } elseif ($style == 'text-align: center;') {
                 $output = "[center]{$output}[/center]";
             } elseif ($style == 'text-align: right;') {
                 $output = "[right]{$output}[/right]";
             }
             return "\n{$output}";
         case 'div':
         default:
             return "{$output}";
     }
 }
Beispiel #30
0
  /**
   * Recursively adds {@link Frame} objects to the tree
   *
   * Recursively build a tree of Frame objects based on a dom tree.
   * No layout information is calculated at this time, although the
   * tree may be adjusted (i.e. nodes and frames for generated content
   * and images may be created).
   *
   * @param DomNode $node the current DomNode being considered
   * @return Frame
   */
  protected function _build_tree_r(DomNode $node) {
    
    $frame = new Frame($node);
    $id = $frame->get_id();
    $this->_registry[ $id ] = $frame;
    
    if ( !$node->hasChildNodes() )
      return $frame;

    // Fixes 'cannot access undefined property for object with
    // overloaded access', fix by Stefan radulian
    // <*****@*****.**>    
    //foreach ($node->childNodes as $child) {

    // Store the children in an array so that the tree can be modified
    $children = array();
    for ($i = 0; $i < $node->childNodes->length; $i++)
      $children[] = $node->childNodes->item($i);

    foreach ($children as $child) {
      // Skip non-displaying nodes
      if ( in_array( mb_strtolower($child->nodeName), self::$_HIDDEN_TAGS) )  {
        if ( mb_strtolower($child->nodeName) != "head" &&
             mb_strtolower($child->nodeName) != "style" ) 
          $child->parentNode->removeChild($child);
        continue;
      }

      // Skip empty text nodes
      if ( $child->nodeName == "#text" && $child->nodeValue == "" ) {
        $child->parentNode->removeChild($child);
        continue;
      }
      
      // Add a container frame for images
      if ( $child->nodeName == "img" ) {
        $img_node = $child->ownerDocument->createElement("img_inner");
     
        // Move attributes to inner node        
        foreach ( $child->attributes as $attr => $attr_node ) {
          // Skip style, but move all other attributes
          if ( $attr == "style" )
            continue;
       
          $img_node->setAttribute($attr, $attr_node->value);
        }

        foreach ( $child->attributes as $attr => $node ) {
          if ( $attr == "style" )
            continue;
          $child->removeAttribute($attr);
        }

        $child->appendChild($img_node);
      }
   
      $frame->append_child($this->_build_tree_r($child), false);

    }
    
    return $frame;
  }