frame_tree.cls.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * @package dompdf
  4. * @link http://dompdf.github.com/
  5. * @author Benj Carson <benjcarson@digitaljunkies.ca>
  6. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  7. */
  8. /**
  9. * Represents an entire document as a tree of frames
  10. *
  11. * The Frame_Tree consists of {@link Frame} objects each tied to specific
  12. * DOMNode objects in a specific DomDocument. The Frame_Tree has the same
  13. * structure as the DomDocument, but adds additional capabalities for
  14. * styling and layout.
  15. *
  16. * @package dompdf
  17. * @access protected
  18. */
  19. class Frame_Tree {
  20. /**
  21. * Tags to ignore while parsing the tree
  22. *
  23. * @var array
  24. */
  25. static protected $_HIDDEN_TAGS = array("area", "base", "basefont", "head", "style",
  26. "meta", "title", "colgroup",
  27. "noembed", "noscript", "param", "#comment");
  28. /**
  29. * The main DomDocument
  30. *
  31. * @see http://ca2.php.net/manual/en/ref.dom.php
  32. * @var DomDocument
  33. */
  34. protected $_dom;
  35. /**
  36. * The root node of the FrameTree.
  37. *
  38. * @var Frame
  39. */
  40. protected $_root;
  41. /**
  42. * Subtrees of absolutely positioned elements
  43. *
  44. * @var array of Frames
  45. */
  46. protected $_absolute_frames;
  47. /**
  48. * A mapping of {@link Frame} objects to DOMNode objects
  49. *
  50. * @var array
  51. */
  52. protected $_registry;
  53. /**
  54. * Class constructor
  55. *
  56. * @param DomDocument $dom the main DomDocument object representing the current html document
  57. */
  58. function __construct(DomDocument $dom) {
  59. $this->_dom = $dom;
  60. $this->_root = null;
  61. $this->_registry = array();
  62. }
  63. function __destruct() {
  64. clear_object($this);
  65. }
  66. /**
  67. * Returns the DomDocument object representing the curent html document
  68. *
  69. * @return DOMDocument
  70. */
  71. function get_dom() {
  72. return $this->_dom;
  73. }
  74. /**
  75. * Returns the root frame of the tree
  76. *
  77. * @return Page_Frame_Decorator
  78. */
  79. function get_root() {
  80. return $this->_root;
  81. }
  82. /**
  83. * Returns a specific frame given its id
  84. *
  85. * @param string $id
  86. * @return Frame
  87. */
  88. function get_frame($id) {
  89. return isset($this->_registry[$id]) ? $this->_registry[$id] : null;
  90. }
  91. /**
  92. * Returns a post-order iterator for all frames in the tree
  93. *
  94. * @return FrameTreeList|Frame[]
  95. */
  96. function get_frames() {
  97. return new FrameTreeList($this->_root);
  98. }
  99. /**
  100. * Builds the tree
  101. */
  102. function build_tree() {
  103. $html = $this->_dom->getElementsByTagName("html")->item(0);
  104. if ( is_null($html) ) {
  105. $html = $this->_dom->firstChild;
  106. }
  107. if ( is_null($html) ) {
  108. throw new DOMPDF_Exception("Requested HTML document contains no data.");
  109. }
  110. $this->fix_tables();
  111. $this->_root = $this->_build_tree_r($html);
  112. }
  113. /**
  114. * Adds missing TBODYs around TR
  115. */
  116. protected function fix_tables(){
  117. $xp = new DOMXPath($this->_dom);
  118. // Move table caption before the table
  119. // FIXME find a better way to deal with it...
  120. $captions = $xp->query("//table/caption");
  121. foreach($captions as $caption) {
  122. $table = $caption->parentNode;
  123. $table->parentNode->insertBefore($caption, $table);
  124. }
  125. $rows = $xp->query("//table/tr");
  126. foreach($rows as $row) {
  127. $tbody = $this->_dom->createElement("tbody");
  128. $tbody = $row->parentNode->insertBefore($tbody, $row);
  129. $tbody->appendChild($row);
  130. }
  131. }
  132. /**
  133. * Recursively adds {@link Frame} objects to the tree
  134. *
  135. * Recursively build a tree of Frame objects based on a dom tree.
  136. * No layout information is calculated at this time, although the
  137. * tree may be adjusted (i.e. nodes and frames for generated content
  138. * and images may be created).
  139. *
  140. * @param DOMNode $node the current DOMNode being considered
  141. * @return Frame
  142. */
  143. protected function _build_tree_r(DOMNode $node) {
  144. $frame = new Frame($node);
  145. $id = $frame->get_id();
  146. $this->_registry[ $id ] = $frame;
  147. if ( !$node->hasChildNodes() ) {
  148. return $frame;
  149. }
  150. // Fixes 'cannot access undefined property for object with
  151. // overloaded access', fix by Stefan radulian
  152. // <stefan.radulian@symbion.at>
  153. //foreach ($node->childNodes as $child) {
  154. // Store the children in an array so that the tree can be modified
  155. $children = array();
  156. for ($i = 0; $i < $node->childNodes->length; $i++) {
  157. $children[] = $node->childNodes->item($i);
  158. }
  159. foreach ($children as $child) {
  160. $node_name = mb_strtolower($child->nodeName);
  161. // Skip non-displaying nodes
  162. if ( in_array($node_name, self::$_HIDDEN_TAGS) ) {
  163. if ( $node_name !== "head" && $node_name !== "style" ) {
  164. $child->parentNode->removeChild($child);
  165. }
  166. continue;
  167. }
  168. // Skip empty text nodes
  169. if ( $node_name === "#text" && $child->nodeValue == "" ) {
  170. $child->parentNode->removeChild($child);
  171. continue;
  172. }
  173. // Skip empty image nodes
  174. if ( $node_name === "img" && $child->getAttribute("src") == "" ) {
  175. $child->parentNode->removeChild($child);
  176. continue;
  177. }
  178. $frame->append_child($this->_build_tree_r($child), false);
  179. }
  180. return $frame;
  181. }
  182. public function insert_node(DOMNode $node, DOMNode $new_node, $pos) {
  183. if ( $pos === "after" || !$node->firstChild ) {
  184. $node->appendChild($new_node);
  185. }
  186. else {
  187. $node->insertBefore($new_node, $node->firstChild);
  188. }
  189. $this->_build_tree_r($new_node);
  190. $frame_id = $new_node->getAttribute("frame_id");
  191. $frame = $this->get_frame($frame_id);
  192. $parent_id = $node->getAttribute("frame_id");
  193. $parent = $this->get_frame($parent_id);
  194. if ( $pos === "before" ) {
  195. $parent->prepend_child($frame, false);
  196. }
  197. else {
  198. $parent->append_child($frame, false);
  199. }
  200. return $frame_id;
  201. }
  202. }