@@ -30,336 +30,336 @@ |
||
30 | 30 | */ |
31 | 31 | class HtmlParser { |
32 | 32 | |
33 | - /** |
|
34 | - * Field iNodeType. |
|
35 | - * May be one of the NODE_TYPE_* constants above. |
|
36 | - */ |
|
37 | - var $iNodeType; |
|
33 | + /** |
|
34 | + * Field iNodeType. |
|
35 | + * May be one of the NODE_TYPE_* constants above. |
|
36 | + */ |
|
37 | + var $iNodeType; |
|
38 | 38 | |
39 | - /** |
|
40 | - * Field iNodeName. |
|
41 | - * For elements, it's the name of the element. |
|
42 | - */ |
|
43 | - var $iNodeName = ""; |
|
39 | + /** |
|
40 | + * Field iNodeName. |
|
41 | + * For elements, it's the name of the element. |
|
42 | + */ |
|
43 | + var $iNodeName = ""; |
|
44 | 44 | |
45 | - /** |
|
46 | - * Field iNodeValue. |
|
47 | - * For text nodes, it's the text. |
|
48 | - */ |
|
49 | - var $iNodeValue = ""; |
|
45 | + /** |
|
46 | + * Field iNodeValue. |
|
47 | + * For text nodes, it's the text. |
|
48 | + */ |
|
49 | + var $iNodeValue = ""; |
|
50 | 50 | |
51 | - /** |
|
52 | - * Field iNodeAttributes. |
|
53 | - * A string-indexed array containing attribute values |
|
54 | - * of the current node. Indexes are always lowercase. |
|
55 | - */ |
|
56 | - var $iNodeAttributes; |
|
51 | + /** |
|
52 | + * Field iNodeAttributes. |
|
53 | + * A string-indexed array containing attribute values |
|
54 | + * of the current node. Indexes are always lowercase. |
|
55 | + */ |
|
56 | + var $iNodeAttributes; |
|
57 | 57 | |
58 | - // The following fields should be |
|
59 | - // considered private: |
|
58 | + // The following fields should be |
|
59 | + // considered private: |
|
60 | 60 | |
61 | - var $iHtmlText; |
|
62 | - var $iHtmlTextLength; |
|
63 | - var $iHtmlTextIndex = 0; |
|
64 | - var $iHtmlCurrentChar; |
|
65 | - var $BOE_ARRAY; |
|
66 | - var $B_ARRAY; |
|
67 | - var $BOS_ARRAY; |
|
61 | + var $iHtmlText; |
|
62 | + var $iHtmlTextLength; |
|
63 | + var $iHtmlTextIndex = 0; |
|
64 | + var $iHtmlCurrentChar; |
|
65 | + var $BOE_ARRAY; |
|
66 | + var $B_ARRAY; |
|
67 | + var $BOS_ARRAY; |
|
68 | 68 | |
69 | - /** |
|
70 | - * Constructor. |
|
71 | - * Constructs an HtmlParser instance with |
|
72 | - * the HTML text given. |
|
73 | - */ |
|
74 | - function HtmlParser ($aHtmlText) { |
|
75 | - $this->iHtmlText = $aHtmlText; |
|
76 | - $this->iHtmlTextLength = strlen($aHtmlText); |
|
77 | - $this->iNodeAttributes = array(); |
|
78 | - $this->setTextIndex (0); |
|
69 | + /** |
|
70 | + * Constructor. |
|
71 | + * Constructs an HtmlParser instance with |
|
72 | + * the HTML text given. |
|
73 | + */ |
|
74 | + function HtmlParser ($aHtmlText) { |
|
75 | + $this->iHtmlText = $aHtmlText; |
|
76 | + $this->iHtmlTextLength = strlen($aHtmlText); |
|
77 | + $this->iNodeAttributes = array(); |
|
78 | + $this->setTextIndex (0); |
|
79 | 79 | |
80 | - $this->BOE_ARRAY = array (" ", "\t", "\r", "\n", "=" ); |
|
81 | - $this->B_ARRAY = array (" ", "\t", "\r", "\n" ); |
|
82 | - $this->BOS_ARRAY = array (" ", "\t", "\r", "\n", "/" ); |
|
83 | - } |
|
80 | + $this->BOE_ARRAY = array (" ", "\t", "\r", "\n", "=" ); |
|
81 | + $this->B_ARRAY = array (" ", "\t", "\r", "\n" ); |
|
82 | + $this->BOS_ARRAY = array (" ", "\t", "\r", "\n", "/" ); |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * Method parse. |
|
87 | - * Parses the next node. Returns false only if |
|
88 | - * the end of the HTML text has been reached. |
|
89 | - * Updates values of iNode* fields. |
|
90 | - */ |
|
91 | - function parse() { |
|
92 | - $text = $this->skipToElement(); |
|
93 | - if ($text != "") { |
|
94 | - $this->iNodeType = NODE_TYPE_TEXT; |
|
95 | - $this->iNodeName = "Text"; |
|
96 | - $this->iNodeValue = $text; |
|
97 | - return true; |
|
98 | - } |
|
99 | - return $this->readTag(); |
|
100 | - } |
|
85 | + /** |
|
86 | + * Method parse. |
|
87 | + * Parses the next node. Returns false only if |
|
88 | + * the end of the HTML text has been reached. |
|
89 | + * Updates values of iNode* fields. |
|
90 | + */ |
|
91 | + function parse() { |
|
92 | + $text = $this->skipToElement(); |
|
93 | + if ($text != "") { |
|
94 | + $this->iNodeType = NODE_TYPE_TEXT; |
|
95 | + $this->iNodeName = "Text"; |
|
96 | + $this->iNodeValue = $text; |
|
97 | + return true; |
|
98 | + } |
|
99 | + return $this->readTag(); |
|
100 | + } |
|
101 | 101 | |
102 | - function clearAttributes() { |
|
103 | - $this->iNodeAttributes = array(); |
|
104 | - } |
|
102 | + function clearAttributes() { |
|
103 | + $this->iNodeAttributes = array(); |
|
104 | + } |
|
105 | 105 | |
106 | - function readTag() { |
|
107 | - if ($this->iCurrentChar != "<") { |
|
108 | - $this->iNodeType = NODE_TYPE_DONE; |
|
109 | - return false; |
|
110 | - } |
|
111 | - $this->clearAttributes(); |
|
112 | - $this->skipMaxInTag ("<", 1); |
|
113 | - if ($this->iCurrentChar == '/') { |
|
114 | - $this->moveNext(); |
|
115 | - $name = $this->skipToBlanksInTag(); |
|
116 | - $this->iNodeType = NODE_TYPE_ENDELEMENT; |
|
117 | - $this->iNodeName = $name; |
|
118 | - $this->iNodeValue = ""; |
|
119 | - $this->skipEndOfTag(); |
|
120 | - return true; |
|
121 | - } |
|
122 | - $name = $this->skipToBlanksOrSlashInTag(); |
|
123 | - if (!$this->isValidTagIdentifier ($name)) { |
|
124 | - $comment = false; |
|
125 | - if (strpos($name, "!--") === 0) { |
|
126 | - $ppos = strpos($name, "--", 3); |
|
127 | - if (strpos($name, "--", 3) === (strlen($name) - 2)) { |
|
128 | - $this->iNodeType = NODE_TYPE_COMMENT; |
|
129 | - $this->iNodeName = "Comment"; |
|
130 | - $this->iNodeValue = "<" . $name . ">"; |
|
131 | - $comment = true; |
|
132 | - } |
|
133 | - else { |
|
134 | - $rest = $this->skipToStringInTag ("-->"); |
|
135 | - if ($rest != "") { |
|
136 | - $this->iNodeType = NODE_TYPE_COMMENT; |
|
137 | - $this->iNodeName = "Comment"; |
|
138 | - $this->iNodeValue = "<" . $name . $rest; |
|
139 | - $comment = true; |
|
140 | - // Already skipped end of tag |
|
141 | - return true; |
|
142 | - } |
|
143 | - } |
|
144 | - } |
|
145 | - if (!$comment) { |
|
146 | - $this->iNodeType = NODE_TYPE_TEXT; |
|
147 | - $this->iNodeName = "Text"; |
|
148 | - $this->iNodeValue = "<" . $name; |
|
149 | - return true; |
|
150 | - } |
|
151 | - } |
|
152 | - else { |
|
153 | - $this->iNodeType = NODE_TYPE_ELEMENT; |
|
154 | - $this->iNodeValue = ""; |
|
155 | - $this->iNodeName = $name; |
|
156 | - while ($this->skipBlanksInTag()) { |
|
157 | - $attrName = $this->skipToBlanksOrEqualsInTag(); |
|
158 | - if ($attrName != "" && $attrName != "/") { |
|
159 | - $this->skipBlanksInTag(); |
|
160 | - if ($this->iCurrentChar == "=") { |
|
161 | - $this->skipEqualsInTag(); |
|
162 | - $this->skipBlanksInTag(); |
|
163 | - $value = $this->readValueInTag(); |
|
164 | - $this->iNodeAttributes[strtolower($attrName)] = $value; |
|
165 | - } |
|
166 | - else { |
|
167 | - $this->iNodeAttributes[strtolower($attrName)] = ""; |
|
168 | - } |
|
169 | - } |
|
170 | - } |
|
171 | - } |
|
172 | - $this->skipEndOfTag(); |
|
173 | - return true; |
|
174 | - } |
|
106 | + function readTag() { |
|
107 | + if ($this->iCurrentChar != "<") { |
|
108 | + $this->iNodeType = NODE_TYPE_DONE; |
|
109 | + return false; |
|
110 | + } |
|
111 | + $this->clearAttributes(); |
|
112 | + $this->skipMaxInTag ("<", 1); |
|
113 | + if ($this->iCurrentChar == '/') { |
|
114 | + $this->moveNext(); |
|
115 | + $name = $this->skipToBlanksInTag(); |
|
116 | + $this->iNodeType = NODE_TYPE_ENDELEMENT; |
|
117 | + $this->iNodeName = $name; |
|
118 | + $this->iNodeValue = ""; |
|
119 | + $this->skipEndOfTag(); |
|
120 | + return true; |
|
121 | + } |
|
122 | + $name = $this->skipToBlanksOrSlashInTag(); |
|
123 | + if (!$this->isValidTagIdentifier ($name)) { |
|
124 | + $comment = false; |
|
125 | + if (strpos($name, "!--") === 0) { |
|
126 | + $ppos = strpos($name, "--", 3); |
|
127 | + if (strpos($name, "--", 3) === (strlen($name) - 2)) { |
|
128 | + $this->iNodeType = NODE_TYPE_COMMENT; |
|
129 | + $this->iNodeName = "Comment"; |
|
130 | + $this->iNodeValue = "<" . $name . ">"; |
|
131 | + $comment = true; |
|
132 | + } |
|
133 | + else { |
|
134 | + $rest = $this->skipToStringInTag ("-->"); |
|
135 | + if ($rest != "") { |
|
136 | + $this->iNodeType = NODE_TYPE_COMMENT; |
|
137 | + $this->iNodeName = "Comment"; |
|
138 | + $this->iNodeValue = "<" . $name . $rest; |
|
139 | + $comment = true; |
|
140 | + // Already skipped end of tag |
|
141 | + return true; |
|
142 | + } |
|
143 | + } |
|
144 | + } |
|
145 | + if (!$comment) { |
|
146 | + $this->iNodeType = NODE_TYPE_TEXT; |
|
147 | + $this->iNodeName = "Text"; |
|
148 | + $this->iNodeValue = "<" . $name; |
|
149 | + return true; |
|
150 | + } |
|
151 | + } |
|
152 | + else { |
|
153 | + $this->iNodeType = NODE_TYPE_ELEMENT; |
|
154 | + $this->iNodeValue = ""; |
|
155 | + $this->iNodeName = $name; |
|
156 | + while ($this->skipBlanksInTag()) { |
|
157 | + $attrName = $this->skipToBlanksOrEqualsInTag(); |
|
158 | + if ($attrName != "" && $attrName != "/") { |
|
159 | + $this->skipBlanksInTag(); |
|
160 | + if ($this->iCurrentChar == "=") { |
|
161 | + $this->skipEqualsInTag(); |
|
162 | + $this->skipBlanksInTag(); |
|
163 | + $value = $this->readValueInTag(); |
|
164 | + $this->iNodeAttributes[strtolower($attrName)] = $value; |
|
165 | + } |
|
166 | + else { |
|
167 | + $this->iNodeAttributes[strtolower($attrName)] = ""; |
|
168 | + } |
|
169 | + } |
|
170 | + } |
|
171 | + } |
|
172 | + $this->skipEndOfTag(); |
|
173 | + return true; |
|
174 | + } |
|
175 | 175 | |
176 | - function isValidTagIdentifier ($name) { |
|
177 | - return preg_match ("/^[A-Za-z0-9_\\-]+$/", $name); |
|
178 | - } |
|
176 | + function isValidTagIdentifier ($name) { |
|
177 | + return preg_match ("/^[A-Za-z0-9_\\-]+$/", $name); |
|
178 | + } |
|
179 | 179 | |
180 | - function skipBlanksInTag() { |
|
181 | - return "" != ($this->skipInTag ($this->B_ARRAY)); |
|
182 | - } |
|
180 | + function skipBlanksInTag() { |
|
181 | + return "" != ($this->skipInTag ($this->B_ARRAY)); |
|
182 | + } |
|
183 | 183 | |
184 | - function skipToBlanksOrEqualsInTag() { |
|
185 | - return $this->skipToInTag ($this->BOE_ARRAY); |
|
186 | - } |
|
184 | + function skipToBlanksOrEqualsInTag() { |
|
185 | + return $this->skipToInTag ($this->BOE_ARRAY); |
|
186 | + } |
|
187 | 187 | |
188 | - function skipToBlanksInTag() { |
|
189 | - return $this->skipToInTag ($this->B_ARRAY); |
|
190 | - } |
|
188 | + function skipToBlanksInTag() { |
|
189 | + return $this->skipToInTag ($this->B_ARRAY); |
|
190 | + } |
|
191 | 191 | |
192 | - function skipToBlanksOrSlashInTag() { |
|
193 | - return $this->skipToInTag ($this->BOS_ARRAY); |
|
194 | - } |
|
192 | + function skipToBlanksOrSlashInTag() { |
|
193 | + return $this->skipToInTag ($this->BOS_ARRAY); |
|
194 | + } |
|
195 | 195 | |
196 | - function skipEqualsInTag() { |
|
197 | - return $this->skipMaxInTag ("=", 1); |
|
198 | - } |
|
196 | + function skipEqualsInTag() { |
|
197 | + return $this->skipMaxInTag ("=", 1); |
|
198 | + } |
|
199 | 199 | |
200 | - function readValueInTag() { |
|
201 | - $ch = $this->iCurrentChar; |
|
202 | - $value = ""; |
|
203 | - if ($ch == "\"") { |
|
204 | - $this->skipMaxInTag ("\"", 1); |
|
205 | - $value = $this->skipToInTag ("\""); |
|
206 | - $this->skipMaxInTag ("\"", 1); |
|
207 | - } |
|
208 | - else if ($ch == "'") { |
|
209 | - $this->skipMaxInTag ("'", 1); |
|
210 | - $value = $this->skipToInTag ("'"); |
|
211 | - $this->skipMaxInTag ("'", 1); |
|
212 | - } |
|
213 | - else { |
|
214 | - $value = $this->skipToBlanksInTag(); |
|
215 | - } |
|
216 | - return $value; |
|
217 | - } |
|
200 | + function readValueInTag() { |
|
201 | + $ch = $this->iCurrentChar; |
|
202 | + $value = ""; |
|
203 | + if ($ch == "\"") { |
|
204 | + $this->skipMaxInTag ("\"", 1); |
|
205 | + $value = $this->skipToInTag ("\""); |
|
206 | + $this->skipMaxInTag ("\"", 1); |
|
207 | + } |
|
208 | + else if ($ch == "'") { |
|
209 | + $this->skipMaxInTag ("'", 1); |
|
210 | + $value = $this->skipToInTag ("'"); |
|
211 | + $this->skipMaxInTag ("'", 1); |
|
212 | + } |
|
213 | + else { |
|
214 | + $value = $this->skipToBlanksInTag(); |
|
215 | + } |
|
216 | + return $value; |
|
217 | + } |
|
218 | 218 | |
219 | - function setTextIndex ($index) { |
|
220 | - $this->iHtmlTextIndex = $index; |
|
221 | - if ($index >= $this->iHtmlTextLength) { |
|
222 | - $this->iCurrentChar = -1; |
|
223 | - } |
|
224 | - else { |
|
225 | - $this->iCurrentChar = $this->iHtmlText{$index}; |
|
226 | - } |
|
227 | - } |
|
219 | + function setTextIndex ($index) { |
|
220 | + $this->iHtmlTextIndex = $index; |
|
221 | + if ($index >= $this->iHtmlTextLength) { |
|
222 | + $this->iCurrentChar = -1; |
|
223 | + } |
|
224 | + else { |
|
225 | + $this->iCurrentChar = $this->iHtmlText{$index}; |
|
226 | + } |
|
227 | + } |
|
228 | 228 | |
229 | - function moveNext() { |
|
230 | - if ($this->iHtmlTextIndex < $this->iHtmlTextLength) { |
|
231 | - $this->setTextIndex ($this->iHtmlTextIndex + 1); |
|
232 | - return true; |
|
233 | - } |
|
234 | - else { |
|
235 | - return false; |
|
236 | - } |
|
237 | - } |
|
229 | + function moveNext() { |
|
230 | + if ($this->iHtmlTextIndex < $this->iHtmlTextLength) { |
|
231 | + $this->setTextIndex ($this->iHtmlTextIndex + 1); |
|
232 | + return true; |
|
233 | + } |
|
234 | + else { |
|
235 | + return false; |
|
236 | + } |
|
237 | + } |
|
238 | 238 | |
239 | - function skipEndOfTag() { |
|
240 | - while (($ch = $this->iCurrentChar) !== -1) { |
|
241 | - if ($ch == ">") { |
|
242 | - $this->moveNext(); |
|
243 | - return; |
|
244 | - } |
|
245 | - $this->moveNext(); |
|
246 | - } |
|
247 | - } |
|
239 | + function skipEndOfTag() { |
|
240 | + while (($ch = $this->iCurrentChar) !== -1) { |
|
241 | + if ($ch == ">") { |
|
242 | + $this->moveNext(); |
|
243 | + return; |
|
244 | + } |
|
245 | + $this->moveNext(); |
|
246 | + } |
|
247 | + } |
|
248 | 248 | |
249 | - function skipInTag ($chars) { |
|
250 | - $sb = ""; |
|
251 | - while (($ch = $this->iCurrentChar) !== -1) { |
|
252 | - if ($ch == ">") { |
|
253 | - return $sb; |
|
254 | - } else { |
|
255 | - $match = false; |
|
256 | - for ($idx = 0; $idx < count($chars); $idx++) { |
|
257 | - if ($ch == $chars[$idx]) { |
|
258 | - $match = true; |
|
259 | - break; |
|
260 | - } |
|
261 | - } |
|
262 | - if (!$match) { |
|
263 | - return $sb; |
|
264 | - } |
|
265 | - $sb .= $ch; |
|
266 | - $this->moveNext(); |
|
267 | - } |
|
268 | - } |
|
269 | - return $sb; |
|
270 | - } |
|
249 | + function skipInTag ($chars) { |
|
250 | + $sb = ""; |
|
251 | + while (($ch = $this->iCurrentChar) !== -1) { |
|
252 | + if ($ch == ">") { |
|
253 | + return $sb; |
|
254 | + } else { |
|
255 | + $match = false; |
|
256 | + for ($idx = 0; $idx < count($chars); $idx++) { |
|
257 | + if ($ch == $chars[$idx]) { |
|
258 | + $match = true; |
|
259 | + break; |
|
260 | + } |
|
261 | + } |
|
262 | + if (!$match) { |
|
263 | + return $sb; |
|
264 | + } |
|
265 | + $sb .= $ch; |
|
266 | + $this->moveNext(); |
|
267 | + } |
|
268 | + } |
|
269 | + return $sb; |
|
270 | + } |
|
271 | 271 | |
272 | - function skipMaxInTag ($chars, $maxChars) { |
|
273 | - $sb = ""; |
|
274 | - $count = 0; |
|
275 | - while (($ch = $this->iCurrentChar) !== -1 && $count++ < $maxChars) { |
|
276 | - if ($ch == ">") { |
|
277 | - return $sb; |
|
278 | - } else { |
|
279 | - $match = false; |
|
280 | - for ($idx = 0; $idx < count($chars); $idx++) { |
|
281 | - if ($ch == $chars[$idx]) { |
|
282 | - $match = true; |
|
283 | - break; |
|
284 | - } |
|
285 | - } |
|
286 | - if (!$match) { |
|
287 | - return $sb; |
|
288 | - } |
|
289 | - $sb .= $ch; |
|
290 | - $this->moveNext(); |
|
291 | - } |
|
292 | - } |
|
293 | - return $sb; |
|
294 | - } |
|
272 | + function skipMaxInTag ($chars, $maxChars) { |
|
273 | + $sb = ""; |
|
274 | + $count = 0; |
|
275 | + while (($ch = $this->iCurrentChar) !== -1 && $count++ < $maxChars) { |
|
276 | + if ($ch == ">") { |
|
277 | + return $sb; |
|
278 | + } else { |
|
279 | + $match = false; |
|
280 | + for ($idx = 0; $idx < count($chars); $idx++) { |
|
281 | + if ($ch == $chars[$idx]) { |
|
282 | + $match = true; |
|
283 | + break; |
|
284 | + } |
|
285 | + } |
|
286 | + if (!$match) { |
|
287 | + return $sb; |
|
288 | + } |
|
289 | + $sb .= $ch; |
|
290 | + $this->moveNext(); |
|
291 | + } |
|
292 | + } |
|
293 | + return $sb; |
|
294 | + } |
|
295 | 295 | |
296 | - function skipToInTag ($chars) { |
|
297 | - $sb = ""; |
|
298 | - while (($ch = $this->iCurrentChar) !== -1) { |
|
299 | - $match = $ch == ">"; |
|
300 | - if (!$match) { |
|
301 | - for ($idx = 0; $idx < count($chars); $idx++) { |
|
302 | - if ($ch == $chars[$idx]) { |
|
303 | - $match = true; |
|
304 | - break; |
|
305 | - } |
|
306 | - } |
|
307 | - } |
|
308 | - if ($match) { |
|
309 | - return $sb; |
|
310 | - } |
|
311 | - $sb .= $ch; |
|
312 | - $this->moveNext(); |
|
313 | - } |
|
314 | - return $sb; |
|
315 | - } |
|
296 | + function skipToInTag ($chars) { |
|
297 | + $sb = ""; |
|
298 | + while (($ch = $this->iCurrentChar) !== -1) { |
|
299 | + $match = $ch == ">"; |
|
300 | + if (!$match) { |
|
301 | + for ($idx = 0; $idx < count($chars); $idx++) { |
|
302 | + if ($ch == $chars[$idx]) { |
|
303 | + $match = true; |
|
304 | + break; |
|
305 | + } |
|
306 | + } |
|
307 | + } |
|
308 | + if ($match) { |
|
309 | + return $sb; |
|
310 | + } |
|
311 | + $sb .= $ch; |
|
312 | + $this->moveNext(); |
|
313 | + } |
|
314 | + return $sb; |
|
315 | + } |
|
316 | 316 | |
317 | - function skipToElement() { |
|
318 | - $sb = ""; |
|
319 | - while (($ch = $this->iCurrentChar) !== -1) { |
|
320 | - if ($ch == "<") { |
|
321 | - return $sb; |
|
322 | - } |
|
323 | - $sb .= $ch; |
|
324 | - $this->moveNext(); |
|
325 | - } |
|
326 | - return $sb; |
|
327 | - } |
|
317 | + function skipToElement() { |
|
318 | + $sb = ""; |
|
319 | + while (($ch = $this->iCurrentChar) !== -1) { |
|
320 | + if ($ch == "<") { |
|
321 | + return $sb; |
|
322 | + } |
|
323 | + $sb .= $ch; |
|
324 | + $this->moveNext(); |
|
325 | + } |
|
326 | + return $sb; |
|
327 | + } |
|
328 | 328 | |
329 | - /** |
|
330 | - * Returns text between current position and $needle, |
|
331 | - * inclusive, or "" if not found. The current index is moved to a point |
|
332 | - * after the location of $needle, or not moved at all |
|
333 | - * if nothing is found. |
|
334 | - */ |
|
335 | - function skipToStringInTag ($needle) { |
|
336 | - $pos = strpos ($this->iHtmlText, $needle, $this->iHtmlTextIndex); |
|
337 | - if ($pos === false) { |
|
338 | - return ""; |
|
339 | - } |
|
340 | - $top = $pos + strlen($needle); |
|
341 | - $retvalue = substr ($this->iHtmlText, $this->iHtmlTextIndex, $top - $this->iHtmlTextIndex); |
|
342 | - $this->setTextIndex ($top); |
|
343 | - return $retvalue; |
|
344 | - } |
|
329 | + /** |
|
330 | + * Returns text between current position and $needle, |
|
331 | + * inclusive, or "" if not found. The current index is moved to a point |
|
332 | + * after the location of $needle, or not moved at all |
|
333 | + * if nothing is found. |
|
334 | + */ |
|
335 | + function skipToStringInTag ($needle) { |
|
336 | + $pos = strpos ($this->iHtmlText, $needle, $this->iHtmlTextIndex); |
|
337 | + if ($pos === false) { |
|
338 | + return ""; |
|
339 | + } |
|
340 | + $top = $pos + strlen($needle); |
|
341 | + $retvalue = substr ($this->iHtmlText, $this->iHtmlTextIndex, $top - $this->iHtmlTextIndex); |
|
342 | + $this->setTextIndex ($top); |
|
343 | + return $retvalue; |
|
344 | + } |
|
345 | 345 | } |
346 | 346 | |
347 | 347 | function HtmlParser_ForFile ($fileName) { |
348 | - return HtmlParser_ForURL($fileName); |
|
348 | + return HtmlParser_ForURL($fileName); |
|
349 | 349 | } |
350 | 350 | |
351 | 351 | function HtmlParser_ForURL ($url) { |
352 | - $fp = fopen ($url, "r"); |
|
353 | - $content = ""; |
|
354 | - while (true) { |
|
355 | - $data = fread ($fp, 8192); |
|
356 | - if (strlen($data) == 0) { |
|
357 | - break; |
|
358 | - } |
|
359 | - $content .= $data; |
|
360 | - } |
|
361 | - fclose ($fp); |
|
362 | - return new HtmlParser ($content); |
|
352 | + $fp = fopen ($url, "r"); |
|
353 | + $content = ""; |
|
354 | + while (true) { |
|
355 | + $data = fread ($fp, 8192); |
|
356 | + if (strlen($data) == 0) { |
|
357 | + break; |
|
358 | + } |
|
359 | + $content .= $data; |
|
360 | + } |
|
361 | + fclose ($fp); |
|
362 | + return new HtmlParser ($content); |
|
363 | 363 | } |
364 | 364 | |
365 | 365 | php?> |
@@ -10,12 +10,12 @@ discard block |
||
10 | 10 | * - Leo West (performance improvements) |
11 | 11 | */ |
12 | 12 | |
13 | -define ("NODE_TYPE_START",0); |
|
14 | -define ("NODE_TYPE_ELEMENT",1); |
|
15 | -define ("NODE_TYPE_ENDELEMENT",2); |
|
16 | -define ("NODE_TYPE_TEXT",3); |
|
17 | -define ("NODE_TYPE_COMMENT",4); |
|
18 | -define ("NODE_TYPE_DONE",5); |
|
13 | +define("NODE_TYPE_START", 0); |
|
14 | +define("NODE_TYPE_ELEMENT", 1); |
|
15 | +define("NODE_TYPE_ENDELEMENT", 2); |
|
16 | +define("NODE_TYPE_TEXT", 3); |
|
17 | +define("NODE_TYPE_COMMENT", 4); |
|
18 | +define("NODE_TYPE_DONE", 5); |
|
19 | 19 | |
20 | 20 | /** |
21 | 21 | * Class HtmlParser. |
@@ -71,15 +71,15 @@ discard block |
||
71 | 71 | * Constructs an HtmlParser instance with |
72 | 72 | * the HTML text given. |
73 | 73 | */ |
74 | - function HtmlParser ($aHtmlText) { |
|
74 | + function HtmlParser($aHtmlText) { |
|
75 | 75 | $this->iHtmlText = $aHtmlText; |
76 | 76 | $this->iHtmlTextLength = strlen($aHtmlText); |
77 | 77 | $this->iNodeAttributes = array(); |
78 | - $this->setTextIndex (0); |
|
78 | + $this->setTextIndex(0); |
|
79 | 79 | |
80 | - $this->BOE_ARRAY = array (" ", "\t", "\r", "\n", "=" ); |
|
81 | - $this->B_ARRAY = array (" ", "\t", "\r", "\n" ); |
|
82 | - $this->BOS_ARRAY = array (" ", "\t", "\r", "\n", "/" ); |
|
80 | + $this->BOE_ARRAY = array(" ", "\t", "\r", "\n", "="); |
|
81 | + $this->B_ARRAY = array(" ", "\t", "\r", "\n"); |
|
82 | + $this->BOS_ARRAY = array(" ", "\t", "\r", "\n", "/"); |
|
83 | 83 | } |
84 | 84 | |
85 | 85 | /** |
@@ -109,7 +109,7 @@ discard block |
||
109 | 109 | return false; |
110 | 110 | } |
111 | 111 | $this->clearAttributes(); |
112 | - $this->skipMaxInTag ("<", 1); |
|
112 | + $this->skipMaxInTag("<", 1); |
|
113 | 113 | if ($this->iCurrentChar == '/') { |
114 | 114 | $this->moveNext(); |
115 | 115 | $name = $this->skipToBlanksInTag(); |
@@ -120,22 +120,22 @@ discard block |
||
120 | 120 | return true; |
121 | 121 | } |
122 | 122 | $name = $this->skipToBlanksOrSlashInTag(); |
123 | - if (!$this->isValidTagIdentifier ($name)) { |
|
123 | + if (!$this->isValidTagIdentifier($name)) { |
|
124 | 124 | $comment = false; |
125 | 125 | if (strpos($name, "!--") === 0) { |
126 | 126 | $ppos = strpos($name, "--", 3); |
127 | 127 | if (strpos($name, "--", 3) === (strlen($name) - 2)) { |
128 | 128 | $this->iNodeType = NODE_TYPE_COMMENT; |
129 | 129 | $this->iNodeName = "Comment"; |
130 | - $this->iNodeValue = "<" . $name . ">"; |
|
130 | + $this->iNodeValue = "<".$name.">"; |
|
131 | 131 | $comment = true; |
132 | 132 | } |
133 | 133 | else { |
134 | - $rest = $this->skipToStringInTag ("-->"); |
|
134 | + $rest = $this->skipToStringInTag("-->"); |
|
135 | 135 | if ($rest != "") { |
136 | 136 | $this->iNodeType = NODE_TYPE_COMMENT; |
137 | 137 | $this->iNodeName = "Comment"; |
138 | - $this->iNodeValue = "<" . $name . $rest; |
|
138 | + $this->iNodeValue = "<".$name.$rest; |
|
139 | 139 | $comment = true; |
140 | 140 | // Already skipped end of tag |
141 | 141 | return true; |
@@ -145,7 +145,7 @@ discard block |
||
145 | 145 | if (!$comment) { |
146 | 146 | $this->iNodeType = NODE_TYPE_TEXT; |
147 | 147 | $this->iNodeName = "Text"; |
148 | - $this->iNodeValue = "<" . $name; |
|
148 | + $this->iNodeValue = "<".$name; |
|
149 | 149 | return true; |
150 | 150 | } |
151 | 151 | } |
@@ -173,42 +173,42 @@ discard block |
||
173 | 173 | return true; |
174 | 174 | } |
175 | 175 | |
176 | - function isValidTagIdentifier ($name) { |
|
177 | - return preg_match ("/^[A-Za-z0-9_\\-]+$/", $name); |
|
176 | + function isValidTagIdentifier($name) { |
|
177 | + return preg_match("/^[A-Za-z0-9_\\-]+$/", $name); |
|
178 | 178 | } |
179 | 179 | |
180 | 180 | function skipBlanksInTag() { |
181 | - return "" != ($this->skipInTag ($this->B_ARRAY)); |
|
181 | + return "" != ($this->skipInTag($this->B_ARRAY)); |
|
182 | 182 | } |
183 | 183 | |
184 | 184 | function skipToBlanksOrEqualsInTag() { |
185 | - return $this->skipToInTag ($this->BOE_ARRAY); |
|
185 | + return $this->skipToInTag($this->BOE_ARRAY); |
|
186 | 186 | } |
187 | 187 | |
188 | 188 | function skipToBlanksInTag() { |
189 | - return $this->skipToInTag ($this->B_ARRAY); |
|
189 | + return $this->skipToInTag($this->B_ARRAY); |
|
190 | 190 | } |
191 | 191 | |
192 | 192 | function skipToBlanksOrSlashInTag() { |
193 | - return $this->skipToInTag ($this->BOS_ARRAY); |
|
193 | + return $this->skipToInTag($this->BOS_ARRAY); |
|
194 | 194 | } |
195 | 195 | |
196 | 196 | function skipEqualsInTag() { |
197 | - return $this->skipMaxInTag ("=", 1); |
|
197 | + return $this->skipMaxInTag("=", 1); |
|
198 | 198 | } |
199 | 199 | |
200 | 200 | function readValueInTag() { |
201 | 201 | $ch = $this->iCurrentChar; |
202 | 202 | $value = ""; |
203 | 203 | if ($ch == "\"") { |
204 | - $this->skipMaxInTag ("\"", 1); |
|
205 | - $value = $this->skipToInTag ("\""); |
|
206 | - $this->skipMaxInTag ("\"", 1); |
|
204 | + $this->skipMaxInTag("\"", 1); |
|
205 | + $value = $this->skipToInTag("\""); |
|
206 | + $this->skipMaxInTag("\"", 1); |
|
207 | 207 | } |
208 | 208 | else if ($ch == "'") { |
209 | - $this->skipMaxInTag ("'", 1); |
|
210 | - $value = $this->skipToInTag ("'"); |
|
211 | - $this->skipMaxInTag ("'", 1); |
|
209 | + $this->skipMaxInTag("'", 1); |
|
210 | + $value = $this->skipToInTag("'"); |
|
211 | + $this->skipMaxInTag("'", 1); |
|
212 | 212 | } |
213 | 213 | else { |
214 | 214 | $value = $this->skipToBlanksInTag(); |
@@ -216,7 +216,7 @@ discard block |
||
216 | 216 | return $value; |
217 | 217 | } |
218 | 218 | |
219 | - function setTextIndex ($index) { |
|
219 | + function setTextIndex($index) { |
|
220 | 220 | $this->iHtmlTextIndex = $index; |
221 | 221 | if ($index >= $this->iHtmlTextLength) { |
222 | 222 | $this->iCurrentChar = -1; |
@@ -228,7 +228,7 @@ discard block |
||
228 | 228 | |
229 | 229 | function moveNext() { |
230 | 230 | if ($this->iHtmlTextIndex < $this->iHtmlTextLength) { |
231 | - $this->setTextIndex ($this->iHtmlTextIndex + 1); |
|
231 | + $this->setTextIndex($this->iHtmlTextIndex + 1); |
|
232 | 232 | return true; |
233 | 233 | } |
234 | 234 | else { |
@@ -246,7 +246,7 @@ discard block |
||
246 | 246 | } |
247 | 247 | } |
248 | 248 | |
249 | - function skipInTag ($chars) { |
|
249 | + function skipInTag($chars) { |
|
250 | 250 | $sb = ""; |
251 | 251 | while (($ch = $this->iCurrentChar) !== -1) { |
252 | 252 | if ($ch == ">") { |
@@ -269,7 +269,7 @@ discard block |
||
269 | 269 | return $sb; |
270 | 270 | } |
271 | 271 | |
272 | - function skipMaxInTag ($chars, $maxChars) { |
|
272 | + function skipMaxInTag($chars, $maxChars) { |
|
273 | 273 | $sb = ""; |
274 | 274 | $count = 0; |
275 | 275 | while (($ch = $this->iCurrentChar) !== -1 && $count++ < $maxChars) { |
@@ -293,7 +293,7 @@ discard block |
||
293 | 293 | return $sb; |
294 | 294 | } |
295 | 295 | |
296 | - function skipToInTag ($chars) { |
|
296 | + function skipToInTag($chars) { |
|
297 | 297 | $sb = ""; |
298 | 298 | while (($ch = $this->iCurrentChar) !== -1) { |
299 | 299 | $match = $ch == ">"; |
@@ -332,34 +332,34 @@ discard block |
||
332 | 332 | * after the location of $needle, or not moved at all |
333 | 333 | * if nothing is found. |
334 | 334 | */ |
335 | - function skipToStringInTag ($needle) { |
|
336 | - $pos = strpos ($this->iHtmlText, $needle, $this->iHtmlTextIndex); |
|
335 | + function skipToStringInTag($needle) { |
|
336 | + $pos = strpos($this->iHtmlText, $needle, $this->iHtmlTextIndex); |
|
337 | 337 | if ($pos === false) { |
338 | 338 | return ""; |
339 | 339 | } |
340 | 340 | $top = $pos + strlen($needle); |
341 | - $retvalue = substr ($this->iHtmlText, $this->iHtmlTextIndex, $top - $this->iHtmlTextIndex); |
|
342 | - $this->setTextIndex ($top); |
|
341 | + $retvalue = substr($this->iHtmlText, $this->iHtmlTextIndex, $top - $this->iHtmlTextIndex); |
|
342 | + $this->setTextIndex($top); |
|
343 | 343 | return $retvalue; |
344 | 344 | } |
345 | 345 | } |
346 | 346 | |
347 | -function HtmlParser_ForFile ($fileName) { |
|
347 | +function HtmlParser_ForFile($fileName) { |
|
348 | 348 | return HtmlParser_ForURL($fileName); |
349 | 349 | } |
350 | 350 | |
351 | -function HtmlParser_ForURL ($url) { |
|
352 | - $fp = fopen ($url, "r"); |
|
351 | +function HtmlParser_ForURL($url) { |
|
352 | + $fp = fopen($url, "r"); |
|
353 | 353 | $content = ""; |
354 | 354 | while (true) { |
355 | - $data = fread ($fp, 8192); |
|
355 | + $data = fread($fp, 8192); |
|
356 | 356 | if (strlen($data) == 0) { |
357 | 357 | break; |
358 | 358 | } |
359 | 359 | $content .= $data; |
360 | 360 | } |
361 | - fclose ($fp); |
|
362 | - return new HtmlParser ($content); |
|
361 | + fclose($fp); |
|
362 | + return new HtmlParser($content); |
|
363 | 363 | } |
364 | 364 | |
365 | 365 | php?> |