Completed
Push — master ( bddea9...637f46 )
by Gilles
03:02
created
src/PHPHtmlParser/Options.php 1 patch
Indentation   +65 added lines, -65 removed lines patch added patch discarded remove patch
@@ -12,76 +12,76 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
src/PHPHtmlParser/Content.php 1 patch
Indentation   +240 added lines, -240 removed lines patch added patch discarded remove patch
@@ -9,244 +9,244 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
src/PHPHtmlParser/CurlInterface.php 1 patch
Indentation   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -9,11 +9,11 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
src/PHPHtmlParser/Curl.php 1 patch
Indentation   +23 added lines, -23 removed lines patch added patch discarded remove patch
@@ -11,31 +11,31 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
src/PHPHtmlParser/Dom/Collection.php 1 patch
Indentation   +139 added lines, -139 removed lines patch added patch discarded remove patch
@@ -15,154 +15,154 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
src/PHPHtmlParser/Dom/HtmlNode.php 1 patch
Indentation   +185 added lines, -185 removed lines patch added patch discarded remove patch
@@ -12,189 +12,189 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
src/PHPHtmlParser/Dom/ArrayNode.php 1 patch
Indentation   +24 added lines, -24 removed lines patch added patch discarded remove patch
@@ -12,30 +12,30 @@
 block discarded – undo
12 12
 abstract class ArrayNode extends AbstractNode implements IteratorAggregate, Countable
13 13
 {
14 14
 
15
-    /**
16
-     * Gets the iterator
17
-     *
18
-     * @return ArrayIterator
19
-     */
20
-    public function getIterator()
21
-    {
22
-        return new ArrayIterator($this->getIteratorArray());
23
-    }
15
+	/**
16
+	 * Gets the iterator
17
+	 *
18
+	 * @return ArrayIterator
19
+	 */
20
+	public function getIterator()
21
+	{
22
+		return new ArrayIterator($this->getIteratorArray());
23
+	}
24 24
 
25
-    /**
26
-     * Returns the count of the iterator array.
27
-     *
28
-     * @return int
29
-     */
30
-    public function count()
31
-    {
32
-        return count($this->getIteratorArray());
33
-    }
25
+	/**
26
+	 * Returns the count of the iterator array.
27
+	 *
28
+	 * @return int
29
+	 */
30
+	public function count()
31
+	{
32
+		return count($this->getIteratorArray());
33
+	}
34 34
 
35
-    /**
36
-     * Returns the array to be used the the iterator.
37
-     *
38
-     * @return array
39
-     */
40
-    abstract protected function getIteratorArray();
35
+	/**
36
+	 * Returns the array to be used the the iterator.
37
+	 *
38
+	 * @return array
39
+	 */
40
+	abstract protected function getIteratorArray();
41 41
 }
Please login to merge, or discard this patch.
src/PHPHtmlParser/Dom/MockNode.php 1 patch
Indentation   +36 added lines, -36 removed lines patch added patch discarded remove patch
@@ -11,44 +11,44 @@
 block discarded – undo
11 11
 class MockNode extends InnerNode
12 12
 {
13 13
 
14
-    /**
15
-     * Mock of innner html.
16
-     */
17
-    public function innerHtml()
18
-    {
19
-    }
14
+	/**
15
+	 * Mock of innner html.
16
+	 */
17
+	public function innerHtml()
18
+	{
19
+	}
20 20
 
21
-    /**
22
-     * Mock of outer html.
23
-     */
24
-    public function outerHtml()
25
-    {
26
-    }
21
+	/**
22
+	 * Mock of outer html.
23
+	 */
24
+	public function outerHtml()
25
+	{
26
+	}
27 27
 
28
-    /**
29
-     * Mock of text.
30
-     */
31
-    public function text()
32
-    {
33
-    }
28
+	/**
29
+	 * Mock of text.
30
+	 */
31
+	public function text()
32
+	{
33
+	}
34 34
 
35
-    /**
36
-     * Clear content of this node
37
-     */
38
-    protected function clear()
39
-    {
40
-        $this->innerHtml = null;
41
-        $this->outerHtml = null;
42
-        $this->text      = null;
43
-    }
35
+	/**
36
+	 * Clear content of this node
37
+	 */
38
+	protected function clear()
39
+	{
40
+		$this->innerHtml = null;
41
+		$this->outerHtml = null;
42
+		$this->text      = null;
43
+	}
44 44
 
45
-    /**
46
-     * Returns all children of this html node.
47
-     *
48
-     * @return array
49
-     */
50
-    protected function getIteratorArray()
51
-    {
52
-        return $this->getChildren();
53
-    }
45
+	/**
46
+	 * Returns all children of this html node.
47
+	 *
48
+	 * @return array
49
+	 */
50
+	protected function getIteratorArray()
51
+	{
52
+		return $this->getChildren();
53
+	}
54 54
 }
Please login to merge, or discard this patch.
src/PHPHtmlParser/Dom.php 1 patch
Indentation   +629 added lines, -629 removed lines patch added patch discarded remove patch
@@ -16,633 +16,633 @@
 block discarded – undo
16 16
 class Dom
