|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Teein\Html\Ast; |
|
5
|
|
|
|
|
6
|
|
|
use Teein\Html\Beautifier\Beautifier; |
|
7
|
|
|
use Teein\Html\VirtualDom\Text as TextInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* A Text represents a text-node. |
|
11
|
|
|
*/ |
|
12
|
|
|
final class Text implements TextInterface |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
protected $text; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Construct a new Text with text-content set to $text |
|
19
|
|
|
* |
|
20
|
|
|
* @param string $text The text-content of the new Text |
|
21
|
|
|
*/ |
|
22
|
12 |
|
public function __construct(string $text) |
|
23
|
|
|
{ |
|
24
|
12 |
|
$this->text = $text; |
|
25
|
12 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Get a beautified Text for debugging-purpose. This returns |
|
29
|
|
|
* the same Text because it is unclear how a beautified |
|
30
|
|
|
* Text should look like. |
|
31
|
|
|
* |
|
32
|
|
|
* This function should not be called directly on a Text. It is |
|
33
|
|
|
* invoked automatically if the document-ancestor is about to be |
|
34
|
|
|
* beautified. However, it is a debugging-utility, so don't take the former |
|
35
|
|
|
* rule too serious, but make sure to remove the call to "beautify" for |
|
36
|
|
|
* production-code. |
|
37
|
|
|
* |
|
38
|
|
|
* @param int $level The current level of indentation |
|
39
|
|
|
*/ |
|
40
|
1 |
|
public function beautify(int $level = 0) : Beautifier |
|
41
|
|
|
{ |
|
42
|
1 |
|
return $this; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get the html-representation of the this Text. |
|
47
|
|
|
*/ |
|
48
|
9 |
|
public function toHtml() : string |
|
49
|
|
|
{ |
|
50
|
9 |
|
return htmlspecialchars($this->text, ENT_HTML5 | ENT_NOQUOTES); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Get the raw-text-representation of the this Text. This method is |
|
55
|
|
|
* only used for <script>- and <style>-elements. These elements use |
|
56
|
|
|
* a different encoding-algorithm. For exmaple, an ampersand "&" is |
|
57
|
|
|
* not replaced by the html-escaping-sequence "&", but left |
|
58
|
|
|
* unmodifier. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $localName A localName, that is escaped inside the |
|
61
|
|
|
* text-content of this Text. |
|
62
|
|
|
*/ |
|
63
|
6 |
|
public function toRawText(string $localName) : string |
|
64
|
|
|
{ |
|
65
|
|
|
$tests = [ |
|
66
|
6 |
|
'/<(' . $localName . ')/ium', // test for forbidden opening tags |
|
67
|
6 |
|
'/<\/(' . $localName . ')/ium', // test for forbidden closing tags |
|
68
|
6 |
|
'/<!--/ium' // test for forbidden opening comments |
|
69
|
|
|
]; |
|
70
|
|
|
$replacements = [ |
|
71
|
6 |
|
'<\\\$1', |
|
72
|
|
|
'<\\/$1', |
|
73
|
|
|
'<\\!--' |
|
74
|
|
|
]; |
|
75
|
6 |
|
return preg_replace($tests, $replacements, $this->text); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Get the text-content of the this Text. |
|
80
|
|
|
*/ |
|
81
|
4 |
|
public function getText() : string |
|
82
|
|
|
{ |
|
83
|
4 |
|
return $this->text; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get a new Text that is like this one but with its text-content set to |
|
88
|
|
|
* $text. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $text The text-content of the new Text |
|
91
|
|
|
*/ |
|
92
|
2 |
|
public function setText(string $text) : TextInterface |
|
93
|
|
|
{ |
|
94
|
2 |
|
return new Text($text); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|