-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNodePathCalculator.php
55 lines (44 loc) · 1.08 KB
/
NodePathCalculator.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
<?php
namespace Gt\DomTemplate;
use Gt\Dom\Element;
use Gt\Dom\Node;
use Stringable;
class NodePathCalculator implements Stringable {
public function __construct(
private readonly Node|Element $element
) {
}
public function __toString():string {
$path = "";
/** @var Element $context */
$context = $this->element;
do {
$contextPath = strtolower($context->tagName);
$attrPath = "";
if($dataTemplateParent = $context->getAttribute(ListElement::ATTRIBUTE_LIST_PARENT)) {
$attrPath .= "@"
. ListElement::ATTRIBUTE_LIST_PARENT
. "='$dataTemplateParent'";
}
if($id = $context->id) {
if($attrPath) {
$attrPath .= " and ";
}
$attrPath .= "@id='$id'";
}
foreach($context->classList as $class) {
if($attrPath) {
$attrPath .= " and ";
}
$attrPath .= "contains(concat(' ',normalize-space(@class),' '),' $class ')";
}
if($attrPath) {
$contextPath .= "[$attrPath]";
}
$path = "/" . $contextPath . $path;
$context = $context->parentElement;
}
while($context instanceof Element);
return $path;
}
}