Skip to content

Commit

Permalink
Use parse5 v5.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Zirro authored and domenic committed Aug 13, 2018
1 parent 9477401 commit c854cc4
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 27 deletions.
8 changes: 4 additions & 4 deletions lib/api.js
Expand Up @@ -65,11 +65,11 @@ class JSDOM {
}

nodeLocation(node) {
if (!idlUtils.implForWrapper(this[window]._document)._parseOptions.locationInfo) {
if (!idlUtils.implForWrapper(this[window]._document)._parseOptions.sourceCodeLocationInfo) {
throw new Error("Location information was not saved for this jsdom. Use includeNodeLocations during creation.");
}

return idlUtils.implForWrapper(node).__location;
return idlUtils.implForWrapper(node).sourceCodeLocation;
}

runVMScript(script) {
Expand Down Expand Up @@ -210,7 +210,7 @@ function transformOptions(options, encoding) {
referrer: "",
contentType: "text/html",
parsingMode: "html",
parseOptions: { locationInfo: false },
parseOptions: { sourceCodeLocationInfo: false },
runScripts: undefined,
encoding,
pretendToBeVisual: false,
Expand Down Expand Up @@ -254,7 +254,7 @@ function transformOptions(options, encoding) {
throw new TypeError("Cannot set includeNodeLocations to true with an XML content type");
}

transformed.windowOptions.parseOptions = { locationInfo: true };
transformed.windowOptions.parseOptions = { sourceCodeLocationInfo: true };
}

transformed.windowOptions.cookieJar = options.cookieJar === undefined ?
Expand Down
2 changes: 1 addition & 1 deletion lib/jsdom/browser/htmltodom.js
Expand Up @@ -8,7 +8,7 @@ const JSDOMParse5Adapter = require("./parse5-adapter-parsing");
const { HTML_NS } = require("../living/helpers/namespaces");

// Horrible monkey-patch to implement https://github.com/inikulin/parse5/issues/237
const OpenElementStack = require("parse5/lib/parser/open_element_stack");
const OpenElementStack = require("parse5/lib/parser/open-element-stack");
const originalPop = OpenElementStack.prototype.pop;
OpenElementStack.prototype.pop = function (...args) {
const before = this.items[this.stackTop];
Expand Down
8 changes: 7 additions & 1 deletion lib/jsdom/browser/parse5-adapter-serialization.js
Expand Up @@ -31,11 +31,17 @@ exports.getTemplateContent = templateElement => templateElement._templateContent
exports.getDocumentMode = document => document._mode;

// Node types

exports.isTextNode = node => node.nodeType === nodeTypes.TEXT_NODE;

exports.isCommentNode = node => node.nodeType === nodeTypes.COMMENT_NODE;

exports.isDocumentTypeNode = node => node.nodeType === nodeTypes.DOCUMENT_TYPE_NODE;

exports.isElementNode = node => node.nodeType === nodeTypes.ELEMENT_NODE;

// Source code location
exports.setNodeSourceCodeLocation = (node, location) => {
node.sourceCodeLocation = location;
};

exports.getNodeSourceCodeLocation = node => node.sourceCodeLocation;
4 changes: 2 additions & 2 deletions lib/jsdom/living/nodes/HTMLScriptElement-impl.js
Expand Up @@ -230,8 +230,8 @@ function processJavaScript(element, code, filename) {
if (!element.src) {
for (const child of domSymbolTree.childrenIterator(element)) {
if (child.nodeType === nodeTypes.TEXT_NODE) {
if (child.__location) {
lineOffset = child.__location.line - 1;
if (child.sourceCodeLocation) {
lineOffset = child.sourceCodeLocation.startLine - 1;
}
break;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -30,7 +30,7 @@
"escodegen": "^1.9.1",
"html-encoding-sniffer": "^1.0.2",
"nwsapi": "^2.0.7",
"parse5": "4.0.0",
"parse5": "^5.0.0",
"pn": "^1.1.0",
"request": "^2.87.0",
"request-promise-native": "^1.0.5",
Expand Down
40 changes: 26 additions & 14 deletions test/api/methods.js
Expand Up @@ -65,12 +65,14 @@ describe("API: JSDOM class's methods", () => {
const node = dom.window.document.querySelector("p");

assert.deepEqual(dom.nodeLocation(node), {
line: 1,
col: 1,
endCol: 13,
endLine: 1,
startLine: 1,
startCol: 1,
startOffset: 0,
endOffset: 12,
startTag: { line: 1, col: 1, startOffset: 0, endOffset: 3 },
endTag: { line: 1, col: 9, startOffset: 8, endOffset: 12 }
startTag: { endCol: 4, endLine: 1, startLine: 1, startCol: 1, startOffset: 0, endOffset: 3 },
endTag: { endCol: 13, endLine: 1, startLine: 1, startCol: 9, startOffset: 8, endOffset: 12 }
});
});

Expand All @@ -79,8 +81,10 @@ describe("API: JSDOM class's methods", () => {
const node = dom.window.document.querySelector("p").firstChild;

assert.deepEqual(dom.nodeLocation(node), {
line: 1,
col: 4,
endCol: 9,
endLine: 1,
startLine: 1,
startCol: 4,
startOffset: 3,
endOffset: 8
});
Expand All @@ -96,27 +100,35 @@ describe("API: JSDOM class's methods", () => {
startTag: {
attrs: {
src: {
line: 2,
col: 14,
endCol: 27,
endLine: 2,
startLine: 2,
startCol: 14,
startOffset: 22,
endOffset: 35
}
},
line: 2,
col: 9,
endCol: 28,
endLine: 2,
startLine: 2,
startCol: 9,
startOffset: 17,
endOffset: 36
},
attrs: {
src: {
line: 2,
col: 14,
endCol: 27,
endLine: 2,
startLine: 2,
startCol: 14,
startOffset: 22,
endOffset: 35
}
},
line: 2,
col: 9,
endCol: 28,
endLine: 2,
startLine: 2,
startCol: 9,
startOffset: 17,
endOffset: 36
});
Expand Down
2 changes: 1 addition & 1 deletion test/api/options-run-scripts.js
Expand Up @@ -46,7 +46,7 @@ describe("API: runScripts constructor option", () => {
virtualConsole.on("jsdomError", err => {
try {
assert.strictEqual(err.type, "unhandled exception");
assert(err.detail.stack.includes("at about:blank:2"));
assert.isTrue(err.detail.stack.includes("at about:blank:2"));
resolve();
} catch (actualErr) {
reject(actualErr);
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Expand Up @@ -2813,9 +2813,9 @@ parse-json@^2.2.0:
dependencies:
error-ex "^1.2.0"

parse5@4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/parse5/-/parse5-4.0.0.tgz#6d78656e3da8d78b4ec0b906f7c08ef1dfe3f608"
parse5@^5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/parse5/-/parse5-5.0.0.tgz#4d02710d44f3c3846197a11e205d4ef17842b81a"

parsejson@0.0.3:
version "0.0.3"
Expand Down

0 comments on commit c854cc4

Please sign in to comment.