[d24f17c] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var applyReviver = require('../doc/applyReviver.js');
|
---|
| 4 | var identity = require('./identity.js');
|
---|
| 5 | var toJS = require('./toJS.js');
|
---|
| 6 |
|
---|
| 7 | class NodeBase {
|
---|
| 8 | constructor(type) {
|
---|
| 9 | Object.defineProperty(this, identity.NODE_TYPE, { value: type });
|
---|
| 10 | }
|
---|
| 11 | /** Create a copy of this node. */
|
---|
| 12 | clone() {
|
---|
| 13 | const copy = Object.create(Object.getPrototypeOf(this), Object.getOwnPropertyDescriptors(this));
|
---|
| 14 | if (this.range)
|
---|
| 15 | copy.range = this.range.slice();
|
---|
| 16 | return copy;
|
---|
| 17 | }
|
---|
| 18 | /** A plain JavaScript representation of this node. */
|
---|
| 19 | toJS(doc, { mapAsMap, maxAliasCount, onAnchor, reviver } = {}) {
|
---|
| 20 | if (!identity.isDocument(doc))
|
---|
| 21 | throw new TypeError('A document argument is required');
|
---|
| 22 | const ctx = {
|
---|
| 23 | anchors: new Map(),
|
---|
| 24 | doc,
|
---|
| 25 | keep: true,
|
---|
| 26 | mapAsMap: mapAsMap === true,
|
---|
| 27 | mapKeyWarned: false,
|
---|
| 28 | maxAliasCount: typeof maxAliasCount === 'number' ? maxAliasCount : 100
|
---|
| 29 | };
|
---|
| 30 | const res = toJS.toJS(this, '', ctx);
|
---|
| 31 | if (typeof onAnchor === 'function')
|
---|
| 32 | for (const { count, res } of ctx.anchors.values())
|
---|
| 33 | onAnchor(res, count);
|
---|
| 34 | return typeof reviver === 'function'
|
---|
| 35 | ? applyReviver.applyReviver(reviver, { '': res }, '', res)
|
---|
| 36 | : res;
|
---|
| 37 | }
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | exports.NodeBase = NodeBase;
|
---|