Skip to content

Commit

Permalink
Small updates to the readme and to JS feature usage
Browse files Browse the repository at this point in the history
Now that we are requiring Node v6, we can use newer JavaScript features.
  • Loading branch information
domenic committed Apr 24, 2017
1 parent 380e2fe commit c854f7e
Show file tree
Hide file tree
Showing 14 changed files with 25 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@
"prefer-const": ["error", { "ignoreReadBeforeAssign": true }],
"prefer-reflect": "off",
"prefer-rest-params": "off",
"prefer-spread": "off", // TODO with new Node versions
"prefer-spread": "error",
"prefer-template": "off",
"require-yield": "error",
"template-curly-spacing": ["error", "never"],
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jsdom is a pure-JavaScript implementation of many web standards, notably the WHA

The latest versions of jsdom require Node.js v6 or newer. (Versions of jsdom below v10 still work with Node.js v4, but are unsupported.)

As of v10, jsdom has a new API (docmented below). The old API is still supported for now; [see its documentation](./lib/old-api.md) for details.
As of v10, jsdom has a new API (documented below). The old API is still supported for now; [see its documentation](./lib/old-api.md) for details.

## Basic usage

Expand All @@ -25,9 +25,9 @@ console.log(dom.window.document.querySelector("p").textContent); // "Hello world
The resulting object is an instance of the `JSDOM` class, which contains a number of useful properties and methods besides `window`. In general it can be used to act on the jsdom from the "outside," doing things that are not possible with the normal DOM APIs. For simple cases, where you don't need any of this functionality, we recommend a coding pattern like

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

Full documentation on everything you can do with the `JSDOM` class is below, in the section "`JSDOM` Object API".
Expand Down Expand Up @@ -108,7 +108,7 @@ By default, jsdom will not load any subresources such as scripts, stylesheets, i
* Scripts, via `<script>`, but only if `runScripts: "dangerously"` is also set
* Images, via `<img>`, but only if the `canvas` (or `canvas-prebuilt`) npm package is also installed (see "Canvas Support" below)

In the future we plan to offer more customization of resource loading via this option, but now the default and the `"usable"` option are the two modes offered.
In the future we plan to offer more customization of resource loading via this option, but for now the default and the `"usable"` option are the two modes offered.

### Virtual consoles

Expand Down Expand Up @@ -232,7 +232,7 @@ Note that this feature only works if you have set the `includeNodeLocations` opt

### Running vm-created scripts with `runVMScript(script)`

The built-in `vm` module of Node.js allows you to create `Script` instances, which can be compiled ahead of time and then run mutliple times on a given "VM context". Behind the scenes, a jsdom `Window` is indeed a VM context. To get access to this ability, use the `runVMScript()` method:
The built-in `vm` module of Node.js allows you to create `Script` instances, which can be compiled ahead of time and then run multiple times on a given "VM context". Behind the scenes, a jsdom `Window` is indeed a VM context. To get access to this ability, use the `runVMScript()` method:

