import Container from './Container';
import NodeList from './NodeList';
/** Return a new {@link Fragment} {@Link Node}.
* @extends Container
*/
class Fragment extends Container {
/**
* @param {FragmentSettings} settings - Custom settings applied to the {@link Fragment}.
* @example
* new Fragment() // returns an empty fragment
*
* new Fragment({ nodes: [ new Element('span') ] }) // returns a fragment with a <span>
*/
constructor (settings) {
super();
/** Type identifer of the Fragment
* @type {'fragment'} */
this.type = 'fragment';
/** Node name of the Fragment
* @type {'#document-fragment'} */
this.name = '#document-fragment';
/** Nodes appended to the Fragment
* @type {Node[]} */
this.nodes = Array.isArray(Object(settings).nodes)
? new NodeList(this, ...Array.from(settings.nodes))
: Object(settings).nodes !== null && Object(settings).nodes !== undefined
? new NodeList(this, settings.nodes)
: new NodeList(this);
/** Source mapping of the Fragment
* @type {FragmentSource} */
this.source = Object(Object(settings).source);
/** Current result applied to the Fragment
* @type {Result} */
this.result = settings.result;
}
/**
* Return a clone of the current {@link Fragment}.
* @param {boolean} isDeep - Whether the descendants of the current Fragment should also be cloned.
*/
clone (isDeep) {
const clone = new Fragment({ ...this, nodes: [] });
if (isDeep) {
clone.nodes = this.nodes.clone(clone);
}
return clone;
}
/**
* Return the current {@link Fragment} as an Array.
* @returns {Array<Node|string>}
* @example
* fragment.toJSON() // returns []
*/
toJSON () {
return this.nodes.toJSON();
}
/**
* Return the current {@link Fragment} as a String.
* @example
* fragment.toJSON() // returns ''
*/
toString () {
return String(this.nodes);
}
}
export default Fragment;
/**
* @typedef {Object} FragmentSettings - Custom settings applied to the Fragment.
* @property {Node[]} nodes - Nodes appended to the Fragment.
* @property {FragmentSource} source - Source mapping of the Fragment.
* @property {Result} result - Result applied to the Fragment.
*
* @typedef {Object} FragmentSource - Source mapping of the Fragment.
*/