[3019] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * DOMPDF - PHP5 HTML to PDF renderer |
---|
| 4 | * |
---|
| 5 | * File: $RCSfile: frame_tree.cls.php,v $ |
---|
| 6 | * Created on: 2004-06-02 |
---|
| 7 | * |
---|
| 8 | * Copyright (c) 2004 - Benj Carson <benjcarson@digitaljunkies.ca> |
---|
| 9 | * |
---|
| 10 | * This library is free software; you can redistribute it and/or |
---|
| 11 | * modify it under the terms of the GNU Lesser General Public |
---|
| 12 | * License as published by the Free Software Foundation; either |
---|
| 13 | * version 2.1 of the License, or (at your option) any later version. |
---|
| 14 | * |
---|
| 15 | * This library is distributed in the hope that it will be useful, |
---|
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 18 | * Lesser General Public License for more details. |
---|
| 19 | * |
---|
| 20 | * You should have received a copy of the GNU Lesser General Public License |
---|
| 21 | * along with this library in the file LICENSE.LGPL; if not, write to the |
---|
| 22 | * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
---|
| 23 | * 02111-1307 USA |
---|
| 24 | * |
---|
| 25 | * Alternatively, you may distribute this software under the terms of the |
---|
| 26 | * PHP License, version 3.0 or later. A copy of this license should have |
---|
| 27 | * been distributed with this file in the file LICENSE.PHP . If this is not |
---|
| 28 | * the case, you can obtain a copy at http://www.php.net/license/3_0.txt. |
---|
| 29 | * |
---|
| 30 | * The latest version of DOMPDF might be available at: |
---|
| 31 | * http://www.digitaljunkies.ca/dompdf |
---|
| 32 | * |
---|
| 33 | * @link http://www.digitaljunkies.ca/dompdf |
---|
| 34 | * @copyright 2004 Benj Carson |
---|
| 35 | * @author Benj Carson <benjcarson@digitaljunkies.ca> |
---|
| 36 | * @package dompdf |
---|
| 37 | * @version 0.5.1 |
---|
| 38 | */ |
---|
| 39 | |
---|
| 40 | /* $Id: frame_tree.cls.php 186 2009-10-19 22:42:06Z eclecticgeek@gmail.com $ */ |
---|
| 41 | |
---|
| 42 | /** |
---|
| 43 | * Represents an entire document as a tree of frames |
---|
| 44 | * |
---|
| 45 | * The Frame_Tree consists of {@link Frame} objects each tied to specific |
---|
| 46 | * DomNode objects in a specific DomDocument. The Frame_Tree has the same |
---|
| 47 | * structure as the DomDocument, but adds additional capabalities for |
---|
| 48 | * styling and layout. |
---|
| 49 | * |
---|
| 50 | * @package dompdf |
---|
| 51 | * @access protected |
---|
| 52 | */ |
---|
| 53 | class Frame_Tree { |
---|
| 54 | |
---|
| 55 | /** |
---|
| 56 | * Tags to ignore while parsing the tree |
---|
| 57 | * |
---|
| 58 | * @var array |
---|
| 59 | */ |
---|
| 60 | static protected $_HIDDEN_TAGS = array("area", "base", "basefont", "head", "style", |
---|
| 61 | "meta", "title", "colgroup", |
---|
| 62 | "noembed", "noscript", "param", "#comment"); |
---|
| 63 | /** |
---|
| 64 | * The main DomDocument |
---|
| 65 | * |
---|
| 66 | * @see http://ca2.php.net/manual/en/ref.dom.php |
---|
| 67 | * @var DomDocument |
---|
| 68 | */ |
---|
| 69 | protected $_dom; |
---|
| 70 | |
---|
| 71 | /** |
---|
| 72 | * The root node of the FrameTree. |
---|
| 73 | * |
---|
| 74 | * @var Frame |
---|
| 75 | */ |
---|
| 76 | protected $_root; |
---|
| 77 | |
---|
| 78 | /** |
---|
| 79 | * Subtrees of absolutely positioned elements |
---|
| 80 | * |
---|
| 81 | * @var array of Frames |
---|
| 82 | */ |
---|
| 83 | protected $_absolute_frames; |
---|
| 84 | |
---|
| 85 | /** |
---|
| 86 | * A mapping of {@link Frame} objects to DomNode objects |
---|
| 87 | * |
---|
| 88 | * @var array |
---|
| 89 | */ |
---|
| 90 | protected $_registry; |
---|
| 91 | |
---|
| 92 | |
---|
| 93 | /** |
---|
| 94 | * Class constructor |
---|
| 95 | * |
---|
| 96 | * @param DomDocument $dom the main DomDocument object representing the current html document |
---|
| 97 | */ |
---|
| 98 | function __construct(DomDocument $dom) { |
---|
| 99 | $this->_dom = $dom; |
---|
| 100 | $this->_root = null; |
---|
| 101 | $this->_registry = array(); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | /** |
---|
| 105 | * Returns the DomDocument object representing the curent html document |
---|
| 106 | * |
---|
| 107 | * @return DomDocument |
---|
| 108 | */ |
---|
| 109 | function get_dom() { return $this->_dom; } |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | * Returns the root frame of the tree |
---|
| 113 | * |
---|
| 114 | * @return Frame |
---|
| 115 | */ |
---|
| 116 | function get_root() { return $this->_root; } |
---|
| 117 | |
---|
| 118 | /** |
---|
| 119 | * Returns a specific frame given its id |
---|
| 120 | * |
---|
| 121 | * @param string $id |
---|
| 122 | * @return Frame |
---|
| 123 | */ |
---|
| 124 | function get_frame($id) { return isset($this->_registry[$id]) ? $this->_registry[$id] : null; } |
---|
| 125 | |
---|
| 126 | /** |
---|
| 127 | * Returns a post-order iterator for all frames in the tree |
---|
| 128 | * |
---|
| 129 | * @return FrameTreeList |
---|
| 130 | */ |
---|
| 131 | function get_frames() { return new FrameTreeList($this->_root); } |
---|
| 132 | |
---|
| 133 | /** |
---|
| 134 | * Builds the tree |
---|
| 135 | */ |
---|
| 136 | function build_tree() { |
---|
| 137 | $html = $this->_dom->getElementsByTagName("html")->item(0); |
---|
| 138 | if ( is_null($html) ) |
---|
| 139 | $html = $this->_dom->firstChild; |
---|
| 140 | |
---|
| 141 | if ( is_null($html) ) |
---|
| 142 | throw new DOMPDF_Exception("Requested HTML document contains no data."); |
---|
| 143 | |
---|
| 144 | $this->_root = $this->_build_tree_r($html); |
---|
| 145 | |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | /** |
---|
| 149 | * Recursively adds {@link Frame} objects to the tree |
---|
| 150 | * |
---|
| 151 | * Recursively build a tree of Frame objects based on a dom tree. |
---|
| 152 | * No layout information is calculated at this time, although the |
---|
| 153 | * tree may be adjusted (i.e. nodes and frames for generated content |
---|
| 154 | * and images may be created). |
---|
| 155 | * |
---|
| 156 | * @param DomNode $node the current DomNode being considered |
---|
| 157 | * @return Frame |
---|
| 158 | */ |
---|
| 159 | protected function _build_tree_r(DomNode $node) { |
---|
| 160 | |
---|
| 161 | $frame = new Frame($node); |
---|
| 162 | $id = $frame->get_id(); |
---|
| 163 | $this->_registry[ $id ] = $frame; |
---|
| 164 | |
---|
| 165 | if ( !$node->hasChildNodes() ) |
---|
| 166 | return $frame; |
---|
| 167 | |
---|
| 168 | // Fixes 'cannot access undefined property for object with |
---|
| 169 | // overloaded access', fix by Stefan radulian |
---|
| 170 | // <stefan.radulian@symbion.at> |
---|
| 171 | //foreach ($node->childNodes as $child) { |
---|
| 172 | |
---|
| 173 | // Store the children in an array so that the tree can be modified |
---|
| 174 | $children = array(); |
---|
| 175 | for ($i = 0; $i < $node->childNodes->length; $i++) |
---|
| 176 | $children[] = $node->childNodes->item($i); |
---|
| 177 | |
---|
| 178 | foreach ($children as $child) { |
---|
| 179 | // Skip non-displaying nodes |
---|
| 180 | if ( in_array( mb_strtolower($child->nodeName), self::$_HIDDEN_TAGS) ) { |
---|
| 181 | if ( mb_strtolower($child->nodeName) != "head" && |
---|
| 182 | mb_strtolower($child->nodeName) != "style" ) |
---|
| 183 | $child->parentNode->removeChild($child); |
---|
| 184 | continue; |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | // Skip empty text nodes |
---|
| 188 | if ( $child->nodeName == "#text" && $child->nodeValue == "" ) { |
---|
| 189 | $child->parentNode->removeChild($child); |
---|
| 190 | continue; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | // Skip empty image nodes |
---|
| 194 | if ( $child->nodeName == "img" && $child->getAttribute("src") == "" ) { |
---|
| 195 | $child->parentNode->removeChild($child); |
---|
| 196 | continue; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | // Add a container frame for images |
---|
| 200 | if ( $child->nodeName == "img" ) { |
---|
| 201 | $img_node = $child->ownerDocument->createElement("img_inner"); |
---|
| 202 | |
---|
| 203 | // Move attributes to inner node |
---|
| 204 | foreach ( $child->attributes as $attr => $attr_node ) { |
---|
| 205 | // Skip style, but move all other attributes |
---|
| 206 | if ( $attr == "style" ) |
---|
| 207 | continue; |
---|
| 208 | |
---|
| 209 | $img_node->setAttribute($attr, $attr_node->value); |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | foreach ( $child->attributes as $attr => $node ) { |
---|
| 213 | if ( $attr == "style" ) |
---|
| 214 | continue; |
---|
| 215 | $child->removeAttribute($attr); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | $child->appendChild($img_node); |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | $frame->append_child($this->_build_tree_r($child), false); |
---|
| 222 | |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | return $frame; |
---|
| 226 | } |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | ?> |
---|