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

normalizeAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
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 DOMAttr;
11
12
/**
13
* Fix unescaped curly braces in HTML attributes
14
*
15
* Will replace
16
*     <hr onclick="if(1){alert(1)}">
17
*     <hr title="x{x">
18
* with
19
*     <hr onclick="if(1){{alert(1)}">
20
*     <hr title="x{{x">
21
*/
22
class FixUnescapedCurlyBracesInHtmlAttributes extends AbstractNormalization
23
{
24
	/**
25
	* {@inheritdoc}
26
	*/
27
	protected $queries = ['//*[namespace-uri() != $XSL]/@*[contains(., "{")]'];
28
29
	/**
30
	* {@inheritdoc}
31
	*/
32
	protected function normalizeAttribute(DOMAttr $attribute)
33
	{
34
		$match = [
35
			'(\\b(?:do|else|(?:if|while)\\s*\\(.*?\\))\\s*\\{(?![{@]))',
36
			'((?<!\\{)(?:\\{\\{)*\\{(?!\\{)[^}]*+$)',
37
			'((?<!\\{)\\{\\s*(?:"[^"]*"|\'[^\']*\'|[a-z]\\w*(?:\\s|:\\s|:(?:["\']|\\w+\\s*,))))i'
38
		];
39
		$replace = [
40
			'$0{',
41
			'{$0',
42
			'{$0'
43
		];
44
		$attrValue        = preg_replace($match, $replace, $attribute->value);
45
		$attribute->value = htmlspecialchars($attrValue, ENT_NOQUOTES, 'UTF-8');
46
	}
47
}