o2system /
html
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * This file is part of the O2System Framework package. |
||||
| 4 | * |
||||
| 5 | * For the full copyright and license information, please view the LICENSE |
||||
| 6 | * file that was distributed with this source code. |
||||
| 7 | * |
||||
| 8 | * @author Steeve Andrian Salim |
||||
| 9 | * @copyright Copyright (c) Steeve Andrian Salim |
||||
| 10 | */ |
||||
| 11 | |||||
| 12 | // ------------------------------------------------------------------------ |
||||
| 13 | |||||
| 14 | namespace O2System\Html\Dom; |
||||
| 15 | |||||
| 16 | // ------------------------------------------------------------------------ |
||||
| 17 | |||||
| 18 | use O2System\Html\Document; |
||||
| 19 | use O2System\Spl\Exceptions\Logic\InvalidArgumentException; |
||||
| 20 | |||||
| 21 | /** |
||||
| 22 | * Class Element |
||||
| 23 | * |
||||
| 24 | * @package O2System\HTML\DOM |
||||
| 25 | */ |
||||
| 26 | class Element extends \DOMElement |
||||
| 27 | {
|
||||
| 28 | /** |
||||
| 29 | * Element::__get |
||||
| 30 | * |
||||
| 31 | * Returns the value for the property specified |
||||
| 32 | * |
||||
| 33 | * @param string $name The name of the property |
||||
| 34 | * |
||||
| 35 | * @return string The value of the property specified |
||||
| 36 | * @throws InvalidArgumentException |
||||
| 37 | */ |
||||
| 38 | public function __get($name) |
||||
| 39 | {
|
||||
| 40 | if ( ! is_string($name)) {
|
||||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
| 41 | throw new InvalidArgumentException('HTML_E_INVALID_ARGUMENT', 0, ['string']);
|
||||
| 42 | } |
||||
| 43 | if ($name === 'innerHTML') {
|
||||
| 44 | $html = $this->ownerDocument->saveHTML($this); |
||||
| 45 | $nodeName = $this->nodeName; |
||||
| 46 | |||||
| 47 | return preg_replace('@^<' . $nodeName . '[^>]*>|</' . $nodeName . '>$@', '', $html);
|
||||
| 48 | } elseif ($name === 'outerHTML') {
|
||||
| 49 | return $this->ownerDocument->saveHTML($this); |
||||
| 50 | } else {
|
||||
| 51 | throw new InvalidArgumentException('HTML_E_INVALID_ARGUMENT', 0, ['HTMLDOMElement::$' . $name]);
|
||||
| 52 | } |
||||
| 53 | } |
||||
| 54 | |||||
| 55 | // ------------------------------------------------------------------------ |
||||
| 56 | |||||
| 57 | /** |
||||
| 58 | * Element::__set |
||||
| 59 | * |
||||
| 60 | * Sets the value for the property specified |
||||
| 61 | * |
||||
| 62 | * @param string $name |
||||
| 63 | * @param string $value |
||||
| 64 | * |
||||
| 65 | * @throws \InvalidArgumentException |
||||
| 66 | * @throws \O2System\Spl\Exceptions\Logic\InvalidArgumentException |
||||
| 67 | */ |
||||
| 68 | public function __set($name, $value) |
||||
| 69 | {
|
||||
| 70 | if ( ! is_string($name)) {
|
||||
|
0 ignored issues
–
show
|
|||||
| 71 | throw new InvalidArgumentException('HTML_E_INVALID_ARGUMENT', 0, ['string']);
|
||||
| 72 | } |
||||
| 73 | |||||
| 74 | if ( ! is_string($value)) {
|
||||
|
0 ignored issues
–
show
|
|||||
| 75 | throw new InvalidArgumentException('HTML_E_INVALID_ARGUMENT', 0, ['string']);
|
||||
| 76 | } |
||||
| 77 | |||||
| 78 | if ($name === 'innerHTML') {
|
||||
| 79 | while ($this->hasChildNodes()) {
|
||||
| 80 | $this->removeChild($this->firstChild); |
||||
| 81 | } |
||||
| 82 | |||||
| 83 | $DOMDocument = new Document(); |
||||
| 84 | $DOMDocument->loadHTML('<body>' . $value . '</body>');
|
||||
| 85 | |||||
| 86 | foreach ($DOMDocument->getElementsByTagName('body')->item(0)->childNodes as $node) {
|
||||
| 87 | $node = $this->ownerDocument->importNode($node, true); |
||||
| 88 | $this->appendChild($node); |
||||
| 89 | } |
||||
| 90 | } elseif ($name === 'outerHTML') {
|
||||
| 91 | $DOMDocument = new Document(); |
||||
| 92 | $DOMDocument->loadHTML('<body>' . $value . '</body>');
|
||||
| 93 | foreach ($DOMDocument->getElementsByTagName('body')->item(0)->childNodes as $node) {
|
||||
| 94 | $node = $this->ownerDocument->importNode($node, true); |
||||
| 95 | $this->parentNode->insertBefore($node, $this); |
||||
| 96 | } |
||||
| 97 | |||||
| 98 | $this->parentNode->removeChild($this); |
||||
| 99 | } |
||||
| 100 | } |
||||
| 101 | |||||
| 102 | // ------------------------------------------------------------------------ |
||||
| 103 | |||||
| 104 | /** |
||||
| 105 | * Element::addAttributes |
||||
| 106 | * |
||||
| 107 | * @param array $attr |
||||
| 108 | * |
||||
| 109 | * @return static |
||||
| 110 | */ |
||||
| 111 | public function addAttributes(array $attr) |
||||
| 112 | {
|
||||
| 113 | foreach ($attr as $name => $value) {
|
||||
| 114 | $this->setAttribute($name, $value); |
||||
| 115 | } |
||||
| 116 | |||||
| 117 | return $this; |
||||
| 118 | } |
||||
| 119 | |||||
| 120 | // ------------------------------------------------------------------------ |
||||
| 121 | |||||
| 122 | /** |
||||
| 123 | * Element::getAttributes |
||||
| 124 | * |
||||
| 125 | * Returns an array containing all attributes |
||||
| 126 | * |
||||
| 127 | * @return array An associative array containing all attributes |
||||
| 128 | */ |
||||
| 129 | public function getAttributes() |
||||
| 130 | {
|
||||
| 131 | $attributesCount = $this->attributes->length; |
||||
| 132 | $attributes = []; |
||||
| 133 | |||||
| 134 | for ($i = 0; $i < $attributesCount; $i++) {
|
||||
| 135 | $attributes[] = $this->attributes->item($i); |
||||
| 136 | } |
||||
| 137 | |||||
| 138 | return $attributes; |
||||
| 139 | } |
||||
| 140 | |||||
| 141 | // ------------------------------------------------------------------------ |
||||
| 142 | |||||
| 143 | /** |
||||
| 144 | * Element::clone |
||||
| 145 | * |
||||
| 146 | * @return \O2System\Html\Dom\Element |
||||
| 147 | */ |
||||
| 148 | public function clones() |
||||
| 149 | {
|
||||
| 150 | return clone $this; |
||||
| 151 | } |
||||
| 152 | |||||
| 153 | // ------------------------------------------------------------------------ |
||||
| 154 | |||||
| 155 | /** |
||||
| 156 | * Element::__toString |
||||
| 157 | * |
||||
| 158 | * Returns the element outerHTML |
||||
| 159 | * |
||||
| 160 | * @return string The element outerHTML |
||||
| 161 | */ |
||||
| 162 | public function __toString() |
||||
| 163 | {
|
||||
| 164 | return $this->outerHTML; |
||||
|
0 ignored issues
–
show
The property
outerHTML does not exist on O2System\Html\Dom\Element. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||||
| 165 | } |
||||
| 166 | |||||
| 167 | // ------------------------------------------------------------------------ |
||||
| 168 | |||||
| 169 | /** |
||||
| 170 | * Element::clear |
||||
| 171 | * |
||||
| 172 | * @return void |
||||
| 173 | */ |
||||
| 174 | public function clear() |
||||
| 175 | {
|
||||
| 176 | if ($this->childNodes->length > 0) {
|
||||
| 177 | foreach ($this->childNodes as $childNode) {
|
||||
| 178 | $this->removeChild($childNode); |
||||
| 179 | } |
||||
| 180 | } |
||||
| 181 | |||||
| 182 | $this->textContent = null; |
||||
| 183 | } |
||||
| 184 | |||||
| 185 | // ------------------------------------------------------------------------ |
||||
| 186 | |||||
| 187 | /** |
||||
| 188 | * Element::html |
||||
| 189 | * |
||||
| 190 | * @param null $newInnerHTML |
||||
|
0 ignored issues
–
show
|
|||||
| 191 | * |
||||
| 192 | * @return string |
||||
| 193 | */ |
||||
| 194 | public function html($newInnerHTML = null) |
||||
| 195 | {
|
||||
| 196 | if (isset($newInnerHTML)) {
|
||||
| 197 | $this->replace($newInnerHTML); |
||||
| 198 | } |
||||
| 199 | |||||
| 200 | return $this->innerHTML; |
||||
|
0 ignored issues
–
show
The property
innerHTML does not exist on O2System\Html\Dom\Element. Since you implemented __get, consider adding a @property annotation.
Loading history...
|
|||||
| 201 | } |
||||
| 202 | |||||
| 203 | // ------------------------------------------------------------------------ |
||||
| 204 | |||||
| 205 | /** |
||||
| 206 | * Element::replace |
||||
| 207 | * |
||||
| 208 | * @param $source |
||||
| 209 | * |
||||
| 210 | * @return \DOMNode |
||||
| 211 | */ |
||||
| 212 | public function replace($source) |
||||
| 213 | {
|
||||
| 214 | $importNode = $this->ownerDocument->importNode($this->ownerDocument->importSourceNode($source), true); |
||||
|
0 ignored issues
–
show
The method
importSourceNode() does not exist on DOMDocument. Are you sure you never get this type here, but always one of the subclasses?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 215 | |||||
| 216 | return $this->parentNode->replaceChild($importNode, $this); |
||||
| 217 | } |
||||
| 218 | |||||
| 219 | // ------------------------------------------------------------------------ |
||||
| 220 | |||||
| 221 | /** |
||||
| 222 | * Element::text |
||||
| 223 | * |
||||
| 224 | * @param null $newTextContent |
||||
|
0 ignored issues
–
show
|
|||||
| 225 | * |
||||
| 226 | * @return null |
||||
| 227 | */ |
||||
| 228 | public function text($newTextContent = null) |
||||
| 229 | {
|
||||
| 230 | if (isset($newTextContent)) {
|
||||
| 231 | $this->textContent = $this->nodeValue = $newTextContent; |
||||
| 232 | } |
||||
| 233 | |||||
| 234 | return $this->textContent; |
||||
|
0 ignored issues
–
show
|
|||||
| 235 | } |
||||
| 236 | |||||
| 237 | // ------------------------------------------------------------------------ |
||||
| 238 | |||||
| 239 | /** |
||||
| 240 | * Element::append |
||||
| 241 | * |
||||
| 242 | * @param $source |
||||
| 243 | * |
||||
| 244 | * @return \DOMNode |
||||
| 245 | */ |
||||
| 246 | public function append($source) |
||||
| 247 | {
|
||||
| 248 | $importNode = $this->ownerDocument->importNode($this->ownerDocument->importSourceNode($source), true); |
||||
| 249 | |||||
| 250 | return $this->insertBefore($importNode, $this->firstChild); |
||||
| 251 | } |
||||
| 252 | |||||
| 253 | // ------------------------------------------------------------------------ |
||||
| 254 | |||||
| 255 | /** |
||||
| 256 | * Element::prepend |
||||
| 257 | * |
||||
| 258 | * @param $source |
||||
| 259 | * |
||||
| 260 | * @return \DOMNode |
||||
| 261 | */ |
||||
| 262 | public function prepend($source) |
||||
| 263 | {
|
||||
| 264 | $importNode = $this->ownerDocument->importNode($this->ownerDocument->importSourceNode($source), true); |
||||
| 265 | |||||
| 266 | return $this->appendChild($importNode); |
||||
| 267 | } |
||||
| 268 | |||||
| 269 | // ------------------------------------------------------------------------ |
||||
| 270 | |||||
| 271 | /** |
||||
| 272 | * Element::before |
||||
| 273 | * |
||||
| 274 | * @param $source |
||||
| 275 | * |
||||
| 276 | * @return \DOMNode |
||||
| 277 | */ |
||||
| 278 | public function before($source) |
||||
| 279 | {
|
||||
| 280 | $importNode = $this->ownerDocument->importNode($this->ownerDocument->importSourceNode($source), true); |
||||
| 281 | |||||
| 282 | return $this->parentNode->insertBefore($importNode, $this); |
||||
| 283 | } |
||||
| 284 | |||||
| 285 | // ------------------------------------------------------------------------ |
||||
| 286 | |||||
| 287 | /** |
||||
| 288 | * Element::after |
||||
| 289 | * |
||||
| 290 | * @param $source |
||||
| 291 | * |
||||
| 292 | * @return bool |
||||
| 293 | */ |
||||
| 294 | public function after($source) |
||||
| 295 | {
|
||||
| 296 | $importNode = $this->ownerDocument->importNode($this->ownerDocument->importSourceNode($source), true); |
||||
| 297 | |||||
| 298 | $isFoundSameNode = false; |
||||
| 299 | |||||
| 300 | foreach ($this->parentNode->childNodes as $key => $childNode) {
|
||||
| 301 | if ($childNode->isSameNode($this)) {
|
||||
| 302 | $isFoundSameNode = true; |
||||
| 303 | continue; |
||||
| 304 | } elseif ($isFoundSameNode) {
|
||||
| 305 | $this->parentNode->insertBefore($importNode, $childNode); |
||||
| 306 | |||||
| 307 | return true; |
||||
| 308 | break; |
||||
|
0 ignored issues
–
show
break is not strictly necessary here and could be removed.
The switch ($x) {
case 1:
return 'foo';
break; // This break is not necessary and can be left off.
}
If you would like to keep this construct to be consistent with other Loading history...
|
|||||
| 309 | } |
||||
| 310 | } |
||||
| 311 | } |
||||
| 312 | |||||
| 313 | // ------------------------------------------------------------------------ |
||||
| 314 | |||||
| 315 | /** |
||||
| 316 | * Element::remove |
||||
| 317 | * |
||||
| 318 | * @return \DOMNode |
||||
| 319 | */ |
||||
| 320 | public function remove() |
||||
| 321 | {
|
||||
| 322 | return $this->parentNode->removeChild($this); |
||||
| 323 | } |
||||
| 324 | } |