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

InlineElements::normalizeElement()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 28
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 12
nc 6
nop 1
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 DOMException;
12
13
class InlineElements extends AbstractNormalization
14
{
15
	/**
16
	* {@inheritdoc}
17
	*/
18
	protected $queries = ['//xsl:element'];
19
20
	/**
21
	* Inline the elements declarations of a template
22
	*
23
	* Will replace
24
	*     <xsl:element name="div"><xsl:apply-templates/></xsl:element>
25
	* with
26
	*     <div><xsl:apply-templates/></div>
27
	*
28
	* @param  DOMElement $element
29
	* @return void
30
	*/
31
	protected function normalizeElement(DOMElement $element)
32
	{
33
		$elName = $element->getAttribute('name');
34
		$dom    = $this->ownerDocument;
35
36
		try
37
		{
38
			// Create the new static element
39
			$newElement = ($element->hasAttribute('namespace'))
40
						? $dom->createElementNS($element->getAttribute('namespace'), $elName)
41
						: $dom->createElement($elName);
42
		}
43
		catch (DOMException $e)
44
		{
45
			// Ignore this element if an exception got thrown
46
			return;
47
		}
48
49
		// Replace the old <xsl:element/> with it. We do it now so that libxml doesn't have to
50
		// redeclare the XSL namespace
51
		$element->parentNode->replaceChild($newElement, $element);
52
53
		// One by one and in order, we move the nodes from the old element to the new one
54
		while ($element->firstChild)
55
		{
56
			$newElement->appendChild($element->removeChild($element->firstChild));
57
		}
58
	}
59
}