frame_tree.cls.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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.dompdf.com/
  32. *
  33. * @link http://www.dompdf.com/
  34. * @copyright 2004 Benj Carson
  35. * @author Benj Carson <benjcarson@digitaljunkies.ca>
  36. * @package dompdf
  37. */
  38. /* $Id: frame_tree.cls.php 216 2010-03-11 22:49:18Z ryan.masten $ */
  39. /**
  40. * Represents an entire document as a tree of frames
  41. *
  42. * The Frame_Tree consists of {@link Frame} objects each tied to specific
  43. * DomNode objects in a specific DomDocument. The Frame_Tree has the same
  44. * structure as the DomDocument, but adds additional capabalities for
  45. * styling and layout.
  46. *
  47. * @package dompdf
  48. * @access protected
  49. */
  50. class Frame_Tree {
  51. /**
  52. * Tags to ignore while parsing the tree
  53. *
  54. * @var array
  55. */
  56. static protected $_HIDDEN_TAGS = array("area", "base", "basefont", "head", "style",
  57. "meta", "title", "colgroup",
  58. "noembed", "noscript", "param", "#comment");
  59. /**
  60. * The main DomDocument
  61. *
  62. * @see http://ca2.php.net/manual/en/ref.dom.php
  63. * @var DomDocument
  64. */
  65. protected $_dom;
  66. /**
  67. * The root node of the FrameTree.
  68. *
  69. * @var Frame
  70. */
  71. protected $_root;
  72. /**
  73. * Subtrees of absolutely positioned elements
  74. *
  75. * @var array of Frames
  76. */
  77. protected $_absolute_frames;
  78. /**
  79. * A mapping of {@link Frame} objects to DomNode objects
  80. *
  81. * @var array
  82. */
  83. protected $_registry;
  84. /**
  85. * Class constructor
  86. *
  87. * @param DomDocument $dom the main DomDocument object representing the current html document
  88. */
  89. function __construct(DomDocument $dom) {
  90. $this->_dom = $dom;
  91. $this->_root = null;
  92. $this->_registry = array();
  93. }
  94. /**
  95. * Returns the DomDocument object representing the curent html document
  96. *
  97. * @return DomDocument
  98. */
  99. function get_dom() { return $this->_dom; }
  100. /**
  101. * Returns the root frame of the tree
  102. *
  103. * @return Frame
  104. */
  105. function get_root() { return $this->_root; }
  106. /**
  107. * Returns a specific frame given its id
  108. *
  109. * @param string $id
  110. * @return Frame
  111. */
  112. function get_frame($id) { return isset($this->_registry[$id]) ? $this->_registry[$id] : null; }
  113. /**
  114. * Returns a post-order iterator for all frames in the tree
  115. *
  116. * @return FrameTreeList
  117. */
  118. function get_frames() { return new FrameTreeList($this->_root); }
  119. /**
  120. * Builds the tree
  121. */
  122. function build_tree() {
  123. $html = $this->_dom->getElementsByTagName("html")->item(0);
  124. if ( is_null($html) )
  125. $html = $this->_dom->firstChild;
  126. if ( is_null($html) )
  127. throw new DOMPDF_Exception("Requested HTML document contains no data.");
  128. $this->_root = $this->_build_tree_r($html);
  129. }
  130. /**
  131. * Recursively adds {@link Frame} objects to the tree
  132. *
  133. * Recursively build a tree of Frame objects based on a dom tree.
  134. * No layout information is calculated at this time, although the
  135. * tree may be adjusted (i.e. nodes and frames for generated content
  136. * and images may be created).
  137. *
  138. * @param DomNode $node the current DomNode being considered
  139. * @return Frame
  140. */
  141. protected function _build_tree_r(DomNode $node) {
  142. $frame = new Frame($node);
  143. $id = $frame->get_id();
  144. $this->_registry[ $id ] = $frame;
  145. if ( !$node->hasChildNodes() )
  146. return $frame;
  147. // Fixes 'cannot access undefined property for object with
  148. // overloaded access', fix by Stefan radulian
  149. // <stefan.radulian@symbion.at>
  150. //foreach ($node->childNodes as $child) {
  151. // Store the children in an array so that the tree can be modified
  152. $children = array();
  153. for ($i = 0; $i < $node->childNodes->length; $i++)
  154. $children[] = $node->childNodes->item($i);
  155. foreach ($children as $child) {
  156. // Skip non-displaying nodes
  157. if ( in_array( mb_strtolower($child->nodeName), self::$_HIDDEN_TAGS) ) {
  158. if ( mb_strtolower($child->nodeName) !== "head" &&
  159. mb_strtolower($child->nodeName) !== "style" )
  160. $child->parentNode->removeChild($child);
  161. continue;
  162. }
  163. // Skip empty text nodes
  164. if ( $child->nodeName === "#text" && $child->nodeValue == "" ) {
  165. $child->parentNode->removeChild($child);
  166. continue;
  167. }
  168. // Skip empty image nodes
  169. if ( $child->nodeName === "img" && $child->getAttribute("src") == "" ) {
  170. $child->parentNode->removeChild($child);
  171. continue;
  172. }
  173. // Add a container frame for images
  174. if ( $child->nodeName === "img" ) {
  175. $img_node = $child->ownerDocument->createElement("img_inner");
  176. // Move attributes to inner node
  177. foreach ( $child->attributes as $attr => $attr_node ) {
  178. // Skip style, but move all other attributes
  179. if ( $attr === "style" )
  180. continue;
  181. $img_node->setAttribute($attr, $attr_node->value);
  182. }
  183. foreach ( $child->attributes as $attr => $node ) {
  184. if ( $attr === "style" )
  185. continue;
  186. $child->removeAttribute($attr);
  187. }
  188. $child->appendChild($img_node);
  189. }
  190. $frame->append_child($this->_build_tree_r($child), false);
  191. }
  192. return $frame;
  193. }
  194. }