|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package s9e\TextFormatter |
|
5
|
|
|
* @copyright Copyright (c) 2010-2023 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 function array_reverse, str_contains, str_replace; |
|
11
|
|
|
use s9e\SweetDOM\Attr; |
|
12
|
|
|
use s9e\SweetDOM\Element; |
|
13
|
|
|
use s9e\TextFormatter\Configurator\Helpers\AVTHelper; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Uninline element attributes |
|
17
|
|
|
* |
|
18
|
|
|
* Will replace |
|
19
|
|
|
* <a href="{@url}">...</a> |
|
20
|
|
|
* with |
|
21
|
|
|
* <a><xsl:attribute name="href"><xsl:value-of select="@url"/></xsl:attribute>...</a> |
|
22
|
|
|
*/ |
|
23
|
|
|
class UninlineAttributes extends AbstractNormalization |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
*/ |
|
28
|
|
|
protected array $queries = ['//*[namespace-uri() != "' . self::XMLNS_XSL . '"][@*]']; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
9 |
|
*/ |
|
33
|
|
|
protected function normalizeElement(Element $element): void |
|
34
|
|
|
{ |
|
35
|
9 |
|
$attributes = []; |
|
36
|
9 |
|
foreach ($element->attributes as $attribute) |
|
37
|
|
|
{ |
|
38
|
9 |
|
$attributes[$attribute->name] = $attribute->value; |
|
39
|
|
|
} |
|
40
|
9 |
|
foreach (array_reverse($attributes) as $attrName => $attrValue) |
|
41
|
|
|
{ |
|
42
|
|
|
if (str_contains($attrValue, '{')) |
|
43
|
|
|
{ |
|
44
|
|
|
$element->prependXslAttribute($attrName) |
|
45
|
|
|
->append(...$this->getAttributeChildren($attrValue)); |
|
46
|
|
|
} |
|
47
|
|
|
else |
|
48
|
|
|
{ |
|
49
|
9 |
|
$element->prependXslAttribute($attrName, str_replace('}}', '}', $attrValue)); |
|
50
|
|
|
} |
|
51
|
9 |
|
$element->removeAttribute($attrName); |
|
52
|
5 |
|
} |
|
53
|
9 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
9 |
|
* Uninline an AVT-style attribute |
|
57
|
|
|
* |
|
58
|
|
|
* @param Attr $attribute Attribute node |
|
59
|
9 |
|
* @return Element xsl:attribute element |
|
60
|
|
|
*/ |
|
61
|
9 |
|
protected function getAttributeChildren(string $attrValue): array |
|
62
|
|
|
{ |
|
63
|
|
|
$children = []; |
|
64
|
|
|
|
|
65
|
|
|
// Build the content of the xsl:attribute element |
|
66
|
|
|
foreach (AVTHelper::parse($attrValue) as [$type, $content]) |
|
67
|
|
|
{ |
|
68
|
|
|
if ($type === 'expression') |
|
69
|
|
|
{ |
|
70
|
4 |
|
$children[] = $this->ownerDocument->nodeCreator->createXslValueOf($content); |
|
71
|
|
|
} |
|
72
|
4 |
|
else |
|
73
|
|
|
{ |
|
74
|
|
|
$children[] = $this->createPolymorphicText($content); |
|
75
|
4 |
|
} |
|
76
|
|
|
} |
|
77
|
4 |
|
|
|
78
|
|
|
return $children; |
|
79
|
|
|
} |
|
80
|
|
|
} |