Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 32 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| taoItems_helpers_Xhtml | |
0.00% |
0 / 32 |
|
0.00% |
0 / 4 |
182 | |
0.00% |
0 / 1 |
| hasScriptElements | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getScriptElements | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
30 | |||
| removeScriptElements | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| addScriptElement | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg |
| 19 | * (under the project TAO & TAO2); |
| 20 | * 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung |
| 21 | * (under the project TAO-TRANSFER); |
| 22 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 23 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 24 | */ |
| 25 | |
| 26 | /** |
| 27 | * This helper class aims at providing utility methods to deal |
| 28 | * with XHTML documents. |
| 29 | * |
| 30 | * @author Jerome Bogaerts <jerome@taotesting.com> |
| 31 | * |
| 32 | */ |
| 33 | class taoItems_helpers_Xhtml |
| 34 | { |
| 35 | /** |
| 36 | * Determines if a given DOMDocument contains <script> elements with |
| 37 | * a 'src' attribute value that matches a given PCRE pattern. |
| 38 | * |
| 39 | * The pattern will be applied only on the base name found in the src attribute. |
| 40 | * |
| 41 | * @param DOMDocument $dom A DOMDocument. |
| 42 | * @param string $pattern A PCRE pattern. |
| 43 | * @return boolean Returns true if the requested script element(s) could be found, othewise false. |
| 44 | */ |
| 45 | public static function hasScriptElements(DOMDocument $dom, $pattern) |
| 46 | { |
| 47 | return count(self::getScriptElements($dom, $pattern)) > 0; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Retrieve <script> elements in the given DOMDocument where the src attribute value |
| 52 | * matches a PCRE pattern. |
| 53 | * |
| 54 | * The pattern will be applied only on the base name found in the src attribute. |
| 55 | * |
| 56 | * @param DOMDocument $dom A DOMDocument |
| 57 | * @param string $pattern A PCRE pattern. |
| 58 | * @return array An array of DOMNode. |
| 59 | */ |
| 60 | public static function getScriptElements(DOMDocument $dom, $pattern) |
| 61 | { |
| 62 | $xpath = new DOMXPath($dom); |
| 63 | $elements = []; |
| 64 | $scan = $xpath->query('/html/head/script'); |
| 65 | |
| 66 | for ($i = 0; $i < $scan->length; $i++) { |
| 67 | $e = $scan->item($i); |
| 68 | |
| 69 | if ($e->hasAttribute('src')) { |
| 70 | $src = $e->getAttribute('src'); |
| 71 | $pathinfo = pathinfo($src); |
| 72 | |
| 73 | if (!empty($pathinfo['basename'])) { |
| 74 | $filename = $pathinfo['basename']; |
| 75 | |
| 76 | if (preg_match($pattern, $filename) === 1) { |
| 77 | $elements[] = $e; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return $elements; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Remove <script> elements in a DOMDocument that have an src attribute value |
| 88 | * that matches a given PCRE pattern. |
| 89 | * |
| 90 | * The pattern will be applied only on the base name found in the src attribute. |
| 91 | * |
| 92 | * @param DOMDocument $dom A DOMDocument. |
| 93 | * @param string $pattern A PCRE pattern. |
| 94 | * @return int The number of found elements. |
| 95 | */ |
| 96 | public static function removeScriptElements(DOMDocument $dom, $pattern) |
| 97 | { |
| 98 | $removed = 0; |
| 99 | |
| 100 | foreach (self::getScriptElements($dom, $pattern) as $e) { |
| 101 | if (!empty($e->parentNode)) { |
| 102 | $e->parentNode->removeChild($e); |
| 103 | $removed++; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | return $removed; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Add a <script> element in the <head> element of a DOMDocument. |
| 112 | * |
| 113 | * @param DOMDocument $dom A DOMDocument. |
| 114 | * @param string $src The value of the 'src' attribute. |
| 115 | * @param string $append (optional) Append or prepend the node to the <head> element. |
| 116 | * @param string $type (optional) The value of the 'type' attribute. |
| 117 | */ |
| 118 | public static function addScriptElement(DOMDocument $dom, $src, $append = true, $type = 'text/javascript') |
| 119 | { |
| 120 | $xpath = new DOMXPath($dom); |
| 121 | $scan = $xpath->query('/html/head'); |
| 122 | |
| 123 | $newNode = $dom->createElement('script'); |
| 124 | $newNode->setAttribute('type', $type); |
| 125 | $newNode->setAttribute('src', $src); |
| 126 | |
| 127 | for ($i = 0; $i < $scan->length; $i++) { |
| 128 | $head = $scan->item($i); |
| 129 | |
| 130 | if ($append == true || $xpath->query('//script', $head)->length == 0) { |
| 131 | $head->appendChild($newNode); |
| 132 | } else { |
| 133 | $refNode = $xpath->query('//script', $head)->item(0); |
| 134 | $head->insertBefore($newNode, $refNode); |
| 135 | } |
| 136 | |
| 137 | break; |
| 138 | } |
| 139 | } |
| 140 | } |