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