1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* (c) Steve Nebes <[email protected]> |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace SN\DaisyDiff\Html\Ancestor\TagToString; |
12
|
|
|
|
13
|
|
|
use SN\DaisyDiff\Html\Ancestor\TagChangeSemantic; |
14
|
|
|
use SN\DaisyDiff\Html\ChangeText; |
15
|
|
|
use SN\DaisyDiff\Html\Dom\TagNode; |
16
|
|
|
use SN\DaisyDiff\Html\Modification\HtmlLayoutChange; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* TagToString |
20
|
|
|
*/ |
21
|
|
|
class TagToString |
22
|
|
|
{ |
23
|
|
|
/** @var TagNode */ |
24
|
|
|
protected $node; |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
protected $sem; |
28
|
|
|
|
29
|
|
|
/** @var HtmlLayoutChange */ |
30
|
|
|
protected $htmlLayoutChange; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param TagNode $node |
34
|
|
|
* @param string $sem |
35
|
|
|
*/ |
36
|
27 |
|
public function __construct(TagNode $node, string $sem) |
37
|
|
|
{ |
38
|
27 |
|
$this->node = $node; |
39
|
27 |
|
$this->sem = $sem; |
40
|
27 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
27 |
|
public function getDescription(): string |
46
|
|
|
{ |
47
|
27 |
|
return $this->getString('diff-' . $this->node->getQName()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param ChangeText $text |
52
|
|
|
*/ |
53
|
19 |
|
public function getRemovedDescription(ChangeText $text): void |
54
|
|
|
{ |
55
|
19 |
|
$this->htmlLayoutChange = new HtmlLayoutChange(); |
56
|
19 |
|
$this->htmlLayoutChange->setEndingTag($this->node->getEndTag()); |
57
|
19 |
|
$this->htmlLayoutChange->setOpeningTag($this->node->getOpeningTag()); |
58
|
19 |
|
$this->htmlLayoutChange->setType(HtmlLayoutChange::TAG_REMOVED); |
59
|
|
|
|
60
|
19 |
|
if ($this->sem === TagChangeSemantic::MOVED) { |
61
|
10 |
|
$text->characters(\sprintf('%s %s ', $this->getMovedOutOf(), \mb_strtolower($this->getArticle()))); |
62
|
10 |
|
$text->startElement('b'); |
63
|
10 |
|
$text->characters(\mb_strtolower($this->getDescription())); |
64
|
10 |
|
$text->endElement('b'); |
65
|
9 |
|
} elseif ($this->sem === TagChangeSemantic::STYLE) { |
66
|
9 |
|
$text->startElement('b'); |
67
|
9 |
|
$text->characters($this->getDescription()); |
68
|
9 |
|
$text->endElement('b'); |
69
|
9 |
|
$text->characters(\sprintf(' %s', \mb_strtolower($this->getStyleRemoved()))); |
70
|
|
|
} else { |
71
|
|
|
$text->startElement('b'); |
72
|
|
|
$text->characters($this->getDescription()); |
73
|
|
|
$text->endElement('b'); |
74
|
|
|
$text->characters(\sprintf(' %s', \mb_strtolower($this->getRemoved()))); |
75
|
|
|
} |
76
|
|
|
|
77
|
19 |
|
$this->addAttributes($text, $this->node->getAttributes()); |
78
|
19 |
|
$text->characters('.'); |
79
|
19 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param ChangeText $text |
83
|
|
|
*/ |
84
|
19 |
|
public function getAddedDescription(ChangeText $text): void |
85
|
|
|
{ |
86
|
19 |
|
$this->htmlLayoutChange = new HtmlLayoutChange(); |
87
|
19 |
|
$this->htmlLayoutChange->setEndingTag($this->node->getEndTag()); |
88
|
19 |
|
$this->htmlLayoutChange->setOpeningTag($this->node->getOpeningTag()); |
89
|
19 |
|
$this->htmlLayoutChange->setType(HtmlLayoutChange::TAG_ADDED); |
90
|
|
|
|
91
|
19 |
|
if ($this->sem === TagChangeSemantic::MOVED) { |
92
|
6 |
|
$text->characters(\sprintf('%s %s ', $this->getMovedTo(), \mb_strtolower($this->getArticle()))); |
93
|
6 |
|
$text->startElement('b'); |
94
|
6 |
|
$text->characters(\mb_strtolower($this->getDescription())); |
95
|
6 |
|
$text->endElement('b'); |
96
|
13 |
|
} elseif ($this->sem === TagChangeSemantic::STYLE) { |
97
|
11 |
|
$text->startElement('b'); |
98
|
11 |
|
$text->characters($this->getDescription()); |
99
|
11 |
|
$text->endElement('b'); |
100
|
11 |
|
$text->characters(\sprintf(' %s', \mb_strtolower($this->getStyleAdded()))); |
101
|
|
|
} else { |
102
|
4 |
|
$text->startElement('b'); |
103
|
4 |
|
$text->characters($this->getDescription()); |
104
|
4 |
|
$text->endElement('b'); |
105
|
4 |
|
$text->characters(\sprintf(' %s', \mb_strtolower($this->getAdded()))); |
106
|
|
|
} |
107
|
|
|
|
108
|
19 |
|
$this->addAttributes($text, $this->node->getAttributes()); |
109
|
19 |
|
$text->characters('.'); |
110
|
19 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
6 |
|
protected function getMovedTo(): string |
116
|
|
|
{ |
117
|
6 |
|
return $this->getString('diff-movedto'); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
11 |
|
protected function getStyleAdded(): string |
124
|
|
|
{ |
125
|
11 |
|
return $this->getString('diff-styleadded'); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
4 |
|
protected function getAdded(): string |
132
|
|
|
{ |
133
|
4 |
|
return $this->getString('diff-added'); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
10 |
|
protected function getMovedOutOf(): string |
140
|
|
|
{ |
141
|
10 |
|
return $this->getString('diff-movedoutof'); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
9 |
|
protected function getStyleRemoved(): string |
148
|
|
|
{ |
149
|
9 |
|
return $this->getString('diff-styleremoved'); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
protected function getRemoved(): string |
156
|
|
|
{ |
157
|
|
|
return $this->getString('diff-removed'); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param ChangeText $text |
162
|
|
|
* @param array $attributes |
163
|
|
|
*/ |
164
|
24 |
|
protected function addAttributes(ChangeText $text, array $attributes): void |
165
|
|
|
{ |
166
|
24 |
|
if (empty($attributes)) { |
167
|
20 |
|
return; |
168
|
|
|
} |
169
|
|
|
|
170
|
9 |
|
$arr = []; |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @var string $qName |
174
|
|
|
* @var string $value |
175
|
|
|
*/ |
176
|
9 |
|
foreach ($attributes as $qName => $value) { |
177
|
9 |
|
$arr[] = \sprintf('%s %s', $this->translateArgument($qName), $value); |
178
|
|
|
} |
179
|
|
|
|
180
|
9 |
|
$text->characters(\sprintf('%s %s', \mb_strtolower($this->getWith()), \implode(', ', $arr))); |
181
|
9 |
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return string |
185
|
|
|
*/ |
186
|
|
|
protected function getAnd(): string |
187
|
|
|
{ |
188
|
|
|
return $this->getString('diff-and'); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @return string |
193
|
|
|
*/ |
194
|
9 |
|
protected function getWith(): string |
195
|
|
|
{ |
196
|
9 |
|
return $this->getString('diff-with'); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param string $name |
201
|
|
|
* @return string |
202
|
|
|
*/ |
203
|
9 |
|
protected function translateArgument(string $name): string |
204
|
|
|
{ |
205
|
9 |
|
if (0 === \strcasecmp($name, 'src')) { |
206
|
|
|
return \mb_strtolower($this->getSource()); |
207
|
|
|
} |
208
|
|
|
|
209
|
9 |
|
if (0 === \strcasecmp($name, 'width')) { |
210
|
4 |
|
return \mb_strtolower($this->getWidth()); |
211
|
|
|
} |
212
|
|
|
|
213
|
5 |
|
if (0 === \strcasecmp($name, 'height')) { |
214
|
|
|
return \mb_strtolower($this->getHeight()); |
215
|
|
|
} |
216
|
|
|
|
217
|
5 |
|
return $name; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @return string |
222
|
|
|
*/ |
223
|
4 |
|
protected function getWidth(): string |
224
|
|
|
{ |
225
|
4 |
|
return $this->getString('diff-width'); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @return string |
230
|
|
|
*/ |
231
|
|
|
protected function getHeight(): string |
232
|
|
|
{ |
233
|
|
|
return $this->getString('diff-height'); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @return string |
238
|
|
|
*/ |
239
|
|
|
protected function getSource(): string |
240
|
|
|
{ |
241
|
|
|
return $this->getString('diff-source'); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @return string |
246
|
|
|
*/ |
247
|
12 |
|
protected function getArticle(): string |
248
|
|
|
{ |
249
|
12 |
|
return $this->getString(\sprintf('diff-%s-article', $this->node->getQName())); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param string $key |
254
|
|
|
* @return string |
255
|
|
|
*/ |
256
|
27 |
|
public function getString(string $key): string |
257
|
|
|
{ |
258
|
|
|
$trans = [ |
259
|
27 |
|
'diff-movedto' => 'Moved to', |
260
|
|
|
'diff-styleadded' => 'Style added', |
261
|
|
|
'diff-added' => 'Added', |
262
|
|
|
'diff-changedto' => 'Changed to', |
263
|
|
|
'diff-movedoutof' => 'Moved out of', |
264
|
|
|
'diff-styleremoved' => 'Style removed', |
265
|
|
|
'diff-removed' => 'Removed', |
266
|
|
|
'diff-changedfrom' => 'Changed from', |
267
|
|
|
'diff-source' => 'Source', |
268
|
|
|
'diff-withdestination' => 'With destination', |
269
|
|
|
'diff-and' => 'And', |
270
|
|
|
'diff-with' => 'With', |
271
|
|
|
'diff-width' => 'Width', |
272
|
|
|
'diff-height' => 'Height', |
273
|
|
|
'diff-html-article' => 'A', |
274
|
|
|
'diff-html' => 'Html page', |
275
|
|
|
'diff-body-article' => 'A', |
276
|
|
|
'diff-body' => 'Html document', |
277
|
|
|
'diff-p-article' => 'A', |
278
|
|
|
'diff-p' => 'Paragraph', |
279
|
|
|
'diff-blockquote-article' => 'A', |
280
|
|
|
'diff-blockquote' => 'Quote', |
281
|
|
|
'diff-h1-article' => 'A', |
282
|
|
|
'diff-h1' => 'Heading (level 1)', |
283
|
|
|
'diff-h2-article' => 'A', |
284
|
|
|
'diff-h2' => 'Heading (level 2)', |
285
|
|
|
'diff-h3-article' => 'A', |
286
|
|
|
'diff-h3' => 'Heading (level 3)', |
287
|
|
|
'diff-h4-article' => 'A', |
288
|
|
|
'diff-h4' => 'Heading (level 4)', |
289
|
|
|
'diff-h5-article' => 'A', |
290
|
|
|
'diff-h5' => 'Heading (level 5)', |
291
|
|
|
'diff-pre-article' => 'A', |
292
|
|
|
'diff-pre' => 'Preformatted block', |
293
|
|
|
'diff-div-article' => 'A', |
294
|
|
|
'diff-div' => 'Division', |
295
|
|
|
'diff-ul-article' => 'An', |
296
|
|
|
'diff-ul' => 'Unordered list', |
297
|
|
|
'diff-ol-article' => 'An', |
298
|
|
|
'diff-ol' => 'Ordered list', |
299
|
|
|
'diff-li-article' => 'A', |
300
|
|
|
'diff-li' => 'List item', |
301
|
|
|
'diff-table-article' => 'A', |
302
|
|
|
'diff-table' => 'Table', |
303
|
|
|
'diff-tbody-article' => 'A', |
304
|
|
|
'diff-tbody' => "Table's content", |
305
|
|
|
'diff-tr-article' => 'A', |
306
|
|
|
'diff-tr' => 'Row', |
307
|
|
|
'diff-td-article' => 'A', |
308
|
|
|
'diff-td' => 'Cell', |
309
|
|
|
'diff-th-article' => 'A', |
310
|
|
|
'diff-th' => 'Header', |
311
|
|
|
'diff-br-article' => 'A', |
312
|
|
|
'diff-br' => 'Break', |
313
|
|
|
'diff-hr-article' => 'A', |
314
|
|
|
'diff-hr' => 'Horizontal rule', |
315
|
|
|
'diff-code-article' => 'A', |
316
|
|
|
'diff-code' => 'Computer code block', |
317
|
|
|
'diff-dl-article' => 'A', |
318
|
|
|
'diff-dl' => 'Definition list', |
319
|
|
|
'diff-dt-article' => 'A', |
320
|
|
|
'diff-dt' => 'Definition term', |
321
|
|
|
'diff-dd-article' => 'A', |
322
|
|
|
'diff-dd' => 'Definition', |
323
|
|
|
'diff-input-article' => 'An', |
324
|
|
|
'diff-input' => 'Input', |
325
|
|
|
'diff-form-article' => 'A', |
326
|
|
|
'diff-form' => 'Form', |
327
|
|
|
'diff-img-article' => 'An', |
328
|
|
|
'diff-img' => 'Image', |
329
|
|
|
'diff-span-article' => 'A', |
330
|
|
|
'diff-span' => 'Span', |
331
|
|
|
'diff-a-article' => 'A', |
332
|
|
|
'diff-a' => 'Link', |
333
|
|
|
'diff-i' => 'Italics', |
334
|
|
|
'diff-b' => 'Bold', |
335
|
|
|
'diff-strong' => 'Strong', |
336
|
|
|
'diff-em' => 'Emphasis', |
337
|
|
|
'diff-font' => 'Font', |
338
|
|
|
'diff-big' => 'Big', |
339
|
|
|
'diff-del' => 'Deleted', |
340
|
|
|
'diff-tt' => 'Fixed width', |
341
|
|
|
'diff-sub' => 'Subscript', |
342
|
|
|
'diff-sup' => 'Superscript', |
343
|
|
|
'diff-strike' => 'Strikethrough', |
344
|
|
|
]; |
345
|
|
|
|
346
|
27 |
|
return $trans[$key] ?? \sprintf('!%s!', $key); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* @return HtmlLayoutChange |
351
|
|
|
*/ |
352
|
22 |
|
public function getHtmlLayoutChange(): HtmlLayoutChange |
353
|
|
|
{ |
354
|
22 |
|
return $this->htmlLayoutChange; |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
|