Skip to content

Commit cb79297

Browse files
committed
Added @Property tags for __get() and __set().
Updated htmldoc::offsetSet() to assume that there is always an index. comment::minify() now checks that there is content. custom::__construct() now check that $tagName is not null. Tightened properties in tag. Fixed bug in tag::append() where clones were not spliced correctly. tag::getIndex() now check that the parent is not null. Fixed some typing issues in tag::minify(). Fixed return value issue in tag::attr().
1 parent 34622a7 commit cb79297

File tree

7 files changed

+48
-32
lines changed

7 files changed

+48
-32
lines changed

composer.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ $ vendor/bin/phpunit
9494

9595
## Support
9696

97-
HTMLdoc supports PHP version 7.3+.
97+
HTMLdoc supports PHP version 7.4+.
9898

9999
## Contributing
100100

src/htmldoc.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
namespace hexydec\html;
44
use \hexydec\tokens\tokenise;
55

6+
/**
7+
* @property-read array $config
8+
* @property-read int $length
9+
*/
610
class htmldoc extends config implements \ArrayAccess, \Iterator {
711

812
/**
@@ -88,11 +92,7 @@ public function toArray() : array {
8892
* @param mixed $value The value of the array key in the children array to be updated
8993
*/
9094
public function offsetSet($i, $value) : void {
91-
if (\is_null($i)) {
92-
$this->children[] = $value;
93-
} else {
94-
$this->children[$i] = $value;
95-
}
95+
$this->children[$i] = $value;
9696
}
9797

9898
/**
@@ -767,7 +767,7 @@ public function remove(string $selector = null) : htmldoc {
767767
* Compile the document as an HTML string and save it to the specified location
768768
*
769769
* @param array $options An array indicating output options, this is merged with htmldoc::$output
770-
* @return string The compiled HTML
770+
* @return string|bool The compiled HTML
771771
*/
772772
public function save(string $file = null, array $options = []) {
773773

src/tokens/comment.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ public function parse(tokenise $tokens) : void {
3939
* @return void
4040
*/
4141
public function minify(array $minify) : void {
42-
if (!empty($minify['comments']['remove']) && (empty($minify['comments']['ie']) || (\mb_strpos($this->content, '[if ') !== 0 && $this->content !== '<![endif]'))) {
43-
$this->content = null;
42+
if (!empty($minify['comments']['remove']) && $this->content) {
43+
if (empty($minify['comments']['ie']) || (\mb_strpos($this->content, '[if ') !== 0 && $this->content !== '<![endif]')) {
44+
$this->content = null;
45+
}
4446
}
4547
}
4648

src/tokens/custom.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class custom implements token {
2727
* @param string $tag The name of the custom tag, note this cannot be null
2828
*/
2929
public function __construct(htmldoc $root, string $tagName = null) {
30-
$this->tagName = $tagName = \mb_strtolower($tagName);
30+
$this->tagName = $tagName = \mb_strtolower($tagName ?? '');
3131
$this->config = $root->config['custom'][$tagName];
3232
}
3333

src/tokens/tag.php

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
namespace hexydec\html;
44
use \hexydec\tokens\tokenise;
55

6+
/**
7+
* @property-write \hexydec\html\tag $parent
8+
* @property-read htmldoc $root
9+
* @property-read array $config
10+
* @property-read tag|null $parent
11+
* @property-read array $parenttags
12+
* @property-read string|null $tagName
13+
* @property-read array $attributes
14+
* @property-read string|null $singleton
15+
* @property-read array $children
16+
*/
617
class tag implements token {
718

819
/**
@@ -16,14 +27,14 @@ class tag implements token {
1627
protected array $config = [];
1728

1829
/**
19-
* @var tag The parent tag object
30+
* @var tag|null The parent tag object
2031
*/
2132
protected ?tag $parent = null;
2233

2334
/**
2435
* @var array Cache for the list of parent tags
2536
*/
26-
protected ?array $parenttags = null;
37+
protected array $parenttags = [];
2738

2839
/**
2940
* @var string The type of tag
@@ -36,7 +47,7 @@ class tag implements token {
3647
protected array $attributes = [];
3748

3849
/**
39-
* @var string If the tag is a singleton, this defines the closing string
50+
* @var string|null If the tag is a singleton, this defines the closing string
4051
*/
4152
protected ?string $singleton = null;
4253

@@ -311,7 +322,7 @@ public function append(array $nodes, int $index = null) : void {
311322

312323
// insert the nodes
313324
if ($index !== null) {
314-
$this->chidren = \array_splice($this->children, $index, 0, $clones);
325+
\array_splice($this->children, $index, 0, $clones);
315326
}
316327
}
317328

@@ -335,9 +346,11 @@ public function prepend(array $nodes) : void {
335346
* @return int|null The index of the current element with the parent, or null if there is no parent
336347
*/
337348
protected function getIndex() : ?int {
338-
foreach ($this->parent->children() AS $key => $item) {
339-
if ($item === $this) {
340-
return $key;
349+
if ($this->parent) {
350+
foreach ($this->parent->children() AS $key => $item) {
351+
if ($item === $this) {
352+
return $key;
353+
}
341354
}
342355
}
343356
return null;
@@ -394,7 +407,7 @@ public function remove(tag $node) : void {
394407
public function minify(array $minify) : void {
395408
$config = $this->config;
396409
$attr = $config['attributes'];
397-
if ($minify['lowercase']) {
410+
if ($minify['lowercase'] && $this->tagName) {
398411
$this->tagName = \mb_strtolower($this->tagName);
399412
}
400413
$folder = null;
@@ -404,6 +417,7 @@ public function minify(array $minify) : void {
404417
// minify attributes
405418
$tag = $this->tagName;
406419
$attributes = $this->attributes;
420+
$host = null;
407421
foreach ($attributes AS $key => $value) {
408422

409423
// lowercase attribute key
@@ -433,7 +447,7 @@ public function minify(array $minify) : void {
433447

434448
// remove host for own domain
435449
if ($minify['urls']['host'] && isset($_SERVER['HTTP_HOST'])) {
436-
$host ??= ['//'.$_SERVER['HTTP_HOST'], $scheme.$_SERVER['HTTP_HOST']];
450+
$host = $host ?? ['//'.$_SERVER['HTTP_HOST'], $scheme.$_SERVER['HTTP_HOST']];
437451
foreach ($host AS $item) {
438452

439453
// check if link goes to root
@@ -570,7 +584,7 @@ public function minify(array $minify) : void {
570584
}
571585

572586
// if last tag, remove closing tag
573-
if (!$children || $next) {
587+
if (empty($children) || $next) {
574588
$this->close = false;
575589
}
576590
}
@@ -778,7 +792,7 @@ public function find(array $selector, bool $searchChildren = true) : array {
778792
*
779793
* @param string $key The key of the attribute whos value you wish to retrieve or update
780794
* @param string $value The value of the attribute to update
781-
* @return string The value of the attrbute or NULL if the attribute does not exist
795+
* @return string|null The value of the attrbute or NULL if the attribute does not exist
782796
*/
783797
public function attr(string $key, ?string $value = null) : ?string {
784798

@@ -787,8 +801,8 @@ public function attr(string $key, ?string $value = null) : ?string {
787801
$this->attributes[$key] = $value;
788802

789803
// get the value
790-
} elseif (\array_key_exists($key, $this->attributes)) {
791-
return $this->attributes[$key] === null ? true : $this->attributes[$key];
804+
} else {
805+
return $this->attributes[$key] ?? null;
792806
}
793807
return null;
794808
}

src/tokens/text.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ class text implements token {
1111
protected htmldoc $root;
1212

1313
/**
14-
* @var tag The parent tag object
14+
* @var tag|null The parent tag object
1515
*/
16-
protected tag $parent;
16+
protected ?tag $parent = null;
1717

1818
/**
1919
* @var string The text content of this object
@@ -26,7 +26,7 @@ class text implements token {
2626
* @param htmldoc $root The parent HTMLdoc object
2727
* @param tag $parent The parent tag object
2828
*/
29-
public function __construct(htmldoc $root, tag $parent = null) {
29+
public function __construct(htmldoc $root, ?tag $parent = null) {
3030
$this->root = $root;
3131
$this->parent = $parent;
3232
}

0 commit comments

Comments
 (0)