Skip to content

Commit 24c28fd

Browse files
committed
Add evalVMScript public API
Closes #1543.
1 parent bc8c3cf commit 24c28fd

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,15 @@ At present jsdom does not handle navigation (such as setting `window.location.hr
548548
jsdom.changeURL(window, "https://example.com/");
549549
```
550550

551+
#### Running vm scripts
552+
553+
Although in most cases it's simplest to just insert a `<script>` element or call `window.eval`, in some cases you want access to the raw [vm context](https://nodejs.org/api/vm.html) underlying jsdom to run scripts. You can do that like so:
554+
555+
```js
556+
const script = new vm.Script("globalVariable = 5;", { filename: "test.js" });
557+
jsdom.evalVMScript(window, script);
558+
```
559+
551560
## What Standards Does jsdom Support, Exactly?
552561

553562
Our mission is to get something very close to a headless browser, with emphasis more on the DOM/HTML side of things than the CSS side. As such, our primary goals are supporting [The DOM Standard](http://dom.spec.whatwg.org/) and [The HTML Standard](http://www.whatwg.org/specs/web-apps/current-work/multipage/). We only support some subset of these so far; in particular we have the subset covered by the outdated DOM 2 spec family down pretty well. We're slowly including more and more from the modern DOM and HTML specs, including some `Node` APIs, `querySelector(All)`, attribute semantics, the history and URL APIs, and the HTML parsing algorithm.

lib/jsdom.js

+4
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,10 @@ exports.blobToBuffer = function (blob) {
285285
return blob && blob[blobSymbols.buffer];
286286
};
287287

288+
exports.evalVMScript = (window, script) => {
289+
return script.runInContext(idlUtils.implForWrapper(window._document)._global);
290+
};
291+
288292
function processHTML(config) {
289293
const window = exports.jsdom(config.html, config).defaultView;
290294
const implImpl = idlUtils.implForWrapper(window.document.implementation);

test/jsdom/misc.js

+10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const https = require("https");
1313
const EventEmitter = require("events").EventEmitter;
1414
const zlib = require("zlib");
1515
const parseURL = require("url").parse;
16+
const vm = require("vm");
1617

1718
function tmpWindow() {
1819
return jsdom.jsdom().defaultView;
@@ -856,6 +857,15 @@ describe("jsdom/miscellaneous", () => {
856857

857858
// these tests require file system access or they start a http server
858859
describe("node specific tests", { skipIfBrowser: true }, () => {
860+
specify("evalVMScript", () => {
861+
const window = jsdom.jsdom().defaultView;
862+
const script = new vm.Script(`globalVariable = "value";`);
863+
864+
jsdom.evalVMScript(window, script);
865+
866+
assert.strictEqual(window.globalVariable, "value");
867+
});
868+
859869
specify("fix_for_issue_172", () => {
860870
jsdom.env(`<html><body><script type="text/javascript"></script></body></html>`, [
861871
"file:" + path.resolve(__dirname, "../jquery-fixtures/jquery-1.6.2.js")

0 commit comments

Comments
 (0)