本文整理汇总了TypeScript中jsdom.JSDOM类的典型用法代码示例。如果您正苦于以下问题:TypeScript JSDOM类的具体用法?TypeScript JSDOM怎么用?TypeScript JSDOM使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了JSDOM类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: test_serialize
function test_serialize() {
const dom = new JSDOM(`<!DOCTYPE html>hello`);
dom.serialize() === '<!DOCTYPE html><html><head></head><body>hello</body></html>';
// Contrast with:
dom.window.document.documentElement.outerHTML === '<html><head></head><body>hello</body></html>';
}
示例2: test_serialize
function test_serialize() {
const dom = new JSDOM(`<!DOCTYPE html>hello`);
dom.serialize() === '<!DOCTYPE html><html><head></head><body>hello</body></html>';
// Contrast with:
// tslint:disable-next-line no-unnecessary-type-assertion
dom.window.document.documentElement!.outerHTML === '<html><head></head><body>hello</body></html>';
}
示例3: test_reconfigure
function test_reconfigure() {
const myFakeTopForTesting = {} as DOMWindow;
const dom = new JSDOM();
dom.window.top === dom.window;
dom.window.location.href === 'about:blank';
dom.reconfigure({ windowTop: myFakeTopForTesting, url: 'https://example.com/' });
dom.window.top === myFakeTopForTesting;
dom.window.location.href === 'https://example.com/';
}
示例4: test_fragment
function test_fragment() {
const frag = JSDOM.fragment(`<p>Hello</p><p><strong>Hi!</strong>`);
frag.childNodes.length === 2;
frag.querySelector('strong')!.textContent = 'Why hello there!';
// etc.
}
示例5: test_fromFile
function test_fromFile() {
const options = {} as FromFileOptions;
JSDOM.fromFile('stuff.html', options).then(dom => {
console.log(dom.serialize());
});
}
示例6: test_fromURL
function test_fromURL() {
const options = {} as FromUrlOptions;
JSDOM.fromURL('https://example.com/', options).then(dom => {
console.log(dom.serialize());
});
}
示例7: test_runVMScript
function test_runVMScript() {
const dom = new JSDOM(``, { runScripts: 'outside-only' });
const s = new Script(`
if (!this.ran) {
this.ran = 0;
}
++this.ran;
`);
dom.runVMScript(s);
dom.runVMScript(s);
dom.runVMScript(s);
(dom.window as any).ran === 3;
}
示例8: function
export default function (html_file) {
return JSDOM.fromFile(html_file).then(dom => {
const external_js = dom.window.document.querySelectorAll('script[type="text/javascript"][src]') as NodeListOf<HTMLScriptElement>;
const inline_js = dom.window.document.querySelectorAll('script[type="text/javascript"]:not([src])');
if (external_js.length === 0) {
return {error: 'No library import'};
}
if (external_js.length > 1) {
return {error: 'Multiple library imports'};
}
if (inline_js.length !== 1) {
return {error: 'No initializer code'};
}
const code_match = inline_js[0].textContent.match(rx_code);
if (!code_match) {
return {error: 'Illegal initializer code'};
}
let init_data;
try {
init_data = json_parse_relax_keys(code_match[1]);
} catch (ex) {
return {error: 'Illegal initializer data'};
}
return {
title: dom.window.document.querySelector("head>title").textContent,
lib: external_js[0].src,
data: init_data
};
});
};
示例9: test_fragment_serialization
function test_fragment_serialization() {
const frag = JSDOM.fragment(`<p>Hello</p>`);
if (frag instanceof Element) {
if (frag.firstChild instanceof Element) {
console.log(frag.firstChild.outerHTML); // logs "<p>Hello</p>"
}
}
}
示例10:
(resolve, reject) => {
JSDOM.fromFile(entryPageFilePath(issue, uid))
.then((jsdom: JSDOM) => {
resolve(jsdom.window.document);
},
(err) => {
reject(err);
});
}
示例11: test_nodeLocation
function test_nodeLocation() {
const dom = new JSDOM(
`<p>Hello
<img src="foo.jpg">
</p>`,
{ includeNodeLocations: true }
);
const document = dom.window.document;
const bodyEl = document.body; // implicitly created
const pEl = document.querySelector('p')!;
const textNode = pEl.firstChild!;
const imgEl = document.querySelector('img')!;
console.log(dom.nodeLocation(bodyEl)); // null; it's not in the source
console.log(dom.nodeLocation(pEl)); // { startOffset: 0, endOffset: 39, startTag: ..., endTag: ... }
console.log(dom.nodeLocation(textNode)); // { startOffset: 3, endOffset: 13 }
console.log(dom.nodeLocation(imgEl)); // { startOffset: 13, endOffset: 32 }
}
示例12: analyzeComicPage
private async analyzeComicPage(url: string): Promise<Comic> {
const ogp = await grabity.grabIt(url),
document = (await JSDOM.fromURL(url)).window.document,
topicMsg = document.getElementById("topics2").textContent;
const comic = {
url,
title: fixchar(ogp.title || ogp["og:title"] || ogp["twitter:title"]).trim(),
thumbnailURL: ogp.image || ogp["og:image"] || ogp["twitter:image:src"],
concluded: (topicMsg.includes("連載は終了しました") || topicMsg.includes("特別読切作品")),
episodes: this.scrapeEpisodes(document),
};
console.info(`MagGarden: Crawled Comic Page - ${comic.title}`);
return comic;
}
示例13: analyzeMagazinePage
protected async analyzeMagazinePage(
url: string,
name: string,
id: string,
): Promise<Magazine> {
const document = (await JSDOM.fromURL(url)).window.document,
comics: Comic[] = [];
for (const comicBox of Array.from(document.querySelectorAll("article.cbox"))) {
const comicURL = comicBox.querySelector(".inner > a.cbox-main").getAttribute("href");
comics.push(await this.analyzeComicPage(comicURL));
}
this.comics = comics;
console.info("MagGarden: Crawled Magazine Index");
return { id, name, comics };
}
示例14: fetchIDL
async function fetchIDL(source: IDLSource) {
const response = await fetch(source.url);
if (source.url.endsWith(".idl")) {
return { idl: await response.text() };
}
const dom = JSDOM.fragment(await response.text());
const elements = Array.from(dom.querySelectorAll(idlSelector))
.filter(el => {
if (el.parentElement && el.parentElement.classList.contains("example")) {
return false;
}
const previous = el.previousElementSibling;
if (!previous) {
return true;
}
return !previous.classList.contains("atrisk") && !previous.textContent!.includes("IDL Index");
});
if (!elements.length) {
throw new Error(`Found no IDL code from ${source.url}`);
}
const idl = elements.map(element => trimCommonIndentation(element.textContent!).trim()).join('\n\n');
const comments = processComments(dom);
return { idl, comments };
}
示例15: fragment
export function fragment(template: string): DocumentFragment {
return JSDOM.fragment(template);
}