Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x | import { VirtualNode } from "../interfaces";
export default function domNodeToVirtualNode(node?: Node, options: any = {}): VirtualNode | string | null {
if (node === null) {
return null;
}
if ((node! as Node).nodeType === 1) { // TODO: Find a faster way of testing that
const element = node! as HTMLElement;
const tag = element.nodeName.toLowerCase();
if (tag === 'script' && !options.allowScripts) {
throw Error('Script elements are not allowed unless the allowScripts option is set to true');
}
const attributes = getAttributes(element.attributes);
const children = getChildren(element.childNodes, options);
return {
tag,
attributes,
children
};
}
else if ((node! as Text).splitText !== undefined) { // text node (fast way of determining)
const content = (node! as Text).textContent || '';
// Do not include text with white space characters ' ', '\t', '\r', '\n'
if (options.excludeTextWithWhiteSpacesOnly &&
/^\s*$/g.test(content)) {
return null;
}
return content;
}
else {
return null;
}
}
function getAttributes(attributes: NamedNodeMap) {
if (attributes === null) {
return null;
}
const count = attributes.length;
if (count == 0) {
return null;
}
const props = {};
for (let i = 0; i < attributes.length; i++) {
const { name, value } = attributes[i];
// if (name.substring(0,2)==='on' && walk.options.allowEvents){
// value = new Function(value); // eslint-disable-line no-new-func
// }
(props as any)[name] = value;
}
return props;
}
function getChildren(childNodes: NodeListOf<ChildNode>, options: any): (VirtualNode | string)[] {
var vnodes: (VirtualNode | string)[] = [];
childNodes.forEach(childNode => {
const vnode = domNodeToVirtualNode(childNode, options);
if (vnode != null) {
vnodes.push(vnode);
}
});
return vnodes;
}
|