|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function findAll(query, elems, options) { |
|
|
const { adapter, xmlMode = false } = options; |
|
|
const result = []; |
|
|
|
|
|
const nodeStack = [elems]; |
|
|
|
|
|
const indexStack = [0]; |
|
|
for (;;) { |
|
|
|
|
|
if (indexStack[0] >= nodeStack[0].length) { |
|
|
|
|
|
if (nodeStack.length === 1) { |
|
|
return result; |
|
|
} |
|
|
nodeStack.shift(); |
|
|
indexStack.shift(); |
|
|
|
|
|
continue; |
|
|
} |
|
|
const elem = nodeStack[0][indexStack[0]++]; |
|
|
if (!adapter.isTag(elem)) |
|
|
continue; |
|
|
if (query(elem)) |
|
|
result.push(elem); |
|
|
if (xmlMode || adapter.getName(elem) !== "template") { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const children = adapter.getChildren(elem); |
|
|
if (children.length > 0) { |
|
|
nodeStack.unshift(children); |
|
|
indexStack.unshift(0); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function findOne(query, elems, options) { |
|
|
const { adapter, xmlMode = false } = options; |
|
|
|
|
|
const nodeStack = [elems]; |
|
|
|
|
|
const indexStack = [0]; |
|
|
for (;;) { |
|
|
|
|
|
if (indexStack[0] >= nodeStack[0].length) { |
|
|
|
|
|
if (nodeStack.length === 1) { |
|
|
return null; |
|
|
} |
|
|
nodeStack.shift(); |
|
|
indexStack.shift(); |
|
|
|
|
|
continue; |
|
|
} |
|
|
const elem = nodeStack[0][indexStack[0]++]; |
|
|
if (!adapter.isTag(elem)) |
|
|
continue; |
|
|
if (query(elem)) |
|
|
return elem; |
|
|
if (xmlMode || adapter.getName(elem) !== "template") { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const children = adapter.getChildren(elem); |
|
|
if (children.length > 0) { |
|
|
nodeStack.unshift(children); |
|
|
indexStack.unshift(0); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
export function getNextSiblings(elem, adapter) { |
|
|
const siblings = adapter.getSiblings(elem); |
|
|
if (siblings.length <= 1) |
|
|
return []; |
|
|
const elemIndex = siblings.indexOf(elem); |
|
|
if (elemIndex < 0 || elemIndex === siblings.length - 1) |
|
|
return []; |
|
|
return siblings.slice(elemIndex + 1).filter(adapter.isTag); |
|
|
} |
|
|
export function getElementParent(node, adapter) { |
|
|
const parent = adapter.getParent(node); |
|
|
return parent != null && adapter.isTag(parent) ? parent : null; |
|
|
} |
|
|
|