Completed
Branch TemplateNormalizations (ae42e4)
by Josh
33:16
created

InlineAttributes::inlineAttribute()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 4
nop 1
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2017 The s9e Authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\TextFormatter\Configurator\TemplateNormalizations;
9
10
use DOMElement;
11
use DOMText;
12
13
class InlineAttributes extends AbstractNormalization
14
{
15
	/**
16
	* {@inheritdoc}
17
	*/
18
	protected $queries = ['//*[namespace-uri() != $XSL]/xsl:attribute'];
19
20
	/**
21
	* Inline the xsl:attribute declarations of a template
22
	*
23
	* Will replace
24
	*     <a><xsl:attribute name="href"><xsl:value-of select="@url"/></xsl:attribute>...</a>
25
	* with
26
	*     <a href="{@url}">...</a>
27
	*
28
	* @param  DOMElement $element
29
	* @return void
30
	*/
31
	protected function normalizeElement(DOMElement $element)
32
	{
33
		$value = '';
34
		foreach ($element->childNodes as $node)
35
		{
36
			if ($node instanceof DOMText || $this->isXsl($node, 'text'))
37
			{
38
				$value .= preg_replace('([{}])', '$0$0', $node->textContent);
39
			}
40
			elseif ($this->isXsl($node, 'value-of'))
41
			{
42
				$value .= '{' . $node->getAttribute('select') . '}';
43
			}
44
			else
45
			{
46
				// Can't inline this attribute
47
				return;
48
			}
49
		}
50
		$element->parentNode->setAttribute($element->getAttribute('name'), $value);
51
		$element->parentNode->removeChild($element);
52
	}
53
}