本文整理匯總了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;
}