1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package s9e\TextFormatter |
5
|
|
|
* @copyright Copyright (c) The s9e authors |
6
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
7
|
|
|
*/ |
8
|
|
|
namespace s9e\TextFormatter\Utils\ParsedDOM; |
9
|
|
|
|
10
|
|
|
use const LIBXML_NSCLEAN, SORT_STRING, false; |
11
|
|
|
use function ksort, substr, strpos; |
12
|
|
|
use s9e\SweetDOM\Document as SweetDocument; |
13
|
|
|
use s9e\TextFormatter\Configurator\Validators\TagName; |
14
|
|
|
use s9e\TextFormatter\Configurator\Validators\AttributeName; |
15
|
|
|
use s9e\TextFormatter\Utils; |
16
|
|
|
|
17
|
|
|
class Document extends SweetDocument |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @link https://www.php.net/manual/domdocument.construct.php |
21
|
|
|
* |
22
|
|
|
* @param string $version Version number of the document |
23
|
|
|
* @param string $encoding Encoding of the document |
24
|
|
|
*/ |
25
|
|
|
public function __construct(string $version = '1.0', string $encoding = 'utf-8') |
26
|
|
|
{ |
27
|
|
|
parent::__construct($version, $encoding); |
28
|
|
|
|
29
|
|
|
$this->registerNodeClass('DOMElement', Element::class); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function __toString(): string |
33
|
|
|
{ |
34
|
|
|
$this->formatOutput = false; |
35
|
|
|
$this->normalizeDocument(); |
36
|
|
|
|
37
|
|
|
$xml = $this->saveXML($this->documentElement, LIBXML_NSCLEAN); |
38
|
|
|
$xml = Utils::encodeUnicodeSupplementaryCharacters($xml); |
39
|
|
|
|
40
|
|
|
return ($xml === '<t/>') ? '<t></t>' : $xml; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @link https://www.php.net/manual/en/domdocument.normalizedocument.php |
45
|
|
|
*/ |
46
|
|
|
public function normalizeDocument(): void |
47
|
|
|
{ |
48
|
|
|
parent::normalizeDocument(); |
49
|
|
|
$this->documentElement->normalize(); |
50
|
|
|
|
51
|
|
|
$nodeName = $this->documentElement->firstOf('.//*[name() != "br"][name() != "p"]') ? 'r' : 't'; |
|
|
|
|
52
|
|
|
|
53
|
|
|
$root = $this->createElement($nodeName); |
54
|
|
|
while (isset($this->documentElement->firstChild)) |
55
|
|
|
{ |
56
|
|
|
$root->appendChild($this->documentElement->firstChild); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
$this->documentElement->replaceWith($root); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Create an element that represents a tag |
63
|
|
|
* |
64
|
|
|
* @param string $tagName |
65
|
|
|
* @param array<string, string> $attributes |
66
|
|
|
* @return Element |
67
|
|
|
*/ |
68
|
|
|
public function createTagElement(string $tagName, array $attributes = []): Element |
69
|
|
|
{ |
70
|
|
|
$tagName = TagName::normalize($tagName); |
71
|
|
|
$pos = strpos($tagName, ':'); |
72
|
|
|
|
73
|
|
|
if ($pos === false) |
74
|
|
|
{ |
75
|
|
|
$element = $this->createElement($tagName); |
76
|
|
|
} |
77
|
|
|
else |
78
|
|
|
{ |
79
|
|
|
$prefix = substr($tagName, 0, $pos); |
80
|
|
|
$namespaceURI = 'urn:s9e:TextFormatter:' . $prefix; |
81
|
|
|
$this->documentElement->setAttributeNS( |
82
|
|
|
'http://www.w3.org/2000/xmlns/', |
83
|
|
|
'xmlns:' . $prefix, |
84
|
|
|
$namespaceURI |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
$element = $this->createElementNS($namespaceURI, $tagName); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
foreach ($this->normalizeAttributeMap($attributes) as $attrName => $attrValue) |
91
|
|
|
{ |
92
|
|
|
$element->setAttribute($attrName, $attrValue); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $element; |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param array<string, string> $attributes |
100
|
|
|
* @return array<string, string> $attributes |
101
|
|
|
*/ |
102
|
|
|
protected function normalizeAttributeMap(array $attributes): array |
103
|
|
|
{ |
104
|
|
|
$map = []; |
105
|
|
|
foreach ($attributes as $attrName => $attrValue) |
106
|
|
|
{ |
107
|
|
|
$attrName = AttributeName::normalize($attrName); |
108
|
|
|
$map[$attrName] = (string) $attrValue; |
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
ksort($map, SORT_STRING); |
112
|
|
|
|
113
|
|
|
return $map; |
114
|
|
|
} |
115
|
|
|
} |