forked from mediaxml/mediaxml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.js
2766 lines (2445 loc) · 66 KB
/
parser.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const { WritableStream } = require('htmlparser2/lib/WritableStream')
const InvertedPromise = require('inverted-promise')
const { Readable } = require('streamx')
const { validate } = require('./validate')
const htmlparser2 = require('htmlparser2')
const { inspect } = require('util')
const camelcase = require('camelcase')
const entities = require('entities')
const defined = require('defined')
const debug = require('debug')('mediaxml')
const {
normalizeAttributeValue,
normalizeAttributeKey,
normalizeAttributes,
} = require('./normalize')
/**
* A special name for a _fragment node_ which is a collection of
* nodes without an explicit parent node.
* @protected
* @memberof parser
* @const
* @type {String}
*/
const FRAGMENT_NODE_NAME = '#fragment'
/**
* A special name for a _text node_ which is container for a body of
* string text.
* @protected
* @memberof parser
* @const
* @type {String}
*/
const TEXT_NODE_NAME = '#text'
/**
* A simple container for a `ParserNode` instance attributes.
* @public
* @memberof parser
*/
class ParserNodeAttributes {
/**
* `ParserNodeAttributes` class constructor.
* @protected
* @param {?Object} attributes
* @param {?Object} opts
*/
constructor(attributes, opts) {
Object.defineProperty(this, 'keylist', {
configurable: false,
enumerable: false,
value: new Set()
})
Object.defineProperty(this, 'originalKeys', {
configurable: false,
enumerable: false,
value: new Set()
})
Object.defineProperty(this, 'options', {
configurable: false,
enumerable: false,
value: opts || {}
})
this.toJSON = null
Object.defineProperty(this, 'toJSON', {
configurable: false,
enumerable: false,
value(opts) {
if (true === opts) {
opts = { normalize: true }
} else {
opts = { ...opts }
}
if (!opts.normalize) {
return { ...this }
}
return normalizeAttributes({ ...this }, {
preserveConsecutiveUppercase: false,
normalizeValues: true,
...opts
})
}
})
if (attributes && 'object' === typeof attributes) {
for (const key in attributes) {
this.set(key, attributes[key])
}
}
}
/**
* Clear all known attribute values for all known keys
*/
clear() {
for (const key of this.keylist) {
delete this[key]
Object.defineProperty(this, key, {
configurable: true,
enumerable: false
})
}
this.keylist.clear()
this.originalKeys.clear()
}
/**
* `true` if instance has a value for a given key.
* @public
* @param {String} name
* @return {Boolean}
*/
has(name) {
if (!name || 'string' !== typeof name) { return false }
return name in this || undefined !== this.get(name)
}
/**
* Get an attribute value by name.
* @public
* @param {String} name
* @return {?Mixed}
*/
get(name) {
return defined(this[normalizeAttributeKey(name, this.options)], null)
}
/**
* Set an attribute value by name.
* @public
* @param {String} name
* @param {?Mixed} value
*/
set(key, value) {
if (key && 'object' === typeof key) {
for (const k in key) {
this.set(k, key[k])
}
} else {
const normalizedKey = normalizeAttributeKey(key, this.options)
value = normalizeAttributeValue(value, this.options)
this.originalKeys.add(key)
this.keylist.add(key)
this.keylist.add(normalizedKey)
if (key !== normalizedKey) {
Object.defineProperty(this, key, {
configurable: true,
enumerable: true,
set: (val) => { value = normalizeAttributeValue(val, this.options) },
get: () => value
})
Object.defineProperty(this, normalizedKey, {
configurable: true,
enumerable: false,
set: (val) => { value = normalizeAttributeValue(val, this.options) },
get: () => value
})
} else {
Object.defineProperty(this, normalizedKey, {
configurable: true,
enumerable: true,
set: (val) => { value = normalizeAttributeValue(val, this.options) },
get: () => value
})
}
}
}
/**
* Computed keys for this attributes object.
* @public
* @return {Array<String>}
*/
keys() {
return Array.from(this.originalKeys)
}
/**
* Computed values for this attributes object.
* @public
* @return {Array<String>}
*/
values() {
return Object.values(this)
}
/**
* Returns an iterable generator for this attributes object.
* @public
* @return {Generator}
*/
iterator() {
return this[Symbol.iterator]()
}
/**
* Implements `Symbol.iterator` symbol for converting this to an
* iterable object of key-value pairs
* @protected
* @return {Iterator}
*/
*[Symbol.iterator]() {
const keys = this.keys()
const values = this.values()
for (let i = 0; i < keys.length; ++i) {
yield [keys[i], values[i]]
}
}
/**
* Implements `util.inspect.custom` symbol for pretty output.
* @private
* @return {String}
*/
[inspect.custom]() {
const attributes = this.toJSON({ normalize: false, normalizeValues: true })
const { name } = this.constructor
const json = {}
for (const key of this.keys()) {
const normal = normalizeAttributeKey(key)
if (normal in attributes) {
json[key] = attributes[normal]
} else if (key in attributes) {
json[key] = attributes[key]
}
}
return `${name} ${inspect(json, { colors: true })}`
}
/**
* Converts attributes to a JSON object.
* @public
* @type {Function}
* @param {?Object|Boolean} opts - JSON output configuration. Set to `true` to just normalize.
* @param {?Object} [opts.normalize = false] - Normalize JSON output
* @param {?Object} [opts.normalizeValues = false] - Normalize JSON output values
* @return {Object}
*/
toJSON() {
return { ...this }
}
}
/**
* A container for a text.
* @public
* @memberof parser
*/
class ParserNodeText extends String {
/**
* Create a new `ParserNodeText` from input. Input is coalesced to a
* string type.
* @public
* @static
* @param {?Mixed} input
* @return {ParserNodeText}
*/
static from(input, ...args) {
return new this(String(defined(input, '')), ...args)
}
/**
* `ParserNodeText` class constructor.
* @private
* @param {String} text
*/
constructor(text, opts, depth) {
text = text || ''
text = text.trim()
super(text)
Object.defineProperty(this, 'text', {
enumerable: false,
configurable: false,
get: () => text
})
Object.defineProperty(this, 'depth', {
enumerable: false,
configurable: false,
get: () => depth || 0
})
let parent = opts && opts.parent ? opts.parent : null
Object.defineProperty(this, 'parent', {
configurable: true,
enumerable: false,
get: () => parent,
set: (value) => { parent = value }
})
const prototype = Object.getPrototypeOf(this.constructor.prototype)
const descriptors = Object.getOwnPropertyDescriptors(prototype)
for (const key in descriptors) {
const descriptor = descriptors[key]
const { value } = descriptor
if (['toString', 'valueOf'].includes(key)) {
continue
} else if ('function' === typeof value) {
descriptor.value = (...args) => {
if ('function' == typeof this.text[key]) {
const result = this.text[key](...args)
if (Array.isArray(result)) {
return result.map((r) => ParserNodeText.from(r))
}
return ParserNodeText.from(result)
}
return this
}
if (descriptor.configurable) {
Object.defineProperty(this, key, descriptor)
}
}
}
}
/**
* The name of this text node.
* @public
* @type {String}
*/
set name(value) { void value }
get name() { return TEXT_NODE_NAME }
/**
* The text content of this text node.
* @public
* @type {String}
*/
set text(value) { void value }
get text() { return '' }
/**
* `true` to indicate this node is a text node.
* @public
* @accessor
* @type {Boolean}
*/
get isText() {
return true
}
/**
* A reference to the parent node of this text node.
* @public
* @accessor
* @type {?ParserNode}
*/
set parent(value) { void value }
get parent() { return null }
/**
* Returns an iterable generator for this text node.
* @public
* @return {Generator}
*/
iterator() {
return this[Symbol.iterator]()
}
/**
* Computed string of this text node.
* @protected
* @return {String}
*/
toString() {
return this.text
}
/**
* Computed value of this text node.
* @protected
* @return {String}
*/
valueOf() {
return this.toString()
}
/**
* Converts this text node to a string for JSON
* output
* @public
* @return {String}
*/
toJSON() {
return this.toString()
}
/**
* Implements `util.inspect.custom` symbol for pretty output.
* @private
* @return {String}
*/
[inspect.custom]() {
return inspect(this.text, { colors: true })
}
/**
* Implements `Symbol.iterator` symbol for converting this text node
* to an iterable string.
* @public
* @return {Iterator}
*/
*[Symbol.iterator]() {
for (let i = 0; i < this.text.length; ++i) {
yield this.text[i]
}
}
}
/**
* A container for CDATA text data.
* @public
* @memberof parser
*/
class ParserNodeCDATA extends ParserNodeText {
/**
* Always `true` to indicate that this is CDATA.
* @public
* @accessor
* @type {Boolean}
*/
get isCDATA() {
return true
}
/**
* Converts this CDATA text container to mark up.
* @public
* @return {String}
*/
toString() {
const { text } = this
return `<![CDATA[${text}]]>`
}
}
/**
* A container for a collection of `ParserNode` instances not represented by a root
* `ParserNode` instance.
* @public
* @memberof parser
*/
class ParserNodeFragment extends Array {
/**
* Create a `ParserNodeFragment` from input.
* @public
* @static
* @param {Mixed} input
* @return {ParserNodeFragment}
*/
static from(input, ...args) {
if (Array.isArray(input)) {
return new this(null, { children: input })
} else if (input instanceof this) {
return new this(input.node.originalAttributes, input.node.options)
} else {
return new this(input, ...args)
}
}
/**
* `ParserNodeFragment` class constructor.
* @private
* @param {?Object} attributes
* @param {?Object} opts
*/
constructor(attributes, opts) {
if (!opts) {
opts = {}
}
const children = Array.isArray(opts.children) ? [ ...opts.children ].filter(Boolean) : []
super(children.length)
for (let i = 0; i < children.length; ++i) {
Object.defineProperty(this, i, {
configurable: false,
enumerable: false,
value: children[i]
})
}
const node = opts.node || ParserNode.from(FRAGMENT_NODE_NAME, attributes, opts)
Object.defineProperty(this, 'node', {
configurable: false,
enumerable: false,
get: () => node
})
Object.freeze(this)
}
/**
* The `ParserNode` instance this fragment wraps.
* @public
* @type {ParserNode}
*/
set node(value) { void value }
get node() { return null }
/**
* `true` if this node is connected to a parent node.
* @public
* @accessor
* @type {Boolean}
*/
get isConnected() {
return false
}
/**
* `true` if this node is not connected to a parent node and is not a root node.
* @public
* @accessor
* @type {Boolean}
*/
get isOrphaned() {
return !this.isConnected && 0 === this.children.length
}
/**
* Will always be `true` because it is a fragment.
* @public
* @accessor
* @type {Boolean}
*/
get isFragment() {
return true
}
/**
* A reference to the children in the underlying `ParserNode` for
* this fragment.
* @public
* @accessor
* @type {Array}
*/
get children() {
return this.node.children
}
/**
* Always `null` as a fragment cannot have a parent.
* @public
* @accessor
* @type {?ParserNode}
*/
get parent() {
return null
}
/**
* Query the nodes this fragment represents.
* @public
* @param {String} queryString
* @param {?Object} opts
* @return {ParserNode|ParserNodeFragment|ParserNodeText}
*/
query(queryString, opts) {
return this.node.query(queryString, {
model: this.children,
...opts,
})
}
/**
* Converts this fragment to a string.
* @public
* @return {String}
*/
toString(...args) {
return this.node.toString(...args)
}
/**
* Converts this fragment to a JSON object.
* @public
* @return {Array}
*/
toJSON() {
return this.children
}
}
/**
* A container for a parsed XML node with references to
* its parent node and children.
* @public
* @memberof parser
* @example
* const metadata = ParserNode.from('Metadata')
* const appData = ParserNode.from('App_Data', { app: 'SVOD', name: 'Type', value: 'title' })
* metadata.appendChild(appData)
*
* console.log(metadata)
* // <Metadata>
* // <App_Data app="SVOD" name="Type" value="title" />
* // </Metadata>
*/
class ParserNode {
/**
* A reference to the `Fragment` class for a `ParserNode` instance.
* @public
* @static
* @accessor
* @type {ParserNodeFragment}
*/
static get Fragment() {
return ParserNodeFragment
}
/**
* A reference to the `Text` class for a `ParserNode` instance.
* @public
* @static
* @accessor
* @type {ParserNodeText}
*/
static get Text() {
return ParserNodeText
}
/**
* Predicate function to help determine if input is a valid `ParserNode` instance.
* @public
* @static
* @param {Mixed} input
* @return {Boolean}
*/
static isParserNode(input) {
return (
input instanceof ParserNode ||
input instanceof this.Fragment ||
input instanceof this.Text ||
input instanceof this
)
}
/**
* Creates a fragment parser node.
* @public
* @static
* @see {ParserNode#from}
* @param {?Object} attributes
* @param {?Object} opts
* @return {ParserNodeFragment}
*/
static createFragment(attributes, opts) {
return this.Fragment.from(attributes, opts)
}
/**
* Creates a fragment parser node.
* @public
* @static
* @see {ParserNodeText#from}
* @param {?String} text
* @return {ParserNodeText}
*/
static createText(text) {
return this.Text.from(text)
}
/**
* Creates a new `ParserNode` from input.
* @public
* @static
* @param {String|ParserNode|Object} nameOrNode
* @param {?Object} attributes
* @param {?Object} opts
* @return {ParserNode}
* @example
* const node = ParserNode.from('App_Data', { app: 'SVOD', name: 'Type', value: 'title' })
* console.log(node)
* // <App_Data app="SVOD" name="Type" value="title" />
*/
static from (nameOrNode, attributes, opts) {
if (this.isParserNode(nameOrNode)) {
opts = attributes
const { originalName, originalAttributes, depth, options } = nameOrNode
return new this(originalName, attributes || originalAttributes, depth, { ...options, ...opts })
}
if ('string' === typeof nameOrNode) {
const trimmed = nameOrNode.trim()
if (/^</.test(trimmed) && />$/.test(trimmed)) {
const tmp = new this()
tmp.innerXML = nameOrNode
if (tmp.children && tmp.children.length) {
return tmp.children[0]
}
}
return new this(nameOrNode, attributes, 0, opts)
}
if (nameOrNode && 'object' === typeof nameOrNode) {
const { originalName, originalAttributes } = nameOrNode
const { name, attributes } = nameOrNode
let { options, children, ...rest } = nameOrNode
if (Array.isArray(children)) {
children = children.map((child) => {
if (child instanceof ParserNode) {
return child
} else if ('string' === typeof child && !/^</.test(child.trim()) && !/>$/.test(child.trim())) {
return this.Text.from(child)
} else {
return this.from(child)
}
})
}
return new this(
originalName || name,
originalAttributes || attributes,
0,
{ ...options, ...rest, children })
}
return new this(nameOrNode, attributes, 0, opts)
}
/**
* Create an empty `ParserNode` instance.
* @public
* @static
* @return {ParserNode}
*/
static empty() {
return this.from()
}
/**
* `ParserNode` class constructor.
* @private
* @param {String} name
* @param {?Object} attributes
* @param {?Number} depth
* @param {?Object} opts
*/
constructor(name, attributes, depth, opts) {
let originalAttributes = attributes
let originalName = name || ''
name = normalizeName(name)
Object.defineProperty(this, 'originalName', {
configurable: false,
enumerable: false,
get: () => originalName,
set: (value) => { this.name = value }
})
Object.defineProperty(this, 'name', {
configurable: true,
enumerable: false,
get: () => name,
set: (value) => {
originalName = value
name = normalizeName(value)
}
})
Object.defineProperty(this, 'depth', {
configurable: true,
enumerable: false,
set: (value) => { depth = value },
get: () => depth || 0
})
let parent = opts && opts.parent ? opts.parent : null
Object.defineProperty(this, 'parent', {
configurable: true,
enumerable: false,
get: () => parent,
set: (value) => { parent = value }
})
Object.defineProperty(this, 'options', {
configurable: false,
enumerable: false,
get: () => opts || null
})
const children = (
Array.from(Array.isArray(opts && opts.children) ? opts.children : [])
.map((child) => child && child._data ? child._data : child)
)
Object.defineProperty(children, 'toJSON', {
configurable: false,
enumerable: false,
get: () => children.map((child) => child.toJSON())
})
Object.defineProperty(this, 'children', {
configurable: false,
enumerable: true,
get: () => children
})
const comments = Array.isArray(opts && opts.comments) ? opts.comments : []
Object.defineProperty(this, 'comments', {
configurable: false,
enumerable: false,
get: () => comments
})
attributes = new ParserNodeAttributes(originalAttributes, opts)
Object.defineProperty(this, 'attributes', {
configurable: false,
enumerable: false,
get: () => attributes
})
Object.defineProperty(this, 'originalAttributes', {
configurable: false,
enumerable: false,
get: () => originalAttributes || {},
set: (value) => { originalAttributes = value }
})
function normalizeName(name) {
if ('string' === typeof name) {
//return name.split(/[_|-]/).map((n) => n.)
return name.replace(/([a-z|A-Z|0-9])([_|-|:])/i, (str, $1, $2) => {
return camelcase($1) + $2
})
}
}
}
/**
* The inner XML representation of this node.
* Setting the inner XML of this node will update body and children
* of this node instance. If a string is given,
* it is parsed and the body and children of this node will
* be updated. If a `ParserNode` instance is given, or an array of
* them, the children will be updated to include those nodes.
* @public
* @accessor
* @type {String}
* @example
* const node = ParserNode.from('rss', {
* 'xmlns:atom': 'http://www.w3.org/2005/Atom',
* 'xmlns:media': http://search.yahoo.com/mrss/',
* 'version': '2.0'
* })
*
* node.innerXML = `
* <channel>
* <title>Calm Meditation</title>
* <link>http://sample-firetv-web-app.s3-website-us-west-2.amazonaws.com</link>
* <language>en-us</language>
* <pubDate>Mon, 02 Apr 2018 16:19:56 -0700</pubDate>
* <lastBuildDate>Mon, 02 Apr 2018 16:19:56 -0700</lastBuildDate>
* <managingEditor>[email protected] (Tom Johnson)</managingEditor>
* <description>Contains short videos capturing still scenes from nature with a music background, intended for calming or meditation purposes. When you're stressed out or upset, watch a few videos. As your mind focuses on the small details, let your worries and frustrations float away. The purpose is not to entertain or to distract, but to help calm, soothe, and surface your inner quiet. The videos contain scenes from the San Tomas Aquinas trail in Santa Clara, California.</description>
* </channel>
* `
*
* console.log(node)
* // <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
* // <channel>
* // <title>Calm Meditation</title>
* // <link>http://sample-firetv-web-app.s3-website-us-west-2.amazonaws.com</link>
* // <language>en-us</language>
* // <pubDate>Mon, 02 Apr 2018 16:19:56 -0700</pubDate>
* // <lastBuildDate>Mon, 02 Apr 2018 16:19:56 -0700</lastBuildDate>
* // <managingEditor>[email protected] (Tom Johnson)</managingEditor>
* // <description>Contains short videos capturing still scenes from nature with a music background, intended for calming or meditation purposes. When you're stressed out or upset, watch a few videos. As your mind focuses on the small details, let your worries and frustrations float away. The purpose is not to entertain or to distract, but to help calm, soothe, and surface your inner quiet. The videos contain scenes from the San Tomas Aquinas trail in Santa Clara, California.</description>
* // </channel>
* // </rss>
*/
get innerXML() { return this.children.join('\n') }
set innerXML(value) {
if (null === value || '' === value) {
this.remove(...this.children)
return
}
if (value instanceof this.constructor) {
this.remove(...this.children)
this.appendChild(value)
return
}
if (Array.isArray(value)) {
this.remove(...this.children)
for (const child of value) {
if (child instanceof this.constructor) {
this.appendChild(child)
}
}
return
}
if ('string' !== typeof value) {
throw new TypeError('Invalid value when setting \'innerXML\'')
}
const pre = `<node>\n`
const post = `\n</node>`
const source = pre + value + post
const parser = Parser.from(validate(source))
this.remove(...this.children)
if (parser.rootNode) {
this.append(...parser.rootNode.children)
}
}
/**
* The outer XML representation of this node.
* Setting the outer XML of this node will update the attributes,
* name, and children of this node instance. If a string is given,
* it is parsed and will represent the new node If a `ParserNode`
* instance is given it will be used to derive the node's new state.
* @public
* @accessor
* @type {String}
* @example
* const node = ParserNode.empty()
* node.outerXML = `
* <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
* <channel>
* <title>Calm Meditation</title>
* <link>http://sample-firetv-web-app.s3-website-us-west-2.amazonaws.com</link>
* <description>Contains short videos capturing still scenes from nature with a music background, intended for calming or meditation purposes. When you're stressed out or upset, watch a few videos. As your mind focuses on the small details, let your worries and frustrations float away. The purpose is not to entertain or to distract, but to help calm, soothe, and surface your inner quiet. The videos contain scenes from the San Tomas Aquinas trail in Santa Clara, California.</description>
* </channel>
* </rss>
* `
*/
get outerXML() { return this.toString() }
set outerXML(value) {
if (null === value || '' === value) {
this.name = ''
this.remove(...this.children)
return
}
if (value instanceof this.constructor) {
this.name = value.originalName || value.name
this.remove(...this.children)
this.append(...value.child)
return
}
if ('string' !== typeof value) {
throw new TypeError('Invalid value when setting \'outerXML\'')
}
const parser = Parser.from(validate(value))
if (parser.rootNode) {
this.name = parser.rootNode.originalName
this.originalAttributes = parser.rootNode.originalAttributes
this.originalName = parser.rootNode.originalName
this.attributes.clear()
this.attributes.set(parser.rootNode.attributes)
this.remove(...this.children)
this.append(...parser.rootNode.children)
}
}
/**
* The original name of this node.
* @public
* @accessor
* @type {String}
*/
set originalName(value) { }