Top Banner
THE JSDOM
27
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: The jsdom

THE JSDOM

Page 2: The jsdom

WHAT IS JSDOM?

A JavaScript implementation of the DOM*, for use with Node.js**

* Actually much more than just the DOM

** Actually it runs in more places than just Node.js

Page 3: The jsdom

2 YEARS AGO…

Page 4: The jsdom

jsdom in action

what jsdom implements

the future of jsdom

Page 5: The jsdom

JSDOM IN ACTION

Page 6: The jsdom

jsdom.env({

url: "http://www.dotjs.eu/",

scripts: ["https://code.jquery.com/jquery-2.1.1.js"],

loaded(errors, window) {

console.log("Number of speakers:");

console.log(window.$(".section-speakers .thumbnail").length);

}

});

Page 7: The jsdom

var testPage = fs.readFileSync("index.html", "utf8");

var desiredOutput = fs.readFileSync("out.txt", "utf8");

browserifyFile("entry.js").then(bundledJS => {

var window = jsdom(testPage).parentWindow;

var scriptEl = window.document.createElement("script");

scriptEl.textContent = bundledJS;

window.document.head.appendChild(scriptEl);

assert.equal(window.document.body.innerHTML, desiredOutput);

});

Page 8: The jsdom

http.createServer((req, res) => {

var pageUrl = url.parse(req.url, true)["proxy-me"];

jsdom.env(pageUrl, (err, window) => {

var images = window.document.querySelectorAll("img");

Promise.all(Array.prototype.map.call(images, imgEl => {

return getImageContents(url, imgEl.src)

.then(rotateImage)

.then(rotatedCanvas => imgEl.src = rotatedCanvas.toDataURL());

})

.then(() => res.end(jsdom.serializeDocument(window.document)));

});

}).listen(5002);

Page 9: The jsdom
Page 10: The jsdom

ZOMBIE.JS

var browser = Browser.create();

browser.visit("/signup")

.then(() => {

browser.fill("email", "[email protected]");

browser.fill("password", "eat-the-living");

return browser.pressButton("Sign Me Up!");

})

.then(() => {

browser.assert.success();

browser.assert.text("title", "Welcome To Brains Depot");

});

Page 11: The jsdom

FACEBOOK’S JEST

Page 12: The jsdom

WHAT JSDOM IMPLEMENTS

Page 13: The jsdom

JUST THE DOM?

Page 14: The jsdom

NOPE.

• DOM

• HTML

• DOM Parsing & Serialization

• XHR

• URL

• CSS Selectors

• CSSOM

• (more crazy CSS specs…)

Page 15: The jsdom

ACTUALLY, IT’S WORSE

Page 16: The jsdom

(WE’RE FIXING THAT…)

Page 17: The jsdom

WEB-PLATFORM-TESTS

https://github.com/w3c/web-platform-tests

Page 18: The jsdom

FORTUNATELY, WE HAVE HELP

jsdom

parse5

cssom

csstyle

nwmatcher

xmlhttprequest

canvas

contextify

Page 19: The jsdom

THE FUTURE OF JSDOM

Page 20: The jsdom

TMPVAR/JSDOM#950

“In the browser, you can do:

window.document.querySelectorAll([

'link[type="text/xml"]',

'link[type="application/rss+xml"]',

'link[type="application/atom+xml"]'

]);

This doesn't work in jsdom.”

Page 21: The jsdom

interface ParentNode {

...

[NewObject] NodeList querySelectorAll(DOMString selectors);

};

Document implements ParentNode;

https://dom.spec.whatwg.org/#parentnode

Page 22: The jsdom

String(['a', 'b', 'c']) === 'a,b,c'

window.document.querySelectorAll([

'link[type="text/xml"]',

'link[type="application/rss+xml"]',

'link[type="application/atom+xml"]'

])

===

window.document.querySelectorAll(

'link[type="text/xml"],link[type="application/rss+xml"],\link[type="application/atom+xml"]'

)

Page 23: The jsdom

WEBIDL

interface Attr {

readonly attribute DOMString? namespaceURI;

readonly attribute DOMString? prefix;

readonly attribute DOMString localName;

readonly attribute DOMString name;

attribute DOMString value;

attribute DOMString textContent; // alias of .value

readonly attribute Element? ownerElement;

readonly attribute boolean specified; // useless; always returns true

};

Page 24: The jsdom

HTMLHRELEMENT.IDL

interface HTMLHRElement : HTMLElement {

[Reflect] attribute DOMString align;

[Reflect] attribute DOMString color;

[Reflect] attribute boolean noShade;

[Reflect] attribute DOMString size;

[Reflect] attribute DOMString width;

};

Page 25: The jsdom

⇒ HTMLHRELEMENT.JS

class HTMLHRElement extends HTMLElement {

get align() { return String(this.getAttribute("align")); }

set align(v) { this.setAttribute("align", String(v)); }

get noShade() { return Boolean(this.getAttribute("noshade")); }

set noShade(v) {

if (v) { this.setAttribute("noshade", ""); }

else { this.removeAttribute("noshade"); }

}

Page 26: The jsdom

https://github.com/domenic/webidl-class-generator

https://github.com/domenic/webidl-html-reflector

https://github.com/domenic/webidl-conversions

Page 27: The jsdom

https://github.com/tmpvar/jsdom