本文整理汇总了TypeScript中parse5.SAXParser.end方法的典型用法代码示例。如果您正苦于以下问题:TypeScript SAXParser.end方法的具体用法?TypeScript SAXParser.end怎么用?TypeScript SAXParser.end使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类parse5.SAXParser
的用法示例。
在下文中一共展示了SAXParser.end方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: findMatchingEnd
export function findMatchingEnd(text: string, pos: number, hasVoidElements: boolean): Match {
const starts: StartPositions = {};
let depth = 0;
let startFound: { name: string, depth: number, position: number };
let startMatch: Match;
let endMatch: Match;
const toId = (name: string) => name + depth;
const isVoid: (name: string) => boolean = hasVoidElements ? isVoidElement : () => false;
const parser = new parse.SAXParser({ locationInfo: true });
parser.on('startTag', (name: string, attrs, selfClosing, location: parse.LocationInfo) => {
const voidd = selfClosing || isVoid(name);
starts[toId(name)] = location;
if (inRange(pos, location.startOffset, name.length)) {
if (voidd) {
startMatch = { length: name.length, start: location.startOffset + START_LEN };
parser.stop();
} else {
startFound = { name, depth, position: location.startOffset };
}
}
if (!voidd) {
depth++;
}
});
parser.on('endTag', (name: string, location: parse.LocationInfo) => {
depth--;
if (startFound && startFound.name === name && startFound.depth === depth) {
endMatch = { length: name.length, start: startFound.position + START_LEN, end: location.startOffset + END_LEN };
parser.stop();
} else if (inRange(pos, location.startOffset + 1, name.length)) {
startMatch = { length: name.length, start: starts[toId(name)].startOffset + START_LEN, end: location.startOffset + END_LEN };
parser.stop();
}
});
parser.end(text);
return endMatch || startMatch;
}