1 | <?php |
||
19 | abstract class TemplateHelper |
||
20 | { |
||
21 | /** |
||
22 | * XSL namespace |
||
23 | */ |
||
24 | const XMLNS_XSL = 'http://www.w3.org/1999/XSL/Transform'; |
||
25 | |||
26 | /** |
||
27 | * Return all attributes (literal or generated) that match given regexp |
||
28 | * |
||
29 | * @param DOMDocument $dom Document |
||
30 | * @param string $regexp Regexp |
||
31 | * @return array Array of DOMNode instances |
||
32 | */ |
||
33 | public static function getAttributesByRegexp(DOMDocument $dom, $regexp) |
||
37 | |||
38 | /** |
||
39 | * Return all DOMNodes whose content is CSS |
||
40 | * |
||
41 | * @param DOMDocument $dom Document |
||
42 | * @return array Array of DOMNode instances |
||
43 | */ |
||
44 | public static function getCSSNodes(DOMDocument $dom) |
||
48 | |||
49 | /** |
||
50 | * Return all elements (literal or generated) that match given regexp |
||
51 | * |
||
52 | * @param DOMDocument $dom Document |
||
53 | * @param string $regexp Regexp |
||
54 | * @return array Array of DOMNode instances |
||
55 | */ |
||
56 | public static function getElementsByRegexp(DOMDocument $dom, $regexp) |
||
60 | |||
61 | /** |
||
62 | * Return all DOMNodes whose content is JavaScript |
||
63 | * |
||
64 | * @param DOMDocument $dom Document |
||
65 | * @return array Array of DOMNode instances |
||
66 | */ |
||
67 | public static function getJSNodes(DOMDocument $dom) |
||
71 | |||
72 | /** |
||
73 | * Return all elements (literal or generated) that match given regexp |
||
74 | * |
||
75 | * Will return all <param/> descendants of <object/> and all attributes of <embed/> whose name |
||
76 | * matches given regexp. This method will NOT catch <param/> elements whose 'name' attribute is |
||
77 | * set via an <xsl:attribute/> |
||
78 | * |
||
79 | * @param DOMDocument $dom Document |
||
80 | * @param string $regexp |
||
81 | * @return array Array of DOMNode instances |
||
82 | */ |
||
83 | public static function getObjectParamsByRegexp(DOMDocument $dom, $regexp) |
||
87 | |||
88 | /** |
||
89 | * Return a list of parameters in use in given XSL |
||
90 | * |
||
91 | * @param string $xsl XSL source |
||
92 | * @return array Alphabetically sorted list of unique parameter names |
||
93 | */ |
||
94 | public static function getParametersFromXSL($xsl) |
||
126 | |||
127 | /** |
||
128 | * Return all DOMNodes whose content is an URL |
||
129 | * |
||
130 | * NOTE: it will also return HTML4 nodes whose content is an URI |
||
131 | * |
||
132 | * @param DOMDocument $dom Document |
||
133 | * @return array Array of DOMNode instances |
||
134 | */ |
||
135 | public static function getURLNodes(DOMDocument $dom) |
||
139 | |||
140 | /** |
||
141 | * Highlight the source of a node inside of a template |
||
142 | * |
||
143 | * @param DOMNode $node Node to highlight |
||
144 | * @param string $prepend HTML to prepend |
||
145 | * @param string $append HTML to append |
||
146 | * @return string Template's source, as HTML |
||
147 | */ |
||
148 | public static function highlightNode(DOMNode $node, $prepend, $append) |
||
196 | |||
197 | /** |
||
198 | * Load a template as an xsl:template node |
||
199 | * |
||
200 | * Will attempt to load it as XML first, then as HTML as a fallback. Either way, an xsl:template |
||
201 | * node is returned |
||
202 | * |
||
203 | * @param string $template |
||
204 | * @return DOMDocument |
||
205 | */ |
||
206 | public static function loadTemplate($template) |
||
210 | |||
211 | /** |
||
212 | * Replace simple templates (in an array, in-place) with a common template |
||
213 | * |
||
214 | * In some situations, renderers can take advantage of multiple tags having the same template. In |
||
215 | * any configuration, there's almost always a number of "simple" tags that are rendered as an |
||
216 | * HTML element of the same name with no HTML attributes. For instance, the system tag "p" used |
||
217 | * for paragraphs, "B" tags used for "b" HTML elements, etc... This method replaces those |
||
218 | * templates with a common template that uses a dynamic element name based on the tag's name, |
||
219 | * either its nodeName or localName depending on whether the tag is namespaced, and normalized to |
||
220 | * lowercase using XPath's translate() function |
||
221 | * |
||
222 | * @param array<string> &$templates Associative array of [tagName => template] |
||
223 | * @param integer $minCount |
||
224 | * @return void |
||
225 | */ |
||
226 | public static function replaceHomogeneousTemplates(array &$templates, $minCount = 3) |
||
273 | |||
274 | /** |
||
275 | * Replace parts of a template that match given regexp |
||
276 | * |
||
277 | * Treats attribute values as plain text. Replacements within XPath expression is unsupported. |
||
278 | * The callback must return an array with two elements. The first must be either of 'expression', |
||
279 | * 'literal' or 'passthrough', and the second element depends on the first. |
||
280 | * |
||
281 | * - 'expression' indicates that the replacement must be treated as an XPath expression such as |
||
282 | * '@foo', which must be passed as the second element. |
||
283 | * - 'literal' indicates a literal (plain text) replacement, passed as its second element. |
||
284 | * - 'passthrough' indicates that the replacement should the tag's content. It works differently |
||
285 | * whether it is inside an attribute's value or a text node. Within an attribute's value, the |
||
286 | * replacement will be the text content of the tag. Within a text node, the replacement |
||
287 | * becomes an <xsl:apply-templates/> node. |
||
288 | * |
||
289 | * @param string $template Original template |
||
290 | * @param string $regexp Regexp for matching parts that need replacement |
||
291 | * @param callback $fn Callback used to get the replacement |
||
292 | * @return string Processed template |
||
293 | */ |
||
294 | public static function replaceTokens($template, $regexp, $fn) |
||
298 | |||
299 | /** |
||
300 | * Serialize a loaded template back into a string |
||
301 | * |
||
302 | * NOTE: removes the root node created by loadTemplate() |
||
303 | * |
||
304 | * @param DOMDocument $dom |
||
305 | * @return string |
||
306 | */ |
||
307 | public static function saveTemplate(DOMDocument $dom) |
||
311 | |||
312 | /** |
||
313 | * Get a list of parameters from given XPath expression |
||
314 | * |
||
315 | * @param DOMNode $node Context node |
||
316 | * @param string $expr XPath expression |
||
317 | * @return string[] |
||
318 | */ |
||
319 | protected static function getParametersFromExpression(DOMNode $node, $expr) |
||
336 | } |