forked from jbowens/jBBCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ElementNode.php
232 lines (202 loc) · 6.09 KB
/
ElementNode.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
namespace JBBCode;
require_once('Node.php');
/**
* @author Jackson Owens
*
* An element within the tree. Consists of a tag name which defines the type of the
* element and any number of Node children. It also contains a CodeDefinition matching
* the tag name of the element.
*/
class ElementNode extends Node {
/* The tagname of this element, for i.e. "b" in [b]bold[/b] */
protected $tagName;
/* The attribute, if any, of this element node */
protected $attribute;
/* The child nodes contained within this element */
protected $children;
/* The code definition that defines this element's behavior */
protected $codeDefinition;
/* How deeply this node is nested */
protected $nestDepth;
/**
* Constructs the elemennt node
*/
public function __construct() {
$this->children = array();
$this->nestDepth = 0;
}
public function accept(NodeVisitor $nodeVisitor) {
$nodeVisitor->visitElementNode($this);
}
/**
* Gets the CodeDefinition that defines this element.
*
* @return this element's code definition
*/
public function getCodeDefinition() {
return $this->codeDefinition;
}
/**
* Sets the CodeDefinition that defines this element.
*
* @param codeDef the code definition that defines this element node
*/
public function setCodeDefinition( CodeDefinition $codeDef ) {
$this->codeDefinition = $codeDef;
}
/**
* Returns the tag name of this element.
*
* @return the element's tag name
*/
public function getTagName() {
return $this->tagName;
}
/**
* Returns the attribute (used as the option in bbcode definitions) of this element.
*
* @return the attribute of this element
*/
public function getAttribute() {
return $this->attribute;
}
/**
* Returns all the children of this element.
*
* @return an array of this node's child nodes
*/
public function getChildren() {
return $this->children;
}
/**
* (non-PHPdoc)
* @see JBBCode.Node::getAsText()
*
* Returns the element as text (not including any bbcode markup)
*
* @return the plain text representation of this node
*/
public function getAsText() {
$s = "";
foreach($this->getChildren() as $child)
$s .= $child->getAsText();
return $s;
}
/**
* (non-PHPdoc)
* @see JBBCode.Node::getAsBBCode()
*
* Returns the element as bbcode (with all unclosed tags closed)
*
* @return the bbcode representation of this element
*/
public function getAsBBCode() {
$str = "[".$this->tagName;
if( $this->attribute != null )
$str .= "=" . $this->attribute;
$str .= "]";
foreach( $this->getChildren() as $child)
$str .= $child->getAsBBCode();
$str .= "[/".$this->tagName."]";
return $str;
}
/**
* (non-PHPdoc)
* @see JBBCode.Node::getAsHTML()
*
* Returns the element as html with all replacements made
*
* @return the html representation of this node
*/
public function getAsHTML()
{
if( $this->codeDefinition )
return $this->codeDefinition->asHtml( $this );
else
return "";
}
/**
* Adds a child to this node's content. A child may be a TextNode, or another ElementNode... or anything else
* that may extend the abstract Node class.
*
* @param child the node to add as a child
*/
public function addChild(Node $child) {
array_push($this->children, $child);
$child->setParent( $this );
}
/**
* Removes a child from this node's contnet.
*
* @param child the child node to remove
*/
public function removeChild(Node $child) {
foreach( $this->children as $key => $value) {
if( $value == $child)
unset($this->children[$key]);
}
}
/**
* Sets the tag name of this element node.
*
* @param tagName the element's new tag name
*/
public function setTagName( $tagName ) {
$this->tagName = $tagName;
}
/**
* Sets the attribute (option) of this element node.
*
* @param attribute the attribute of this element node
*/
public function setAttribute( $attribute ) {
$this->attribute = $attribute;
}
/**
* Traverses the parse tree upwards, going from parent to parent, until it finds a parent who has the given tag name. Returns the
* parent with the matching tag name if it exists, otherwise returns null.
*
* @param str the tag name to search for
*
* @return the closest parent with the given tag name
*/
public function closestParentOfType( $str ) {
$str = strtolower($str);
$currentEl = $this;
while( strtolower($currentEl->getTagName()) != $str && $currentEl->hasParent() )
$currentEl = $currentEl->getParent();
if( strtolower($currentEl->getTagName()) != $str )
return null;
else
return $currentEl;
}
/**
* Sets the nest depth used for nest limits
*
* @param nd the nest depth of this node
*/
public function setNestDepth( $nd ) {
$this->nestDepth = $nd;
}
/**
* Gets the nest depth of this element
*
* @return this element's nest depth
*/
public function getNestDepth() {
return $this->nestDepth;
}
/**
* (non-PHPdoc)
* @see JBBCode.Node::beyondDefinitionLimit()
*
* Returns true if this element is beyond the nest limit defined in the CodeDefinition.
*
* @return true if this element is beyond its nest limit, false otherwise
*/
public function beyondDefinitionLimit() {
$codeLimit = $this->codeDefinition->getNestLimit();
return ( $codeLimit > 0 && $this->nestDepth > $codeLimit );
}
}