```js
const { Script } = require("vm");
Expand Down Expand Up @@ -294,7 +294,7 @@ The returned promise will fulfill with a `JSDOM` instance if the URL is valid an
The options provided to `fromURL()` are similar to those provided to the `JSDOM` constructor, with the following additional restrictions and consequences:

- The `url` and `contentType` options cannot be provided.
- The `referrer` option is used as the HTTP `Referer` request header of the initial request, in addition to subresource requests.
- The `referrer` option is used as the HTTP `Referer` request header of the initial request.
- The `userAgent` option is used as the HTTP `User-Agent` request header of any requests.
- The resulting jsdom's URL, content type, and referrer are determined from the response.
- Any cookies set via HTTP `Set-Cookie` response headers are stored in the jsdom's cookie jar. Similarly, any cookies already in a supplied cookie jar are sent as HTTP `Cookie` request headers.
Expand Down
5 changes: 2 additions & 3 deletions lib/jsdom/browser/Window.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,8 @@ function Window(options) {
});

function wrapConsoleMethod(method) {
return function () {
const args = Array.prototype.slice.call(arguments);
window._virtualConsole.emit.apply(window._virtualConsole, [method].concat(args));
return (...args) => {
window._virtualConsole.emit(method, ...args);
};
}

Expand Down
4 changes: 1 addition & 3 deletions lib/jsdom/browser/htmltodom.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,7 @@ class HtmlToDom {
const entityMatcher = /<!ENTITY ([^ ]+) "([^"]+)">/g;
let result;
while ((result = entityMatcher.exec(dt))) {
// TODO Node v6 const [, name, value] = result;
const name = result[1];
const value = result[2];
const [, name, value] = result;
if (!(name in parser.ENTITIES)) {
parser.ENTITIES[name] = value;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/jsdom/living/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ exports.hasAttribute = function (element, A) {
const attributesNNM = element._attributes;
const attributeList = attributesNNM[INTERNAL].attributeList;

return attributeList.indexOf(A) !== -1;
return attributeList.includes(A);
};

exports.hasAttributeByName = function (element, name) {
Expand Down
3 changes: 1 addition & 2 deletions lib/jsdom/living/events/Event-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ const EventInit = require("../generated/EventInit");

class EventImpl {
constructor(args, privateData) {
const type = args[0]; // TODO: Replace with destructuring
const eventInitDict = args[1] || EventInit.convert(undefined);
const [type, eventInitDict = EventInit.convert(undefined)] = args;

this.type = type;

Expand Down
2 changes: 1 addition & 1 deletion lib/jsdom/living/html-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function resetHTMLCollectionTo(collection, impls) {
}

// Don't override existing named ones
if (keys.indexOf(value) !== -1) {
if (keys.includes(value)) {
return;
}

Expand Down
3 changes: 1 addition & 2 deletions lib/jsdom/living/node-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ class NodeList {
}
}

forEach(callback) {
const thisArg = arguments[1]; // TODO Node v6: use default arguments
forEach(callback, thisArg = undefined) {
let values = Array.from(this);
let i = 0;
while (i < values.length) {
Expand Down
11 changes: 3 additions & 8 deletions lib/jsdom/living/nodes/Document-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,12 +458,7 @@ class DocumentImpl extends NodeImpl {
}

writeln() {
const args = [];
for (let i = 0; i < arguments.length; ++i) {
args.push(arguments[i]);
}
args.push("\n");
this.write.apply(this, args);
this.write(...arguments, "\n");
}

getElementById(id) {
Expand Down Expand Up @@ -644,7 +639,7 @@ class DocumentImpl extends NodeImpl {
createProcessingInstruction(target, data) {
validateName(target);

if (data.indexOf("?>") !== -1) {
if (data.includes("?>")) {
throw new DOMException(DOMException.INVALID_CHARACTER_ERR,
"Processing instruction data cannot contain the string \"?>\"");
}
Expand All @@ -664,7 +659,7 @@ class DocumentImpl extends NodeImpl {
"Cannot create CDATA sections in HTML documents");
}

if (data.indexOf("]]>") !== -1) {
if (data.includes("]]>")) {
throw new DOMException(DOMException.INVALID_CHARACTER_ERR,
"CDATA section data cannot contain the string \"]]>\"");
}
Expand Down
4 changes: 2 additions & 2 deletions lib/jsdom/living/nodes/HTMLTableElement-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class HTMLTableElementImpl extends HTMLElementImpl {
if (this.tHead) {
sections.push(this.tHead);
}
sections.push.apply(sections, childrenByHTMLLocalName(this, "tbody"));
sections.push(...childrenByHTMLLocalName(this, "tbody"));
if (this.tFoot) {
sections.push(this.tFoot);
}
Expand All @@ -39,7 +39,7 @@ class HTMLTableElementImpl extends HTMLElementImpl {

const rows = [];
for (const s of sections) {
rows.push.apply(rows, childrenByHTMLLocalName(s, "tr"));
rows.push(...childrenByHTMLLocalName(s, "tr"));
}
return rows;
});
Expand Down
2 changes: 1 addition & 1 deletion lib/jsdom/virtual-console.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module.exports = class VirtualConsole extends EventEmitter {
for (const method of Object.keys(anyConsole)) {
if (typeof anyConsole[method] === "function") {
function onMethodCall() {
anyConsole[method].apply(anyConsole, arguments);
anyConsole[method](...arguments);
}
this.on(method, onMethodCall);
}
Expand Down
2 changes: 1 addition & 1 deletion test/to-port-to-wpts/files/current-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
document.getElementById("test").innerHTML = String(
document.currentScript &&
document.currentScript instanceof HTMLScriptElement &&
document.currentScript.src.indexOf("files/current-script.js") !== -1
document.currentScript.src.includes("files/current-script.js")
);
4 changes: 2 additions & 2 deletions test/to-port-to-wpts/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ exports["the history object should update correctly when calling forward/back/go
[{ foo: "baz" }, "title 2", "/baz"],
[{ foo: "buzz" }, "title 3", "/buzz"]
].forEach(args => {
window.history.pushState.apply(window.history, args);
window.history.pushState(...args);
});

// Sanity check
Expand Down Expand Up @@ -133,7 +133,7 @@ exports["the history object should update correctly when calling pushState with
[{ foo: "baz" }, "title 2", "/baz"],
[{ foo: "buzz" }, "title 3", "/buzz"]
].forEach(args => {
window.history.pushState.apply(window.history, args);
window.history.pushState(...args);
});

// Sanity check
Expand Down
4 changes: 2 additions & 2 deletions test/web-platform-tests/create-jsdom.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ module.exports = (urlPrefix, testPath) => {
errors.push(new Error(`test harness should not timeout: ${testPath}`));
}

errors.push.apply(errors, doneErrors);
errors.push.apply(errors, unhandledExceptions);
errors.push(...doneErrors);
errors.push(...unhandledExceptions);

if (errors.length === 1) {
reject(new Error(errors[0]));
Expand Down

0 comments on commit c854f7e

Please sign in to comment.