17 17
 {
18 18
 
19
-    /**
20
-     * The charset we would like the output to be in.
21
-     *
22
-     * @var string
23
-     */
24
-    protected $defaultCharset = 'UTF-8';
25
-
26
-    /**
27
-     * Contains the root node of this dom tree.
28
-     *
29
-     * @var HtmlNode
30
-     */
31
-    public $root;
32
-
33
-    /**
34
-     * The raw version of the document string.
35
-     *
36
-     * @var string
37
-     */
38
-    protected $raw;
39
-
40
-    /**
41
-     * The document string.
42
-     *
43
-     * @var Content
44
-     */
45
-    protected $content = null;
46
-
47
-    /**
48
-     * The original file size of the document.
49
-     *
50
-     * @var int
51
-     */
52
-    protected $rawSize;
53
-
54
-    /**
55
-     * The size of the document after it is cleaned.
56
-     *
57
-     * @var int
58
-     */
59
-    protected $size;
60
-
61
-    /**
62
-     * A global options array to be used by all load calls.
63
-     *
64
-     * @var array
65
-     */
66
-    protected $globalOptions = [];
67
-
68
-    /**
69
-     * A persistent option object to be used for all options in the
70
-     * parsing of the file.
71
-     *
72
-     * @var Options
73
-     */
74
-    protected $options;
75
-
76
-    /**
77
-     * A list of tags which will always be self closing
78
-     *
79
-     * @var array
80
-     */
81
-    protected $selfClosing = [
82
-        'img',
83
-        'br',
84
-        'input',
85
-        'meta',
86
-        'link',
87
-        'hr',
88
-        'base',
89
-        'embed',
90
-        'spacer',
91
-    ];
92
-
93
-    /**
94
-     * Returns the inner html of the root node.
95
-     *
96
-     * @return string
97
-     */
98
-    public function __toString()
99
-    {
100
-        return $this->root->innerHtml();
101
-    }
102
-
103
-    /**
104
-     * A simple wrapper around the root node.
105
-     *
106
-     * @param string $name
107
-     * @return mixed
108
-     */
109
-    public function __get($name)
110
-    {
111
-        return $this->root->$name;
112
-    }
113
-
114
-    /**
115
-     * Attempts to load the dom from any resource, string, file, or URL.
116
-     *
117
-     * @param string $str
118
-     * @param array $options
119
-     * @return $this
120
-     */
121
-    public function load($str, $options = [])
122
-    {
123
-        // check if it's a file
124
-        if (strpos($str, "\n") === false && is_file($str)) {
125
-            return $this->loadFromFile($str, $options);
126
-        }
127
-        // check if it's a url
128
-        if (preg_match("/^https?:\/\//i", $str)) {
129
-            return $this->loadFromUrl($str, $options);
130
-        }
131
-
132
-        return $this->loadStr($str, $options);
133
-    }
134
-
135
-    /**
136
-     * Loads the dom from a document file/url
137
-     *
138
-     * @param string $file
139
-     * @param array $options
140
-     * @return $this
141
-     */
142
-    public function loadFromFile($file, $options = [])
143
-    {
144
-        return $this->loadStr(file_get_contents($file), $options);
145
-    }
146
-
147
-    /**
148
-     * Use a curl interface implementation to attempt to load
149
-     * the content from a url.
150
-     *
151
-     * @param string $url
152
-     * @param array $options
153
-     * @param CurlInterface $curl
154
-     * @return $this
155
-     */
156
-    public function loadFromUrl($url, $options = [], CurlInterface $curl = null)
157
-    {
158
-        if (is_null($curl)) {
159
-            // use the default curl interface
160
-            $curl = new Curl;
161
-        }
162
-        $content = $curl->get($url);
163
-
164
-        return $this->loadStr($content, $options);
165
-    }
166
-
167
-    /**
168
-     * Parsers the html of the given string. Used for load(), loadFromFile(),
169
-     * and loadFromUrl().
170
-     *
171
-     * @param string $str
172
-     * @param array $option
173
-     * @return $this
174
-     */
175
-    public function loadStr($str, $option)
176
-    {
177
-        $this->options = new Options;
178
-        $this->options->setOptions($this->globalOptions)
179
-                      ->setOptions($option);
180
-
181
-        $this->rawSize = strlen($str);
182
-        $this->raw     = $str;
183
-
184
-        $html = $this->clean($str);
185
-
186
-        $this->size    = strlen($str);
187
-        $this->content = new Content($html);
188
-
189
-        $this->parse();
190
-        $this->detectCharset();
191
-
192
-        return $this;
193
-    }
194
-
195
-    /**
196
-     * Sets a global options array to be used by all load calls.
197
-     *
198
-     * @param array $options
199
-     * @return $this
200
-     */
201
-    public function setOptions(array $options)
202
-    {
203
-        $this->globalOptions = $options;
204
-
205
-        return $this;
206
-    }
207
-
208
-    /**
209
-     * Find elements by css selector on the root node.
210
-     *
211
-     * @param string $selector
212
-     * @param int $nth
213
-     * @return array
214
-     */
215
-    public function find($selector, $nth = null)
216
-    {
217
-        $this->isLoaded();
218
-
219
-        return $this->root->find($selector, $nth);
220
-    }
221
-
222
-    /**
223
-     * Adds the tag (or tags in an array) to the list of tags that will always
224
-     * be self closing.
225
-     *
226
-     * @param string|array $tag
227
-     * @return $this
228
-     */
229
-    public function addSelfClosingTag($tag)
230
-    {
231
-        if ( ! is_array($tag)) {
232
-            $tag = [$tag];
233
-        }
234
-        foreach ($tag as $value) {
235
-            $this->selfClosing[] = $value;
236
-        }
237
-
238
-        return $this;
239
-    }
240
-
241
-    /**
242
-     * Removes the tag (or tags in an array) from the list of tags that will
243
-     * always be self closing.
244
-     *
245
-     * @param string|array $tag
246
-     * @return $this
247
-     */
248
-    public function removeSelfClosingTag($tag)
249
-    {
250
-        if ( ! is_array($tag)) {
251
-            $tag = [$tag];
252
-        }
253
-        $this->selfClosing = array_diff($this->selfClosing, $tag);
254
-
255
-        return $this;
256
-    }
257
-
258
-    /**
259
-     * Sets the list of self closing tags to empty.
260
-     *
261
-     * @return $this
262
-     */
263
-    public function clearSelfClosingTags()
264
-    {
265
-        $this->selfClosing = [];
266
-
267
-        return $this;
268
-    }
269
-
270
-    /**
271
-     * Simple wrapper function that returns the first child.
272
-     *
273
-     * @return \PHPHtmlParser\Dom\AbstractNode
274
-     */
275
-    public function firstChild()
276
-    {
277
-        $this->isLoaded();
278
-
279
-        return $this->root->firstChild();
280
-    }
281
-
282
-    /**
283
-     * Simple wrapper function that returns the last child.
284
-     *
285
-     * @return \PHPHtmlParser\Dom\AbstractNode
286
-     */
287
-    public function lastChild()
288
-    {
289
-        $this->isLoaded();
290
-
291
-        return $this->root->lastChild();
292
-    }
293
-
294
-    /**
295
-     * Simple wrapper function that returns an element by the
296
-     * id.
297
-     *
298
-     * @param string $id
299
-     * @return \PHPHtmlParser\Dom\AbstractNode
300
-     */
301
-    public function getElementById($id)
302
-    {
303
-        $this->isLoaded();
304
-
305
-        return $this->find('#'.$id, 0);
306
-    }
307
-
308
-    /**
309
-     * Simple wrapper function that returns all elements by
310
-     * tag name.
311
-     *
312
-     * @param string $name
313
-     * @return array
314
-     */
315
-    public function getElementsByTag($name)
316
-    {
317
-        $this->isLoaded();
318
-
319
-        return $this->find($name);
320
-    }
321
-
322
-    /**
323
-     * Simple wrapper function that returns all elements by
324
-     * class name.
325
-     *
326
-     * @param string $class
327
-     * @return array
328
-     */
329
-    public function getElementsByClass($class)
330
-    {
331
-        $this->isLoaded();
332
-
333
-        return $this->find('.'.$class);
334
-    }
335
-
336
-    /**
337
-     * Checks if the load methods have been called.
338
-     *
339
-     * @throws NotLoadedException
340
-     */
341
-    protected function isLoaded()
342
-    {
343
-        if (is_null($this->content)) {
344
-            throw new NotLoadedException('Content is not loaded!');
345
-        }
346
-    }
347
-
348
-    /**
349
-     * Cleans the html of any none-html information.
350
-     *
351
-     * @param string $str
352
-     * @return string
353
-     */
354
-    protected function clean($str)
355
-    {
356
-        if ($this->options->get('cleanupInput') != true) {
357
-            // skip entire cleanup step
358
-            return $str;
359
-        }
360
-
361
-        // remove white space before closing tags
362
-        $str = mb_eregi_replace("'\s+>", "'>", $str);
363
-        $str = mb_eregi_replace('"\s+>', '">', $str);
364
-
365
-        // clean out the \n\r
366
-        $replace = ' ';
367
-        if ($this->options->get('preserveLineBreaks')) {
368
-            $replace = '&#10;';
369
-        }
370
-        $str = str_replace(["\r\n", "\r", "\n"], $replace, $str);
371
-
372
-        // strip the doctype
373
-        $str = mb_eregi_replace("<!doctype(.*?)>", '', $str);
374
-
375
-        // strip out comments
376
-        $str = mb_eregi_replace("<!--(.*?)-->", '', $str);
377
-
378
-        // strip out cdata
379
-        $str = mb_eregi_replace("<!\[CDATA\[(.*?)\]\]>", '', $str);
380
-
381
-        // strip out <script> tags
382
-        if ($this->options->get('removeScripts') == true) {
383
-            $str = mb_eregi_replace("<\s*script[^>]*[^/]>(.*?)<\s*/\s*script\s*>", '', $str);
384
-            $str = mb_eregi_replace("<\s*script\s*>(.*?)<\s*/\s*script\s*>", '', $str);
385
-        }
386
-
387
-        // strip out <style> tags
388
-        if ($this->options->get('removeStyles') == true) {
389
-            $str = mb_eregi_replace("<\s*style[^>]*[^/]>(.*?)<\s*/\s*style\s*>", '', $str);
390
-            $str = mb_eregi_replace("<\s*style\s*>(.*?)<\s*/\s*style\s*>", '', $str);
391
-        }
392
-
393
-        // strip out server side scripts
394
-        $str = mb_eregi_replace("(<\?)(.*?)(\?>)", '', $str);
395
-
396
-        // strip smarty scripts
397
-        $str = mb_eregi_replace("(\{\w)(.*?)(\})", '', $str);
398
-
399
-        return $str;
400
-    }
401
-
402
-    /**
403
-     * Attempts to parse the html in content.
404
-     */
405
-    protected function parse()
406
-    {
407
-        // add the root node
408
-        $this->root = new HtmlNode('root');
409
-        $activeNode = $this->root;
410
-        while ( ! is_null($activeNode)) {
411
-            $str = $this->content->copyUntil('<');
412
-            if ($str == '') {
413
-                $info = $this->parseTag();
414
-                if ( ! $info['status']) {
415
-                    // we are done here
416
-                    $activeNode = null;
417
-                    continue;
418
-                }
419
-
420
-                // check if it was a closing tag
421
-                if ($info['closing']) {
422
-                    $originalNode = $activeNode;
423
-                    while ($activeNode->getTag()->name() != $info['tag']) {
424
-                        $activeNode = $activeNode->getParent();
425
-                        if (is_null($activeNode)) {
426
-                            // we could not find opening tag
427
-                            $activeNode = $originalNode;
428
-                            break;
429
-                        }
430
-                    }
431
-                    if ( ! is_null($activeNode)) {
432
-                        $activeNode = $activeNode->getParent();
433
-                    }
434
-                    continue;
435
-                }
436
-
437
-                if ( ! isset($info['node'])) {
438
-                    continue;
439
-                }
440
-
441
-                /** @var AbstractNode $node */
442
-                $node = $info['node'];
443
-                $activeNode->addChild($node);
444
-
445
-                // check if node is self closing
446
-                if ( ! $node->getTag()->isSelfClosing()) {
447
-                    $activeNode = $node;
448
-                }
449
-            } else if ($this->options->whitespaceTextNode ||
450
-                trim($str) != ''
451
-            ) {
452
-                // we found text we care about
453
-                $textNode = new TextNode($str);
454
-                $activeNode->addChild($textNode);
455
-            }
456
-        }
457
-    }
458
-
459
-    /**
460
-     * Attempt to parse a tag out of the content.
461
-     *
462
-     * @return array
463
-     * @throws StrictException
464
-     */
465
-    protected function parseTag()
466
-    {
467
-        $return = [
468
-            'status'  => false,
469
-            'closing' => false,
470
-            'node'    => null,
471
-        ];
472
-        if ($this->content->char() != '<') {
473
-            // we are not at the beginning of a tag
474
-            return $return;
475
-        }
476
-
477
-        // check if this is a closing tag
478
-        if ($this->content->fastForward(1)->char() == '/') {
479
-            // end tag
480
-            $tag = $this->content->fastForward(1)
481
-                                 ->copyByToken('slash', true);
482
-            // move to end of tag
483
-            $this->content->copyUntil('>');
484
-            $this->content->fastForward(1);
485
-
486
-            // check if this closing tag counts
487
-            $tag = strtolower($tag);
488
-            if (in_array($tag, $this->selfClosing)) {
489
-                $return['status'] = true;
490
-
491
-                return $return;
492
-            } else {
493
-                $return['status']  = true;
494
-                $return['closing'] = true;
495
-                $return['tag']     = strtolower($tag);
496
-            }
497
-
498
-            return $return;
499
-        }
500
-
501
-        $tag  = strtolower($this->content->copyByToken('slash', true));
502
-        $node = new HtmlNode($tag);
503
-
504
-        // attributes
505
-        while ($this->content->char() != '>' &&
506
-            $this->content->char() != '/') {
507
-            $space = $this->content->skipByToken('blank', true);
508
-            if (empty($space)) {
509
-                $this->content->fastForward(1);
510
-                continue;
511
-            }
512
-
513
-            $name = $this->content->copyByToken('equal', true);
514
-            if ($name == '/') {
515
-                break;
516
-            }
517
-
518
-            if (empty($name)) {
519
-                $this->content->fastForward(1);
520
-                continue;
521
-            }
522
-
523
-            $this->content->skipByToken('blank');
524
-            if ($this->content->char() == '=') {
525
-                $attr = [];
526
-                $this->content->fastForward(1)
527
-                              ->skipByToken('blank');
528
-                switch ($this->content->char()) {
529
-                    case '"':
530
-                        $attr['doubleQuote'] = true;
531
-                        $this->content->fastForward(1);
532
-                        $string = $this->content->copyUntil('"', true, true);
533
-                        do {
534
-                            $moreString = $this->content->copyUntilUnless('"', '=>');
535
-                            $string .= $moreString;
536
-                        } while ( ! empty($moreString));
537
-                        $attr['value'] = $string;
538
-                        $this->content->fastForward(1);
539
-                        $node->getTag()->$name = $attr;
540
-                        break;
541
-                    case "'":
542
-                        $attr['doubleQuote'] = false;
543
-                        $this->content->fastForward(1);
544
-                        $string = $this->content->copyUntil("'", true, true);
545
-                        do {
546
-                            $moreString = $this->content->copyUntilUnless("'", '=>');
547
-                            $string .= $moreString;
548
-                        } while ( ! empty($moreString));
549
-                        $attr['value'] = $string;
550
-                        $this->content->fastForward(1);
551
-                        $node->getTag()->$name = $attr;
552
-                        break;
553
-                    default:
554
-                        $attr['doubleQuote']   = true;
555
-                        $attr['value']         = $this->content->copyByToken('attr', true);
556
-                        $node->getTag()->$name = $attr;
557
-                        break;
558
-                }
559
-            } else {
560
-                // no value attribute
561
-                if ($this->options->strict) {
562
-                    // can't have this in strict html
563
-                    $character = $this->content->getPosition();
564
-                    throw new StrictException("Tag '$tag' has an attribute '$name' with out a value! (character #$character)");
565
-                }
566
-                $node->getTag()->$name = [
567
-                    'value'       => null,
568
-                    'doubleQuote' => true,
569
-                ];
570
-                if ($this->content->char() != '>') {
571
-                    $this->content->rewind(1);
572
-                }
573
-            }
574
-        }
575
-
576
-        $this->content->skipByToken('blank');
577
-        if ($this->content->char() == '/') {
578
-            // self closing tag
579
-            $node->getTag()->selfClosing();
580
-            $this->content->fastForward(1);
581
-        } elseif (in_array($tag, $this->selfClosing)) {
582
-
583
-            // Should be a self closing tag, check if we are strict
584
-            if ($this->options->strict) {
585
-                $character = $this->content->getPosition();
586
-                throw new StrictException("Tag '$tag' is not self closing! (character #$character)");
587
-            }
588
-
589
-            // We force self closing on this tag.
590
-            $node->getTag()->selfClosing();
591
-        }
592
-
593
-        $this->content->fastForward(1);
594
-
595
-        $return['status'] = true;
596
-        $return['node']   = $node;
597
-
598
-        return $return;
599
-    }
600
-
601
-    /**
602
-     * Attempts to detect the charset that the html was sent in.
603
-     *
604
-     * @return bool
605
-     */
606
-    protected function detectCharset()
607
-    {
608
-        // set the default
609
-        $encode = new Encode;
610
-        $encode->from($this->defaultCharset);
611
-        $encode->to($this->defaultCharset);
612
-
613
-        if ( ! is_null($this->options->enforceEncoding)) {
614
-            //  they want to enforce the given encoding
615
-            $encode->from($this->options->enforceEncoding);
616
-            $encode->to($this->options->enforceEncoding);
617
-
618
-            return false;
619
-        }
620
-
621
-        $meta = $this->root->find('meta[http-equiv=Content-Type]', 0);
622
-        if (is_null($meta)) {
623
-            // could not find meta tag
624
-            $this->root->propagateEncoding($encode);
625
-
626
-            return false;
627
-        }
628
-        $content = $meta->content;
629
-        if (empty($content)) {
630
-            // could not find content
631
-            $this->root->propagateEncoding($encode);
632
-
633
-            return false;
634
-        }
635
-        $matches = [];
636
-        if (preg_match('/charset=(.+)/', $content, $matches)) {
637
-            $encode->from(trim($matches[1]));
638
-            $this->root->propagateEncoding($encode);
639
-
640
-            return true;
641
-        }
642
-
643
-        // no charset found
644
-        $this->root->propagateEncoding($encode);
645
-
646
-        return false;
647
-    }
19
+	/**
20
+	 * The charset we would like the output to be in.
21
+	 *
22
+	 * @var string
23
+	 */
24
+	protected $defaultCharset = 'UTF-8';
25
+
26
+	/**
27
+	 * Contains the root node of this dom tree.
28
+	 *
29
+	 * @var HtmlNode
30
+	 */
31
+	public $root;
32
+
33
+	/**
34
+	 * The raw version of the document string.
35
+	 *
36
+	 * @var string
37
+	 */
38
+	protected $raw;
39
+
40
+	/**
41
+	 * The document string.
42
+	 *
43
+	 * @var Content
44
+	 */
45
+	protected $content = null;
46
+
47
+	/**
48
+	 * The original file size of the document.
49
+	 *
50
+	 * @var int
51
+	 */
52
+	protected $rawSize;
53
+
54
+	/**
55
+	 * The size of the document after it is cleaned.
56
+	 *
57
+	 * @var int
58
+	 */
59
+	protected $size;
60
+
61
+	/**
62
+	 * A global options array to be used by all load calls.
63
+	 *
64
+	 * @var array
65
+	 */
66
+	protected $globalOptions = [];
67
+
68
+	/**
69
+	 * A persistent option object to be used for all options in the
70
+	 * parsing of the file.
71
+	 *
72
+	 * @var Options
73
+	 */
74
+	protected $options;
75
+
76
+	/**
77
+	 * A list of tags which will always be self closing
78
+	 *
79
+	 * @var array
80
+	 */
81
+	protected $selfClosing = [
82
+		'img',
83
+		'br',
84
+		'input',
85
+		'meta',
86
+		'link',
87
+		'hr',
88
+		'base',
89
+		'embed',
90
+		'spacer',
91
+	];
92
+
93
+	/**
94
+	 * Returns the inner html of the root node.
95
+	 *
96
+	 * @return string
97
+	 */
98
+	public function __toString()
99
+	{
100
+		return $this->root->innerHtml();
101
+	}
102
+
103
+	/**
104
+	 * A simple wrapper around the root node.
105
+	 *
106
+	 * @param string $name
107
+	 * @return mixed
108
+	 */
109
+	public function __get($name)
110
+	{
111
+		return $this->root->$name;
112
+	}
113
+
114
+	/**
115
+	 * Attempts to load the dom from any resource, string, file, or URL.
116
+	 *
117
+	 * @param string $str
118
+	 * @param array $options
119
+	 * @return $this
120
+	 */
121
+	public function load($str, $options = [])
122
+	{
123
+		// check if it's a file
124
+		if (strpos($str, "\n") === false && is_file($str)) {
125
+			return $this->loadFromFile($str, $options);
126
+		}
127
+		// check if it's a url
128
+		if (preg_match("/^https?:\/\//i", $str)) {
129
+			return $this->loadFromUrl($str, $options);
130
+		}
131
+
132
+		return $this->loadStr($str, $options);
133
+	}
134
+
135
+	/**
136
+	 * Loads the dom from a document file/url
137
+	 *
138
+	 * @param string $file
139
+	 * @param array $options
140
+	 * @return $this
141
+	 */
142
+	public function loadFromFile($file, $options = [])
143
+	{
144
+		return $this->loadStr(file_get_contents($file), $options);
145
+	}
146
+
147
+	/**
148
+	 * Use a curl interface implementation to attempt to load
149
+	 * the content from a url.
150
+	 *
151
+	 * @param string $url
152
+	 * @param array $options
153
+	 * @param CurlInterface $curl
154
+	 * @return $this
155
+	 */
156
+	public function loadFromUrl($url, $options = [], CurlInterface $curl = null)
157
+	{
158
+		if (is_null($curl)) {
159
+			// use the default curl interface
160
+			$curl = new Curl;
161
+		}
162
+		$content = $curl->get($url);
163
+
164
+		return $this->loadStr($content, $options);
165
+	}
166
+
167
+	/**
168
+	 * Parsers the html of the given string. Used for load(), loadFromFile(),
169
+	 * and loadFromUrl().
170
+	 *
171
+	 * @param string $str
172
+	 * @param array $option
173
+	 * @return $this
174
+	 */
175
+	public function loadStr($str, $option)
176
+	{
177
+		$this->options = new Options;
178
+		$this->options->setOptions($this->globalOptions)
179
+					  ->setOptions($option);
180
+
181
+		$this->rawSize = strlen($str);
182
+		$this->raw     = $str;
183
+
184
+		$html = $this->clean($str);
185
+
186
+		$this->size    = strlen($str);
187
+		$this->content = new Content($html);
188
+
189
+		$this->parse();
190
+		$this->detectCharset();
191
+
192
+		return $this;
193
+	}
194
+
195
+	/**
196
+	 * Sets a global options array to be used by all load calls.
197
+	 *
198
+	 * @param array $options
199
+	 * @return $this
200
+	 */
201
+	public function setOptions(array $options)
202
+	{
203
+		$this->globalOptions = $options;
204
+
205
+		return $this;
206
+	}
207
+
208
+	/**
209
+	 * Find elements by css selector on the root node.
210
+	 *
211
+	 * @param string $selector
212
+	 * @param int $nth
213
+	 * @return array
214
+	 */
215
+	public function find($selector, $nth = null)
216
+	{
217
+		$this->isLoaded();
218
+
219
+		return $this->root->find($selector, $nth);
220
+	}
221
+
222
+	/**
223
+	 * Adds the tag (or tags in an array) to the list of tags that will always
224
+	 * be self closing.
225
+	 *
226
+	 * @param string|array $tag
227
+	 * @return $this
228
+	 */
229
+	public function addSelfClosingTag($tag)
230
+	{
231
+		if ( ! is_array($tag)) {
232
+			$tag = [$tag];
233
+		}
234
+		foreach ($tag as $value) {
235
+			$this->selfClosing[] = $value;
236
+		}
237
+
238
+		return $this;
239
+	}
240
+
241
+	/**
242
+	 * Removes the tag (or tags in an array) from the list of tags that will
243
+	 * always be self closing.
244
+	 *
245
+	 * @param string|array $tag
246
+	 * @return $this
247
+	 */
248
+	public function removeSelfClosingTag($tag)
249
+	{
250
+		if ( ! is_array($tag)) {
251
+			$tag = [$tag];
252
+		}
253
+		$this->selfClosing = array_diff($this->selfClosing, $tag);
254
+
255
+		return $this;
256
+	}
257
+
258
+	/**
259
+	 * Sets the list of self closing tags to empty.
260
+	 *
261
+	 * @return $this
262
+	 */
263
+	public function clearSelfClosingTags()
264
+	{
265
+		$this->selfClosing = [];
266
+
267
+		return $this;
268
+	}
269
+
270
+	/**
271
+	 * Simple wrapper function that returns the first child.
272
+	 *
273
+	 * @return \PHPHtmlParser\Dom\AbstractNode
274
+	 */
275
+	public function firstChild()
276
+	{
277
+		$this->isLoaded();
278
+
279
+		return $this->root->firstChild();
280
+	}
281
+
282
+	/**
283
+	 * Simple wrapper function that returns the last child.
284
+	 *
285
+	 * @return \PHPHtmlParser\Dom\AbstractNode
286
+	 */
287
+	public function lastChild()
288
+	{
289
+		$this->isLoaded();
290
+
291
+		return $this->root->lastChild();
292
+	}
293
+
294
+	/**
295
+	 * Simple wrapper function that returns an element by the
296
+	 * id.
297
+	 *
298
+	 * @param string $id
299
+	 * @return \PHPHtmlParser\Dom\AbstractNode
300
+	 */
301
+	public function getElementById($id)
302
+	{
303
+		$this->isLoaded();
304
+
305
+		return $this->find('#'.$id, 0);
306
+	}
307
+
308
+	/**
309
+	 * Simple wrapper function that returns all elements by
310
+	 * tag name.
311
+	 *
312
+	 * @param string $name
313
+	 * @return array
314
+	 */
315
+	public function getElementsByTag($name)
316
+	{
317
+		$this->isLoaded();
318
+
319
+		return $this->find($name);
320
+	}
321
+
322
+	/**
323
+	 * Simple wrapper function that returns all elements by
324
+	 * class name.
325
+	 *
326
+	 * @param string $class
327
+	 * @return array
328
+	 */
329
+	public function getElementsByClass($class)
330
+	{
331
+		$this->isLoaded();
332
+
333
+		return $this->find('.'.$class);
334
+	}
335
+
336
+	/**
337
+	 * Checks if the load methods have been called.
338
+	 *
339
+	 * @throws NotLoadedException
340
+	 */
341
+	protected function isLoaded()
342
+	{
343
+		if (is_null($this->content)) {
344
+			throw new NotLoadedException('Content is not loaded!');
345
+		}
346
+	}
347
+
348
+	/**
349
+	 * Cleans the html of any none-html information.
350
+	 *
351
+	 * @param string $str
352
+	 * @return string
353
+	 */
354
+	protected function clean($str)
355
+	{
356
+		if ($this->options->get('cleanupInput') != true) {
357
+			// skip entire cleanup step
358
+			return $str;
359
+		}
360
+
361
+		// remove white space before closing tags
362
+		$str = mb_eregi_replace("'\s+>", "'>", $str);
363
+		$str = mb_eregi_replace('"\s+>', '">', $str);
364
+
365
+		// clean out the \n\r
366
+		$replace = ' ';
367
+		if ($this->options->get('preserveLineBreaks')) {
368
+			$replace = '&#10;';
369
+		}
370
+		$str = str_replace(["\r\n", "\r", "\n"], $replace, $str);
371
+
372
+		// strip the doctype
373
+		$str = mb_eregi_replace("<!doctype(.*?)>", '', $str);
374
+
375
+		// strip out comments
376
+		$str = mb_eregi_replace("<!--(.*?)-->", '', $str);
377
+
378
+		// strip out cdata
379
+		$str = mb_eregi_replace("<!\[CDATA\[(.*?)\]\]>", '', $str);
380
+
381
+		// strip out <script> tags
382
+		if ($this->options->get('removeScripts') == true) {
383
+			$str = mb_eregi_replace("<\s*script[^>]*[^/]>(.*?)<\s*/\s*script\s*>", '', $str);
384
+			$str = mb_eregi_replace("<\s*script\s*>(.*?)<\s*/\s*script\s*>", '', $str);
385
+		}
386
+
387
+		// strip out <style> tags
388
+		if ($this->options->get('removeStyles') == true) {
389
+			$str = mb_eregi_replace("<\s*style[^>]*[^/]>(.*?)<\s*/\s*style\s*>", '', $str);
390
+			$str = mb_eregi_replace("<\s*style\s*>(.*?)<\s*/\s*style\s*>", '', $str);
391
+		}
392
+
393
+		// strip out server side scripts
394
+		$str = mb_eregi_replace("(<\?)(.*?)(\?>)", '', $str);
395
+
396
+		// strip smarty scripts
397
+		$str = mb_eregi_replace("(\{\w)(.*?)(\})", '', $str);
398
+
399
+		return $str;
400
+	}
401
+
402
+	/**
403
+	 * Attempts to parse the html in content.
404
+	 */
405
+	protected function parse()
406
+	{
407
+		// add the root node
408
+		$this->root = new HtmlNode('root');
409
+		$activeNode = $this->root;
410
+		while ( ! is_null($activeNode)) {
411
+			$str = $this->content->copyUntil('<');
412
+			if ($str == '') {
413
+				$info = $this->parseTag();
414
+				if ( ! $info['status']) {
415
+					// we are done here
416
+					$activeNode = null;
417
+					continue;
418
+				}
419
+
420
+				// check if it was a closing tag
421
+				if ($info['closing']) {
422
+					$originalNode = $activeNode;
423
+					while ($activeNode->getTag()->name() != $info['tag']) {
424
+						$activeNode = $activeNode->getParent();
425
+						if (is_null($activeNode)) {
426
+							// we could not find opening tag
427
+							$activeNode = $originalNode;
428
+							break;
429
+						}
430
+					}
431
+					if ( ! is_null($activeNode)) {
432
+						$activeNode = $activeNode->getParent();
433
+					}
434
+					continue;
435
+				}
436
+
437
+				if ( ! isset($info['node'])) {
438
+					continue;
439
+				}
440
+
441
+				/** @var AbstractNode $node */
442
+				$node = $info['node'];
443
+				$activeNode->addChild($node);
444
+
445
+				// check if node is self closing
446
+				if ( ! $node->getTag()->isSelfClosing()) {
447
+					$activeNode = $node;
448
+				}
449
+			} else if ($this->options->whitespaceTextNode ||
450
+				trim($str) != ''
451
+			) {
452
+				// we found text we care about
453
+				$textNode = new TextNode($str);
454
+				$activeNode->addChild($textNode);
455
+			}
456
+		}
457
+	}
458
+
459
+	/**
460
+	 * Attempt to parse a tag out of the content.
461
+	 *
462
+	 * @return array
463
+	 * @throws StrictException
464
+	 */
465
+	protected function parseTag()
466
+	{
467
+		$return = [
468
+			'status'  => false,
469
+			'closing' => false,
470
+			'node'    => null,
471
+		];
472
+		if ($this->content->char() != '<') {
473
+			// we are not at the beginning of a tag
474
+			return $return;
475
+		}
476
+
477
+		// check if this is a closing tag
478
+		if ($this->content->fastForward(1)->char() == '/') {
479
+			// end tag
480
+			$tag = $this->content->fastForward(1)
481
+								 ->copyByToken('slash', true);
482
+			// move to end of tag
483
+			$this->content->copyUntil('>');
484
+			$this->content->fastForward(1);
485
+
486
+			// check if this closing tag counts
487
+			$tag = strtolower($tag);
488
+			if (in_array($tag, $this->selfClosing)) {
489
+				$return['status'] = true;
490
+
491
+				return $return;
492
+			} else {
493
+				$return['status']  = true;
494
+				$return['closing'] = true;
495
+				$return['tag']     = strtolower($tag);
496
+			}
497
+
498
+			return $return;
499
+		}
500
+
501
+		$tag  = strtolower($this->content->copyByToken('slash', true));
502
+		$node = new HtmlNode($tag);
503
+
504
+		// attributes
505
+		while ($this->content->char() != '>' &&
506
+			$this->content->char() != '/') {
507
+			$space = $this->content->skipByToken('blank', true);
508
+			if (empty($space)) {
509
+				$this->content->fastForward(1);
510
+				continue;
511
+			}
512
+
513
+			$name = $this->content->copyByToken('equal', true);
514
+			if ($name == '/') {
515
+				break;
516
+			}
517
+
518
+			if (empty($name)) {
519
+				$this->content->fastForward(1);
520
+				continue;
521
+			}
522
+
523
+			$this->content->skipByToken('blank');
524
+			if ($this->content->char() == '=') {
525
+				$attr = [];
526
+				$this->content->fastForward(1)
527
+							  ->skipByToken('blank');
528
+				switch ($this->content->char()) {
529
+					case '"':
530
+						$attr['doubleQuote'] = true;
531
+						$this->content->fastForward(1);
532
+						$string = $this->content->copyUntil('"', true, true);
533
+						do {
534
+							$moreString = $this->content->copyUntilUnless('"', '=>');
535
+							$string .= $moreString;
536
+						} while ( ! empty($moreString));
537
+						$attr['value'] = $string;
538
+						$this->content->fastForward(1);
539
+						$node->getTag()->$name = $attr;
540
+						break;
541
+					case "'":
542
+						$attr['doubleQuote'] = false;
543
+						$this->content->fastForward(1);
544
+						$string = $this->content->copyUntil("'", true, true);
545
+						do {
546
+							$moreString = $this->content->copyUntilUnless("'", '=>');
547
+							$string .= $moreString;
548
+						} while ( ! empty($moreString));
549
+						$attr['value'] = $string;
550
+						$this->content->fastForward(1);
551
+						$node->getTag()->$name = $attr;
552
+						break;
553
+					default:
554
+						$attr['doubleQuote']   = true;
555
+						$attr['value']         = $this->content->copyByToken('attr', true);
556
+						$node->getTag()->$name = $attr;
557
+						break;
558
+				}
559
+			} else {
560
+				// no value attribute
561
+				if ($this->options->strict) {
562
+					// can't have this in strict html
563
+					$character = $this->content->getPosition();
564
+					throw new StrictException("Tag '$tag' has an attribute '$name' with out a value! (character #$character)");
565
+				}
566
+				$node->getTag()->$name = [
567
+					'value'       => null,
568
+					'doubleQuote' => true,
569
+				];
570
+				if ($this->content->char() != '>') {
571
+					$this->content->rewind(1);
572
+				}
573
+			}
574
+		}
575
+
576
+		$this->content->skipByToken('blank');
577
+		if ($this->content->char() == '/') {
578
+			// self closing tag
579
+			$node->getTag()->selfClosing();
580
+			$this->content->fastForward(1);
581
+		} elseif (in_array($tag, $this->selfClosing)) {
582
+
583
+			// Should be a self closing tag, check if we are strict
584
+			if ($this->options->strict) {
585
+				$character = $this->content->getPosition();
586
+				throw new StrictException("Tag '$tag' is not self closing! (character #$character)");
587
+			}
588
+
589
+			// We force self closing on this tag.
590
+			$node->getTag()->selfClosing();
591
+		}
592
+
593
+		$this->content->fastForward(1);
594
+
595
+		$return['status'] = true;
596
+		$return['node']   = $node;
597
+
598
+		return $return;
599
+	}
600
+
601
+	/**
602
+	 * Attempts to detect the charset that the html was sent in.
603
+	 *
604
+	 * @return bool
605
+	 */
606
+	protected function detectCharset()
607
+	{
608
+		// set the default
609
+		$encode = new Encode;
610
+		$encode->from($this->defaultCharset);
611
+		$encode->to($this->defaultCharset);
612
+
613
+		if ( ! is_null($this->options->enforceEncoding)) {
614
+			//  they want to enforce the given encoding
615
+			$encode->from($this->options->enforceEncoding);
616
+			$encode->to($this->options->enforceEncoding);
617
+
618
+			return false;
619
+		}
620
+
621
+		$meta = $this->root->find('meta[http-equiv=Content-Type]', 0);
622
+		if (is_null($meta)) {
623
+			// could not find meta tag
624
+			$this->root->propagateEncoding($encode);
625
+
626
+			return false;
627
+		}
628
+		$content = $meta->content;
629
+		if (empty($content)) {
630
+			// could not find content
631
+			$this->root->propagateEncoding($encode);
632
+
633
+			return false;
634
+		}
635
+		$matches = [];
636
+		if (preg_match('/charset=(.+)/', $content, $matches)) {
637
+			$encode->from(trim($matches[1]));
638
+			$this->root->propagateEncoding($encode);
639
+
640
+			return true;
641
+		}
642
+
643
+		// no charset found
644
+		$this->root->propagateEncoding($encode);
645
+
646
+		return false;
647
+	}
648 648
 }
Please login to merge, or discard this patch.