|
"use strict"; |
|
Object.defineProperty(exports, "__esModule", { value: true }); |
|
exports.update = exports.getParse = void 0; |
|
var domutils_1 = require("domutils"); |
|
var 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); |
|
} |
|
var doc = content; |
|
if (!Array.isArray(doc) && (0, domhandler_1.isDocument)(doc)) { |
|
|
|
return doc; |
|
} |
|
|
|
var root = new domhandler_1.Document([]); |
|
|
|
update(doc, root); |
|
return root; |
|
}; |
|
} |
|
exports.getParse = getParse; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function update(newChilds, parent) { |
|
|
|
var arr = Array.isArray(newChilds) ? newChilds : [newChilds]; |
|
|
|
if (parent) { |
|
parent.children = arr; |
|
} |
|
else { |
|
parent = null; |
|
} |
|
|
|
for (var i = 0; i < arr.length; i++) { |
|
var 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; |
|
} |
|
exports.update = update; |
|
|