<commit_before>import { JSDOM, VirtualConsole, CookieJar, FromUrlOptions, FromFileOptions, DOMWindow, ResourceLoader, FetchOptions } from 'jsdom';
import { CookieJar as ToughCookieJar, MemoryCookieStore } from 'tough-cookie';
import { Script } from 'vm';

function test_basic_usage() {
    const dom = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`);
    console.log(dom.window.document.querySelector('p')!.textContent); // "Hello world"

    const { window } = new JSDOM(`...`);
    // or even
    const { document } = (new JSDOM(`...`)).window;
}

function test_executing_scripts1() {
    const dom = new JSDOM(`<body>
  <script>document.body.appendChild(document.createElement("hr"));</script>
</body>`);

    // The script will not be executed, by default:
    dom.window.document.body.children.length === 1;
}

function test_executing_scripts2() {
    const dom = new JSDOM(`<body>
  <script>document.body.appendChild(document.createElement("hr"));</script>
</body>`, { runScripts: 'dangerously' });

    // The script will be executed and modify the DOM:
    dom.window.document.body.children.length === 2;
}

function test_executing_scripts3() {
    const window = (new JSDOM(``, { runScripts: 'outside-only' })).window;

    window.eval(`document.body.innerHTML = "<p>Hello, world!</p>";`);
    window.document.body.children.length === 1;
}

function test_virtualConsole() {
    const virtualConsole = new VirtualConsole();
    const dom = new JSDOM(``, { virtualConsole });

    virtualConsole.on('error', () => { });
    virtualConsole.on('warn', () => { });
    virtualConsole.on('info', () => { });
    virtualConsole.on('dir', () => { });
    // ... etc. See https://console.spec.whatwg.org/#logging

    virtualConsole.sendTo(console);

    const c = console;
    virtualConsole.sendTo(c, { omitJSDOMErrors: true });
}

function test_cookieJar() {
    const store = {} as MemoryCookieStore;
    const options = {} as ToughCookieJar.Options;

    const cookieJar = new CookieJar(store, options);
    const dom = new JSDOM(``, { cookieJar });
}

function test_beforeParse() {
    const dom = new JSDOM(`<p>Hello</p>`, {
        beforeParse(window) {
            window.document.childNodes.length === 0;
        }
    });
}

function test_storageQuota() {
    new JSDOM('', { storageQuota: 1337 });
}

function test_pretendToBeVisual() {
    new JSDOM('', { pretendToBeVisual: true });
}

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>';<commit_msg>jsdom: Add no-unnecessary-type-assertion disables (#30318)<commit_after>import { JSDOM, VirtualConsole, CookieJar, FromUrlOptions, FromFileOptions, DOMWindow, ResourceLoader, FetchOptions } from 'jsdom';
import { CookieJar as ToughCookieJar, MemoryCookieStore } from 'tough-cookie';
import { Script } from 'vm';

function test_basic_usage() {
    const dom = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`);
    console.log(dom.window.document.querySelector('p')!.textContent); // "Hello world"

    const { window } = new JSDOM(`...`);
    // or even
    const { document } = (new JSDOM(`...`)).window;
}

function test_executing_scripts1() {
    const dom = new JSDOM(`<body>
  <script>document.body.appendChild(document.createElement("hr"));</script>
</body>`);

    // The script will not be executed, by default:
    dom.window.document.body.children.length === 1;
}

function test_executing_scripts2() {
    const dom = new JSDOM(`<body>
  <script>document.body.appendChild(document.createElement("hr"));</script>
</body>`, { runScripts: 'dangerously' });

    // The script will be executed and modify the DOM:
    dom.window.document.body.children.length === 2;
}

function test_executing_scripts3() {
    const window = (new JSDOM(``, { runScripts: 'outside-only' })).window;

    window.eval(`document.body.innerHTML = "<p>Hello, world!</p>";`);
    window.document.body.children.length === 1;
}

function test_virtualConsole() {
    const virtualConsole = new VirtualConsole();
    const dom = new JSDOM(``, { virtualConsole });

    virtualConsole.on('error', () => { });
    virtualConsole.on('warn', () => { });
    virtualConsole.on('info', () => { });
    virtualConsole.on('dir', () => { });
    // ... etc. See https://console.spec.whatwg.org/#logging

    virtualConsole.sendTo(console);

    const c = console;
    virtualConsole.sendTo(c, { omitJSDOMErrors: true });
}

function test_cookieJar() {
    const store = {} as MemoryCookieStore;
    const options = {} as ToughCookieJar.Options;

    const cookieJar = new CookieJar(store, options);
    const dom = new JSDOM(``, { cookieJar });
}

function test_beforeParse() {
    const dom = new JSDOM(`<p>Hello</p>`, {
        beforeParse(window) {
            window.document.childNodes.length === 0;
        }
    });
}

function test_storageQuota() {
    new JSDOM('', { storageQuota: 1337 });
}

function test_pretendToBeVisual() {
    new JSDOM('', { pretendToBeVisual: true });
}

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>';
{'fraction_non_alphanumeric': 0.17467803373843643, 'fraction_numerical': 0.0048975149646290584, 'mean_word_length': 3.8969804618117228, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 69, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6582801', 'n_tokens_mistral': 1786, 'n_tokens_neox': 1720, 'n_words': 357}