forked from mediaxml/mediaxml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
document.js
263 lines (238 loc) · 5.46 KB
/
document.js
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
const { ParserNode, Parser } = require('./parser')
const { Entity } = require('./entity')
/**
* An abstract document node.
* @public
* @abstract
* @memberof document
* @see [ParserNode](#parserparsernode)
* @param {String} name
* @param {?Object} attributes
* @param {?Number} depth
* @param {?Object} opts
*/
class Node extends ParserNode { }
/**
* An abstract document object model.
* @public
* @abstract
* @memberof document
*/
class AbstractDocument extends Node {
/**
* A reference to the `Node` class used by this
* document.
* @public
* @static
* @accessor
* @type {Node}
*/
static get Node() {
return Node
}
/**
* The node name of the document. This static class property is an abstract
* accessor used to define the name of the document node name. By default,
* this value is an empty string.
* @public
* @static
* @abstract
* @accessor
* @type {String}
*/
static get nodeName() {
return ''
}
/**
* Create a new `Document` instance from input
* @public
* @static
* @param {Document|Parser|String|ReadableStream} input
* @param {?Object} opts
* @return {Document}
*/
static from(input, opts) {
const { nodeName } = this
opts = { nodeName, ...opts }
if (input instanceof this) {
return new this(input.parser, opts)
}
if (input instanceof Parser) {
return new this(input, opts)
}
if (input && 'object' === typeof input && input.parser instanceof Parser) {
return new this(input.parser, opts)
}
if ('string' === typeof input) {
if (/[<|>|=|"|'|.]+/g.test(input)) {
return new this(Parser.from(input), opts)
} else {
return new this(Parser.from(input), {
...opts,
nodeName: input
})
}
}
// default parser
return new this(Parser.from(input), opts)
}
/**
* `Document` class constructor.
* @protected
* @constructor
* @param {Parser} parser
* @param {?Object} opts
*/
constructor(parser, opts) {
opts = { ...opts }
super(opts.nodeName, opts.attributes, 0, opts)
this.parser = null
Object.defineProperty(this, 'parser', {
configurable: false,
enumerable: false,
value: parser
})
const init = () => {
const { rootNode } = parser
if (rootNode) {
this.name = rootNode.originalName
this.comments.push(...rootNode.comments)
this.attributes.set(rootNode.attributes)
this.children.splice(0, this.children.length, ...rootNode.children)
Object.assign(this.originalAttributes, rootNode.originalAttributes)
}
}
// sync
if (parser.rootNode) {
init()
} else {
parser.then(init)
}
}
/**
* Calls `callback()` when the document is "ready".
* @public
* @param {Function} callback
* @return {Promise}
*/
ready(callback) {
return this.parser.then(callback)
}
/**
* Creates and appends a child node to this node.
* @public
* @param {ParserNode} node
* @return {ParserNode}
* @throws TypeError
*/
createChild(...args) {
const child = this.constructor.Node.from(...args)
return this.appendChild(child)
}
/**
* Computed keys for this instance.
* @return {Array<String>}
*/
keys() {
return Entity.prototype.keys.call(this, { Super: Node })
}
/**
* Returns a plain JSON object of this instance.
* @public
* @return {Object}
*/
toJSON() {
return this.keys().reduce((json, key) => ({ ...json, [key]: this[key] }), {})
}
}
/**
* An abstract document object model for XML.
* @public
* @abstract
* @memberof document
* @param {Parser} parser
* @param {?Object} opts
*/
class Document extends AbstractDocument {
/**
* The default node name of a document.
* @public
* @static
* @accessor
* @type {String}
*/
static get nodeName() {
return 'xml'
}
}
/**
* Factory for creating `Document` instances.
* @public
* @memberof document
* @return {Document}
*/
function createDocument(...args) {
return Document.from(...args)
}
/**
* Factory for creating `Node` instances.
* @public
* @memberof document
* @return {Node}
*/
function createNode(...args) {
return Node.from(...args)
}
/**
* A core module for working with and building XML documents. The
* interfaces provided by this module are porcelain and be used instead
* of the [_parser API_](#parser).
* @public
* @module document
* @see [ParserNode](#parserparsernode)
* @example
* const { createDocument, createNode } = require('mediaxml/document')
*
* const assets = []
* const document = createDocument(`
* <?xml version="1.0"?>
* <package>
* <assets />
* </package>
* `)
*
* assets.push(createNode('asset', {
* name: 'first',
* url: 'https://example.com/first.mp4'
* }))
*
* assets.push(createNode('asset', {
* name: 'second',
* url: 'https://example.com/second.mp4'
* }))
*
* document.query('[name="assets"]').append(...assets)
*
* console.log(document)
* // <package>
* // <assets>
* // <asset name="first" url="https://example.com/first.mp4" />
* // <asset name="second" url="https://example.com/second.mp4" />
* // </assets>
* // </package>
*
* const urls = document.query('**[name="asset"]:attr(url)')
*
* console.log(urls)
* // ParserNodeFragment(2) [
* // 'https://example.com/first.mp4',
* // 'https://example.com/second.mp4'
* // ]
*/
module.exports = {
AbstractDocument,
createDocument,
createNode,
Document,
Node
}