|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BeyondCode\TagHelper\Html; |
|
4
|
|
|
|
|
5
|
|
|
use simplehtmldom_1_5\simple_html_dom_node; |
|
6
|
|
|
|
|
7
|
|
|
class HtmlElement |
|
8
|
|
|
{ |
|
9
|
|
|
/** @var simple_html_dom_node */ |
|
10
|
|
|
protected $domNode; |
|
11
|
|
|
|
|
12
|
|
|
public static function create(simple_html_dom_node $node) |
|
13
|
|
|
{ |
|
14
|
|
|
return new static($node); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(simple_html_dom_node $domNode) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->domNode = $domNode; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function hasAttribute(string $attribute): bool |
|
23
|
|
|
{ |
|
24
|
|
|
return $this->domNode->hasAttribute($attribute); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function getAttribute(string $attribute, $default = null) |
|
28
|
|
|
{ |
|
29
|
|
|
$attribute = $this->domNode->getAttribute($attribute); |
|
30
|
|
|
|
|
31
|
|
|
return $attribute === false ? $default : $attribute; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function getAttributeForBlade(string $attribute, $default = null) |
|
35
|
|
|
{ |
|
36
|
|
|
if ($this->domNode->hasAttribute(':'.$attribute)) { |
|
37
|
|
|
return $this->getAttribute(':'.$attribute, $default); |
|
38
|
|
|
} |
|
39
|
|
|
$attribute = $this->domNode->getAttribute($attribute); |
|
40
|
|
|
|
|
41
|
|
|
return $attribute === false ? $default : "'".$attribute."'"; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function setAttribute(string $attribute, string $value) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->domNode->setAttribute($attribute, $value); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function removeAttribute(string $attribute) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->domNode->setAttribute($attribute, null); |
|
52
|
|
|
$this->domNode->setAttribute(':'.$attribute, null); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function getInnerText(): string |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->domNode->innertext(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function setInnerText(string $text) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->domNode->innertext = $text; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function getPlainText(): string |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->domNode->text(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function prependInnerText(string $prepend) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->setInnerText($prepend.$this->getInnerText()); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function appendInnerText(string $append) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->setInnerText($this->getInnerText().$append); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getOuterText(): string |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->domNode->outertext(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function setOuterText(string $text) |
|
86
|
|
|
{ |
|
87
|
|
|
$this->domNode->outertext = $text; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function setTag(string $tag) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->domNode->tag = $tag; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function __call($method, $args) |
|
96
|
|
|
{ |
|
97
|
|
|
return call_user_func_array([$this->domNode, $method], $args); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function __get($key) |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->domNode->$key; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function __set($key, $val) |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->domNode->$key = $val; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|