本文整理匯總了TypeScript中htmlparser2.Parser類的典型用法代碼示例。如果您正苦於以下問題:TypeScript Parser類的具體用法?TypeScript Parser怎麽用?TypeScript Parser使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Parser類的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: rej
return new Promise<number>((res, rej) => {
let lineIndex: number = 0;
let changeFound: boolean;
let tags: string[] = [];
let parser = new htmlparser.Parser({
onopentag: function (name: string, attribs: any) {
tags.push(name);
if (name === 'div' && !attribs.class) {
lineIndex++;
} else if (name === 'div' && attribs.class === changeClass) {
changeFound = true;
parser.end();
}
},
onclosetag: function (name) {
// Terminate once last tag is closed
tags.pop();
if (!changeFound && tags.length === 0) {
parser.end();
}
},
onend: function () {
if (!changeFound) {
return rej(`No changes in the diff found.`);
}
return res(lineIndex);
}
});
parser.write(html);
});
示例2: Promise
return new Promise(async (res, rej) => {
const html = await this.spectron.waitFor(this.spectron.client.getHTML, selector);
let lineIndex: number = 0;
let stringFound: boolean;
let parser = new htmlparser.Parser({
onopentag: function (name: string, attribs: any) {
if (name === 'div' && attribs.class === 'view-line') {
lineIndex++;
}
},
ontext: function (text) {
if (!stringFound && text === string) {
stringFound = true;
parser.end();
}
},
onend: function () {
if (!stringFound) {
return rej(`No ${string} in editor found.`);
}
return res(lineIndex);
}
});
parser.write(html);
});
示例3: make
/**
* 將 source 轉換成 dom 節點樹
*
* @param {string} source
* @returns
*/
function make (source: string) {
let handler = new htmlparser.DomHandler()
let parser = new htmlparser.Parser(handler, {
lowerCaseAttributeNames: false
})
parser.write(source)
parser.done()
return handler.dom
}
示例4: rej
return new Promise<number>((res, rej) => {
let lineIndex: number = 0;
let foldFound: boolean;
let parser = new htmlparser.Parser({
onopentag: function (name: string, attribs: any) {
if (name === 'div' && !attribs.class) {
lineIndex++;
} else if (name === 'div' && attribs.class.indexOf('cldr folding') !== -1) {
foldFound = true;
parser.end();
}
},
onend: function () {
if (!foldFound) {
return rej(`No foldable elements found.`);
}
return res(lineIndex);
}
});
parser.write(html);
});
示例5: parseHTML
export default function parseHTML(src: string) {
var handler = new DomHandler();
var p = new Parser(handler);
p.parseComplete(src);
function fix(dom: DomNode | DomNode[]): DomNode[] {
if (Array.isArray(dom)) {
return dom.map(fix).reduce(function(a, b) {
return a.concat(b);
}, []);
} else if (dom.type != 'tag') {
return [dom];
} else if (selfClosing.indexOf(dom.name.toLowerCase()) === -1) {
if (dom.children) dom.children = fix(dom.children);
else dom.children = [];
return [dom];
} else {
var c = dom.children;
dom.children = [];
return [dom as DomNode].concat(fix(c || []));
}
}
return new DOM(fix(handler.dom));
}
示例6: function
onclosetag: function (name) {
// Terminate once last tag is closed
tags.pop();
if (!changeFound && tags.length === 0) {
parser.end();
}
},
示例7: function
onopentag: function (name: string, attribs: any) {
if (name === 'div' && !attribs.class) {
lineIndex++;
} else if (name === 'div' && attribs.class.indexOf('cldr folding') !== -1) {
foldFound = true;
parser.end();
}
},