|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package s9e\TextFormatter |
|
5
|
|
|
* @copyright Copyright (c) 2010-2016 The s9e Authors |
|
6
|
|
|
* @license http://www.opensource.org/licenses/mit-license'); The MIT License |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace s9e\TextFormatter\Configurator; |
|
9
|
|
|
|
|
10
|
|
|
use ArrayAccess; |
|
11
|
|
|
use Iterator; |
|
12
|
|
|
use s9e\TextFormatter\Configurator\Collections\TemplateNormalizationList; |
|
13
|
|
|
use s9e\TextFormatter\Configurator\Helpers\TemplateHelper; |
|
14
|
|
|
use s9e\TextFormatter\Configurator\Items\Tag; |
|
15
|
|
|
use s9e\TextFormatter\Configurator\Traits\CollectionProxy; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @method mixed add(mixed $value, null $void) |
|
19
|
|
|
* @method mixed append(mixed $value) |
|
20
|
|
|
* @method array asConfig() |
|
21
|
|
|
* @method void clear() |
|
22
|
|
|
* @method bool contains(mixed $value) |
|
23
|
|
|
* @method integer count() |
|
24
|
|
|
* @method mixed current() |
|
25
|
|
|
* @method void delete(string $key) |
|
26
|
|
|
* @method bool exists(string $key) |
|
27
|
|
|
* @method mixed get(string $key) |
|
28
|
|
|
* @method mixed indexOf(mixed $value) |
|
29
|
|
|
* @method mixed insert(integer $offset, mixed $value) |
|
30
|
|
|
* @method integer|string key() |
|
31
|
|
|
* @method mixed next() |
|
32
|
|
|
* @method integer normalizeKey(mixed $key) |
|
33
|
|
|
* @method TemplateNormalization normalizeValue(mixed $value) |
|
34
|
|
|
* @method bool offsetExists(string|integer $offset) |
|
35
|
|
|
* @method mixed offsetGet(string|integer $offset) |
|
36
|
|
|
* @method void offsetSet(mixed $offset, mixed $value) |
|
37
|
|
|
* @method void offsetUnset(string|integer $offset) |
|
38
|
|
|
* @method string onDuplicate(string|null $action) |
|
39
|
|
|
* @method mixed prepend(mixed $value) |
|
40
|
|
|
* @method integer remove(mixed $value) |
|
41
|
|
|
* @method void rewind() |
|
42
|
|
|
* @method mixed set(string $key, mixed $value) |
|
43
|
|
|
* @method bool valid() |
|
44
|
|
|
*/ |
|
45
|
|
|
class TemplateNormalizer implements ArrayAccess, Iterator |
|
46
|
|
|
{ |
|
47
|
|
|
use CollectionProxy; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @var TemplateNormalizationList Collection of TemplateNormalization instances |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $collection; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Constructor |
|
56
|
|
|
* |
|
57
|
|
|
* Will load the default normalization rules |
|
58
|
|
|
*/ |
|
59
|
|
|
public function __construct() |
|
60
|
|
|
{ |
|
61
|
|
|
$this->collection = new TemplateNormalizationList; |
|
62
|
|
|
|
|
63
|
|
|
$this->collection->append('PreserveSingleSpaces'); |
|
64
|
|
|
$this->collection->append('RemoveComments'); |
|
65
|
|
|
$this->collection->append('RemoveInterElementWhitespace'); |
|
66
|
|
|
$this->collection->append('FixUnescapedCurlyBracesInHtmlAttributes'); |
|
67
|
|
|
$this->collection->append('FoldArithmeticConstants'); |
|
68
|
|
|
$this->collection->append('InlineAttributes'); |
|
69
|
|
|
$this->collection->append('InlineCDATA'); |
|
70
|
|
|
$this->collection->append('InlineElements'); |
|
71
|
|
|
$this->collection->append('InlineInferredValues'); |
|
72
|
|
|
$this->collection->append('InlineTextElements'); |
|
73
|
|
|
$this->collection->append('InlineXPathLiterals'); |
|
74
|
|
|
$this->collection->append('MinifyXPathExpressions'); |
|
75
|
|
|
$this->collection->append('NormalizeAttributeNames'); |
|
76
|
|
|
$this->collection->append('NormalizeElementNames'); |
|
77
|
|
|
$this->collection->append('NormalizeUrls'); |
|
78
|
|
|
$this->collection->append('OptimizeConditionalAttributes'); |
|
79
|
|
|
$this->collection->append('OptimizeConditionalValueOf'); |
|
80
|
|
|
$this->collection->append('OptimizeChoose'); |
|
81
|
|
|
$this->collection->append('SetRelNoreferrerOnTargetedLinks'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Normalize a tag's templates |
|
86
|
|
|
* |
|
87
|
|
|
* @param Tag $tag Tag whose templates will be normalized |
|
88
|
|
|
* @return void |
|
89
|
|
|
*/ |
|
90
|
|
|
public function normalizeTag(Tag $tag) |
|
91
|
|
|
{ |
|
92
|
|
|
if (isset($tag->template) && !$tag->template->isNormalized()) |
|
93
|
|
|
{ |
|
94
|
|
|
$tag->template->normalize($this); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Normalize a template |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $template Original template |
|
102
|
|
|
* @return string Normalized template |
|
103
|
|
|
*/ |
|
104
|
|
|
public function normalizeTemplate($template) |
|
105
|
|
|
{ |
|
106
|
|
|
$dom = TemplateHelper::loadTemplate($template); |
|
107
|
|
|
|
|
108
|
|
|
// We'll keep track of what normalizations have been applied |
|
109
|
|
|
$applied = []; |
|
110
|
|
|
|
|
111
|
|
|
// Apply all the normalizations until no more change is made or we've reached the maximum |
|
112
|
|
|
// number of loops |
|
113
|
|
|
$loops = 5; |
|
114
|
|
|
do |
|
115
|
|
|
{ |
|
116
|
|
|
$old = $template; |
|
117
|
|
|
|
|
118
|
|
|
foreach ($this->collection as $k => $normalization) |
|
119
|
|
|
{ |
|
120
|
|
|
if (isset($applied[$k]) && !empty($normalization->onlyOnce)) |
|
121
|
|
|
{ |
|
122
|
|
|
continue; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
$normalization->normalize($dom->documentElement); |
|
126
|
|
|
$applied[$k] = 1; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$template = TemplateHelper::saveTemplate($dom); |
|
130
|
|
|
} |
|
131
|
|
|
while (--$loops && $template !== $old); |
|
132
|
|
|
|
|
133
|
|
|
return $template; |
|
134
|
|
|
} |
|
135
|
|
|
} |