當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript SAXParser.end方法代碼示例

本文整理匯總了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;
}
開發者ID:krizzdewizz,項目名稱:vscode-tag-rename,代碼行數:43,代碼來源:rename.ts


注:本文中的parse5.SAXParser.end方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。