Skip to content

Commit 86c3b46

Browse files
committed
Explicitly namespaced all native functions into the root namespace, as it is clearer and performs better.
Bumped version to 1.1.1.
1 parent 874787b commit 86c3b46

File tree

10 files changed

+151
-151
lines changed

10 files changed

+151
-151
lines changed

composer.lock

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "HTMLdoc",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "A token based HTML Document parser and minifier written in PHP",
55
"main": "index.js",
66
"directories": {

src/config.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class config {
1515
* @param array $config An array of configuration parameters that is recursively merged with the default config
1616
*/
1717
public function __construct(array $config = []) {
18-
$this->config = array_replace_recursive($this->config, [
18+
$this->config = \array_replace_recursive($this->config, [
1919
'elements' => [
2020
'inline' => [
2121
'b', 'u', 'big', 'i', 'small', 'ttspan', 'em', 'a', 'strong', 'sub', 'sup', 'abbr', 'acronym', 'cite', 'code', 'dfn', 'em', 'kbd', 'strong', 'samp', 'var', 'span'
@@ -66,7 +66,7 @@ public function __construct(array $config = []) {
6666
'style' => [
6767
'class' => '\\hexydec\\html\\style',
6868
'config' => [
69-
'minifier' => class_exists('\\hexydec\\css\\cssdoc') ? function (string $css, array $minify) {
69+
'minifier' => \class_exists('\\hexydec\\css\\cssdoc') ? function (string $css, array $minify) {
7070
$obj = new \hexydec\css\cssdoc();
7171
if ($obj->load($css)) {
7272
$obj->minify($minify);
@@ -81,7 +81,7 @@ public function __construct(array $config = []) {
8181
'script' => [
8282
'class' => '\\hexydec\\html\\script',
8383
'config' => [
84-
'minifier' => class_exists('\\hexydec\\jslite\\jslite') ? function (string $css, array $minify) {
84+
'minifier' => \class_exists('\\hexydec\\jslite\\jslite') ? function (string $css, array $minify) {
8585
$obj = new \hexydec\jslite\jslite();
8686
if ($obj->load($css)) {
8787
$obj->minify($minify);

src/htmldoc.php

+47-47
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function __get(string $var) {
6666
if ($var == 'config') {
6767
return $this->config;
6868
} elseif ($var == 'length') {
69-
return count($this->children);
69+
return \count($this->children);
7070
}
7171
return null;
7272
}
@@ -87,7 +87,7 @@ public function toArray() : array {
8787
* @param mixed $value The value of the array key in the children array to be updated
8888
*/
8989
public function offsetSet($i, $value) : void {
90-
if (is_null($i)) $this->children[] = $value;
90+
if (\is_null($i)) $this->children[] = $value;
9191
else $this->children[$i] = $value;
9292
}
9393

@@ -176,23 +176,23 @@ public function valid() : bool {
176176
public function open(string $url, $context = null, string &$error = null) {
177177

178178
// open a handle to the stream
179-
if (($handle = @fopen($url, 'rb', false, $context)) === false) {
179+
if (($handle = @\fopen($url, 'rb', false, $context)) === false) {
180180
$error = 'Could not open file "'.$url.'"';
181181

182182
// retrieve the stream contents
183-
} elseif (($html = stream_get_contents($handle)) === false) {
183+
} elseif (($html = \stream_get_contents($handle)) === false) {
184184
$error = 'Could not read file "'.$url.'"';
185185

186186
// success
187187
} else {
188188

189189
// find charset in headers
190190
$charset = null;
191-
$meta = stream_get_meta_data($handle);
191+
$meta = \stream_get_meta_data($handle);
192192
if (!empty($meta['wrapper_data'])) {
193193
foreach ($meta['wrapper_data'] AS $item) {
194-
if (mb_stripos($item, 'Content-Type:') === 0 && ($charset = mb_stristr($item, 'charset=')) !== false) {
195-
$charset = mb_substr($charset, 8);
194+
if (\mb_stripos($item, 'Content-Type:') === 0 && ($charset = \mb_stristr($item, 'charset=')) !== false) {
195+
$charset = \mb_substr($charset, 8);
196196
break;
197197
}
198198
}
@@ -218,7 +218,7 @@ public function load(string $html, string $charset = null, &$error = null) : boo
218218

219219
// detect the charset
220220
if ($charset || ($charset = $this->getCharsetFromHtml($html)) !== null) {
221-
$html = mb_convert_encoding($html, mb_internal_encoding(), $charset);
221+
$html = \mb_convert_encoding($html, \mb_internal_encoding(), $charset);
222222
}
223223

224224
// reset the document
@@ -246,20 +246,20 @@ public function load(string $html, string $charset = null, &$error = null) : boo
246246
* @return string The defined or detected charset or null if the charset is not defined
247247
*/
248248
protected function getCharsetFromHtml(string $html) : ?string {
249-
if (preg_match('/<meta[^>]+charset[^>]+>/i', $html, $match)) {
249+
if (\preg_match('/<meta[^>]+charset[^>]+>/i', $html, $match)) {
250250
$obj = new htmldoc($this->config);
251-
if ($obj->load($match[0], mb_internal_encoding())) {
251+
if ($obj->load($match[0], \mb_internal_encoding())) {
252252

253253
// <meta charset="xxx" />
254254
if (($value = $obj->attr('charset')) !== null) {
255255
return $value;
256256

257257
// <meta http-equiv="Content-Type" content="text/html; charset=xxx" />
258-
} elseif (($value = $obj->eq(0)->attr('content')) !== null && ($charset = mb_stristr($value, 'charset=')) !== false) {
259-
return mb_substr($charset, 8);
258+
} elseif (($value = $obj->eq(0)->attr('content')) !== null && ($charset = \mb_stristr($value, 'charset=')) !== false) {
259+
return \mb_substr($charset, 8);
260260
}
261261
}
262-
} elseif (($charset = mb_detect_encoding($html)) !== false) {
262+
} elseif (($charset = \mb_detect_encoding($html)) !== false) {
263263
return $charset;
264264
}
265265
return null;
@@ -284,7 +284,7 @@ protected function parse(tokenise $tokens) : bool {
284284
* @return array An array of selector components
285285
*/
286286
protected function parseSelector(string $selector) {
287-
$selector = trim($selector);
287+
$selector = \trim($selector);
288288
$tokens = new tokenise(self::$selectors, $selector);
289289
if (($token = $tokens->next()) !== null) {
290290
$selectors = $parts = [];
@@ -293,15 +293,15 @@ protected function parseSelector(string $selector) {
293293
switch ($token['type']) {
294294
case 'id':
295295
$parts[] = [
296-
'id' => mb_substr($token['value'], 1),
296+
'id' => \mb_substr($token['value'], 1),
297297
'join' => $join
298298
];
299299
$join = null;
300300
break;
301301

302302
case 'class':
303303
$parts[] = [
304-
'class' => mb_substr($token['value'], 1),
304+
'class' => \mb_substr($token['value'], 1),
305305
'join' => $join
306306
];
307307
$join = null;
@@ -320,9 +320,9 @@ protected function parseSelector(string $selector) {
320320
while (($token = $tokens->next()) !== false) {
321321
if ($token['type'] === 'squareclose') {
322322
break;
323-
} elseif (in_array($token['type'], ['string', 'quotes'])) {
323+
} elseif (\in_array($token['type'], ['string', 'quotes'])) {
324324
if ($token['type'] == 'quotes') {
325-
$token['value'] = stripslashes(mb_substr($token['value'], 1, -1));
325+
$token['value'] = \stripslashes(\mb_substr($token['value'], 1, -1));
326326
}
327327
$item[isset($item['attribute']) ? 'value' : 'attribute'] = $token['value'];
328328
} elseif ($token['type'] === 'comparison') {
@@ -335,14 +335,14 @@ protected function parseSelector(string $selector) {
335335

336336
case 'pseudo':
337337
$parts[] = [
338-
'pseudo' => mb_substr($token['value'], 1),
338+
'pseudo' => \mb_substr($token['value'], 1),
339339
'join' => $join
340340
];
341341
$join = null;
342342
break;
343343

344344
case 'join':
345-
$join = trim($token['value']);
345+
$join = \trim($token['value']);
346346
break;
347347

348348
case 'whitespace':
@@ -400,7 +400,7 @@ public function get(int $index = null) {
400400
// build children that are tags
401401
$children = [];
402402
foreach ($this->children AS $item) {
403-
if (get_class($item) === 'hexydec\\html\\tag') {
403+
if (\get_class($item) === 'hexydec\\html\\tag') {
404404
$children[] = $item;
405405
}
406406
}
@@ -412,7 +412,7 @@ public function get(int $index = null) {
412412

413413
// check if index is minus
414414
if ($index < 0) {
415-
$index = count($children) + $index;
415+
$index = \count($children) + $index;
416416
}
417417

418418
// return index if set
@@ -432,12 +432,12 @@ public function find(string $selector) : htmldoc {
432432
$found = [];
433433

434434
// parse selector and find tags
435-
if (is_array($selector) || ($selector = $this->parseSelector($selector)) !== false) {
435+
if (\is_array($selector) || ($selector = $this->parseSelector($selector)) !== false) {
436436
foreach ($this->children AS $item) {
437-
if (get_class($item) === 'hexydec\\html\\tag') {
437+
if (\get_class($item) === 'hexydec\\html\\tag') {
438438
foreach ($selector AS $value) {
439439
if (($items = $item->find($value)) !== false) {
440-
$found = array_merge($found, $items);
440+
$found = \array_merge($found, $items);
441441
}
442442
}
443443
}
@@ -479,7 +479,7 @@ public function last() : htmldoc {
479479
public function eq(int $index) : htmldoc {
480480
$doc = new htmldoc($this->config);
481481
if ($index < 0) {
482-
$index = count($this->children) + $index;
482+
$index = \count($this->children) + $index;
483483
}
484484
if (isset($this->children[$index])) {
485485
$doc->collection([$this->children[$index]]);
@@ -504,7 +504,7 @@ public function children() : htmldoc {
504504
*/
505505
public function attr(string $key) : ?string {
506506
foreach ($this->children AS $item) {
507-
if (get_class($item) === 'hexydec\\html\\tag') {
507+
if (\get_class($item) === 'hexydec\\html\\tag') {
508508
return $item->attr($key);
509509
}
510510
}
@@ -521,12 +521,12 @@ public function text() : string {
521521
foreach ($this->children AS $item) {
522522

523523
// only get text from these objects
524-
if (in_array(get_class($item), ['hexydec\\html\\tag', 'hexydec\\html\\text'], true)) {
524+
if (\in_array(\get_class($item), ['hexydec\\html\\tag', 'hexydec\\html\\text'], true)) {
525525
$value = $item->text();
526-
$text = array_merge($text, is_array($value) ? $value : [$value]);
526+
$text = \array_merge($text, \is_array($value) ? $value : [$value]);
527527
}
528528
}
529-
return implode(' ', $text);
529+
return \implode(' ', $text);
530530
}
531531

532532
/**
@@ -548,7 +548,7 @@ protected function collection(array $nodes) : void {
548548
public function minify(array $minify = []) : void {
549549

550550
// merge config
551-
$minify = array_replace_recursive($this->config['minify'], $minify);
551+
$minify = \array_replace_recursive($this->config['minify'], $minify);
552552

553553
// set minify output parameters
554554
if ($minify['quotes']) {
@@ -568,24 +568,24 @@ public function minify(array $minify = []) : void {
568568
}
569569

570570
// sort classes by occurence, then by string
571-
if (is_array($minify['attributes'])) {
571+
if (\is_array($minify['attributes'])) {
572572

573573
// sort attribute values by most frequent
574574
if ($minify['attributes']['sort'] && !empty($this->cache['attr'])) {
575-
arsort($this->cache['attr']);
576-
arsort($this->cache['attrvalues']);
575+
\arsort($this->cache['attr']);
576+
\arsort($this->cache['attrvalues']);
577577
$attr = [];
578578
foreach ($this->cache['attrvalues'] AS $item => $occurences) {
579579
if ($occurences > 5) {
580-
$item = mb_strstr($item, '=', true);
581-
if (!in_array($item, $attr)) {
580+
$item = \mb_strstr($item, '=', true);
581+
if (!\in_array($item, $attr)) {
582582
$attr[] = $item;
583583
}
584584
} else {
585585
break;
586586
}
587587
}
588-
$minify['attributes']['sort'] = array_unique(array_merge($attr, array_keys($this->cache['attr'])));
588+
$minify['attributes']['sort'] = \array_unique(\array_merge($attr, \array_keys($this->cache['attr'])));
589589
}
590590
}
591591

@@ -602,7 +602,7 @@ public function minify(array $minify = []) : void {
602602
* @return string The compiled HTML
603603
*/
604604
public function html(array $options = []) : string {
605-
$options = $options ? array_merge($this->config['output'], $options) : $this->config['output'];
605+
$options = $options ? \array_merge($this->config['output'], $options) : $this->config['output'];
606606

607607
// presets
608608
if (!empty($options['xml'])) {
@@ -639,16 +639,16 @@ public function save(string $file = null, array $options = []) {
639639
}
640640

641641
// convert to target charset
642-
$html = mb_convert_encoding($html, $options['charset']);
642+
$html = \mb_convert_encoding($html, $options['charset']);
643643
}
644644

645645
// send back as string
646646
if (!$file) {
647647
return $html;
648648

649649
// save file
650-
} elseif (file_put_contents($file, $html) === false) {
651-
trigger_error('File could not be written', E_USER_WARNING);
650+
} elseif (\file_put_contents($file, $html) === false) {
651+
\trigger_error('File could not be written', E_USER_WARNING);
652652
} else {
653653
return true;
654654
}
@@ -667,21 +667,21 @@ protected function htmlentities(string $html, string $charset) : string {
667667
// generate single-byte characters
668668
$str = '';
669669
for ($i = 1; $i < 256; $i++) {
670-
$str .= chr($i);
670+
$str .= \chr($i);
671671
}
672-
$str = mb_convert_encoding($str, mb_internal_encoding(), $charset);
672+
$str = \mb_convert_encoding($str, \mb_internal_encoding(), $charset);
673673

674674
// build html entities conversion map
675675
$replace = [];
676-
foreach (preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY) AS $chr) {
677-
$ent = mb_convert_encoding($chr, 'HTML-ENTITIES');
676+
foreach (\preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY) AS $chr) {
677+
$ent = \mb_convert_encoding($chr, 'HTML-ENTITIES');
678678
if ($ent != $chr) {
679679
$replace[$chr] = $ent;
680680
}
681681
}
682682

683683
// convert entities
684-
$html = mb_convert_encoding($html, 'HTML-ENTITIES');
685-
return str_replace(array_values($replace), array_keys($replace), $html);
684+
$html = \mb_convert_encoding($html, 'HTML-ENTITIES');
685+
return \str_replace(\array_values($replace), \array_keys($replace), $html);
686686
}
687687
}

0 commit comments

Comments
 (0)