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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
<?php
/**
* PHP EPub Meta library
*
* @author Andreas Gohr <andi@splitbrain.org>
* @author Sébastien Lucas <sebastien@slucas.fr>
*/
namespace SebLucas\EPubMeta\Dom;
use DOMElement;
/**
* EPUB-specific subclass of DOMElement
*
* Source: https://github.com/splitbrain/php-epub-meta
* @author Andreas Gohr <andi@splitbrain.org> © 2012
* @author Simon Schrape <simon@epubli.com> © 2015–2018
* @author Sébastien Lucas <sebastien@slucas.fr>
*
* @property string $nodeValueUnescaped
*/
class Element extends DOMElement
{
/** @var array<string, string> */
public static $namespaces = [
'n' => 'urn:oasis:names:tc:opendocument:xmlns:container',
'opf' => 'http://www.idpf.org/2007/opf',
'dc' => 'http://purl.org/dc/elements/1.1/',
'ncx' => 'http://www.daisy.org/z3986/2005/ncx/',
];
/**
* Summary of __construct
* @param string $name
* @param string $value
* @param string $namespaceUri
*/
public function __construct($name, $value = '', $namespaceUri = '')
{
[$prefix, $name] = $this->splitQualifiedName($name);
$value = htmlspecialchars($value);
if (!$namespaceUri && $prefix) {
//$namespaceUri = XmlNamespace::getUri($prefix);
$namespaceUri = static::$namespaces[$prefix];
}
parent::__construct($name, $value, $namespaceUri);
}
/**
* Summary of __get
* @param string $name
* @return string|null
*/
public function __get($name)
{
switch ($name) {
case 'nodeValueUnescaped':
return htmlspecialchars_decode($this->nodeValue);
}
return null;
}
/**
* Summary of __set
* @param string $name
* @param mixed $value
* @return void
*/
public function __set($name, $value)
{
switch ($name) {
case 'nodeValueUnescaped':
$this->nodeValue = htmlspecialchars($value);
}
}
/**
* Create and append a new child
*
* Works with our epub namespaces and omits default namespaces
* @param string $name
* @param string $value
* @return Element|bool
*/
public function newChild($name, $value = '')
{
[$localName, $namespaceUri] = $this->getNameContext($name);
// this doesn't call the constructor: $node = $this->ownerDocument->createElement($name,$value);
$node = new Element($namespaceUri ? $name : $localName, $value, $namespaceUri);
/** @var Element $node */
$node = $this->appendChild($node);
return $node;
}
/**
* Simple EPUB namespace aware attribute getter
* @param string $name
* @return string
*/
public function getAttrib($name)
{
[$localName, $namespaceUri] = $this->getNameContext($name);
// return value if none was given
if ($namespaceUri) {
return $this->getAttributeNS($namespaceUri, $localName);
} else {
return $this->getAttribute($localName);
}
}
/**
* Simple EPUB namespace aware attribute setter
* @param string $name
* @param mixed $value
* @return void
*/
public function setAttrib($name, $value)
{
[$localName, $namespaceUri] = $this->getNameContext($name);
if ($namespaceUri) {
$this->setAttributeNS($namespaceUri, $localName, $value);
} else {
$this->setAttribute($localName, $value);
}
}
/**
* Simple EPUB namespace aware attribute remover
* @param string $name
* @return void
*/
public function removeAttrib($name)
{
[$localName, $namespaceUri] = $this->getNameContext($name);
if ($namespaceUri) {
$this->removeAttributeNS($namespaceUri, $localName);
} else {
$this->removeAttribute($localName);
}
}
/**
* Remove this node from the DOM
* @return void
*/
public function delete()
{
$this->parentNode->removeChild($this);
}
/**
* Split given name in namespace prefix and local part
*
* @param string $name
* @return array<string> (prefix, name)
*/
protected function splitQualifiedName($name)
{
$list = explode(':', $name, 2);
if (count($list) < 2) {
array_unshift($list, '');
}
return $list;
}
/**
* @param string $name
* @return array<string>
*/
protected function getNameContext($name)
{
[$prefix, $localName] = $this->splitQualifiedName($name);
$namespaceUri = '';
if ($prefix) {
//$namespaceUri = XmlNamespace::getUri($prefix);
$namespaceUri = static::$namespaces[$prefix];
if (
!$this->namespaceURI && $this->isDefaultNamespace($namespaceUri)
|| $this->namespaceURI == $namespaceUri
) {
$namespaceUri = '';
}
}
return [$localName, $namespaceUri];
}
}
|