FrameTree.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. namespace Dompdf\Frame;
  3. use DOMDocument;
  4. use DOMNode;
  5. use DOMElement;
  6. use DOMXPath;
  7. use Dompdf\Exception;
  8. use Dompdf\Frame;
  9. /**
  10. * @package dompdf
  11. * @link http://dompdf.github.com/
  12. * @author Benj Carson <benjcarson@digitaljunkies.ca>
  13. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  14. */
  15. /**
  16. * Represents an entire document as a tree of frames
  17. *
  18. * The FrameTree consists of {@link Frame} objects each tied to specific
  19. * DOMNode objects in a specific DomDocument. The FrameTree has the same
  20. * structure as the DomDocument, but adds additional capabilities for
  21. * styling and layout.
  22. *
  23. * @package dompdf
  24. */
  25. class FrameTree
  26. {
  27. /**
  28. * Tags to ignore while parsing the tree
  29. *
  30. * @var array
  31. */
  32. protected static $HIDDEN_TAGS = [
  33. "area",
  34. "base",
  35. "basefont",
  36. "head",
  37. "style",
  38. "meta",
  39. "title",
  40. "colgroup",
  41. "noembed",
  42. "param",
  43. "#comment"
  44. ];
  45. /**
  46. * The main DomDocument
  47. *
  48. * @see http://ca2.php.net/manual/en/ref.dom.php
  49. * @var DOMDocument
  50. */
  51. protected $_dom;
  52. /**
  53. * The root node of the FrameTree.
  54. *
  55. * @var Frame
  56. */
  57. protected $_root;
  58. /**
  59. * Subtrees of absolutely positioned elements
  60. *
  61. * @var array of Frames
  62. */
  63. protected $_absolute_frames;
  64. /**
  65. * A mapping of {@link Frame} objects to DOMNode objects
  66. *
  67. * @var array
  68. */
  69. protected $_registry;
  70. /**
  71. * Class constructor
  72. *
  73. * @param DOMDocument $dom the main DomDocument object representing the current html document
  74. */
  75. public function __construct(DomDocument $dom)
  76. {
  77. $this->_dom = $dom;
  78. $this->_root = null;
  79. $this->_registry = [];
  80. }
  81. /**
  82. * Returns the DOMDocument object representing the current html document
  83. *
  84. * @return DOMDocument
  85. */
  86. public function get_dom()
  87. {
  88. return $this->_dom;
  89. }
  90. /**
  91. * Returns the root frame of the tree
  92. *
  93. * @return Frame
  94. */
  95. public function get_root()
  96. {
  97. return $this->_root;
  98. }
  99. /**
  100. * Returns a specific frame given its id
  101. *
  102. * @param string $id
  103. *
  104. * @return Frame|null
  105. */
  106. public function get_frame($id)
  107. {
  108. return isset($this->_registry[$id]) ? $this->_registry[$id] : null;
  109. }
  110. /**
  111. * Returns a post-order iterator for all frames in the tree
  112. *
  113. * @return FrameTreeList|Frame[]
  114. */
  115. public function get_frames()
  116. {
  117. return new FrameTreeList($this->_root);
  118. }
  119. /**
  120. * Builds the tree
  121. */
  122. public function build_tree()
  123. {
  124. $html = $this->_dom->getElementsByTagName("html")->item(0);
  125. if (is_null($html)) {
  126. $html = $this->_dom->firstChild;
  127. }
  128. if (is_null($html)) {
  129. throw new Exception("Requested HTML document contains no data.");
  130. }
  131. $this->fix_tables();
  132. $this->_root = $this->_build_tree_r($html);
  133. }
  134. /**
  135. * Adds missing TBODYs around TR
  136. */
  137. protected function fix_tables()
  138. {
  139. $xp = new DOMXPath($this->_dom);
  140. // Move table caption before the table
  141. // FIXME find a better way to deal with it...
  142. $captions = $xp->query('//table/caption');
  143. foreach ($captions as $caption) {
  144. $table = $caption->parentNode;
  145. $table->parentNode->insertBefore($caption, $table);
  146. }
  147. $firstRows = $xp->query('//table/tr[1]');
  148. /** @var DOMElement $tableChild */
  149. foreach ($firstRows as $tableChild) {
  150. $tbody = $this->_dom->createElement('tbody');
  151. $tableNode = $tableChild->parentNode;
  152. do {
  153. if ($tableChild->nodeName === 'tr') {
  154. $tmpNode = $tableChild;
  155. $tableChild = $tableChild->nextSibling;
  156. $tableNode->removeChild($tmpNode);
  157. $tbody->appendChild($tmpNode);
  158. } else {
  159. if ($tbody->hasChildNodes() === true) {
  160. $tableNode->insertBefore($tbody, $tableChild);
  161. $tbody = $this->_dom->createElement('tbody');
  162. }
  163. $tableChild = $tableChild->nextSibling;
  164. }
  165. } while ($tableChild);
  166. if ($tbody->hasChildNodes() === true) {
  167. $tableNode->appendChild($tbody);
  168. }
  169. }
  170. }
  171. // FIXME: temporary hack, preferably we will improve rendering of sequential #text nodes
  172. /**
  173. * Remove a child from a node
  174. *
  175. * Remove a child from a node. If the removed node results in two
  176. * adjacent #text nodes then combine them.
  177. *
  178. * @param DOMNode $node the current DOMNode being considered
  179. * @param array $children an array of nodes that are the children of $node
  180. * @param int $index index from the $children array of the node to remove
  181. */
  182. protected function _remove_node(DOMNode $node, array &$children, $index)
  183. {
  184. $child = $children[$index];
  185. $previousChild = $child->previousSibling;
  186. $nextChild = $child->nextSibling;
  187. $node->removeChild($child);
  188. if (isset($previousChild, $nextChild)) {
  189. if ($previousChild->nodeName === "#text" && $nextChild->nodeName === "#text") {
  190. $previousChild->nodeValue .= $nextChild->nodeValue;
  191. $this->_remove_node($node, $children, $index+1);
  192. }
  193. }
  194. array_splice($children, $index, 1);
  195. }
  196. /**
  197. * Recursively adds {@link Frame} objects to the tree
  198. *
  199. * Recursively build a tree of Frame objects based on a dom tree.
  200. * No layout information is calculated at this time, although the
  201. * tree may be adjusted (i.e. nodes and frames for generated content
  202. * and images may be created).
  203. *
  204. * @param DOMNode $node the current DOMNode being considered
  205. *
  206. * @return Frame
  207. */
  208. protected function _build_tree_r(DOMNode $node)
  209. {
  210. $frame = new Frame($node);
  211. $id = $frame->get_id();
  212. $this->_registry[$id] = $frame;
  213. if (!$node->hasChildNodes()) {
  214. return $frame;
  215. }
  216. // Store the children in an array so that the tree can be modified
  217. $children = [];
  218. $length = $node->childNodes->length;
  219. for ($i = 0; $i < $length; $i++) {
  220. $children[] = $node->childNodes->item($i);
  221. }
  222. $index = 0;
  223. // INFO: We don't advance $index if a node is removed to avoid skipping nodes
  224. while ($index < count($children)) {
  225. $child = $children[$index];
  226. $nodeName = strtolower($child->nodeName);
  227. // Skip non-displaying nodes
  228. if (in_array($nodeName, self::$HIDDEN_TAGS)) {
  229. if ($nodeName !== "head" && $nodeName !== "style") {
  230. $this->_remove_node($node, $children, $index);
  231. } else {
  232. $index++;
  233. }
  234. continue;
  235. }
  236. // Skip empty text nodes
  237. if ($nodeName === "#text" && $child->nodeValue === "") {
  238. $this->_remove_node($node, $children, $index);
  239. continue;
  240. }
  241. // Skip empty image nodes
  242. if ($nodeName === "img" && $child->getAttribute("src") === "") {
  243. $this->_remove_node($node, $children, $index);
  244. continue;
  245. }
  246. if (is_object($child)) {
  247. $frame->append_child($this->_build_tree_r($child), false);
  248. }
  249. $index++;
  250. }
  251. return $frame;
  252. }
  253. /**
  254. * @param DOMElement $node
  255. * @param DOMElement $new_node
  256. * @param string $pos
  257. *
  258. * @return mixed
  259. */
  260. public function insert_node(DOMElement $node, DOMElement $new_node, $pos)
  261. {
  262. if ($pos === "after" || !$node->firstChild) {
  263. $node->appendChild($new_node);
  264. } else {
  265. $node->insertBefore($new_node, $node->firstChild);
  266. }
  267. $this->_build_tree_r($new_node);
  268. $frame_id = $new_node->getAttribute("frame_id");
  269. $frame = $this->get_frame($frame_id);
  270. $parent_id = $node->getAttribute("frame_id");
  271. $parent = $this->get_frame($parent_id);
  272. if ($parent) {
  273. if ($pos === "before") {
  274. $parent->prepend_child($frame, false);
  275. } else {
  276. $parent->append_child($frame, false);
  277. }
  278. }
  279. return $frame_id;
  280. }
  281. }