Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSDOM port #542

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jsdom/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!package.json
8 changes: 8 additions & 0 deletions jsdom/example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { JSDOM } from "./jsdom.ts";

let dom = new JSDOM(`
<!DOCTYPE html>
<p>Hello, Deno!</p>
`);
let p = dom.window.document.querySelector("p");
console.log(p.textContent); // => "Hello, Deno!"
15 changes: 15 additions & 0 deletions jsdom/jsdom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import "./vendor/jsdom.js";

// TODO: Investigate how to reuse typings from:
// - https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/jsdom/index.d.ts
// - https://github.com/microsoft/TypeScript/blob/master/lib/lib.dom.d.ts
MarkTiedemann marked this conversation as resolved.
Show resolved Hide resolved

interface JSDOM {
new (html: string, options?: { runScripts?: "dangerously" }): JSDOM;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
readonly window: any;
fragment: (html: string) => any;
serialize(): string;
}

export let JSDOM = (window as any).jsdom.JSDOM as JSDOM;
10 changes: 10 additions & 0 deletions jsdom/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"scripts": {
"vendor": "webpack-cli"
},
"devDependencies": {
"jsdom": "15.1.1",
"webpack": "4.36.1",
"webpack-cli": "3.3.6"
}
}
MarkTiedemann marked this conversation as resolved.
Show resolved Hide resolved
31 changes: 31 additions & 0 deletions jsdom/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { JSDOM } from "./jsdom.ts";
import { assertEquals, assert } from "../testing/asserts.ts";
import { test, runIfMain } from "../testing/mod.ts";

// TODO: Add proper tests. All tests are copied from
// JSDOM's readme (https://github.com/jsdom/jsdom).

test(function helloWorld() {
let dom = new JSDOM(`<!DOCTYPE html><p>Hello world</p>`);
let text = dom.window.document.querySelector("p").textContent;
assertEquals(text, "Hello world");
});

test(function runScripts() {
let dom = new JSDOM(
`<body>
<script>document.body.appendChild(document.createElement("hr"));</script>
</body>`,
{ runScripts: "dangerously" }
);
let children = dom.window.document.body.children.length;
assertEquals(children, 2);
});

test(function fragment() {
let frag = JSDOM.fragment(`<p>Hello</p><p><strong>Hi!</strong>`);
assertEquals(frag.childNodes.length, 2);
assertEquals(frag.querySelector("strong").textContent, "Hi!");
});

runIfMain(import.meta);
603 changes: 603 additions & 0 deletions jsdom/vendor/jsdom.js

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions jsdom/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const path = require("path");

module.exports = {
entry: "./node_modules/jsdom/lib/api.js",
output: {
path: path.join(__dirname, "vendor"),
filename: "./jsdom.js",
library: "jsdom",
libraryTarget: "window"
},
mode: "production",
node: {
child_process: "empty",
fs: "empty",
net: "empty",
tls: "empty"
},
performance: {
// TODO: Investigate whether the bundle size can be reduced.
hints: false
}
};