forked from ezsystems/ezpublish-kernel
/
Style.php
177 lines (155 loc) · 4.99 KB
/
Style.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
* File containing the eZ\Publish\Core\FieldType\RichText\Converter\Render\Style class.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace eZ\Publish\Core\FieldType\RichText\Converter\Render;
use DOMDocument;
use DOMElement;
use DOMNode;
use DOMXPath;
use eZ\Publish\Core\FieldType\RichText\Converter;
use eZ\Publish\Core\FieldType\RichText\Converter\Render;
use eZ\Publish\Core\FieldType\RichText\RendererInterface;
/**
* RichText Style converter injects rendered style payloads into style elements.
*/
class Style extends Render implements Converter
{
/**
* @var Converter
*/
private $richTextConverter;
/**
* Style constructor.
*
* @param RendererInterface $renderer
* @param Converter $richTextConverter
*/
public function __construct(RendererInterface $renderer, Converter $richTextConverter)
{
$this->richTextConverter = $richTextConverter;
parent::__construct($renderer);
}
/**
* Injects rendered payloads into Custom Style elements.
*
* @param \DOMDocument $document
*
* @return \DOMDocument
*/
public function convert(DOMDocument $document)
{
$xpath = new DOMXPath($document);
$xpath->registerNamespace('docbook', 'http://docbook.org/ns/docbook');
$xpathExpression = '//docbook:ezstyle | //docbook:ezstyleinline';
$styles = $xpath->query($xpathExpression);
/** @var \DOMElement[] $stylesSorted */
$stylesSorted = [];
$maxDepth = 0;
foreach ($styles as $style) {
$depth = $this->getNodeDepth($style);
if ($depth > $maxDepth) {
$maxDepth = $depth;
}
$stylesSorted[$depth][] = $style;
}
ksort($stylesSorted, SORT_NUMERIC);
foreach ($stylesSorted as $styles) {
foreach ($styles as $style) {
$this->processStyle($document, $style);
}
}
return $document;
}
/**
* Processes given template $style in a given $document.
*
* @param \DOMDocument $document
* @param \DOMElement $style
*/
protected function processStyle(DOMDocument $document, DOMElement $style)
{
$content = null;
$styleName = $style->getAttribute('name');
$parameters = [
'name' => $styleName,
'content' => $this->saveNodeXML($style),
];
if ($style->hasAttribute('ezxhtml:align')) {
$parameters['align'] = $style->getAttribute('ezxhtml:align');
}
$content = $this->renderer->renderStyle(
$styleName,
$parameters,
$style->localName === 'ezstyleinline'
);
if (isset($content)) {
// If current tag is wrapped inside another Custom Style tag we can't use CDATA section
// for its content as these can't be nested.
// CDATA section will be used only for content of root wrapping tag, content of tags
// inside it will be added as XML fragments.
if ($this->isWrapped($style)) {
$fragment = $document->createDocumentFragment();
$fragment->appendXML($content);
$style->parentNode->replaceChild($fragment, $style);
} else {
$payload = $document->createElement('ezpayload');
$payload->appendChild($document->createCDATASection($content));
$style->appendChild($payload);
}
}
}
/**
* Returns if the given $node is wrapped inside another template node.
*
* @param \DOMNode $node
*
* @return bool
*/
protected function isWrapped(DomNode $node)
{
while ($node = $node->parentNode) {
if ($node->localName === 'ezstyle' || $node->localName === 'ezstyleinline') {
return true;
}
}
return false;
}
/**
* Returns depth of given $node in a DOMDocument.
*
* @param \DOMNode $node
*
* @return int
*/
protected function getNodeDepth(DomNode $node)
{
// initial depth for top level elements (to avoid "ifs")
$depth = -2;
while ($node) {
++$depth;
$node = $node->parentNode;
}
return $depth;
}
/**
* Returns XML fragment string for given $node.
*
* @param \DOMNode $node
*
* @return string
*/
protected function saveNodeXML(DOMNode $node)
{
$innerDoc = new DOMDocument();
/** @var \DOMNode $child */
foreach ($node->childNodes as $child) {
$innerDoc->appendChild($innerDoc->importNode($child, true));
}
$convertedInnerDoc = $this->richTextConverter->convert($innerDoc);
return trim($convertedInnerDoc ? $convertedInnerDoc->saveHTML() : $innerDoc->saveHTML());
}
}