本文整理匯總了TypeScript中cheerio類的典型用法代碼示例。如果您正苦於以下問題:TypeScript cheerio類的具體用法?TypeScript cheerio怎麽用?TypeScript cheerio使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了cheerio類的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: cheerio
/*
* LOADING
*/
let html =
`<ul id="fruits">
<li class="orange">Apple</li>
<li class="class">Orange</li>
<li class="pear">Pear</li>
<input type="text" />
</ul>`;
// Preferred Method
var $ = cheerio.load(html);
// Directly load element
cheerio(html);
cheerio('ul', html);
cheerio('li', 'ul', html);
const $fromElement = cheerio.load($("ul").get(0));
if ($fromElement("ul > li").length !== 3) {
throw new Error("Expecting 3 elements when passing `CheerioElement` to `load()`");
}
$ = cheerio.load(html, {
normalizeWhitespace: true,
xmlMode: true
});
$ = cheerio.load(html, {
示例2: _parseTemplate
protected _parseTemplate (template: string): Translations {
let collection: Translations = new Translations();
template = this._normalizeTemplateAttributes(template);
const selector = '[translate], [ng2-translate]';
$(template)
.find(selector)
.addBack(selector)
.each((i: number, element: CheerioElement) => {
const $element = $(element);
const attr = $element.attr('translate') || $element.attr('ng2-translate');
// The key is never the attribute value
// if (attr) {
// collection = collection.add(attr);
// } else {
$element
.contents()
.toArray()
.filter(node => node.type === 'text')
.map(node => node.nodeValue.trim())
.filter(text => text.length > 0)
.forEach(text => collection = collection.add(text));
// }
});
return collection;
}
示例3: Error
files.forEach((file) => {
// add few helper methods
extend(file);
const $contents = $(file.getContentsAsString());
const isMain = $contents.closest('head,body').length;
const isTemplate = $contents.closest(':not(head,body)').length;
if (file.isNodeModule()) {
// push directly as a template
templateFiles.push(file);
} else {
// cannot be both
if (isMain && isTemplate) {
const fileName = file.getBasename();
const errorMsg = `${fileName} has wrong layout`;
throw Error(errorMsg);
}
if (isMain > 0) {
mainFiles.push(file);
} else {
templateFiles.push(file);
}
}
});
示例4:
$$$.each((index, element) => {
var tag = element.tagName;
last_child.append(element);
if (contTags[tag.toLowerCase()]) {
paragraphs.push(last_child.html());
last_child = $('<div></div>');
}
});
示例5: compileOneFile
public compileOneFile(file): CompileResult {
const $contents = $(file.getContentsAsString());
const $head = $contents.closest('head');
const $body = $contents.closest('body');
return {
head: $head.html() || '',
body: $body.html() || '',
};
}
示例6: compileOneFile
public compileOneFile(file: FileObject): CompileResult {
const $contents = $(file.getContentsAsString());
const $head = $contents.closest('head');
const $body = $contents.closest('body');
return {
head: {
contents: $head.html() || '',
},
body: {
contents: $body.html() || '',
attrs: $body[0] ? $body[0].attribs : undefined,
},
};
}
示例7: splitCallbackWithCheerio
export function splitCallbackWithCheerio(content: Cheerio): SplitResult {
var $$ = $.load('<div id="outermost"></div>');
$$('#outermost').append(content);
var $$$ = $$('#outermost > *');
var last_child = $('<div></div>');
var paragraphs = [ ];
$$$.each((index, element) => {
var tag = element.tagName;
last_child.append(element);
if (contTags[tag.toLowerCase()]) {
paragraphs.push(last_child.html());
last_child = $('<div></div>');
}
});
return paragraphs;
}
示例8:
.each((i: number, element: CheerioElement) => {
const $element = $(element);
const attr = $element.attr('translate') || $element.attr('ng2-translate');
// The key is never the attribute value
// if (attr) {
// collection = collection.add(attr);
// } else {
$element
.contents()
.toArray()
.filter(node => node.type === 'text')
.map(node => node.nodeValue.trim())
.filter(text => text.length > 0)
.forEach(text => collection = collection.add(text));
// }
});
示例9: splitCallback
export function splitCallback(content: string): SplitResult {
var $$ = $.load(`<div id="outermost">${content}</div>`)
var $$$ = $$('#outermost > *');
var last_child = $('<div></div>');
var paragraphs = [ ];
$$$.each((index, element) => {
var tag = element.tagName;
last_child.append(element);
if (contTags[tag.toLowerCase()]) {
// $.html() would not contain the outer element def.
paragraphs.push(last_child.html());
var new_element = $('<div></div>');
last_child = new_element;
}
});
return paragraphs;
}
示例10: Error
files.forEach((file) => {
// skips files from node_modules
if (!!file.getPathInPackage().startsWith('node_modules')) {
return;
}
const $contents = $(file.getContentsAsString());
const isMain = $contents.closest('head,body').length;
const isTemplate = $contents.closest(':not(head,body)').length;
if (isMain && isTemplate) {
const fileName = file.getBasename();
const errorMsg = `${fileName} has wrong layout`;
throw Error(errorMsg);
}
if (isMain > 0) {
mainFiles.push(file);
} else {
templateFiles.push(file);
}
});