Skip to content

Commit e8cfb37

Browse files
author
Greg Bowler
authored
DOMStringMap improvements with PHP 8.3 support (#450)
* tweak: allow select disabled property * test: isolate bug #448 * test: fix numerical issue for #448 * test: test all camel case conversion fixes #448 * ci: php 8.3 support * feature: php 8.3 support
1 parent 7b62fb3 commit e8cfb37

7 files changed

Lines changed: 76 additions & 22 deletions

File tree

.github/workflows/ci.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
runs-on: ubuntu-latest
88
strategy:
99
matrix:
10-
php: [ 8.1, 8.2 ]
10+
php: [ 8.1, 8.2, 8.3 ]
1111

1212
steps:
1313
- uses: actions/checkout@v3
@@ -37,7 +37,7 @@ jobs:
3737
needs: [ composer ]
3838
strategy:
3939
matrix:
40-
php: [ 8.1, 8.2 ]
40+
php: [ 8.1, 8.2, 8.3 ]
4141

4242
outputs:
4343
coverage: ${{ steps.store-coverage.outputs.coverage_text }}
@@ -90,7 +90,7 @@ jobs:
9090
needs: [ composer ]
9191
strategy:
9292
matrix:
93-
php: [ 8.1, 8.2 ]
93+
php: [ 8.1, 8.2, 8.3 ]
9494

9595
steps:
9696
- uses: actions/download-artifact@v3
@@ -112,7 +112,7 @@ jobs:
112112
needs: [ composer ]
113113
strategy:
114114
matrix:
115-
php: [ 8.1, 8.2 ]
115+
php: [ 8.1, 8.2, 8.3 ]
116116

117117
steps:
118118
- uses: actions/download-artifact@v3
@@ -136,7 +136,7 @@ jobs:
136136
needs: [ composer ]
137137
strategy:
138138
matrix:
139-
php: [ 8.1, 8.2 ]
139+
php: [ 8.1, 8.2, 8.3 ]
140140

141141
steps:
142142
- uses: actions/download-artifact@v3

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "phpgt/dom",
3-
"description": "The modern DOM API for PHP projects.",
3+
"description": "Modern DOM API.",
44
"type": "library",
55

66
"require": {

src/DOMStringMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function count():int {
5252

5353
private function correctCamelCase(string $name):string {
5454
preg_match_all(
55-
'/((?:^|[A-Z])[a-z\-]+)/',
55+
'/((?:^|[A-Z])[0-9a-z\-]+)/',
5656
$name,
5757
$matches
5858
);

src/Document.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,10 @@ public function getElementById(string $elementId):?Element {
335335

336336
/**
337337
* @see Node::isEqualNode()
338-
* @param Node|Element $otherNode
339-
* @noinspection PhpParameterNameChangedDuringInheritanceInspection
340338
*/
341-
public function isEqualNode(Node|Element|DOMNode $otherNode):bool {
339+
public function isEqualNode(
340+
null|Node|Element|Document|DocumentType|Attr|ProcessingInstruction|DOMNode $otherNode
341+
):bool {
342342
return $this->documentElement->isEqualNode($otherNode);
343343
}
344344

src/Element.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Countable;
66
use DOMElement;
77
use DOMNamedNodeMap;
8+
use DOMNode;
89
use Gt\Dom\Exception\InvalidAdjacentPositionException;
910
use Gt\Dom\Exception\XPathQueryException;
1011
use Gt\PropFunc\MagicProp;
@@ -329,14 +330,11 @@ public function getAttributeNames():array {
329330
* 'beforeend': Just inside the targetElement, after its last child.
330331
* 'afterend': After the targetElement itself.
331332
*
332-
* @param Node|Element $element The element to be inserted into the tree.
333-
* @return ?Element The element that was inserted, or null, if the
334-
* insertion failed.
335333
* @link https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentElement
336334
*/
337335
public function insertAdjacentElement(
338336
string $position,
339-
Node|Element|DocumentFragment|Text $element
337+
DOMElement|DOMNode|Element|Node|DocumentFragment|Text $element
340338
):?Element {
341339
switch($position) {
342340
case "beforebegin":

src/RegisteredNodeClass.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Gt\Dom;
33

4+
use DOMNameSpaceNode;
45
use DOMNode;
56

67
/**
@@ -22,13 +23,14 @@ trait RegisteredNodeClass {
2223
* Returns a Boolean which indicates whether or not two nodes are of
2324
* the same type and all their defining data points match.
2425
*
25-
* @param Node|Element|Document|DocumentType|Attr|ProcessingInstruction $otherNode
26+
* @param null|Node|Element|Document|DocumentType|Attr|ProcessingInstruction|DOMNode $otherNode
2627
* The Node to compare equality with.
27-
* @return bool
2828
* @link https://developer.mozilla.org/en-US/docs/Web/API/Node/isEqualNode
2929
*/
3030
// phpcs:ignore
31-
public function isEqualNode(Node|Element|Document|DocumentType|Attr|ProcessingInstruction|DOMNode $otherNode):bool {
31+
public function isEqualNode(
32+
null|Node|Element|Document|DocumentType|Attr|ProcessingInstruction|DOMNode $otherNode
33+
):bool {
3234
if($otherNode instanceof Document) {
3335
$otherNode = $otherNode->documentElement;
3436
}
@@ -163,13 +165,11 @@ public function compareDocumentPosition(DOMNode|Node|Element $otherNode):int {
163165
* Returns a Boolean value indicating whether or not a node is a
164166
* descendant of the calling node.
165167
*
166-
* @param Node $otherNode
167-
* @return bool
168168
* @link https://developer.mozilla.org/en-US/docs/Web/API/Node/contains
169169
*/
170170
public function contains(
171171
Node|Element|Text|ProcessingInstruction|DocumentType|DocumentFragment
172-
|Document|Comment|CdataSection|Attr $otherNode
172+
|Document|Comment|CdataSection|Attr|DOMNode|DOMNameSpaceNode|null $otherNode
173173
):bool {
174174
$context = $otherNode;
175175

test/phpunit/DOMStringMapTest.php

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php /** @noinspection PhpUndefinedFieldInspection */
22
namespace Gt\Dom\Test;
33

44
use Gt\Dom\DOMStringMap;
@@ -19,7 +19,7 @@ public function testGetterSetter():void {
1919
self::assertSame($keyValuePairs["example"], $sut->example);
2020
}
2121

22-
public function testGetterCamelCaseConversion():void {
22+
public function testGetter_fromCamelCase():void {
2323
$keyValuePairs = [
2424
"this-is-camel-case" => uniqid("example-"),
2525
];
@@ -34,6 +34,44 @@ public function testGetterCamelCaseConversion():void {
3434
self::assertSame($keyValuePairs["this-is-camel-case"], $sut->thisIsCamelCase);
3535
}
3636

37+
public function testGetter_fromHyphenated():void {
38+
$keyValuePairs = [
39+
"this-is-camel-case" => uniqid("example-"),
40+
];
41+
$getter = function() use (&$keyValuePairs) {
42+
return $keyValuePairs;
43+
};
44+
$setter = function(array $kvp) use (&$keyValuePairs) {
45+
$keyValuePairs = $kvp;
46+
};
47+
$sut = new DOMStringMap($getter, $setter);
48+
49+
self::assertSame($keyValuePairs["this-is-camel-case"], $sut->get("this-is-camel-case"));
50+
self::assertSame($keyValuePairs["this-is-camel-case"], $sut->get("thisIsCamelCase"));
51+
self::assertArrayNotHasKey("thisIsCamelCase", $keyValuePairs);
52+
}
53+
54+
public function testSetter_fromHyphenated():void {
55+
$keyValuePairs = [
56+
"this-is-camel-case" => uniqid("example-"),
57+
];
58+
$getter = function() use (&$keyValuePairs) {
59+
return $keyValuePairs;
60+
};
61+
$setter = function(array $kvp) use (&$keyValuePairs) {
62+
$keyValuePairs = $kvp;
63+
};
64+
$sut = new DOMStringMap($getter, $setter);
65+
66+
$sut->set("this-is-camel-case", "update1");
67+
$sut->set("thisIsCamelCase", "update2");
68+
$sut->set("other-key", "other-update");
69+
70+
self::assertCount(2, $keyValuePairs);
71+
self::assertSame("update2", $sut->thisIsCamelCase);
72+
self::assertSame("other-update", $sut->otherKey);
73+
}
74+
3775
public function testSetterCamelCaseConversion():void {
3876
$keyValuePairs = [];
3977
$getter = function() use (&$keyValuePairs) {
@@ -47,4 +85,22 @@ public function testSetterCamelCaseConversion():void {
4785
self::assertSame("example123", $sut->get("thisIsCamelCase"));
4886
self::assertSame("example123", $sut->get("this-is-camel-case"));
4987
}
88+
89+
public function testSetter_withNumbers():void {
90+
$keyValuePairs = [];
91+
$getter = function() use (&$keyValuePairs) {
92+
return $keyValuePairs;
93+
};
94+
$setter = function(array $kvp) use (&$keyValuePairs) {
95+
$keyValuePairs = $kvp;
96+
};
97+
$sut = new DOMStringMap($getter, $setter);
98+
$sut->example1 = "one";
99+
$sut->example2 = "two";
100+
$sut->example3 = "three";
101+
102+
self::assertSame("one", $sut->example1);
103+
self::assertSame("two", $sut->example2);
104+
self::assertSame("three", $sut->example3);
105+
}
50106
}

0 commit comments

Comments
 (0)