-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathHtmlPage.php
367 lines (333 loc) · 9.61 KB
/
HtmlPage.php
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<?php
namespace Wa72\HtmlPageDom;
use Symfony\Component\CssSelector\CssSelector;
use Wa72\HtmlPrettymin\PrettyMin;
/**
* This class represents a complete HTML document.
*
* It offers convenience functions for getting and setting elements of the document
* such as setTitle(), getTitle(), setMeta($name, $value), getBody().
*
* It uses HtmlPageCrawler to navigate and manipulate the DOM tree.
*
* @author Christoph Singer
* @license MIT
*/
class HtmlPage
{
/**
*
* @var \DOMDocument
*/
protected $dom;
/**
* @var string
*/
protected $charset;
/**
* @var string
*/
protected $url;
/**
*
* @var HtmlPageCrawler
*/
protected $crawler;
public function __construct($content = '', $url = '', $charset = 'UTF-8')
{
$this->charset = $charset;
$this->url = $url;
if ($content == '') {
$content = '<!DOCTYPE html><html><head><title></title></head><body></body></html>';
}
$current = libxml_use_internal_errors(true);
$disableEntities = libxml_disable_entity_loader(true);
$this->dom = new \DOMDocument('1.0', $charset);
$this->dom->loadHTML('<meta http-equiv="Content-Type" content="text/html;charset='.$charset.'">');
$this->dom->validateOnParse = true;
if (function_exists('mb_convert_encoding') && in_array(strtolower($charset), array_map('strtolower', mb_list_encodings()))) {
$content = mb_convert_encoding($content, 'HTML-ENTITIES', $charset);
}
@$this->dom->loadHTML($content);
libxml_use_internal_errors($current);
libxml_disable_entity_loader($disableEntities);
$this->crawler = new HtmlPageCrawler($this->dom);
}
/**
* Get a HtmlPageCrawler object containing the root node of the HTML document
*
* @return HtmlPageCrawler
*/
public function getCrawler()
{
return $this->crawler;
}
/**
* Get a DOMDocument object for the HTML document
*
* @return \DOMDocument
*/
public function getDOMDocument()
{
return $this->dom;
}
/**
* Sets the page title of the HTML document
*
* @param string $title
*/
public function setTitle($title)
{
$t = $this->dom->getElementsByTagName('title')->item(0);
if ($t == null) {
$t = $this->dom->createElement('title');
$this->getHeadNode()->appendChild($t);
}
$t->nodeValue = htmlspecialchars($title);
}
/**
* Get the page title of the HTML document
*
* @return null|string
*/
public function getTitle()
{
$t = $this->dom->getElementsByTagName('title')->item(0);
if ($t == null) {
return null;
} else {
return $t->nodeValue;
}
}
/**
* Set a META tag with specified 'name' and 'content' attributes
*
* @TODO: add support for multiple meta tags with the same name but different languages
*
* @param $name
* @param $content
*/
public function setMeta($name, $content)
{
$c = $this->filterXPath('descendant-or-self::meta[@name = \'' . $name . '\']');
if (count($c) == 0) {
$node = $this->dom->createElement('meta');
$node->setAttribute('name', $name);
$this->getHeadNode()->appendChild($node);
$c->addNode($node);
}
$c->setAttribute('content', $content);
}
/**
* Remove all meta tags with the specified name attribute
*
* @param string $name
*/
public function removeMeta($name)
{
$meta = $this->filterXPath('descendant-or-self::meta[@name = \'' . $name . '\']');
$meta->remove();
}
/**
* Get the content attribute of a meta tag with the specified name attribute
*
* @param string $name
* @return null|string
*/
public function getMeta($name)
{
$node = $this->filterXPath('descendant-or-self::meta[@name = \'' . $name . '\']')->getNode(0);
if ($node instanceof \DOMElement) {
return $node->getAttribute('content');
} else {
return null;
}
}
/**
* Set the base tag with href attribute set to parameter $url
*
* @param string $url
*/
public function setBaseHref($url)
{
$node = $this->filterXPath('descendant-or-self::base')->getNode(0);
if ($node == null) {
$node = $this->dom->createElement('base');
$this->getHeadNode()->appendChild($node);
}
$node->setAttribute('href', $url);
}
/**
* Get the href attribute from the base tag, null if not present in document
*
* @return null|string
*/
public function getBaseHref()
{
$node = $this->filterXPath('descendant-or-self::base')->getNode(0);
if ($node instanceof \DOMElement) {
return $node->getAttribute('href');
} else {
return null;
}
}
/**
* Sets innerHTML content of an element specified by elementId
*
* @param string $elementId
* @param string $html
*/
public function setHtmlById($elementId, $html)
{
$this->getElementById($elementId)->setInnerHtml($html);
}
/**
* Get the document's HEAD section as DOMElement
*
* @return \DOMElement
*/
public function getHeadNode()
{
$head = $this->dom->getElementsByTagName('head')->item(0);
if ($head == null) {
$head = $this->dom->createElement('head');
$head = $this->dom->documentElement->insertBefore($head, $this->getBodyNode());
}
return $head;
}
/**
* Get the document's body as DOMElement
*
* @return \DOMElement
*/
public function getBodyNode()
{
$body = $this->dom->getElementsByTagName('body')->item(0);
if ($body == null) {
$body = $this->dom->createElement('body');
$body = $this->dom->documentElement->appendChild($body);
}
return $body;
}
/**
* Get the document's HEAD section wrapped in a HtmlPageCrawler instance
*
* @return HtmlPageCrawler
*/
public function getHead()
{
return new HtmlPageCrawler($this->getHeadNode());
}
/**
* Get the document's body wrapped in a HtmlPageCrawler instance
*
* @return HtmlPageCrawler
*/
public function getBody()
{
return new HtmlPageCrawler($this->getBodyNode());
}
public function __toString()
{
return $this->dom->saveHTML();
}
/**
* Save this document to a HTML file or return HTML code as string
*
* @param string $filename If provided, output will be saved to this file, otherwise returned
* @return string|void
*/
public function save($filename = '')
{
$html = $this->__toString();
if (function_exists('mb_convert_encoding') && in_array(strtolower($this->charset), array_map('strtolower', mb_list_encodings()))) {
$html = mb_convert_encoding($html, $this->charset, 'HTML-ENTITIES');
}
if ($filename != '') {
file_put_contents($filename, $html);
return;
} else {
return $html;
}
}
/**
* Get an element in the document by it's id attribute
*
* @param string $id
* @return HtmlPageCrawler
*/
public function getElementById($id)
{
return $this->filterXPath('descendant-or-self::*[@id = \'' . $id . '\']');
}
/**
* Filter nodes by using a CSS selector
*
* @param string $selector CSS selector
* @return HtmlPageCrawler
*/
public function filter($selector)
{
//echo "\n" . CssSelector::toXPath($selector) . "\n";
return $this->crawler->filter($selector);
}
/**
* Filter nodes by XPath expression
*
* @param string $xpath XPath expression
* @return HtmlPageCrawler
*/
public function filterXPath($xpath)
{
return $this->crawler->filterXPath($xpath);
}
/**
* remove newlines from string and minimize whitespace (multiple whitespace characters replaced by one space)
*
* useful for cleaning up text retrieved by HtmlPageCrawler::text() (nodeValue of a DOMNode)
*
* @param string $string
* @return string
*/
public static function trimNewlines($string)
{
return Helpers::trimNewlines($string);
}
public function __clone()
{
$this->dom = $this->dom->cloneNode(true);
$this->crawler = new HtmlPageCrawler($this->dom);
}
/**
* minify the HTML document
*
* @param array $options Options passed to PrettyMin::__construct()
* @return HtmlPage
* @throws \Exception
*/
public function minify(array $options = array())
{
if (!class_exists('Wa72\\HtmlPrettymin\\PrettyMin')) {
throw new \Exception('Function minify needs composer package wa72/html-pretty-min');
}
$pm = new PrettyMin($options);
$pm->load($this->dom)->minify();
return $this;
}
/**
* indent the HTML document
*
* @param array $options Options passed to PrettyMin::__construct()
* @return HtmlPage
* @throws \Exception
*/
public function indent(array $options = array())
{
if (!class_exists('Wa72\\HtmlPrettymin\\PrettyMin')) {
throw new \Exception('Function indent needs composer package wa72/html-pretty-min');
}
$pm = new PrettyMin($options);
$pm->load($this->dom)->indent();
return $this;
}
}