forked from Teein/Html
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRawTextElement.php
More file actions
109 lines (98 loc) · 3.64 KB
/
RawTextElement.php
File metadata and controls
109 lines (98 loc) · 3.64 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
declare(strict_types=1);
namespace Teein\Html\Ast;
use Teein\Html\Ast\Element;
use Teein\Html\Beautifier\Beautifier;
use Teein\Html\VirtualDom\Text as TextInterface;
use Teein\Html\VirtualDom\Element as ElementInterface;
/**
* A RawTextElement represents a html-element which contents are restricted
* to a certain kind of foreign-language. Currently, this is only used to
* represent <script>-elements, that may comprise JavaScript-content and
* <style>-element, that may comprise CSS-content. These elements use a
* different encoding-algorithm when serialized.
*/
final class RawTextElement extends Element implements ElementInterface
{
protected $text;
/**
* Construct a new RawTextElement with localName set to $localName,
* attributes set to $attributes and text-content set to $text
*
* @param string $localName The localName of the new RawTextElement
* @param array $attributes The attributes of the new RawTextElement
* @param TextInterface $text The text-content of the new RawTextElement
*/
public function __construct(string $localName, array $attributes, TextInterface $text)
{
$this->localName = $localName;
$this->attributes = $attributes;
$this->text = $text;
}
/**
* Get a beautified RawTextElement for debugging-purpose. This inserts a
* new line after the opening tag "<$localName>" and right before the
* closing tag "</$localName>. Additionally, "beautify" is invoked
* recursively on all childNodes $childNodes.
* The original RawTextElement is left unmodified.
*
* This function should not be called directly on a RawTextElement. It is
* invoked automatically if the document-ancestor is about to be
* beautified. However, it is a debugging-utility, so don't take the former
* rule too serious, but make sure to remove the call to "beautify" for
* production-code.
*
* @param int $level The current level of indentation
*/
public function beautify(int $level = 0) : Beautifier
{
return $this;
}
/**
* Get the html-representation of the this RawTextElement.
*/
public function toHtml() : string
{
$htmlAttributes = $this->attributesToHtml();
$htmlLocalName = $this->localName;
$htmlText = $this->text->toRawText($htmlLocalName);
return "<{$htmlLocalName}{$htmlAttributes}>$htmlText</$htmlLocalName>";
}
/**
* Get the text-content of the this RawTextElement.
*/
public function getText() : TextInterface
{
return $this->text;
}
/**
* Get a new RawTextElement that is like this one but with its text-content
* set to $text
*
* @param TextInterface $text The text-content of the new RawTextElement
*/
public function setText(TextInterface $text) : ElementInterface
{
return new RawTextElement($this->localName, $this->attributes, $text);
}
/**
* Get a new RawTextElement that is like this one but with its localName
* set to $localName
*
* @param string $localName The localName of the new RawTextElement
*/
public function setLocalName(string $localName) : ElementInterface
{
return new RawTextElement($localName, $this->attributes, $this->text);
}
/**
* Get a new RawTextElement that is like this one but with its attributes
* set to $attributes
*
* @param array $attributes The attributes of the new RawTextElement
*/
public function setAttributes(array $attributes) : ElementInterface
{
return new RawTextElement($this->localName, $attributes, $this->text);
}
}