Skip to content

Commit

Permalink
Use correct line number for <script> if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyGu authored and domenic committed Oct 2, 2017
1 parent 13ece77 commit a7a6498
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
16 changes: 14 additions & 2 deletions lib/jsdom/living/nodes/HTMLScriptElement-impl.js
Expand Up @@ -6,7 +6,7 @@ const HTMLElementImpl = require("./HTMLElement-impl").implementation;
const { reflectURLAttribute } = require("../../utils");
const resourceLoader = require("../../browser/resource-loader");
const reportException = require("../helpers/runtime-script-errors");
const { domSymbolTree } = require("../helpers/internal-constants");
const { domSymbolTree, locationInfo } = require("../helpers/internal-constants");
const nodeTypes = require("../node-type");

const jsMIMETypes = new Set([
Expand Down Expand Up @@ -124,8 +124,20 @@ function processJavaScript(element, code, filename) {
if (window) {
document._currentScript = element;

let lineOffset = 0;
if (!element.src) {
for (const child of domSymbolTree.childrenIterator(element)) {
if (child.nodeType === nodeTypes.TEXT_NODE) {
if (child[locationInfo]) {
lineOffset = child[locationInfo].line - 1;
}
break;
}
}
}

try {
vm.runInContext(code, window, { filename, displayErrors: false });
vm.runInContext(code, window, { filename, lineOffset, displayErrors: false });
} catch (e) {
reportException(window, e, filename);
} finally {
Expand Down
24 changes: 23 additions & 1 deletion test/api/options-run-scripts.js
Expand Up @@ -3,7 +3,7 @@ const { assert } = require("chai");
const { describe, it } = require("mocha-sugar-free");
const { delay } = require("../util.js");

const { JSDOM } = require("../..");
const { JSDOM, VirtualConsole } = require("../..");

describe("API: runScripts constructor option", () => {
describe("<script>s and eval()", () => {
Expand Down Expand Up @@ -37,6 +37,28 @@ describe("API: runScripts constructor option", () => {
assert.strictEqual(dom.window.document.body.children.length, 3);
});

it("should execute <script>s with correct location when set to \"dangerously\" and includeNodeLocations", () => {
const virtualConsole = new VirtualConsole();
const promise = new Promise((resolve, reject) => {
virtualConsole.on("jsdomError", err => {
try {
assert.strictEqual(err.type, "unhandled exception");
assert(err.detail.stack.includes("at about:blank:2"));
resolve();
} catch (actualErr) {
reject(actualErr);
}
});
});

// eslint-disable-next-line no-new
new JSDOM(`<body>
<script>throw new Error();</script>
</body>`, { runScripts: "dangerously", includeNodeLocations: true, virtualConsole });

return promise;
});

it("should only run eval when set to \"outside-only\"", () => {
const dom = new JSDOM(`<body>
<script>document.body.appendChild(document.createElement("hr"));</script>
Expand Down

0 comments on commit a7a6498

Please sign in to comment.