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

InlineTextElements::normalize()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 29
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 5
nop 1
dl 0
loc 29
rs 6.7272
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A InlineTextElements::isFollowedByText() 0 4 2
A InlineTextElements::isPrecededByText() 0 4 2
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
12
class InlineTextElements extends AbstractNormalization
13
{
14
	/**
15
	* {@inheritdoc}
16
	*/
17
	protected $queries = ['//xsl:text'];
18
19
	/**
20
	* Test whether an element is followed by a text node
21
	*
22
	* @param  DOMElement $element
23
	* @return bool
24
	*/
25
	protected function isFollowedByText(DOMElement $element)
26
	{
27
		return ($element->nextSibling && $element->nextSibling->nodeType === XML_TEXT_NODE);
28
	}
29
30
	/**
31
	* Test whether an element is preceded by a text node
32
	*
33
	* @param  DOMElement $element
34
	* @return bool
35
	*/
36
	protected function isPrecededByText(DOMElement $element)
37
	{
38
		return ($element->previousSibling && $element->previousSibling->nodeType === XML_TEXT_NODE);
39
	}
40
41
	/**
42
	* {@inheritdoc}
43
	*/
44
	protected function normalizeElement(DOMElement $element)
45
	{
46
		// If this node's content is whitespace, ensure it's preceded or followed by a text node
47
		if (trim($element->textContent) === '')
48
		{
49
			if (!$this->isFollowedByText($element) && !$this->isPrecededByText($element))
50
			{
51
				// This would become inter-element whitespace, therefore we can't inline
52
				return;
53
			}
54
		}
55
		$element->parentNode->replaceChild($this->createTextNode($element->textContent), $element);
56
	}
57
}