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