| "use strict"; |
| Object.defineProperty(exports, "__esModule", { value: true }); |
| exports.getParse = getParse; |
| exports.update = update; |
| const domutils_1 = require("domutils"); |
| const domhandler_1 = require("domhandler"); |
| |
| |
| |
| |
| |
| |
| function getParse(parser) { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| return function parse(content, options, isDocument, context) { |
| if (typeof Buffer !== 'undefined' && Buffer.isBuffer(content)) { |
| content = content.toString(); |
| } |
| if (typeof content === 'string') { |
| return parser(content, options, isDocument, context); |
| } |
| const doc = content; |
| if (!Array.isArray(doc) && (0, domhandler_1.isDocument)(doc)) { |
| |
| return doc; |
| } |
| |
| const root = new domhandler_1.Document([]); |
| |
| update(doc, root); |
| return root; |
| }; |
| } |
| |
| |
| |
| |
| |
| |
| |
| function update(newChilds, parent) { |
| |
| const arr = Array.isArray(newChilds) ? newChilds : [newChilds]; |
| |
| if (parent) { |
| parent.children = arr; |
| } |
| else { |
| parent = null; |
| } |
| |
| for (let i = 0; i < arr.length; i++) { |
| const node = arr[i]; |
| |
| if (node.parent && node.parent.children !== arr) { |
| (0, domutils_1.removeElement)(node); |
| } |
| if (parent) { |
| node.prev = arr[i - 1] || null; |
| node.next = arr[i + 1] || null; |
| } |
| else { |
| node.prev = node.next = null; |
| } |
| node.parent = parent; |
| } |
| return parent; |
| } |
| |