Skip to content

Commit 13be0e7

Browse files
committed
A pass through the readme for clarity
1 parent 11968a1 commit 13be0e7

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

README.md

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,12 @@ jsdom.env({
9090

9191
### How it works
9292

93-
`jsdom.env` is built for ease of use, which is rare in the world of the DOM! Since the web has some absolutely horrible JavaScript on it, as of jsdom 0.2.0 `jsdom.env` will not process external resources (scripts, images, etc). If you want to process the JavaScript use one of the methods below (`jsdom.jsdom` or `jsdom.jQueryify`)
93+
The do-what-I-mean API is used like so:
9494

9595
```js
9696
jsdom.env(string, [scripts], [config], callback);
9797
```
9898

99-
The arguments are:
100-
10199
- `string`: may be a URL, file name, or HTML fragment
102100
- `scripts`: a string or array of strings, containing file names or URLs that will be inserted as `<script>` tags
103101
- `config`: see below
@@ -170,6 +168,10 @@ Now that you know about `created` and `loaded`, you can see that `done` is essen
170168

171169
If you used jsdom before v1.0.0, it only had a `done` callback, and it was kind of buggy, sometimes behaving one way, and sometimes another. Due to some excellent work by [@Sebmaster](https://github.com/Sebmaster) in [#792](https://github.com/tmpvar/jsdom/pull/792), we fixed it up into the above lifecycle. For more information on the migration, see [the wiki](https://github.com/tmpvar/jsdom/wiki/PR-792).
172170

171+
### On running scripts and being safe
172+
173+
By default, `jsdom.env` will not process and run external JavaScript, since our sandbox is not foolproof. That is, code running inside the DOM's `<script>`s can, if it tries hard enough, get access to the Node environment, and thus to your machine. If you want to (carefully!) enable running JavaScript, you can use `jsdom.jsdom`, `jsdom.jQueryify`, or modify the defaults passed to `jsdom.env`.
174+
173175
## For the hardcore: `jsdom.jsdom`
174176

175177
The `jsdom.jsdom` method does less things automatically; it takes in only HTML source, and does not let you to separately supply script that it will inject and execute. It just gives you back a `document` object, with usable `document.parentWindow`, and starts asynchronously executing any `<script>`s included in the HTML source. You can listen for the `'load'` event to wait until scripts are done loading and executing, just like you would in a normal HTML page.
@@ -182,15 +184,15 @@ var doc = jsdom(markup, options);
182184
var window = doc.parentWindow;
183185
```
184186

185-
- `markup` is an HTML/XML document to be parsed. You can also pass `undefined` to get the basic document, equivalent to what a browser will give if you open up an empty `.html` file. Our parser currently doesn't do that well with missing `<html>`, `<head>`, and `<body>` tags, but we're working to fix that.
187+
- `markup` is a HTML document to be parsed. You can also pass `undefined` to get the basic document, equivalent to what a browser will give if you open up an empty `.html` file.
186188

187-
- `options` See the explanation of the `config` object above.
189+
- `options`: see the explanation of the `config` object above.
188190

189191
### Flexibility
190192

191-
One of the goals of jsdom is to be as minimal and light as possible. This section details how someone can change the behavior of `Document`s on the fly. These features are baked into the `DOMImplementation` that every `Document` has, and may be tweaked in two ways:
193+
One of the goals of jsdom is to be as minimal and light as possible. This section details how someone can change the behavior of `Document`s before they are created. These features are baked into the `DOMImplementation` that every `Document` has, and may be tweaked in two ways:
192194

193-
1. When you create a new `Document` using the jsdom builder (`require("jsdom").jsdom()`)
195+
1. When you create a new `Document`, by overriding the configuration:
194196

195197
```js
196198
var jsdom = require("jsdom").jsdom;
@@ -212,32 +214,33 @@ One of the goals of jsdom is to be as minimal and light as possible. This sectio
212214
};
213215
```
214216

215-
#### Default Features
217+
#### External Resources
216218

217219
Default features are extremely important for jsdom as they lower the configuration requirement and present developers a set of consistent default behaviors. The following sections detail the available features, their defaults, and the values that jsdom uses.
218220

219-
220221
`FetchExternalResources`
221222

222223
- _Default_: `["script"]`
223224
- _Allowed_: `["script", "img", "css", "frame", "iframe", "link"]` or `false`
225+
- _Default for `jsdom.env`_: `false`
224226

225-
Enables/disables fetching files over the file system/HTTP.
227+
Enables/disables fetching files over the file system/HTTP
226228

227229
`ProcessExternalResources`
228230

229231
- _Default_: `["script"]`
230232
- _Allowed_: `["script"]` or `false`
233+
- _Default for `jsdom.env`_: `false`
231234

232-
Disabling this will disable script execution (currently only JavaScript).
235+
Enables/disables JavaScript execution
233236

234237
`SkipExternalResources`
235238

236-
- _Default_: `false`
239+
- _Default_: `false` (allow all)
237240
- _Allowed_: `/url to be skipped/` or `false`
238241
- _Example_: `/http:\/\/example.org/js/bad\.js/`
239242

240-
Do not download and process resources with url matching a regular expression.
243+
Filters resource downloading and processing to disallow those maching the given regular expression
241244

242245
### Canvas
243246

@@ -249,7 +252,7 @@ jsdom includes support for using the [canvas](https://npmjs.org/package/canvas)
249252

250253
```js
251254
var jsdom = require("jsdom").jsdom;
252-
var document = jsdom("<html><head></head><body>hello world</body></html>");
255+
var document = jsdom("hello world");
253256
var window = document.parentWindow;
254257

255258
console.log(window.document.innerHTML);
@@ -318,10 +321,9 @@ Unfortunately, doing this kind of magic requires C++. And in Node.js, using C++
318321
modules." Native modules are compiled at installation time so that they work precisely for your machine; that is, you
319322
don't download a contextify binary from npm, but instead build one locally after downloading the source from npm.
320323

321-
322-
Unfortunately, getting C++ compiled within npm's installation system can be tricky, especially for Windows users. Thus,
323-
one of the most common problems with jsdom is trying to use it without the proper compilation tools installed.
324-
Here's what you need to compile Contextify, and thus to install jsdom:
324+
Getting C++ compiled within npm's installation system can be tricky, especially for Windows users. Thus, one of the
325+
most common problems with jsdom is trying to use it without the proper compilation tools installed. Here's what you
326+
need to compile Contextify, and thus to install jsdom:
325327

326328
### Windows
327329

0 commit comments

Comments
 (0)