Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
83.87% |
26 / 31 |
|
62.50% |
5 / 8 |
CRAP | |
0.00% |
0 / 1 |
HtmlElementBase | |
83.87% |
26 / 31 |
|
62.50% |
5 / 8 |
15.94 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createTagElement | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 | |||
createText | |
50.00% |
2 / 4 |
|
0.00% |
0 / 1 |
2.50 | |||
createComment | |
50.00% |
2 / 4 |
|
0.00% |
0 / 1 |
2.50 | |||
appendChild | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
addTagElement | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
addComment | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
addText | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
path | n/a |
0 / 0 |
n/a |
0 / 0 |
0 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace PeServer\Core\Html; |
6 | |
7 | use DOMNode; |
8 | use DOMException; |
9 | use PeServer\Core\Html\HtmlDocument; |
10 | use PeServer\Core\Html\HtmlNodeBase; |
11 | use PeServer\Core\Throws\HtmlException; |
12 | use PeServer\Core\Throws\HtmlTagElementException; |
13 | use PeServer\Core\Throws\Throws; |
14 | use Throwable; |
15 | |
16 | abstract class HtmlElementBase extends HtmlNodeBase |
17 | { |
18 | protected function __construct(HtmlDocument $document, DOMNode $current) |
19 | { |
20 | parent::__construct($document, $current); |
21 | } |
22 | |
23 | #region function |
24 | |
25 | public function createTagElement(string $tagName): HtmlTagElement |
26 | { |
27 | try { |
28 | $element = $this->document->raw->createElement($tagName); |
29 | } catch (DOMException $ex) { |
30 | Throws::reThrow(HtmlException::class, $ex); |
31 | } |
32 | |
33 | if ($element === false) { // @phpstan-ignore-line |
34 | throw new HtmlException(); |
35 | } |
36 | |
37 | return new HtmlTagElement($this->document, $element); |
38 | } |
39 | |
40 | public function createText(string $text): HtmlTextElement |
41 | { |
42 | try { |
43 | $node = $this->document->raw->createTextNode($text); |
44 | return new HtmlTextElement($this->document, $node); |
45 | } catch (Throwable $ex) { |
46 | Throws::reThrow(HtmlException::class, $ex); |
47 | } |
48 | } |
49 | |
50 | public function createComment(string $text): HtmlCommentElement |
51 | { |
52 | try { |
53 | $node = $this->document->raw->createComment($text); |
54 | return new HtmlCommentElement($this->document, $node); |
55 | } catch (Throwable $ex) { |
56 | Throws::reThrow(HtmlException::class, $ex); |
57 | } |
58 | } |
59 | |
60 | public function appendChild(HtmlTagElement|HtmlTextElement|HtmlCommentElement|DOMNode $node): void |
61 | { |
62 | if ($node instanceof HtmlTagElement) { |
63 | $node = $node->raw; |
64 | } elseif ($node instanceof HtmlTextElement) { |
65 | $node = $node->raw; |
66 | } elseif ($node instanceof HtmlCommentElement) { |
67 | $node = $node->raw; |
68 | } |
69 | |
70 | $this->currentNode->appendChild($node); |
71 | } |
72 | |
73 | /** |
74 | * HTML要素を作って追加する。 |
75 | * |
76 | * @param string $tagName |
77 | * @return HtmlTagElement |
78 | */ |
79 | public function addTagElement(string $tagName): HtmlTagElement |
80 | { |
81 | $node = $this->createTagElement($tagName); |
82 | |
83 | $this->appendChild($node); |
84 | |
85 | return $node; |
86 | } |
87 | |
88 | public function addComment(string $comment): HtmlCommentElement |
89 | { |
90 | $node = $this->createComment($comment); |
91 | |
92 | $this->appendChild($node); |
93 | |
94 | return $node; |
95 | } |
96 | |
97 | public function addText(string $text): HtmlTextElement |
98 | { |
99 | $node = $this->createText($text); |
100 | |
101 | $this->appendChild($node); |
102 | |
103 | return $node; |
104 | } |
105 | |
106 | abstract public function path(): HtmlXPath; |
107 | |
108 | #endregion |
109 | } |