| @@ -47,7 +47,7 @@ | ||
| 47 | 47 | * node object. | 
| 48 | 48 | * | 
| 49 | 49 | * @param AbstractNode $node | 
| 50 | - * @return array|Collection | |
| 50 | + * @return Collection | |
| 51 | 51 | */ | 
| 52 | 52 | public function find(AbstractNode $node) | 
| 53 | 53 |      { | 
| @@ -15,361 +15,361 @@ | ||
| 15 | 15 | class Selector | 
| 16 | 16 |  { | 
| 17 | 17 | |
| 18 | - /** | |
| 19 | - * Pattern of CSS selectors, modified from 'mootools' | |
| 20 | - * | |
| 21 | - * @var string | |
| 22 | - */ | |
| 23 | - protected $pattern = "/([\w-:\*>]*)(?:\#([\w-]+)|\.([\w-]+))?(?:\[@?(!?[\w-:]+)(?:([!*^$]?=)[\"']?(.*?)[\"']?)?\])?([\/, ]+)/is"; | |
| 24 | - | |
| 25 | - protected $selectors = []; | |
| 26 | - | |
| 27 | - /** | |
| 28 | - * Constructs with the selector string | |
| 29 | - * | |
| 30 | - * @param string $selector | |
| 31 | - */ | |
| 32 | - public function __construct($selector) | |
| 33 | -    { | |
| 34 | - $this->parseSelectorString($selector); | |
| 35 | - } | |
| 36 | - | |
| 37 | - /** | |
| 38 | - * Returns the selectors that where found in __construct | |
| 39 | - * | |
| 40 | - * @return array | |
| 41 | - */ | |
| 42 | - public function getSelectors() | |
| 43 | -    { | |
| 44 | - return $this->selectors; | |
| 45 | - } | |
| 46 | - | |
| 47 | - /** | |
| 48 | - * Attempts to find the selectors starting from the given | |
| 49 | - * node object. | |
| 50 | - * | |
| 51 | - * @param AbstractNode $node | |
| 52 | - * @return array|Collection | |
| 53 | - */ | |
| 54 | - public function find(AbstractNode $node) | |
| 55 | -    { | |
| 56 | - $results = new Collection; | |
| 57 | -        foreach ($this->selectors as $selector) { | |
| 58 | - $nodes = [$node]; | |
| 59 | -            if (count($selector) == 0) { | |
| 60 | - continue; | |
| 61 | - } | |
| 62 | - | |
| 63 | - $options = []; | |
| 64 | -            foreach ($selector as $rule) { | |
| 65 | -                if ($rule['alterNext']) { | |
| 66 | - $options[] = $this->alterNext($rule); | |
| 67 | - continue; | |
| 68 | - } | |
| 69 | - $nodes = $this->seek($nodes, $rule, $options); | |
| 70 | - // clear the options | |
| 71 | - $options = []; | |
| 72 | - } | |
| 73 | - | |
| 74 | - // this is the final set of nodes | |
| 75 | -            foreach ($nodes as $result) { | |
| 76 | - $results[] = $result; | |
| 77 | - } | |
| 78 | - } | |
| 79 | - | |
| 80 | - return $results; | |
| 81 | - } | |
| 82 | - | |
| 83 | - /** | |
| 84 | - * Parses the selector string | |
| 85 | - * | |
| 86 | - * @param string $selector | |
| 87 | - */ | |
| 88 | - protected function parseSelectorString($selector) | |
| 89 | -    { | |
| 90 | - $matches = []; | |
| 91 | - preg_match_all($this->pattern, trim($selector).' ', $matches, PREG_SET_ORDER); | |
| 92 | - | |
| 93 | - // skip tbody | |
| 94 | - $result = []; | |
| 95 | -        foreach ($matches as $match) { | |
| 96 | - // default values | |
| 97 | - $tag = strtolower(trim($match[1])); | |
| 98 | - $operator = '='; | |
| 99 | - $key = null; | |
| 100 | - $value = null; | |
| 101 | - $noKey = false; | |
| 102 | - $alterNext = false; | |
| 103 | - | |
| 104 | - // check for elements that alter the behavior of the next element | |
| 105 | -            if ($tag == '>') { | |
| 106 | - $alterNext = true; | |
| 107 | - } | |
| 108 | - | |
| 109 | - // check for id selector | |
| 110 | -            if ( ! empty($match[2])) { | |
| 111 | - $key = 'id'; | |
| 112 | - $value = $match[2]; | |
| 113 | - } | |
| 114 | - | |
| 115 | - // check for class selector | |
| 116 | -            if ( ! empty($match[3])) { | |
| 117 | - $key = 'class'; | |
| 118 | - $value = $match[3]; | |
| 119 | - } | |
| 120 | - | |
| 121 | - // and final attribute selector | |
| 122 | -            if ( ! empty($match[4])) { | |
| 123 | - $key = strtolower($match[4]); | |
| 124 | - } | |
| 125 | -            if ( ! empty($match[5])) { | |
| 126 | - $operator = $match[5]; | |
| 127 | - } | |
| 128 | -            if ( ! empty($match[6])) { | |
| 129 | - $value = $match[6]; | |
| 130 | - } | |
| 131 | - | |
| 132 | - // check for elements that do not have a specified attribute | |
| 133 | -            if (isset($key[0]) && $key[0] == '!') { | |
| 134 | - $key = substr($key, 1); | |
| 135 | - $noKey = true; | |
| 136 | - } | |
| 137 | - | |
| 138 | - $result[] = [ | |
| 139 | - 'tag' => $tag, | |
| 140 | - 'key' => $key, | |
| 141 | - 'value' => $value, | |
| 142 | - 'operator' => $operator, | |
| 143 | - 'noKey' => $noKey, | |
| 144 | - 'alterNext' => $alterNext, | |
| 145 | - ]; | |
| 146 | -            if (trim($match[7]) == ',') { | |
| 147 | - $this->selectors[] = $result; | |
| 148 | - $result = []; | |
| 149 | - } | |
| 150 | - } | |
| 151 | - | |
| 152 | - // save last results | |
| 153 | -        if (count($result) > 0) { | |
| 154 | - $this->selectors[] = $result; | |
| 155 | - } | |
| 156 | - } | |
| 157 | - | |
| 158 | - /** | |
| 159 | - * Attempts to find all children that match the rule | |
| 160 | - * given. | |
| 161 | - * | |
| 162 | - * @param array $nodes | |
| 163 | - * @param array $rule | |
| 164 | - * @param array $options | |
| 165 | - * @return array | |
| 166 | - * @recursive | |
| 167 | - */ | |
| 168 | - protected function seek(array $nodes, array $rule, array $options) | |
| 169 | -    { | |
| 170 | - // XPath index | |
| 171 | - if (count($rule['tag']) > 0 && | |
| 172 | - count($rule['key']) > 0 && | |
| 173 | - is_numeric($rule['key']) | |
| 174 | -        ) { | |
| 175 | - $count = 0; | |
| 176 | - /** @var AbstractNode $node */ | |
| 177 | -            foreach ($nodes as $node) { | |
| 178 | - if ($rule['tag'] == '*' || | |
| 179 | -                    $rule['tag'] == $node->getTag()->name()) { | |
| 180 | - ++$count; | |
| 181 | -                    if ($count == $rule['key']) { | |
| 182 | - // found the node we wanted | |
| 183 | - return [$node]; | |
| 184 | - } | |
| 185 | - } | |
| 186 | - } | |
| 187 | - | |
| 188 | - return []; | |
| 189 | - } | |
| 190 | - | |
| 191 | - $options = $this->flattenOptions($options); | |
| 192 | - | |
| 193 | - $return = []; | |
| 194 | - /** @var InnerNode $node */ | |
| 195 | -        foreach ($nodes as $node) { | |
| 196 | - // check if we are a leaf | |
| 197 | - if ($node instanceof LeafNode || | |
| 198 | -                 ! $node->hasChildren()) { | |
| 199 | - continue; | |
| 200 | - } | |
| 201 | - | |
| 202 | - $children = []; | |
| 203 | - $child = $node->firstChild(); | |
| 204 | -            while ( ! is_null($child)) { | |
| 205 | - // wild card, grab all | |
| 206 | -                if ($rule['tag'] == '*' && is_null($rule['key'])) { | |
| 207 | - $return[] = $child; | |
| 208 | -                    try { | |
| 209 | - $child = $node->nextChild($child->id()); | |
| 210 | -                    } catch (ChildNotFoundException $e) { | |
| 211 | - // no more children | |
| 212 | - $child = null; | |
| 213 | - } | |
| 214 | - continue; | |
| 215 | - } | |
| 216 | - | |
| 217 | - $pass = true; | |
| 218 | - // check tag | |
| 219 | - if ( ! empty($rule['tag']) && $rule['tag'] != $child->getTag()->name() && | |
| 220 | - $rule['tag'] != '*' | |
| 221 | -                ) { | |
| 222 | - // child failed tag check | |
| 223 | - $pass = false; | |
| 224 | - } | |
| 225 | - | |
| 226 | - // check key | |
| 227 | -                if ($pass && ! is_null($rule['key'])) { | |
| 228 | -                    if ($rule['noKey']) { | |
| 229 | -                        if ( ! is_null($child->getAttribute($rule['key']))) { | |
| 230 | - $pass = false; | |
| 231 | - } | |
| 232 | -                    } else { | |
| 233 | - if ($rule['key'] != 'plaintext' && | |
| 234 | - is_null($child->getAttribute($rule['key'])) | |
| 235 | -                        ) { | |
| 236 | - $pass = false; | |
| 237 | - } | |
| 238 | - } | |
| 239 | - } | |
| 240 | - | |
| 241 | - // compare values | |
| 242 | - if ($pass && ! is_null($rule['key']) && | |
| 243 | - ! is_null($rule['value']) && $rule['value'] != '*' | |
| 244 | -                ) { | |
| 245 | -                    if ($rule['key'] == 'plaintext') { | |
| 246 | - // plaintext search | |
| 247 | - $nodeValue = $child->text(); | |
| 248 | -                    } else { | |
| 249 | - // normal search | |
| 250 | - $nodeValue = $child->getAttribute($rule['key']); | |
| 251 | - } | |
| 252 | - | |
| 253 | - $check = $this->match($rule['operator'], $rule['value'], $nodeValue); | |
| 254 | - | |
| 255 | - // handle multiple classes | |
| 256 | -                    if ( ! $check && $rule['key'] == 'class') { | |
| 257 | -                        $childClasses = explode(' ', $child->getAttribute('class')); | |
| 258 | -                        foreach ($childClasses as $class) { | |
| 259 | -                            if ( ! empty($class)) { | |
| 260 | - $check = $this->match($rule['operator'], $rule['value'], $class); | |
| 261 | - } | |
| 262 | -                            if ($check) { | |
| 263 | - break; | |
| 264 | - } | |
| 265 | - } | |
| 266 | - } | |
| 267 | - | |
| 268 | -                    if ( ! $check) { | |
| 269 | - $pass = false; | |
| 270 | - } | |
| 271 | - } | |
| 272 | - | |
| 273 | -                if ($pass) { | |
| 274 | - // it passed all checks | |
| 275 | - $return[] = $child; | |
| 276 | -                } else { | |
| 277 | - // this child failed to be matched | |
| 278 | - if ($child instanceof InnerNode && | |
| 279 | -                        $child->hasChildren()) { | |
| 280 | - // we still want to check its children | |
| 281 | - $children[] = $child; | |
| 282 | - } | |
| 283 | - } | |
| 284 | - | |
| 285 | -                try { | |
| 286 | - // get next child | |
| 287 | - $child = $node->nextChild($child->id()); | |
| 288 | -                } catch (ChildNotFoundException $e) { | |
| 289 | - // no more children | |
| 290 | - $child = null; | |
| 291 | - } | |
| 292 | - } | |
| 293 | - | |
| 294 | - if (( ! isset($options['checkGrandChildren']) || | |
| 295 | - $options['checkGrandChildren']) | |
| 296 | - && count($children) > 0 | |
| 297 | -            ) { | |
| 298 | - // we have children that failed but are not leaves. | |
| 299 | - $matches = $this->seek($children, $rule, $options); | |
| 300 | -                foreach ($matches as $match) { | |
| 301 | - $return[] = $match; | |
| 302 | - } | |
| 303 | - } | |
| 304 | - } | |
| 305 | - | |
| 306 | - return $return; | |
| 307 | - } | |
| 308 | - | |
| 309 | - /** | |
| 310 | - * Attempts to match the given arguments with the given operator. | |
| 311 | - * | |
| 312 | - * @param string $operator | |
| 313 | - * @param string $pattern | |
| 314 | - * @param string $value | |
| 315 | - * @return bool | |
| 316 | - */ | |
| 317 | - protected function match($operator, $pattern, $value) | |
| 318 | -    { | |
| 319 | - $value = strtolower($value); | |
| 320 | - $pattern = strtolower($pattern); | |
| 321 | -        switch ($operator) { | |
| 322 | - case '=': | |
| 323 | - return $value === $pattern; | |
| 324 | - case '!=': | |
| 325 | - return $value !== $pattern; | |
| 326 | - case '^=': | |
| 327 | -                return preg_match('/^'.preg_quote($pattern, '/').'/', $value); | |
| 328 | - case '$=': | |
| 329 | -                return preg_match('/'.preg_quote($pattern, '/').'$/', $value); | |
| 330 | - case '*=': | |
| 331 | -                if ($pattern[0] == '/') { | |
| 332 | - return preg_match($pattern, $value); | |
| 333 | - } | |
| 334 | - | |
| 335 | -                return preg_match("/".$pattern."/i", $value); | |
| 336 | - } | |
| 337 | - | |
| 338 | - return false; | |
| 339 | - } | |
| 340 | - | |
| 341 | - /** | |
| 342 | - * Attempts to figure out what the alteration will be for | |
| 343 | - * the next element. | |
| 344 | - * | |
| 345 | - * @param array $rule | |
| 346 | - * @return array | |
| 347 | - */ | |
| 348 | - protected function alterNext($rule) | |
| 349 | -    { | |
| 350 | - $options = []; | |
| 351 | -        if ($rule['tag'] == '>') { | |
| 352 | - $options['checkGrandChildren'] = false; | |
| 353 | - } | |
| 354 | - | |
| 355 | - return $options; | |
| 356 | - } | |
| 357 | - | |
| 358 | - /** | |
| 359 | - * Flattens the option array. | |
| 360 | - * | |
| 361 | - * @param array $optionsArray | |
| 362 | - * @return array | |
| 363 | - */ | |
| 364 | - protected function flattenOptions(array $optionsArray) | |
| 365 | -    { | |
| 366 | - $options = []; | |
| 367 | -        foreach ($optionsArray as $optionArray) { | |
| 368 | -            foreach ($optionArray as $key => $option) { | |
| 369 | - $options[$key] = $option; | |
| 370 | - } | |
| 371 | - } | |
| 372 | - | |
| 373 | - return $options; | |
| 374 | - } | |
| 18 | + /** | |
| 19 | + * Pattern of CSS selectors, modified from 'mootools' | |
| 20 | + * | |
| 21 | + * @var string | |
| 22 | + */ | |
| 23 | + protected $pattern = "/([\w-:\*>]*)(?:\#([\w-]+)|\.([\w-]+))?(?:\[@?(!?[\w-:]+)(?:([!*^$]?=)[\"']?(.*?)[\"']?)?\])?([\/, ]+)/is"; | |
| 24 | + | |
| 25 | + protected $selectors = []; | |
| 26 | + | |
| 27 | + /** | |
| 28 | + * Constructs with the selector string | |
| 29 | + * | |
| 30 | + * @param string $selector | |
| 31 | + */ | |
| 32 | + public function __construct($selector) | |
| 33 | +	{ | |
| 34 | + $this->parseSelectorString($selector); | |
| 35 | + } | |
| 36 | + | |
| 37 | + /** | |
| 38 | + * Returns the selectors that where found in __construct | |
| 39 | + * | |
| 40 | + * @return array | |
| 41 | + */ | |
| 42 | + public function getSelectors() | |
| 43 | +	{ | |
| 44 | + return $this->selectors; | |
| 45 | + } | |
| 46 | + | |
| 47 | + /** | |
| 48 | + * Attempts to find the selectors starting from the given | |
| 49 | + * node object. | |
| 50 | + * | |
| 51 | + * @param AbstractNode $node | |
| 52 | + * @return array|Collection | |
| 53 | + */ | |
| 54 | + public function find(AbstractNode $node) | |
| 55 | +	{ | |
| 56 | + $results = new Collection; | |
| 57 | +		foreach ($this->selectors as $selector) { | |
| 58 | + $nodes = [$node]; | |
| 59 | +			if (count($selector) == 0) { | |
| 60 | + continue; | |
| 61 | + } | |
| 62 | + | |
| 63 | + $options = []; | |
| 64 | +			foreach ($selector as $rule) { | |
| 65 | +				if ($rule['alterNext']) { | |
| 66 | + $options[] = $this->alterNext($rule); | |
| 67 | + continue; | |
| 68 | + } | |
| 69 | + $nodes = $this->seek($nodes, $rule, $options); | |
| 70 | + // clear the options | |
| 71 | + $options = []; | |
| 72 | + } | |
| 73 | + | |
| 74 | + // this is the final set of nodes | |
| 75 | +			foreach ($nodes as $result) { | |
| 76 | + $results[] = $result; | |
| 77 | + } | |
| 78 | + } | |
| 79 | + | |
| 80 | + return $results; | |
| 81 | + } | |
| 82 | + | |
| 83 | + /** | |
| 84 | + * Parses the selector string | |
| 85 | + * | |
| 86 | + * @param string $selector | |
| 87 | + */ | |
| 88 | + protected function parseSelectorString($selector) | |
| 89 | +	{ | |
| 90 | + $matches = []; | |
| 91 | + preg_match_all($this->pattern, trim($selector).' ', $matches, PREG_SET_ORDER); | |
| 92 | + | |
| 93 | + // skip tbody | |
| 94 | + $result = []; | |
| 95 | +		foreach ($matches as $match) { | |
| 96 | + // default values | |
| 97 | + $tag = strtolower(trim($match[1])); | |
| 98 | + $operator = '='; | |
| 99 | + $key = null; | |
| 100 | + $value = null; | |
| 101 | + $noKey = false; | |
| 102 | + $alterNext = false; | |
| 103 | + | |
| 104 | + // check for elements that alter the behavior of the next element | |
| 105 | +			if ($tag == '>') { | |
| 106 | + $alterNext = true; | |
| 107 | + } | |
| 108 | + | |
| 109 | + // check for id selector | |
| 110 | +			if ( ! empty($match[2])) { | |
| 111 | + $key = 'id'; | |
| 112 | + $value = $match[2]; | |
| 113 | + } | |
| 114 | + | |
| 115 | + // check for class selector | |
| 116 | +			if ( ! empty($match[3])) { | |
| 117 | + $key = 'class'; | |
| 118 | + $value = $match[3]; | |
| 119 | + } | |
| 120 | + | |
| 121 | + // and final attribute selector | |
| 122 | +			if ( ! empty($match[4])) { | |
| 123 | + $key = strtolower($match[4]); | |
| 124 | + } | |
| 125 | +			if ( ! empty($match[5])) { | |
| 126 | + $operator = $match[5]; | |
| 127 | + } | |
| 128 | +			if ( ! empty($match[6])) { | |
| 129 | + $value = $match[6]; | |
| 130 | + } | |
| 131 | + | |
| 132 | + // check for elements that do not have a specified attribute | |
| 133 | +			if (isset($key[0]) && $key[0] == '!') { | |
| 134 | + $key = substr($key, 1); | |
| 135 | + $noKey = true; | |
| 136 | + } | |
| 137 | + | |
| 138 | + $result[] = [ | |
| 139 | + 'tag' => $tag, | |
| 140 | + 'key' => $key, | |
| 141 | + 'value' => $value, | |
| 142 | + 'operator' => $operator, | |
| 143 | + 'noKey' => $noKey, | |
| 144 | + 'alterNext' => $alterNext, | |
| 145 | + ]; | |
| 146 | +			if (trim($match[7]) == ',') { | |
| 147 | + $this->selectors[] = $result; | |
| 148 | + $result = []; | |
| 149 | + } | |
| 150 | + } | |
| 151 | + | |
| 152 | + // save last results | |
| 153 | +		if (count($result) > 0) { | |
| 154 | + $this->selectors[] = $result; | |
| 155 | + } | |
| 156 | + } | |
| 157 | + | |
| 158 | + /** | |
| 159 | + * Attempts to find all children that match the rule | |
| 160 | + * given. | |
| 161 | + * | |
| 162 | + * @param array $nodes | |
| 163 | + * @param array $rule | |
| 164 | + * @param array $options | |
| 165 | + * @return array | |
| 166 | + * @recursive | |
| 167 | + */ | |
| 168 | + protected function seek(array $nodes, array $rule, array $options) | |
| 169 | +	{ | |
| 170 | + // XPath index | |
| 171 | + if (count($rule['tag']) > 0 && | |
| 172 | + count($rule['key']) > 0 && | |
| 173 | + is_numeric($rule['key']) | |
| 174 | +		) { | |
| 175 | + $count = 0; | |
| 176 | + /** @var AbstractNode $node */ | |
| 177 | +			foreach ($nodes as $node) { | |
| 178 | + if ($rule['tag'] == '*' || | |
| 179 | +					$rule['tag'] == $node->getTag()->name()) { | |
| 180 | + ++$count; | |
| 181 | +					if ($count == $rule['key']) { | |
| 182 | + // found the node we wanted | |
| 183 | + return [$node]; | |
| 184 | + } | |
| 185 | + } | |
| 186 | + } | |
| 187 | + | |
| 188 | + return []; | |
| 189 | + } | |
| 190 | + | |
| 191 | + $options = $this->flattenOptions($options); | |
| 192 | + | |
| 193 | + $return = []; | |
| 194 | + /** @var InnerNode $node */ | |
| 195 | +		foreach ($nodes as $node) { | |
| 196 | + // check if we are a leaf | |
| 197 | + if ($node instanceof LeafNode || | |
| 198 | +				 ! $node->hasChildren()) { | |
| 199 | + continue; | |
| 200 | + } | |
| 201 | + | |
| 202 | + $children = []; | |
| 203 | + $child = $node->firstChild(); | |
| 204 | +			while ( ! is_null($child)) { | |
| 205 | + // wild card, grab all | |
| 206 | +				if ($rule['tag'] == '*' && is_null($rule['key'])) { | |
| 207 | + $return[] = $child; | |
| 208 | +					try { | |
| 209 | + $child = $node->nextChild($child->id()); | |
| 210 | +					} catch (ChildNotFoundException $e) { | |
| 211 | + // no more children | |
| 212 | + $child = null; | |
| 213 | + } | |
| 214 | + continue; | |
| 215 | + } | |
| 216 | + | |
| 217 | + $pass = true; | |
| 218 | + // check tag | |
| 219 | + if ( ! empty($rule['tag']) && $rule['tag'] != $child->getTag()->name() && | |
| 220 | + $rule['tag'] != '*' | |
| 221 | +				) { | |
| 222 | + // child failed tag check | |
| 223 | + $pass = false; | |
| 224 | + } | |
| 225 | + | |
| 226 | + // check key | |
| 227 | +				if ($pass && ! is_null($rule['key'])) { | |
| 228 | +					if ($rule['noKey']) { | |
| 229 | +						if ( ! is_null($child->getAttribute($rule['key']))) { | |
| 230 | + $pass = false; | |
| 231 | + } | |
| 232 | +					} else { | |
| 233 | + if ($rule['key'] != 'plaintext' && | |
| 234 | + is_null($child->getAttribute($rule['key'])) | |
| 235 | +						) { | |
| 236 | + $pass = false; | |
| 237 | + } | |
| 238 | + } | |
| 239 | + } | |
| 240 | + | |
| 241 | + // compare values | |
| 242 | + if ($pass && ! is_null($rule['key']) && | |
| 243 | + ! is_null($rule['value']) && $rule['value'] != '*' | |
| 244 | +				) { | |
| 245 | +					if ($rule['key'] == 'plaintext') { | |
| 246 | + // plaintext search | |
| 247 | + $nodeValue = $child->text(); | |
| 248 | +					} else { | |
| 249 | + // normal search | |
| 250 | + $nodeValue = $child->getAttribute($rule['key']); | |
| 251 | + } | |
| 252 | + | |
| 253 | + $check = $this->match($rule['operator'], $rule['value'], $nodeValue); | |
| 254 | + | |
| 255 | + // handle multiple classes | |
| 256 | +					if ( ! $check && $rule['key'] == 'class') { | |
| 257 | +						$childClasses = explode(' ', $child->getAttribute('class')); | |
| 258 | +						foreach ($childClasses as $class) { | |
| 259 | +							if ( ! empty($class)) { | |
| 260 | + $check = $this->match($rule['operator'], $rule['value'], $class); | |
| 261 | + } | |
| 262 | +							if ($check) { | |
| 263 | + break; | |
| 264 | + } | |
| 265 | + } | |
| 266 | + } | |
| 267 | + | |
| 268 | +					if ( ! $check) { | |
| 269 | + $pass = false; | |
| 270 | + } | |
| 271 | + } | |
| 272 | + | |
| 273 | +				if ($pass) { | |
| 274 | + // it passed all checks | |
| 275 | + $return[] = $child; | |
| 276 | +				} else { | |
| 277 | + // this child failed to be matched | |
| 278 | + if ($child instanceof InnerNode && | |
| 279 | +						$child->hasChildren()) { | |
| 280 | + // we still want to check its children | |
| 281 | + $children[] = $child; | |
| 282 | + } | |
| 283 | + } | |
| 284 | + | |
| 285 | +				try { | |
| 286 | + // get next child | |
| 287 | + $child = $node->nextChild($child->id()); | |
| 288 | +				} catch (ChildNotFoundException $e) { | |
| 289 | + // no more children | |
| 290 | + $child = null; | |
| 291 | + } | |
| 292 | + } | |
| 293 | + | |
| 294 | + if (( ! isset($options['checkGrandChildren']) || | |
| 295 | + $options['checkGrandChildren']) | |
| 296 | + && count($children) > 0 | |
| 297 | +			) { | |
| 298 | + // we have children that failed but are not leaves. | |
| 299 | + $matches = $this->seek($children, $rule, $options); | |
| 300 | +				foreach ($matches as $match) { | |
| 301 | + $return[] = $match; | |
| 302 | + } | |
| 303 | + } | |
| 304 | + } | |
| 305 | + | |
| 306 | + return $return; | |
| 307 | + } | |
| 308 | + | |
| 309 | + /** | |
| 310 | + * Attempts to match the given arguments with the given operator. | |
| 311 | + * | |
| 312 | + * @param string $operator | |
| 313 | + * @param string $pattern | |
| 314 | + * @param string $value | |
| 315 | + * @return bool | |
| 316 | + */ | |
| 317 | + protected function match($operator, $pattern, $value) | |
| 318 | +	{ | |
| 319 | + $value = strtolower($value); | |
| 320 | + $pattern = strtolower($pattern); | |
| 321 | +		switch ($operator) { | |
| 322 | + case '=': | |
| 323 | + return $value === $pattern; | |
| 324 | + case '!=': | |
| 325 | + return $value !== $pattern; | |
| 326 | + case '^=': | |
| 327 | +				return preg_match('/^'.preg_quote($pattern, '/').'/', $value); | |
| 328 | + case '$=': | |
| 329 | +				return preg_match('/'.preg_quote($pattern, '/').'$/', $value); | |
| 330 | + case '*=': | |
| 331 | +				if ($pattern[0] == '/') { | |
| 332 | + return preg_match($pattern, $value); | |
| 333 | + } | |
| 334 | + | |
| 335 | +				return preg_match("/".$pattern."/i", $value); | |
| 336 | + } | |
| 337 | + | |
| 338 | + return false; | |
| 339 | + } | |
| 340 | + | |
| 341 | + /** | |
| 342 | + * Attempts to figure out what the alteration will be for | |
| 343 | + * the next element. | |
| 344 | + * | |
| 345 | + * @param array $rule | |
| 346 | + * @return array | |
| 347 | + */ | |
| 348 | + protected function alterNext($rule) | |
| 349 | +	{ | |
| 350 | + $options = []; | |
| 351 | +		if ($rule['tag'] == '>') { | |
| 352 | + $options['checkGrandChildren'] = false; | |
| 353 | + } | |
| 354 | + | |
| 355 | + return $options; | |
| 356 | + } | |
| 357 | + | |
| 358 | + /** | |
| 359 | + * Flattens the option array. | |
| 360 | + * | |
| 361 | + * @param array $optionsArray | |
| 362 | + * @return array | |
| 363 | + */ | |
| 364 | + protected function flattenOptions(array $optionsArray) | |
| 365 | +	{ | |
| 366 | + $options = []; | |
| 367 | +		foreach ($optionsArray as $optionArray) { | |
| 368 | +			foreach ($optionArray as $key => $option) { | |
| 369 | + $options[$key] = $option; | |
| 370 | + } | |
| 371 | + } | |
| 372 | + | |
| 373 | + return $options; | |
| 374 | + } | |
| 375 | 375 | } | 
| @@ -57,7 +57,7 @@ discard block | ||
| 57 | 57 | * new object. | 
| 58 | 58 | * | 
| 59 | 59 | * @param string $str | 
| 60 | - * @return $this | |
| 60 | + * @return Dom | |
| 61 | 61 | */ | 
| 62 | 62 | public static function load($str) | 
| 63 | 63 |      { | 
| @@ -72,7 +72,7 @@ discard block | ||
| 72 | 72 | * new object. | 
| 73 | 73 | * | 
| 74 | 74 | * @param string $file | 
| 75 | - * @return $this | |
| 75 | + * @return Dom | |
| 76 | 76 | */ | 
| 77 | 77 | public static function loadFromFile($file) | 
| 78 | 78 |      { | 
| @@ -88,7 +88,7 @@ discard block | ||
| 88 | 88 | * | 
| 89 | 89 | * @param string $url | 
| 90 | 90 | * @param CurlInterface $curl | 
| 91 | - * @return $this | |
| 91 | + * @return Dom | |
| 92 | 92 | */ | 
| 93 | 93 | public static function loadFromUrl($url, CurlInterface $curl = null) | 
| 94 | 94 |      { | 
| @@ -11,102 +11,102 @@ | ||
| 11 | 11 | final class StaticDom | 
| 12 | 12 |  { | 
| 13 | 13 | |
| 14 | - private static $dom = null; | |
| 14 | + private static $dom = null; | |
| 15 | 15 | |
| 16 | - /** | |
| 17 | - * Attempts to call the given method on the most recent created dom | |
| 18 | - * from bellow. | |
| 19 | - * | |
| 20 | - * @param string $method | |
| 21 | - * @param array $arguments | |
| 22 | - * @throws NotLoadedException | |
| 23 | - * @return mixed | |
| 24 | - */ | |
| 25 | - public static function __callStatic($method, $arguments) | |
| 26 | -    { | |
| 27 | -        if (self::$dom instanceof Dom) { | |
| 28 | - return call_user_func_array([self::$dom, $method], $arguments); | |
| 29 | -        } else { | |
| 30 | -            throw new NotLoadedException('The dom is not loaded. Can not call a dom method.'); | |
| 31 | - } | |
| 32 | - } | |
| 16 | + /** | |
| 17 | + * Attempts to call the given method on the most recent created dom | |
| 18 | + * from bellow. | |
| 19 | + * | |
| 20 | + * @param string $method | |
| 21 | + * @param array $arguments | |
| 22 | + * @throws NotLoadedException | |
| 23 | + * @return mixed | |
| 24 | + */ | |
| 25 | + public static function __callStatic($method, $arguments) | |
| 26 | +	{ | |
| 27 | +		if (self::$dom instanceof Dom) { | |
| 28 | + return call_user_func_array([self::$dom, $method], $arguments); | |
| 29 | +		} else { | |
| 30 | +			throw new NotLoadedException('The dom is not loaded. Can not call a dom method.'); | |
| 31 | + } | |
| 32 | + } | |
| 33 | 33 | |
| 34 | - /** | |
| 35 | - * Call this to mount the static facade. The facade allows you to use | |
| 36 | - * this object as a $className. | |
| 37 | - * | |
| 38 | - * @param string $className | |
| 39 | - * @param Dom $dom | |
| 40 | - * @return bool | |
| 41 | - */ | |
| 42 | - public static function mount($className = 'Dom', Dom $dom = null) | |
| 43 | -    { | |
| 44 | -        if (class_exists($className)) { | |
| 45 | - return false; | |
| 46 | - } | |
| 47 | - class_alias(__CLASS__, $className); | |
| 48 | -        if ($dom instanceof Dom) { | |
| 49 | - self::$dom = $dom; | |
| 50 | - } | |
| 34 | + /** | |
| 35 | + * Call this to mount the static facade. The facade allows you to use | |
| 36 | + * this object as a $className. | |
| 37 | + * | |
| 38 | + * @param string $className | |
| 39 | + * @param Dom $dom | |
| 40 | + * @return bool | |
| 41 | + */ | |
| 42 | + public static function mount($className = 'Dom', Dom $dom = null) | |
| 43 | +	{ | |
| 44 | +		if (class_exists($className)) { | |
| 45 | + return false; | |
| 46 | + } | |
| 47 | + class_alias(__CLASS__, $className); | |
| 48 | +		if ($dom instanceof Dom) { | |
| 49 | + self::$dom = $dom; | |
| 50 | + } | |
| 51 | 51 | |
| 52 | - return true; | |
| 53 | - } | |
| 52 | + return true; | |
| 53 | + } | |
| 54 | 54 | |
| 55 | - /** | |
| 56 | - * Creates a new dom object and calls load() on the | |
| 57 | - * new object. | |
| 58 | - * | |
| 59 | - * @param string $str | |
| 60 | - * @return $this | |
| 61 | - */ | |
| 62 | - public static function load($str) | |
| 63 | -    { | |
| 64 | - $dom = new Dom; | |
| 65 | - self::$dom = $dom; | |
| 55 | + /** | |
| 56 | + * Creates a new dom object and calls load() on the | |
| 57 | + * new object. | |
| 58 | + * | |
| 59 | + * @param string $str | |
| 60 | + * @return $this | |
| 61 | + */ | |
| 62 | + public static function load($str) | |
| 63 | +	{ | |
| 64 | + $dom = new Dom; | |
| 65 | + self::$dom = $dom; | |
| 66 | 66 | |
| 67 | - return $dom->load($str); | |
| 68 | - } | |
| 67 | + return $dom->load($str); | |
| 68 | + } | |
| 69 | 69 | |
| 70 | - /** | |
| 71 | - * Creates a new dom object and calls loadFromFile() on the | |
| 72 | - * new object. | |
| 73 | - * | |
| 74 | - * @param string $file | |
| 75 | - * @return $this | |
| 76 | - */ | |
| 77 | - public static function loadFromFile($file) | |
| 78 | -    { | |
| 79 | - $dom = new Dom; | |
| 80 | - self::$dom = $dom; | |
| 70 | + /** | |
| 71 | + * Creates a new dom object and calls loadFromFile() on the | |
| 72 | + * new object. | |
| 73 | + * | |
| 74 | + * @param string $file | |
| 75 | + * @return $this | |
| 76 | + */ | |
| 77 | + public static function loadFromFile($file) | |
| 78 | +	{ | |
| 79 | + $dom = new Dom; | |
| 80 | + self::$dom = $dom; | |
| 81 | 81 | |
| 82 | - return $dom->loadFromFile($file); | |
| 83 | - } | |
| 82 | + return $dom->loadFromFile($file); | |
| 83 | + } | |
| 84 | 84 | |
| 85 | - /** | |
| 86 | - * Creates a new dom object and calls loadFromUrl() on the | |
| 87 | - * new object. | |
| 88 | - * | |
| 89 | - * @param string $url | |
| 90 | - * @param CurlInterface $curl | |
| 91 | - * @return $this | |
| 92 | - */ | |
| 93 | - public static function loadFromUrl($url, CurlInterface $curl = null) | |
| 94 | -    { | |
| 95 | - $dom = new Dom; | |
| 96 | - self::$dom = $dom; | |
| 97 | -        if (is_null($curl)) { | |
| 98 | - // use the default curl interface | |
| 99 | - $curl = new Curl; | |
| 100 | - } | |
| 85 | + /** | |
| 86 | + * Creates a new dom object and calls loadFromUrl() on the | |
| 87 | + * new object. | |
| 88 | + * | |
| 89 | + * @param string $url | |
| 90 | + * @param CurlInterface $curl | |
| 91 | + * @return $this | |
| 92 | + */ | |
| 93 | + public static function loadFromUrl($url, CurlInterface $curl = null) | |
| 94 | +	{ | |
| 95 | + $dom = new Dom; | |
| 96 | + self::$dom = $dom; | |
| 97 | +		if (is_null($curl)) { | |
| 98 | + // use the default curl interface | |
| 99 | + $curl = new Curl; | |
| 100 | + } | |
| 101 | 101 | |
| 102 | - return $dom->loadFromUrl($url, $curl); | |
| 103 | - } | |
| 102 | + return $dom->loadFromUrl($url, $curl); | |
| 103 | + } | |
| 104 | 104 | |
| 105 | - /** | |
| 106 | - * Sets the $dom variable to null. | |
| 107 | - */ | |
| 108 | - public static function unload() | |
| 109 | -    { | |
| 110 | - self::$dom = null; | |
| 111 | - } | |
| 105 | + /** | |
| 106 | + * Sets the $dom variable to null. | |
| 107 | + */ | |
| 108 | + public static function unload() | |
| 109 | +	{ | |
| 110 | + self::$dom = null; | |
| 111 | + } | |
| 112 | 112 | } | 
| @@ -12,76 +12,76 @@ | ||
| 12 | 12 | class Options | 
| 13 | 13 |  { | 
| 14 | 14 | |
| 15 | - /** | |
| 16 | - * The default options array | |
| 17 | - * | |
| 18 | - * @param array | |
| 19 | - */ | |
| 20 | - protected $defaults = [ | |
| 21 | - 'whitespaceTextNode' => true, | |
| 22 | - 'strict' => false, | |
| 23 | - 'enforceEncoding' => null, | |
| 24 | - 'cleanupInput' => true, | |
| 25 | - 'removeScripts' => true, | |
| 26 | - 'removeStyles' => true, | |
| 27 | - 'preserveLineBreaks' => false, | |
| 28 | - ]; | |
| 15 | + /** | |
| 16 | + * The default options array | |
| 17 | + * | |
| 18 | + * @param array | |
| 19 | + */ | |
| 20 | + protected $defaults = [ | |
| 21 | + 'whitespaceTextNode' => true, | |
| 22 | + 'strict' => false, | |
| 23 | + 'enforceEncoding' => null, | |
| 24 | + 'cleanupInput' => true, | |
| 25 | + 'removeScripts' => true, | |
| 26 | + 'removeStyles' => true, | |
| 27 | + 'preserveLineBreaks' => false, | |
| 28 | + ]; | |
| 29 | 29 | |
| 30 | - /** | |
| 31 | - * The list of all current options set. | |
| 32 | - * | |
| 33 | - * @param array | |
| 34 | - */ | |
| 35 | - protected $options = []; | |
| 30 | + /** | |
| 31 | + * The list of all current options set. | |
| 32 | + * | |
| 33 | + * @param array | |
| 34 | + */ | |
| 35 | + protected $options = []; | |
| 36 | 36 | |
| 37 | - /** | |
| 38 | - * Sets the default options in the options array | |
| 39 | - */ | |
| 40 | - public function __construct() | |
| 41 | -    { | |
| 42 | - $this->options = $this->defaults; | |
| 43 | - } | |
| 37 | + /** | |
| 38 | + * Sets the default options in the options array | |
| 39 | + */ | |
| 40 | + public function __construct() | |
| 41 | +	{ | |
| 42 | + $this->options = $this->defaults; | |
| 43 | + } | |
| 44 | 44 | |
| 45 | - /** | |
| 46 | - * A magic get to call the get() method. | |
| 47 | - * | |
| 48 | - * @param string $key | |
| 49 | - * @return mixed | |
| 50 | - * @uses $this->get() | |
| 51 | - */ | |
| 52 | - public function __get($key) | |
| 53 | -    { | |
| 54 | - return $this->get($key); | |
| 55 | - } | |
| 45 | + /** | |
| 46 | + * A magic get to call the get() method. | |
| 47 | + * | |
| 48 | + * @param string $key | |
| 49 | + * @return mixed | |
| 50 | + * @uses $this->get() | |
| 51 | + */ | |
| 52 | + public function __get($key) | |
| 53 | +	{ | |
| 54 | + return $this->get($key); | |
| 55 | + } | |
| 56 | 56 | |
| 57 | - /** | |
| 58 | - * Sets a new options param to override the current option array. | |
| 59 | - * | |
| 60 | - * @param array $options | |
| 61 | - * @return $this | |
| 62 | - */ | |
| 63 | - public function setOptions(array $options) | |
| 64 | -    { | |
| 65 | -        foreach ($options as $key => $option) { | |
| 66 | - $this->options[$key] = $option; | |
| 67 | - } | |
| 57 | + /** | |
| 58 | + * Sets a new options param to override the current option array. | |
| 59 | + * | |
| 60 | + * @param array $options | |
| 61 | + * @return $this | |
| 62 | + */ | |
| 63 | + public function setOptions(array $options) | |
| 64 | +	{ | |
| 65 | +		foreach ($options as $key => $option) { | |
| 66 | + $this->options[$key] = $option; | |
| 67 | + } | |
| 68 | 68 | |
| 69 | - return $this; | |
| 70 | - } | |
| 69 | + return $this; | |
| 70 | + } | |
| 71 | 71 | |
| 72 | - /** | |
| 73 | - * Gets the value associated to the key, or null if the key is not | |
| 74 | - * found. | |
| 75 | - * | |
| 76 | - * @param string | |
| 77 | - * @return mixed | |
| 78 | - */ | |
| 79 | - public function get($key) | |
| 80 | -    { | |
| 81 | -        if (isset($this->options[$key])) { | |
| 82 | - return $this->options[$key]; | |
| 83 | - } | |
| 72 | + /** | |
| 73 | + * Gets the value associated to the key, or null if the key is not | |
| 74 | + * found. | |
| 75 | + * | |
| 76 | + * @param string | |
| 77 | + * @return mixed | |
| 78 | + */ | |
| 79 | + public function get($key) | |
| 80 | +	{ | |
| 81 | +		if (isset($this->options[$key])) { | |
| 82 | + return $this->options[$key]; | |
| 83 | + } | |
| 84 | 84 | |
| 85 | - return null; | |
| 86 | - } | |
| 85 | + return null; | |
| 86 | + } | |
| 87 | 87 | } | 
| @@ -9,244 +9,244 @@ | ||
| 9 | 9 | class Content | 
| 10 | 10 |  { | 
| 11 | 11 | |
| 12 | - /** | |
| 13 | - * The content string. | |
| 14 | - * | |
| 15 | - * @var string | |
| 16 | - */ | |
| 17 | - protected $content; | |
| 18 | - | |
| 19 | - /** | |
| 20 | - * The size of the content. | |
| 21 | - * | |
| 22 | - * @var integer | |
| 23 | - */ | |
| 24 | - protected $size; | |
| 25 | - | |
| 26 | - /** | |
| 27 | - * The current position we are in the content. | |
| 28 | - * | |
| 29 | - * @var integer | |
| 30 | - */ | |
| 31 | - protected $pos; | |
| 32 | - | |
| 33 | - /** | |
| 34 | - * The following 4 strings are tags that are important to us. | |
| 35 | - * | |
| 36 | - * @var string | |
| 37 | - */ | |
| 38 | - protected $blank = " \t\r\n"; | |
| 39 | - protected $equal = ' =/>'; | |
| 40 | - protected $slash = " />\r\n\t"; | |
| 41 | - protected $attr = ' >'; | |
| 42 | - | |
| 43 | - /** | |
| 44 | - * Content constructor. | |
| 45 | - * | |
| 46 | - * @param $content | |
| 47 | - */ | |
| 48 | - public function __construct($content) | |
| 49 | -    { | |
| 50 | - $this->content = $content; | |
| 51 | - $this->size = strlen($content); | |
| 52 | - $this->pos = 0; | |
| 53 | - } | |
| 54 | - | |
| 55 | - /** | |
| 56 | - * Returns the current position of the content. | |
| 57 | - * | |
| 58 | - * @return int | |
| 59 | - */ | |
| 60 | - public function getPosition() | |
| 61 | -    { | |
| 62 | - return $this->pos; | |
| 63 | - } | |
| 64 | - | |
| 65 | - /** | |
| 66 | - * Gets the current character we are at. | |
| 67 | - * | |
| 68 | - * @param int $char | |
| 69 | - * @return string | |
| 70 | - */ | |
| 71 | - public function char($char = null) | |
| 72 | -    { | |
| 73 | - $pos = $this->pos; | |
| 74 | -        if ( ! is_null($char)) { | |
| 75 | - $pos = $char; | |
| 76 | - } | |
| 77 | - | |
| 78 | -        if ( ! isset($this->content[$pos])) { | |
| 79 | - return ''; | |
| 80 | - } | |
| 81 | - | |
| 82 | - return $this->content[$pos]; | |
| 83 | - } | |
| 84 | - | |
| 85 | - /** | |
| 86 | - * Moves the current position forward. | |
| 87 | - * | |
| 88 | - * @param int $count | |
| 89 | - * @return $this | |
| 90 | - */ | |
| 91 | - public function fastForward($count) | |
| 92 | -    { | |
| 93 | - $this->pos += $count; | |
| 94 | - | |
| 95 | - return $this; | |
| 96 | - } | |
| 97 | - | |
| 98 | - /** | |
| 99 | - * Moves the current position backward. | |
| 100 | - * | |
| 101 | - * @param int $count | |
| 102 | - * @return $this | |
| 103 | - */ | |
| 104 | - public function rewind($count) | |
| 105 | -    { | |
| 106 | - $this->pos -= $count; | |
| 107 | -        if ($this->pos < 0) { | |
| 108 | - $this->pos = 0; | |
| 109 | - } | |
| 110 | - | |
| 111 | - return $this; | |
| 112 | - } | |
| 113 | - | |
| 114 | - /** | |
| 115 | - * Copy the content until we find the given string. | |
| 116 | - * | |
| 117 | - * @param string $string | |
| 118 | - * @param bool $char | |
| 119 | - * @param bool $escape | |
| 120 | - * @return string | |
| 121 | - */ | |
| 122 | - public function copyUntil($string, $char = false, $escape = false) | |
| 123 | -    { | |
| 124 | -        if ($this->pos >= $this->size) { | |
| 125 | - // nothing left | |
| 126 | - return ''; | |
| 127 | - } | |
| 128 | - | |
| 129 | -        if ($escape) { | |
| 130 | - $position = $this->pos; | |
| 131 | - $found = false; | |
| 132 | -            while ( ! $found) { | |
| 133 | - $position = strpos($this->content, $string, $position); | |
| 134 | -                if ($position === false) { | |
| 135 | - // reached the end | |
| 136 | - $found = true; | |
| 137 | - continue; | |
| 138 | - } | |
| 139 | - | |
| 140 | -                if ($this->char($position - 1) == '\\') { | |
| 141 | - // this character is escaped | |
| 142 | - ++$position; | |
| 143 | - continue; | |
| 144 | - } | |
| 145 | - | |
| 146 | - $found = true; | |
| 147 | - } | |
| 148 | -        } elseif ($char) { | |
| 149 | - $position = strcspn($this->content, $string, $this->pos); | |
| 150 | - $position += $this->pos; | |
| 151 | -        } else { | |
| 152 | - $position = strpos($this->content, $string, $this->pos); | |
| 153 | - } | |
| 154 | - | |
| 155 | -        if ($position === false) { | |
| 156 | - // could not find character, just return the remaining of the content | |
| 157 | - $return = substr($this->content, $this->pos, $this->size - $this->pos); | |
| 158 | - $this->pos = $this->size; | |
| 159 | - | |
| 160 | - return $return; | |
| 161 | - } | |
| 162 | - | |
| 163 | -        if ($position == $this->pos) { | |
| 164 | - // we are at the right place | |
| 165 | - return ''; | |
| 166 | - } | |
| 167 | - | |
| 168 | - $return = substr($this->content, $this->pos, $position - $this->pos); | |
| 169 | - // set the new position | |
| 170 | - $this->pos = $position; | |
| 171 | - | |
| 172 | - return $return; | |
| 173 | - } | |
| 174 | - | |
| 175 | - /** | |
| 176 | - * Copies the content until the string is found and return it | |
| 177 | - * unless the 'unless' is found in the substring. | |
| 178 | - * | |
| 179 | - * @param string $string | |
| 180 | - * @param string $unless | |
| 181 | - * @return string | |
| 182 | - */ | |
| 183 | - public function copyUntilUnless($string, $unless) | |
| 184 | -    { | |
| 185 | - $lastPos = $this->pos; | |
| 186 | - $this->fastForward(1); | |
| 187 | - $foundString = $this->copyUntil($string, true, true); | |
| 188 | - | |
| 189 | - $position = strcspn($foundString, $unless); | |
| 190 | -        if ($position == strlen($foundString)) { | |
| 191 | - return $string.$foundString; | |
| 192 | - } | |
| 193 | - // rewind changes and return nothing | |
| 194 | - $this->pos = $lastPos; | |
| 195 | - | |
| 196 | - return ''; | |
| 197 | - } | |
| 198 | - | |
| 199 | - /** | |
| 200 | - * Copies the content until it reaches the token string., | |
| 201 | - * | |
| 202 | - * @param string $token | |
| 203 | - * @param bool $char | |
| 204 | - * @param bool $escape | |
| 205 | - * @return string | |
| 206 | - * @uses $this->copyUntil() | |
| 207 | - */ | |
| 208 | - public function copyByToken($token, $char = false, $escape = false) | |
| 209 | -    { | |
| 210 | - $string = $this->$token; | |
| 211 | - | |
| 212 | - return $this->copyUntil($string, $char, $escape); | |
| 213 | - } | |
| 214 | - | |
| 215 | - /** | |
| 216 | - * Skip a given set of characters. | |
| 217 | - * | |
| 218 | - * @param string $string | |
| 219 | - * @param bool $copy | |
| 220 | - * @return $this|string | |
| 221 | - */ | |
| 222 | - public function skip($string, $copy = false) | |
| 223 | -    { | |
| 224 | - $len = strspn($this->content, $string, $this->pos); | |
| 225 | - | |
| 226 | - // make it chainable if they don't want a copy | |
| 227 | - $return = $this; | |
| 228 | -        if ($copy) { | |
| 229 | - $return = substr($this->content, $this->pos, $len); | |
| 230 | - } | |
| 231 | - | |
| 232 | - // update the position | |
| 233 | - $this->pos += $len; | |
| 234 | - | |
| 235 | - return $return; | |
| 236 | - } | |
| 237 | - | |
| 238 | - /** | |
| 239 | - * Skip a given token of pre-defined characters. | |
| 240 | - * | |
| 241 | - * @param string $token | |
| 242 | - * @param bool $copy | |
| 243 | - * @return null|string | |
| 244 | - * @uses $this->skip() | |
| 245 | - */ | |
| 246 | - public function skipByToken($token, $copy = false) | |
| 247 | -    { | |
| 248 | - $string = $this->$token; | |
| 249 | - | |
| 250 | - return $this->skip($string, $copy); | |
| 251 | - } | |
| 12 | + /** | |
| 13 | + * The content string. | |
| 14 | + * | |
| 15 | + * @var string | |
| 16 | + */ | |
| 17 | + protected $content; | |
| 18 | + | |
| 19 | + /** | |
| 20 | + * The size of the content. | |
| 21 | + * | |
| 22 | + * @var integer | |
| 23 | + */ | |
| 24 | + protected $size; | |
| 25 | + | |
| 26 | + /** | |
| 27 | + * The current position we are in the content. | |
| 28 | + * | |
| 29 | + * @var integer | |
| 30 | + */ | |
| 31 | + protected $pos; | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * The following 4 strings are tags that are important to us. | |
| 35 | + * | |
| 36 | + * @var string | |
| 37 | + */ | |
| 38 | + protected $blank = " \t\r\n"; | |
| 39 | + protected $equal = ' =/>'; | |
| 40 | + protected $slash = " />\r\n\t"; | |
| 41 | + protected $attr = ' >'; | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * Content constructor. | |
| 45 | + * | |
| 46 | + * @param $content | |
| 47 | + */ | |
| 48 | + public function __construct($content) | |
| 49 | +	{ | |
| 50 | + $this->content = $content; | |
| 51 | + $this->size = strlen($content); | |
| 52 | + $this->pos = 0; | |
| 53 | + } | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * Returns the current position of the content. | |
| 57 | + * | |
| 58 | + * @return int | |
| 59 | + */ | |
| 60 | + public function getPosition() | |
| 61 | +	{ | |
| 62 | + return $this->pos; | |
| 63 | + } | |
| 64 | + | |
| 65 | + /** | |
| 66 | + * Gets the current character we are at. | |
| 67 | + * | |
| 68 | + * @param int $char | |
| 69 | + * @return string | |
| 70 | + */ | |
| 71 | + public function char($char = null) | |
| 72 | +	{ | |
| 73 | + $pos = $this->pos; | |
| 74 | +		if ( ! is_null($char)) { | |
| 75 | + $pos = $char; | |
| 76 | + } | |
| 77 | + | |
| 78 | +		if ( ! isset($this->content[$pos])) { | |
| 79 | + return ''; | |
| 80 | + } | |
| 81 | + | |
| 82 | + return $this->content[$pos]; | |
| 83 | + } | |
| 84 | + | |
| 85 | + /** | |
| 86 | + * Moves the current position forward. | |
| 87 | + * | |
| 88 | + * @param int $count | |
| 89 | + * @return $this | |
| 90 | + */ | |
| 91 | + public function fastForward($count) | |
| 92 | +	{ | |
| 93 | + $this->pos += $count; | |
| 94 | + | |
| 95 | + return $this; | |
| 96 | + } | |
| 97 | + | |
| 98 | + /** | |
| 99 | + * Moves the current position backward. | |
| 100 | + * | |
| 101 | + * @param int $count | |
| 102 | + * @return $this | |
| 103 | + */ | |
| 104 | + public function rewind($count) | |
| 105 | +	{ | |
| 106 | + $this->pos -= $count; | |
| 107 | +		if ($this->pos < 0) { | |
| 108 | + $this->pos = 0; | |
| 109 | + } | |
| 110 | + | |
| 111 | + return $this; | |
| 112 | + } | |
| 113 | + | |
| 114 | + /** | |
| 115 | + * Copy the content until we find the given string. | |
| 116 | + * | |
| 117 | + * @param string $string | |
| 118 | + * @param bool $char | |
| 119 | + * @param bool $escape | |
| 120 | + * @return string | |
| 121 | + */ | |
| 122 | + public function copyUntil($string, $char = false, $escape = false) | |
| 123 | +	{ | |
| 124 | +		if ($this->pos >= $this->size) { | |
| 125 | + // nothing left | |
| 126 | + return ''; | |
| 127 | + } | |
| 128 | + | |
| 129 | +		if ($escape) { | |
| 130 | + $position = $this->pos; | |
| 131 | + $found = false; | |
| 132 | +			while ( ! $found) { | |
| 133 | + $position = strpos($this->content, $string, $position); | |
| 134 | +				if ($position === false) { | |
| 135 | + // reached the end | |
| 136 | + $found = true; | |
| 137 | + continue; | |
| 138 | + } | |
| 139 | + | |
| 140 | +				if ($this->char($position - 1) == '\\') { | |
| 141 | + // this character is escaped | |
| 142 | + ++$position; | |
| 143 | + continue; | |
| 144 | + } | |
| 145 | + | |
| 146 | + $found = true; | |
| 147 | + } | |
| 148 | +		} elseif ($char) { | |
| 149 | + $position = strcspn($this->content, $string, $this->pos); | |
| 150 | + $position += $this->pos; | |
| 151 | +		} else { | |
| 152 | + $position = strpos($this->content, $string, $this->pos); | |
| 153 | + } | |
| 154 | + | |
| 155 | +		if ($position === false) { | |
| 156 | + // could not find character, just return the remaining of the content | |
| 157 | + $return = substr($this->content, $this->pos, $this->size - $this->pos); | |
| 158 | + $this->pos = $this->size; | |
| 159 | + | |
| 160 | + return $return; | |
| 161 | + } | |
| 162 | + | |
| 163 | +		if ($position == $this->pos) { | |
| 164 | + // we are at the right place | |
| 165 | + return ''; | |
| 166 | + } | |
| 167 | + | |
| 168 | + $return = substr($this->content, $this->pos, $position - $this->pos); | |
| 169 | + // set the new position | |
| 170 | + $this->pos = $position; | |
| 171 | + | |
| 172 | + return $return; | |
| 173 | + } | |
| 174 | + | |
| 175 | + /** | |
| 176 | + * Copies the content until the string is found and return it | |
| 177 | + * unless the 'unless' is found in the substring. | |
| 178 | + * | |
| 179 | + * @param string $string | |
| 180 | + * @param string $unless | |
| 181 | + * @return string | |
| 182 | + */ | |
| 183 | + public function copyUntilUnless($string, $unless) | |
| 184 | +	{ | |
| 185 | + $lastPos = $this->pos; | |
| 186 | + $this->fastForward(1); | |
| 187 | + $foundString = $this->copyUntil($string, true, true); | |
| 188 | + | |
| 189 | + $position = strcspn($foundString, $unless); | |
| 190 | +		if ($position == strlen($foundString)) { | |
| 191 | + return $string.$foundString; | |
| 192 | + } | |
| 193 | + // rewind changes and return nothing | |
| 194 | + $this->pos = $lastPos; | |
| 195 | + | |
| 196 | + return ''; | |
| 197 | + } | |
| 198 | + | |
| 199 | + /** | |
| 200 | + * Copies the content until it reaches the token string., | |
| 201 | + * | |
| 202 | + * @param string $token | |
| 203 | + * @param bool $char | |
| 204 | + * @param bool $escape | |
| 205 | + * @return string | |
| 206 | + * @uses $this->copyUntil() | |
| 207 | + */ | |
| 208 | + public function copyByToken($token, $char = false, $escape = false) | |
| 209 | +	{ | |
| 210 | + $string = $this->$token; | |
| 211 | + | |
| 212 | + return $this->copyUntil($string, $char, $escape); | |
| 213 | + } | |
| 214 | + | |
| 215 | + /** | |
| 216 | + * Skip a given set of characters. | |
| 217 | + * | |
| 218 | + * @param string $string | |
| 219 | + * @param bool $copy | |
| 220 | + * @return $this|string | |
| 221 | + */ | |
| 222 | + public function skip($string, $copy = false) | |
| 223 | +	{ | |
| 224 | + $len = strspn($this->content, $string, $this->pos); | |
| 225 | + | |
| 226 | + // make it chainable if they don't want a copy | |
| 227 | + $return = $this; | |
| 228 | +		if ($copy) { | |
| 229 | + $return = substr($this->content, $this->pos, $len); | |
| 230 | + } | |
| 231 | + | |
| 232 | + // update the position | |
| 233 | + $this->pos += $len; | |
| 234 | + | |
| 235 | + return $return; | |
| 236 | + } | |
| 237 | + | |
| 238 | + /** | |
| 239 | + * Skip a given token of pre-defined characters. | |
| 240 | + * | |
| 241 | + * @param string $token | |
| 242 | + * @param bool $copy | |
| 243 | + * @return null|string | |
| 244 | + * @uses $this->skip() | |
| 245 | + */ | |
| 246 | + public function skipByToken($token, $copy = false) | |
| 247 | +	{ | |
| 248 | + $string = $this->$token; | |
| 249 | + | |
| 250 | + return $this->skip($string, $copy); | |
| 251 | + } | |
| 252 | 252 | } | 
| @@ -9,11 +9,11 @@ | ||
| 9 | 9 | interface CurlInterface | 
| 10 | 10 |  { | 
| 11 | 11 | |
| 12 | - /** | |
| 13 | - * This method should return the content of the url in a string | |
| 14 | - * | |
| 15 | - * @param string $url | |
| 16 | - * @return string | |
| 17 | - */ | |
| 18 | - public function get($url); | |
| 12 | + /** | |
| 13 | + * This method should return the content of the url in a string | |
| 14 | + * | |
| 15 | + * @param string $url | |
| 16 | + * @return string | |
| 17 | + */ | |
| 18 | + public function get($url); | |
| 19 | 19 | } | 
| @@ -11,31 +11,31 @@ | ||
| 11 | 11 | class Curl implements CurlInterface | 
| 12 | 12 |  { | 
| 13 | 13 | |
| 14 | - /** | |
| 15 | - * A simple curl implementation to get the content of the url. | |
| 16 | - * | |
| 17 | - * @param string $url | |
| 18 | - * @return string | |
| 19 | - * @throws CurlException | |
| 20 | - */ | |
| 21 | - public function get($url) | |
| 22 | -    { | |
| 23 | - $ch = curl_init($url); | |
| 14 | + /** | |
| 15 | + * A simple curl implementation to get the content of the url. | |
| 16 | + * | |
| 17 | + * @param string $url | |
| 18 | + * @return string | |
| 19 | + * @throws CurlException | |
| 20 | + */ | |
| 21 | + public function get($url) | |
| 22 | +	{ | |
| 23 | + $ch = curl_init($url); | |
| 24 | 24 | |
| 25 | -        if ( ! ini_get('open_basedir')) { | |
| 26 | - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | |
| 27 | - } | |
| 25 | +		if ( ! ini_get('open_basedir')) { | |
| 26 | + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | |
| 27 | + } | |
| 28 | 28 | |
| 29 | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
| 30 | - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); | |
| 29 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
| 30 | + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); | |
| 31 | 31 | |
| 32 | - $content = curl_exec($ch); | |
| 33 | -        if ($content === false) { | |
| 34 | - // there was a problem | |
| 35 | - $error = curl_error($ch); | |
| 36 | -            throw new CurlException('Error retrieving "'.$url.'" ('.$error.')'); | |
| 37 | - } | |
| 32 | + $content = curl_exec($ch); | |
| 33 | +		if ($content === false) { | |
| 34 | + // there was a problem | |
| 35 | + $error = curl_error($ch); | |
| 36 | +			throw new CurlException('Error retrieving "'.$url.'" ('.$error.')'); | |
| 37 | + } | |
| 38 | 38 | |
| 39 | - return $content; | |
| 40 | - } | |
| 39 | + return $content; | |
| 40 | + } | |
| 41 | 41 | } | 
| @@ -9,97 +9,97 @@ | ||
| 9 | 9 | class TextNode extends AbstractNode | 
| 10 | 10 |  { | 
| 11 | 11 | |
| 12 | - /** | |
| 13 | - * This is a text node. | |
| 14 | - * | |
| 15 | - * @var Tag | |
| 16 | - */ | |
| 17 | - protected $tag; | |
| 12 | + /** | |
| 13 | + * This is a text node. | |
| 14 | + * | |
| 15 | + * @var Tag | |
| 16 | + */ | |
| 17 | + protected $tag; | |
| 18 | 18 | |
| 19 | - /** | |
| 20 | - * This is the text in this node. | |
| 21 | - * | |
| 22 | - * @var string | |
| 23 | - */ | |
| 24 | - protected $text; | |
| 19 | + /** | |
| 20 | + * This is the text in this node. | |
| 21 | + * | |
| 22 | + * @var string | |
| 23 | + */ | |
| 24 | + protected $text; | |
| 25 | 25 | |
| 26 | - /** | |
| 27 | - * This is the converted version of the text. | |
| 28 | - * | |
| 29 | - * @var string | |
| 30 | - */ | |
| 31 | - protected $convertedText = null; | |
| 26 | + /** | |
| 27 | + * This is the converted version of the text. | |
| 28 | + * | |
| 29 | + * @var string | |
| 30 | + */ | |
| 31 | + protected $convertedText = null; | |
| 32 | 32 | |
| 33 | - /** | |
| 34 | - * Sets the text for this node. | |
| 35 | - * | |
| 36 | - * @param string $text | |
| 37 | - */ | |
| 38 | - public function __construct($text) | |
| 39 | -    { | |
| 40 | - // remove double spaces | |
| 41 | -        $text = mb_ereg_replace('\s+', ' ', $text); | |
| 33 | + /** | |
| 34 | + * Sets the text for this node. | |
| 35 | + * | |
| 36 | + * @param string $text | |
| 37 | + */ | |
| 38 | + public function __construct($text) | |
| 39 | +	{ | |
| 40 | + // remove double spaces | |
| 41 | +		$text = mb_ereg_replace('\s+', ' ', $text); | |
| 42 | 42 | |
| 43 | - // restore line breaks | |
| 44 | -        $text = str_replace('
', "\n", $text); | |
| 43 | + // restore line breaks | |
| 44 | +		$text = str_replace('
', "\n", $text); | |
| 45 | 45 | |
| 46 | - $this->text = $text; | |
| 47 | -        $this->tag  = new Tag('text'); | |
| 48 | - parent::__construct(); | |
| 49 | - } | |
| 46 | + $this->text = $text; | |
| 47 | +		$this->tag  = new Tag('text'); | |
| 48 | + parent::__construct(); | |
| 49 | + } | |
| 50 | 50 | |
| 51 | - /** | |
| 52 | - * Returns the text of this node. | |
| 53 | - * | |
| 54 | - * @return string | |
| 55 | - */ | |
| 56 | - public function text() | |
| 57 | -    { | |
| 58 | - // convert charset | |
| 59 | -        if ( ! is_null($this->encode)) { | |
| 60 | -            if ( ! is_null($this->convertedText)) { | |
| 61 | - // we already know the converted value | |
| 62 | - return $this->convertedText; | |
| 63 | - } | |
| 64 | - $text = $this->encode->convert($this->text); | |
| 51 | + /** | |
| 52 | + * Returns the text of this node. | |
| 53 | + * | |
| 54 | + * @return string | |
| 55 | + */ | |
| 56 | + public function text() | |
| 57 | +	{ | |
| 58 | + // convert charset | |
| 59 | +		if ( ! is_null($this->encode)) { | |
| 60 | +			if ( ! is_null($this->convertedText)) { | |
| 61 | + // we already know the converted value | |
| 62 | + return $this->convertedText; | |
| 63 | + } | |
| 64 | + $text = $this->encode->convert($this->text); | |
| 65 | 65 | |
| 66 | - // remember the conversion | |
| 67 | - $this->convertedText = $text; | |
| 66 | + // remember the conversion | |
| 67 | + $this->convertedText = $text; | |
| 68 | 68 | |
| 69 | - return $text; | |
| 70 | -        } else { | |
| 71 | - return $this->text; | |
| 72 | - } | |
| 73 | - } | |
| 69 | + return $text; | |
| 70 | +		} else { | |
| 71 | + return $this->text; | |
| 72 | + } | |
| 73 | + } | |
| 74 | 74 | |
| 75 | - /** | |
| 76 | - * This node has no html, just return the text. | |
| 77 | - * | |
| 78 | - * @return string | |
| 79 | - * @uses $this->text() | |
| 80 | - */ | |
| 81 | - public function innerHtml() | |
| 82 | -    { | |
| 83 | - return $this->text(); | |
| 84 | - } | |
| 75 | + /** | |
| 76 | + * This node has no html, just return the text. | |
| 77 | + * | |
| 78 | + * @return string | |
| 79 | + * @uses $this->text() | |
| 80 | + */ | |
| 81 | + public function innerHtml() | |
| 82 | +	{ | |
| 83 | + return $this->text(); | |
| 84 | + } | |
| 85 | 85 | |
| 86 | - /** | |
| 87 | - * This node has no html, just return the text. | |
| 88 | - * | |
| 89 | - * @return string | |
| 90 | - * @uses $this->text() | |
| 91 | - */ | |
| 92 | - public function outerHtml() | |
| 93 | -    { | |
| 94 | - return $this->text(); | |
| 95 | - } | |
| 86 | + /** | |
| 87 | + * This node has no html, just return the text. | |
| 88 | + * | |
| 89 | + * @return string | |
| 90 | + * @uses $this->text() | |
| 91 | + */ | |
| 92 | + public function outerHtml() | |
| 93 | +	{ | |
| 94 | + return $this->text(); | |
| 95 | + } | |
| 96 | 96 | |
| 97 | - /** | |
| 98 | - * Call this when something in the node tree has changed. Like a child has been added | |
| 99 | - * or a parent has been changed. | |
| 100 | - */ | |
| 101 | - protected function clear() | |
| 102 | -    { | |
| 103 | - $this->convertedText = null; | |
| 104 | - } | |
| 97 | + /** | |
| 98 | + * Call this when something in the node tree has changed. Like a child has been added | |
| 99 | + * or a parent has been changed. | |
| 100 | + */ | |
| 101 | + protected function clear() | |
| 102 | +	{ | |
| 103 | + $this->convertedText = null; | |
| 104 | + } | |
| 105 | 105 | } | 
| @@ -15,154 +15,154 @@ | ||
| 15 | 15 | class Collection implements IteratorAggregate, ArrayAccess, Countable | 
| 16 | 16 |  { | 
| 17 | 17 | |
| 18 | - /** | |
| 19 | - * The collection of Nodes. | |
| 20 | - * | |
| 21 | - * @param array | |
| 22 | - */ | |
| 23 | - protected $collection = []; | |
| 18 | + /** | |
| 19 | + * The collection of Nodes. | |
| 20 | + * | |
| 21 | + * @param array | |
| 22 | + */ | |
| 23 | + protected $collection = []; | |
| 24 | 24 | |
| 25 | - /** | |
| 26 | - * Attempts to call the method on the first node in | |
| 27 | - * the collection. | |
| 28 | - * | |
| 29 | - * @param string $method | |
| 30 | - * @param array $arguments | |
| 31 | - * @return mixed; | |
| 32 | - * @throws EmptyCollectionException | |
| 33 | - */ | |
| 34 | - public function __call($method, $arguments) | |
| 35 | -    { | |
| 36 | - $node = reset($this->collection); | |
| 37 | -        if ($node instanceof AbstractNode) { | |
| 38 | - return call_user_func_array([$node, $method], $arguments); | |
| 39 | -        } else { | |
| 40 | -            throw new EmptyCollectionException('The collection does not contain any Nodes.'); | |
| 41 | - } | |
| 42 | - } | |
| 25 | + /** | |
| 26 | + * Attempts to call the method on the first node in | |
| 27 | + * the collection. | |
| 28 | + * | |
| 29 | + * @param string $method | |
| 30 | + * @param array $arguments | |
| 31 | + * @return mixed; | |
| 32 | + * @throws EmptyCollectionException | |
| 33 | + */ | |
| 34 | + public function __call($method, $arguments) | |
| 35 | +	{ | |
| 36 | + $node = reset($this->collection); | |
| 37 | +		if ($node instanceof AbstractNode) { | |
| 38 | + return call_user_func_array([$node, $method], $arguments); | |
| 39 | +		} else { | |
| 40 | +			throw new EmptyCollectionException('The collection does not contain any Nodes.'); | |
| 41 | + } | |
| 42 | + } | |
| 43 | 43 | |
| 44 | - /** | |
| 45 | - * Attempts to apply the magic get to the first node | |
| 46 | - * in the collection. | |
| 47 | - * | |
| 48 | - * @param mixed $key | |
| 49 | - * @return mixed | |
| 50 | - * @throws EmptyCollectionException | |
| 51 | - */ | |
| 52 | - public function __get($key) | |
| 53 | -    { | |
| 54 | - $node = reset($this->collection); | |
| 55 | -        if ($node instanceof AbstractNode) { | |
| 56 | - return $node->$key; | |
| 57 | -        } else { | |
| 58 | -            throw new EmptyCollectionException('The collection does not contain any Nodes.'); | |
| 59 | - } | |
| 60 | - } | |
| 44 | + /** | |
| 45 | + * Attempts to apply the magic get to the first node | |
| 46 | + * in the collection. | |
| 47 | + * | |
| 48 | + * @param mixed $key | |
| 49 | + * @return mixed | |
| 50 | + * @throws EmptyCollectionException | |
| 51 | + */ | |
| 52 | + public function __get($key) | |
| 53 | +	{ | |
| 54 | + $node = reset($this->collection); | |
| 55 | +		if ($node instanceof AbstractNode) { | |
| 56 | + return $node->$key; | |
| 57 | +		} else { | |
| 58 | +			throw new EmptyCollectionException('The collection does not contain any Nodes.'); | |
| 59 | + } | |
| 60 | + } | |
| 61 | 61 | |
| 62 | - /** | |
| 63 | - * Applies the magic string method to the first node in | |
| 64 | - * the collection. | |
| 65 | - * | |
| 66 | - * @return string | |
| 67 | - * @throws EmptyCollectionException | |
| 68 | - */ | |
| 69 | - public function __toString() | |
| 70 | -    { | |
| 71 | - $node = reset($this->collection); | |
| 72 | -        if ($node instanceof AbstractNode) { | |
| 73 | - return (string)$node; | |
| 74 | -        } else { | |
| 75 | -            throw new EmptyCollectionException('The collection does not contain any Nodes.'); | |
| 76 | - } | |
| 77 | - } | |
| 62 | + /** | |
| 63 | + * Applies the magic string method to the first node in | |
| 64 | + * the collection. | |
| 65 | + * | |
| 66 | + * @return string | |
| 67 | + * @throws EmptyCollectionException | |
| 68 | + */ | |
| 69 | + public function __toString() | |
| 70 | +	{ | |
| 71 | + $node = reset($this->collection); | |
| 72 | +		if ($node instanceof AbstractNode) { | |
| 73 | + return (string)$node; | |
| 74 | +		} else { | |
| 75 | +			throw new EmptyCollectionException('The collection does not contain any Nodes.'); | |
| 76 | + } | |
| 77 | + } | |
| 78 | 78 | |
| 79 | - /** | |
| 80 | - * Returns the count of the collection. | |
| 81 | - * | |
| 82 | - * @return int | |
| 83 | - */ | |
| 84 | - public function count() | |
| 85 | -    { | |
| 86 | - return count($this->collection); | |
| 87 | - } | |
| 79 | + /** | |
| 80 | + * Returns the count of the collection. | |
| 81 | + * | |
| 82 | + * @return int | |
| 83 | + */ | |
| 84 | + public function count() | |
| 85 | +	{ | |
| 86 | + return count($this->collection); | |
| 87 | + } | |
| 88 | 88 | |
| 89 | - /** | |
| 90 | - * Returns an iterator for the collection. | |
| 91 | - * | |
| 92 | - * @return ArrayIterator | |
| 93 | - */ | |
| 94 | - public function getIterator() | |
| 95 | -    { | |
| 96 | - return new ArrayIterator($this->collection); | |
| 97 | - } | |
| 89 | + /** | |
| 90 | + * Returns an iterator for the collection. | |
| 91 | + * | |
| 92 | + * @return ArrayIterator | |
| 93 | + */ | |
| 94 | + public function getIterator() | |
| 95 | +	{ | |
| 96 | + return new ArrayIterator($this->collection); | |
| 97 | + } | |
| 98 | 98 | |
| 99 | - /** | |
| 100 | - * Set an attribute by the given offset | |
| 101 | - * | |
| 102 | - * @param mixed $offset | |
| 103 | - * @param mixed $value | |
| 104 | - */ | |
| 105 | - public function offsetSet($offset, $value) | |
| 106 | -    { | |
| 107 | -        if (is_null($offset)) { | |
| 108 | - $this->collection[] = $value; | |
| 109 | -        } else { | |
| 110 | - $this->collection[$offset] = $value; | |
| 111 | - } | |
| 112 | - } | |
| 99 | + /** | |
| 100 | + * Set an attribute by the given offset | |
| 101 | + * | |
| 102 | + * @param mixed $offset | |
| 103 | + * @param mixed $value | |
| 104 | + */ | |
| 105 | + public function offsetSet($offset, $value) | |
| 106 | +	{ | |
| 107 | +		if (is_null($offset)) { | |
| 108 | + $this->collection[] = $value; | |
| 109 | +		} else { | |
| 110 | + $this->collection[$offset] = $value; | |
| 111 | + } | |
| 112 | + } | |
| 113 | 113 | |
| 114 | - /** | |
| 115 | - * Checks if an offset exists. | |
| 116 | - * | |
| 117 | - * @param mixed $offset | |
| 118 | - * @return bool | |
| 119 | - */ | |
| 120 | - public function offsetExists($offset) | |
| 121 | -    { | |
| 122 | - return isset($this->collection[$offset]); | |
| 123 | - } | |
| 114 | + /** | |
| 115 | + * Checks if an offset exists. | |
| 116 | + * | |
| 117 | + * @param mixed $offset | |
| 118 | + * @return bool | |
| 119 | + */ | |
| 120 | + public function offsetExists($offset) | |
| 121 | +	{ | |
| 122 | + return isset($this->collection[$offset]); | |
| 123 | + } | |
| 124 | 124 | |
| 125 | - /** | |
| 126 | - * Unset a collection Node. | |
| 127 | - * | |
| 128 | - * @param mixed $offset | |
| 129 | - */ | |
| 130 | - public function offsetUnset($offset) | |
| 131 | -    { | |
| 132 | - unset($this->collection[$offset]); | |
| 133 | - } | |
| 125 | + /** | |
| 126 | + * Unset a collection Node. | |
| 127 | + * | |
| 128 | + * @param mixed $offset | |
| 129 | + */ | |
| 130 | + public function offsetUnset($offset) | |
| 131 | +	{ | |
| 132 | + unset($this->collection[$offset]); | |
| 133 | + } | |
| 134 | 134 | |
| 135 | - /** | |
| 136 | - * Gets a node at the given offset, or null | |
| 137 | - * | |
| 138 | - * @param mixed $offset | |
| 139 | - * @return mixed | |
| 140 | - */ | |
| 141 | - public function offsetGet($offset) | |
| 142 | -    { | |
| 143 | - return isset($this->collection[$offset]) ? $this->collection[$offset] : null; | |
| 144 | - } | |
| 135 | + /** | |
| 136 | + * Gets a node at the given offset, or null | |
| 137 | + * | |
| 138 | + * @param mixed $offset | |
| 139 | + * @return mixed | |
| 140 | + */ | |
| 141 | + public function offsetGet($offset) | |
| 142 | +	{ | |
| 143 | + return isset($this->collection[$offset]) ? $this->collection[$offset] : null; | |
| 144 | + } | |
| 145 | 145 | |
| 146 | - /** | |
| 147 | - * Returns this collection as an array. | |
| 148 | - * | |
| 149 | - * @return array | |
| 150 | - */ | |
| 151 | - public function toArray() | |
| 152 | -    { | |
| 153 | - return $this->collection; | |
| 154 | - } | |
| 146 | + /** | |
| 147 | + * Returns this collection as an array. | |
| 148 | + * | |
| 149 | + * @return array | |
| 150 | + */ | |
| 151 | + public function toArray() | |
| 152 | +	{ | |
| 153 | + return $this->collection; | |
| 154 | + } | |
| 155 | 155 | |
| 156 | - /** | |
| 157 | - * Similar to jQuery "each" method. Calls the callback with each | |
| 158 | - * Node in this collection. | |
| 159 | - * | |
| 160 | - * @param callback $callback | |
| 161 | - */ | |
| 162 | - public function each($callback) | |
| 163 | -    { | |
| 164 | -        foreach ($this->collection as $key => $value) { | |
| 165 | - $callback($value, $key); | |
| 166 | - } | |
| 167 | - } | |
| 156 | + /** | |
| 157 | + * Similar to jQuery "each" method. Calls the callback with each | |
| 158 | + * Node in this collection. | |
| 159 | + * | |
| 160 | + * @param callback $callback | |
| 161 | + */ | |
| 162 | + public function each($callback) | |
| 163 | +	{ | |
| 164 | +		foreach ($this->collection as $key => $value) { | |
| 165 | + $callback($value, $key); | |
| 166 | + } | |
| 167 | + } | |
| 168 | 168 | } | 
| @@ -12,189 +12,189 @@ | ||
| 12 | 12 | class HtmlNode extends ArrayNode | 
| 13 | 13 |  { | 
| 14 | 14 | |
| 15 | - /** | |
| 16 | - * Remembers what the innerHtml was if it was scanned previously. | |
| 17 | - */ | |
| 18 | - protected $innerHtml = null; | |
| 19 | - | |
| 20 | - /** | |
| 21 | - * Remembers what the outerHtml was if it was scanned previously. | |
| 22 | - * | |
| 23 | - * @var string | |
| 24 | - */ | |
| 25 | - protected $outerHtml = null; | |
| 26 | - | |
| 27 | - /** | |
| 28 | - * Remembers what the text was if it was scanned previously. | |
| 29 | - * | |
| 30 | - * @var string | |
| 31 | - */ | |
| 32 | - protected $text = null; | |
| 33 | - | |
| 34 | - /** | |
| 35 | - * Remembers what the text was when we looked into all our | |
| 36 | - * children nodes. | |
| 37 | - * | |
| 38 | - * @var string | |
| 39 | - */ | |
| 40 | - protected $textWithChildren = null; | |
| 41 | - | |
| 42 | - /** | |
| 43 | - * Sets up the tag of this node. | |
| 44 | - * | |
| 45 | - * @param $tag | |
| 46 | - */ | |
| 47 | - public function __construct($tag) | |
| 48 | -    { | |
| 49 | -        if ( ! $tag instanceof Tag) { | |
| 50 | - $tag = new Tag($tag); | |
| 51 | - } | |
| 52 | - $this->tag = $tag; | |
| 53 | - parent::__construct(); | |
| 54 | - } | |
| 55 | - | |
| 56 | - /** | |
| 57 | - * Gets the inner html of this node. | |
| 58 | - * | |
| 59 | - * @return string | |
| 60 | - * @throws UnknownChildTypeException | |
| 61 | - */ | |
| 62 | - public function innerHtml() | |
| 63 | -    { | |
| 64 | -        if ( ! $this->hasChildren()) { | |
| 65 | - // no children | |
| 66 | - return ''; | |
| 67 | - } | |
| 68 | - | |
| 69 | -        if ( ! is_null($this->innerHtml)) { | |
| 70 | - // we already know the result. | |
| 71 | - return $this->innerHtml; | |
| 72 | - } | |
| 73 | - | |
| 74 | - $child = $this->firstChild(); | |
| 75 | - $string = ''; | |
| 76 | - | |
| 77 | - // continue to loop until we are out of children | |
| 78 | -        while ( ! is_null($child)) { | |
| 79 | -            if ($child instanceof TextNode) { | |
| 80 | - $string .= $child->text(); | |
| 81 | -            } elseif ($child instanceof HtmlNode) { | |
| 82 | - $string .= $child->outerHtml(); | |
| 83 | -            } else { | |
| 84 | -                throw new UnknownChildTypeException('Unknown child type "'.get_class($child).'" found in node'); | |
| 85 | - } | |
| 86 | - | |
| 87 | -            try { | |
| 88 | - $child = $this->nextChild($child->id()); | |
| 89 | -            } catch (ChildNotFoundException $e) { | |
| 90 | - // no more children | |
| 91 | - $child = null; | |
| 92 | - } | |
| 93 | - } | |
| 94 | - | |
| 95 | - // remember the results | |
| 96 | - $this->innerHtml = $string; | |
| 97 | - | |
| 98 | - return $string; | |
| 99 | - } | |
| 100 | - | |
| 101 | - /** | |
| 102 | - * Gets the html of this node, including it's own | |
| 103 | - * tag. | |
| 104 | - * | |
| 105 | - * @return string | |
| 106 | - */ | |
| 107 | - public function outerHtml() | |
| 108 | -    { | |
| 109 | - // special handling for root | |
| 110 | -        if ($this->tag->name() == 'root') { | |
| 111 | - return $this->innerHtml(); | |
| 112 | - } | |
| 113 | - | |
| 114 | -        if ( ! is_null($this->outerHtml)) { | |
| 115 | - // we already know the results. | |
| 116 | - return $this->outerHtml; | |
| 117 | - } | |
| 118 | - | |
| 119 | - $return = $this->tag->makeOpeningTag(); | |
| 120 | -        if ($this->tag->isSelfClosing()) { | |
| 121 | - // ignore any children... there should not be any though | |
| 122 | - return $return; | |
| 123 | - } | |
| 124 | - | |
| 125 | - // get the inner html | |
| 126 | - $return .= $this->innerHtml(); | |
| 127 | - | |
| 128 | - // add closing tag | |
| 129 | - $return .= $this->tag->makeClosingTag(); | |
| 130 | - | |
| 131 | - // remember the results | |
| 132 | - $this->outerHtml = $return; | |
| 133 | - | |
| 134 | - return $return; | |
| 135 | - } | |
| 136 | - | |
| 137 | - /** | |
| 138 | - * Gets the text of this node (if there is any text). Or get all the text | |
| 139 | - * in this node, including children. | |
| 140 | - * | |
| 141 | - * @param bool $lookInChildren | |
| 142 | - * @return string | |
| 143 | - */ | |
| 144 | - public function text($lookInChildren = false) | |
| 145 | -    { | |
| 146 | -        if ($lookInChildren) { | |
| 147 | -            if ( ! is_null($this->textWithChildren)) { | |
| 148 | - // we already know the results. | |
| 149 | - return $this->textWithChildren; | |
| 150 | - } | |
| 151 | -        } elseif ( ! is_null($this->text)) { | |
| 152 | - // we already know the results. | |
| 153 | - return $this->text; | |
| 154 | - } | |
| 155 | - | |
| 156 | - // find out if this node has any text children | |
| 157 | - $text = ''; | |
| 158 | -        foreach ($this->children as $child) { | |
| 159 | - /** @var AbstractNode $node */ | |
| 160 | - $node = $child['node']; | |
| 161 | -            if ($node instanceof TextNode) { | |
| 162 | - $text .= $child['node']->text; | |
| 163 | - } elseif ($lookInChildren && | |
| 164 | - $node instanceof HtmlNode | |
| 165 | -            ) { | |
| 166 | - $text .= $node->text($lookInChildren); | |
| 167 | - } | |
| 168 | - } | |
| 169 | - | |
| 170 | - // remember our result | |
| 171 | -        if ($lookInChildren) { | |
| 172 | - $this->textWithChildren = $text; | |
| 173 | -        } else { | |
| 174 | - $this->text = $text; | |
| 175 | - } | |
| 176 | - | |
| 177 | - return $text; | |
| 178 | - } | |
| 179 | - | |
| 180 | - /** | |
| 181 | - * Call this when something in the node tree has changed. Like a child has been added | |
| 182 | - * or a parent has been changed. | |
| 183 | - */ | |
| 184 | - protected function clear() | |
| 185 | -    { | |
| 186 | - $this->innerHtml = null; | |
| 187 | - $this->outerHtml = null; | |
| 188 | - $this->text = null; | |
| 189 | - } | |
| 190 | - | |
| 191 | - /** | |
| 192 | - * Returns all children of this html node. | |
| 193 | - * | |
| 194 | - * @return array | |
| 195 | - */ | |
| 196 | - protected function getIteratorArray() | |
| 197 | -    { | |
| 198 | - return $this->getChildren(); | |
| 199 | - } | |
| 15 | + /** | |
| 16 | + * Remembers what the innerHtml was if it was scanned previously. | |
| 17 | + */ | |
| 18 | + protected $innerHtml = null; | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * Remembers what the outerHtml was if it was scanned previously. | |
| 22 | + * | |
| 23 | + * @var string | |
| 24 | + */ | |
| 25 | + protected $outerHtml = null; | |
| 26 | + | |
| 27 | + /** | |
| 28 | + * Remembers what the text was if it was scanned previously. | |
| 29 | + * | |
| 30 | + * @var string | |
| 31 | + */ | |
| 32 | + protected $text = null; | |
| 33 | + | |
| 34 | + /** | |
| 35 | + * Remembers what the text was when we looked into all our | |
| 36 | + * children nodes. | |
| 37 | + * | |
| 38 | + * @var string | |
| 39 | + */ | |
| 40 | + protected $textWithChildren = null; | |
| 41 | + | |
| 42 | + /** | |
| 43 | + * Sets up the tag of this node. | |
| 44 | + * | |
| 45 | + * @param $tag | |
| 46 | + */ | |
| 47 | + public function __construct($tag) | |
| 48 | +	{ | |
| 49 | +		if ( ! $tag instanceof Tag) { | |
| 50 | + $tag = new Tag($tag); | |
| 51 | + } | |
| 52 | + $this->tag = $tag; | |
| 53 | + parent::__construct(); | |
| 54 | + } | |
| 55 | + | |
| 56 | + /** | |
| 57 | + * Gets the inner html of this node. | |
| 58 | + * | |
| 59 | + * @return string | |
| 60 | + * @throws UnknownChildTypeException | |
| 61 | + */ | |
| 62 | + public function innerHtml() | |
| 63 | +	{ | |
| 64 | +		if ( ! $this->hasChildren()) { | |
| 65 | + // no children | |
| 66 | + return ''; | |
| 67 | + } | |
| 68 | + | |
| 69 | +		if ( ! is_null($this->innerHtml)) { | |
| 70 | + // we already know the result. | |
| 71 | + return $this->innerHtml; | |
| 72 | + } | |
| 73 | + | |
| 74 | + $child = $this->firstChild(); | |
| 75 | + $string = ''; | |
| 76 | + | |
| 77 | + // continue to loop until we are out of children | |
| 78 | +		while ( ! is_null($child)) { | |
| 79 | +			if ($child instanceof TextNode) { | |
| 80 | + $string .= $child->text(); | |
| 81 | +			} elseif ($child instanceof HtmlNode) { | |
| 82 | + $string .= $child->outerHtml(); | |
| 83 | +			} else { | |
| 84 | +				throw new UnknownChildTypeException('Unknown child type "'.get_class($child).'" found in node'); | |
| 85 | + } | |
| 86 | + | |
| 87 | +			try { | |
| 88 | + $child = $this->nextChild($child->id()); | |
| 89 | +			} catch (ChildNotFoundException $e) { | |
| 90 | + // no more children | |
| 91 | + $child = null; | |
| 92 | + } | |
| 93 | + } | |
| 94 | + | |
| 95 | + // remember the results | |
| 96 | + $this->innerHtml = $string; | |
| 97 | + | |
| 98 | + return $string; | |
| 99 | + } | |
| 100 | + | |
| 101 | + /** | |
| 102 | + * Gets the html of this node, including it's own | |
| 103 | + * tag. | |
| 104 | + * | |
| 105 | + * @return string | |
| 106 | + */ | |
| 107 | + public function outerHtml() | |
| 108 | +	{ | |
| 109 | + // special handling for root | |
| 110 | +		if ($this->tag->name() == 'root') { | |
| 111 | + return $this->innerHtml(); | |
| 112 | + } | |
| 113 | + | |
| 114 | +		if ( ! is_null($this->outerHtml)) { | |
| 115 | + // we already know the results. | |
| 116 | + return $this->outerHtml; | |
| 117 | + } | |
| 118 | + | |
| 119 | + $return = $this->tag->makeOpeningTag(); | |
| 120 | +		if ($this->tag->isSelfClosing()) { | |
| 121 | + // ignore any children... there should not be any though | |
| 122 | + return $return; | |
| 123 | + } | |
| 124 | + | |
| 125 | + // get the inner html | |
| 126 | + $return .= $this->innerHtml(); | |
| 127 | + | |
| 128 | + // add closing tag | |
| 129 | + $return .= $this->tag->makeClosingTag(); | |
| 130 | + | |
| 131 | + // remember the results | |
| 132 | + $this->outerHtml = $return; | |
| 133 | + | |
| 134 | + return $return; | |
| 135 | + } | |
| 136 | + | |
| 137 | + /** | |
| 138 | + * Gets the text of this node (if there is any text). Or get all the text | |
| 139 | + * in this node, including children. | |
| 140 | + * | |
| 141 | + * @param bool $lookInChildren | |
| 142 | + * @return string | |
| 143 | + */ | |
| 144 | + public function text($lookInChildren = false) | |
| 145 | +	{ | |
| 146 | +		if ($lookInChildren) { | |
| 147 | +			if ( ! is_null($this->textWithChildren)) { | |
| 148 | + // we already know the results. | |
| 149 | + return $this->textWithChildren; | |
| 150 | + } | |
| 151 | +		} elseif ( ! is_null($this->text)) { | |
| 152 | + // we already know the results. | |
| 153 | + return $this->text; | |
| 154 | + } | |
| 155 | + | |
| 156 | + // find out if this node has any text children | |
| 157 | + $text = ''; | |
| 158 | +		foreach ($this->children as $child) { | |
| 159 | + /** @var AbstractNode $node */ | |
| 160 | + $node = $child['node']; | |
| 161 | +			if ($node instanceof TextNode) { | |
| 162 | + $text .= $child['node']->text; | |
| 163 | + } elseif ($lookInChildren && | |
| 164 | + $node instanceof HtmlNode | |
| 165 | +			) { | |
| 166 | + $text .= $node->text($lookInChildren); | |
| 167 | + } | |
| 168 | + } | |
| 169 | + | |
| 170 | + // remember our result | |
| 171 | +		if ($lookInChildren) { | |
| 172 | + $this->textWithChildren = $text; | |
| 173 | +		} else { | |
| 174 | + $this->text = $text; | |
| 175 | + } | |
| 176 | + | |
| 177 | + return $text; | |
| 178 | + } | |
| 179 | + | |
| 180 | + /** | |
| 181 | + * Call this when something in the node tree has changed. Like a child has been added | |
| 182 | + * or a parent has been changed. | |
| 183 | + */ | |
| 184 | + protected function clear() | |
| 185 | +	{ | |
| 186 | + $this->innerHtml = null; | |
| 187 | + $this->outerHtml = null; | |
| 188 | + $this->text = null; | |
| 189 | + } | |
| 190 | + | |
| 191 | + /** | |
| 192 | + * Returns all children of this html node. | |
| 193 | + * | |
| 194 | + * @return array | |
| 195 | + */ | |
| 196 | + protected function getIteratorArray() | |
| 197 | +	{ | |
| 198 | + return $this->getChildren(); | |
| 199 | + } | |
| 200 | 200 | } |