1 | <?php |
||
17 | class Normalizer |
||
18 | { |
||
19 | /** |
||
20 | * Structure of a basic HTML document. |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $tree = array( |
||
25 | 'doctype' => '', |
||
26 | 'html' => array( |
||
27 | 'start' => '<html>', |
||
28 | 'end' => '</html>', |
||
29 | 'content' => array(), |
||
30 | ), |
||
31 | 'head' => array( |
||
32 | 'start' => '<head>', |
||
33 | 'end' => '</head>', |
||
34 | 'content' => array(), |
||
35 | ), |
||
36 | 'body' => array( |
||
37 | 'start' => '<body>', |
||
38 | 'end' => '</body>', |
||
39 | 'content' => array(), |
||
40 | ), |
||
41 | ); |
||
42 | |||
43 | /** |
||
44 | * What root element did we last add to. |
||
45 | * |
||
46 | * @var string|null |
||
47 | */ |
||
48 | protected $previousKey = null; |
||
49 | |||
50 | /** |
||
51 | * Parse a HTML document. |
||
52 | * |
||
53 | * @param string $html |
||
54 | * @return void |
||
55 | */ |
||
56 | 14 | public function loadHtml($html) |
|
87 | |||
88 | /** |
||
89 | * Format the document in a structured way (ensures root elements exists and moves scripts/css into <body>). |
||
90 | * |
||
91 | * @return string |
||
92 | */ |
||
93 | 14 | public function saveHtml() |
|
121 | |||
122 | /** |
||
123 | * Add a node into the tree for the correct parent. |
||
124 | * |
||
125 | * @param string $node |
||
126 | * @return void |
||
127 | */ |
||
128 | 14 | protected function addToTree($node) |
|
162 | |||
163 | /** |
||
164 | * Add a node to the the tree. |
||
165 | * |
||
166 | * @param string $key |
||
167 | * @param string $node |
||
168 | * @param bool $setPrevious |
||
169 | * @return void |
||
170 | */ |
||
171 | 14 | protected function addTo($key, $node, $setPrevious) |
|
188 | |||
189 | /** |
||
190 | * Get the name of a node without </> |
||
191 | * |
||
192 | * @param string $node |
||
193 | * @return string |
||
194 | */ |
||
195 | 13 | protected function nodeName($node) |
|
203 | } |
||
204 |