diff --git a/.eslintrc.js b/.eslintrc.js
new file mode 100644
index 0000000000000000000000000000000000000000..c3774cdab1caca5ae83179dd165e1b87c054fa93
--- /dev/null
+++ b/.eslintrc.js
@@ -0,0 +1,20 @@
+module.exports = {
+ env: {
+ es6: true,
+ node: true
+ },
+ extends: ['eslint:recommended', 'prettier'],
+ plugins: ['prettier'],
+ rules: {
+ 'prettier/prettier': 'error',
+ 'no-console': 'error',
+ curly: ['error', 'all'],
+ 'prefer-arrow-callback': 'error',
+ 'one-var': ['error', 'never'],
+ 'no-var': 'error',
+ 'prefer-const': 'error'
+ },
+ parserOptions: {
+ ecmaVersion: 6
+ }
+};
diff --git a/README.OpenSource b/README.OpenSource
index 7c930ff301cd1d677de372ed31dc7a8c06040e89..82c6c7adf27b91859734b1fd0322188fa87b564d 100644
--- a/README.OpenSource
+++ b/README.OpenSource
@@ -1,11 +1,11 @@
-[
- {
- "Name": "parse5",
- "License": "MIT",
- "License File": "NOTICE",
- "Version Number": " 5.1.1",
- "Owner": "sunbingxin@huawei.com",
- "Upstream URL": "https://github.com/inikulin/parse5.git",
- "Description": "HTML parser and serializer."
- }
-]
+[
+ {
+ "Name": "parse5",
+ "License": "MIT",
+ "License File": "NOTICE",
+ "Version Number": " 6.0.1",
+ "Owner": "sunbingxin@huawei.com",
+ "Upstream URL": "https://github.com/inikulin/parse5.git",
+ "Description": "HTML parser and serializer."
+ }
+]
diff --git a/bundle.json b/bundle.json
deleted file mode 100644
index 4d74a141727c1b79a91e9e7498bb05cab0d12f43..0000000000000000000000000000000000000000
--- a/bundle.json
+++ /dev/null
@@ -1,30 +0,0 @@
-{
- "name": "@ohos/parse5",
- "description": "HTML parser and serializer.",
- "version": "3.1",
- "license": "Apache V2",
- "publishAs": "code-segment",
- "segment": {
- "destPath": "third_party/parse5"
- },
- "dirs": {},
- "scripts": {},
- "component": {
- "name": "thirdparty_parse5",
- "subsystem": "developtools",
- "syscap": [],
- "features": [],
- "adapted_system_type": [],
- "rom": "",
- "ram": "",
- "deps": {
- "components": [],
- "third_party": []
- },
- "build": {
- "sub_component": [],
- "inner_kits": [],
- "test": []
- }
- }
-}
\ No newline at end of file
diff --git a/docs/version-history.md b/docs/version-history.md
index 8d60a00aed8a6d9a8c72699386ec6ed19672a8ce..43ddae41907dd632fde06b9d88ec95758ffab867 100644
--- a/docs/version-history.md
+++ b/docs/version-history.md
@@ -1,5 +1,15 @@
# Version history
+# 6.0.1
+* Fixed: Handling of self-closing `
` tags (by [@43081j](https://github.com/43081j)).
+* Fixed: Broken link in TreeAdapter document (GH [#317](https://github.com/inikulin/parse5/issues/317)) (by [@ursm](https://github.com/ursm)).
+* Fixed: SAXParser example (GH [#316](https://github.com/inikulin/parse5/issues/316)) (by [@mvasilkov](https://github.com/mvasilkov)).
+
+# 6.0.0
+* Added (**breaking**): Tree adapter interface now has `updateNodeSourceCodeLocation` method which
+enables usage of custom location info formats (GH [#314](https://github.com/inikulin/parse5/issues/314)) (by [@DMartens](https://github.com/DMartens)).
+
+
# 5.1.1
* Fixed: Serialization of attributes in non-standard namespaces (by [@Zirro](https://github.com/Zirro)).
* Fixed: Quirks and limited-quirks mode detection by doctype (by [@squidfunk](https://github.com/squidfunk)).
diff --git a/lerna.json b/lerna.json
index fa42581014ca898c3518e629bea4d9d5fd76d15b..6cf0b0f3a06d3a5a3d933fb13cfe7ffdc761545e 100644
--- a/lerna.json
+++ b/lerna.json
@@ -1,5 +1,5 @@
{
"lerna": "2.10.2",
"packages": ["packages/*"],
- "version": "5.1.1"
+ "version": "6.0.1"
}
diff --git a/packages/parse5-html-rewriting-stream/package.json b/packages/parse5-html-rewriting-stream/package.json
index b95de57bbdb7bb8b0bea6ed004f19fff660a7696..e4bf660d8ac3855841105174c4716ca73bf2c429 100644
--- a/packages/parse5-html-rewriting-stream/package.json
+++ b/packages/parse5-html-rewriting-stream/package.json
@@ -1,7 +1,7 @@
{
"name": "parse5-html-rewriting-stream",
"description": "Streaming HTML rewriter.",
- "version": "5.1.1",
+ "version": "6.0.1",
"author": "Ivan Nikulin (https://github.com/inikulin)",
"contributors": "https://github.com/inikulin/parse5/graphs/contributors",
"homepage": "https://github.com/inikulin/parse5",
@@ -17,8 +17,8 @@
"license": "MIT",
"main": "./lib/index.js",
"dependencies": {
- "parse5": "^5.1.1",
- "parse5-sax-parser": "^5.1.1"
+ "parse5": "^6.0.1",
+ "parse5-sax-parser": "^6.0.1"
},
"repository": {
"type": "git",
diff --git a/packages/parse5-htmlparser2-tree-adapter/lib/index.js b/packages/parse5-htmlparser2-tree-adapter/lib/index.js
index 824047383a89ca8a5890185a2f87afe88a96cbbb..58464d78a41e54377eb0c6edd2510e373942a49e 100644
--- a/packages/parse5-htmlparser2-tree-adapter/lib/index.js
+++ b/packages/parse5-htmlparser2-tree-adapter/lib/index.js
@@ -342,3 +342,7 @@ exports.setNodeSourceCodeLocation = function(node, location) {
exports.getNodeSourceCodeLocation = function(node) {
return node.sourceCodeLocation;
};
+
+exports.updateNodeSourceCodeLocation = function(node, endLocation) {
+ node.sourceCodeLocation = Object.assign(node.sourceCodeLocation, endLocation);
+};
diff --git a/packages/parse5-htmlparser2-tree-adapter/package.json b/packages/parse5-htmlparser2-tree-adapter/package.json
index 4c71c1b5f188efd9b57a8dc1b2ca7e3ead9c28d0..cb8d818a261d1c745f11788e77e809c37671008f 100644
--- a/packages/parse5-htmlparser2-tree-adapter/package.json
+++ b/packages/parse5-htmlparser2-tree-adapter/package.json
@@ -1,7 +1,7 @@
{
"name": "parse5-htmlparser2-tree-adapter",
"description": "htmlparser2 tree adapter for parse5.",
- "version": "5.1.1",
+ "version": "6.0.1",
"author": "Ivan Nikulin (https://github.com/inikulin)",
"contributors": "https://github.com/inikulin/parse5/graphs/contributors",
"homepage": "https://github.com/inikulin/parse5",
@@ -14,7 +14,7 @@
"license": "MIT",
"main": "./lib/index.js",
"dependencies": {
- "parse5": "^5.1.1"
+ "parse5": "^6.0.1"
},
"repository": {
"type": "git",
diff --git a/packages/parse5-parser-stream/package.json b/packages/parse5-parser-stream/package.json
index 284dd04e9e8320b465642f9d9b04c4760334cdfb..068e07566b83baa4be08069082498770820902d8 100644
--- a/packages/parse5-parser-stream/package.json
+++ b/packages/parse5-parser-stream/package.json
@@ -1,7 +1,7 @@
{
"name": "parse5-parser-stream",
"description": "Streaming HTML parser with scripting support.",
- "version": "5.1.1",
+ "version": "6.0.1",
"author": "Ivan Nikulin (https://github.com/inikulin)",
"contributors": "https://github.com/inikulin/parse5/graphs/contributors",
"homepage": "https://github.com/inikulin/parse5",
@@ -14,7 +14,7 @@
"license": "MIT",
"main": "./lib/index.js",
"dependencies": {
- "parse5": "^5.1.1"
+ "parse5": "^6.0.1"
},
"repository": {
"type": "git",
diff --git a/packages/parse5-plain-text-conversion-stream/package.json b/packages/parse5-plain-text-conversion-stream/package.json
index 7db65e74fd55cae977fd2b1bb2cabcf348a70e40..974a0d13f031f4f85e128c4ef6fb2d1f6476b4bb 100644
--- a/packages/parse5-plain-text-conversion-stream/package.json
+++ b/packages/parse5-plain-text-conversion-stream/package.json
@@ -1,7 +1,7 @@
{
"name": "parse5-plain-text-conversion-stream",
"description": "Stream that converts plain text files into HTML document.",
- "version": "5.1.1",
+ "version": "6.0.1",
"author": "Ivan Nikulin (https://github.com/inikulin)",
"contributors": "https://github.com/inikulin/parse5/graphs/contributors",
"homepage": "https://github.com/inikulin/parse5",
@@ -17,8 +17,8 @@
"license": "MIT",
"main": "./lib/index.js",
"dependencies": {
- "parse5": "^5.1.1",
- "parse5-parser-stream": "^5.1.1"
+ "parse5": "^6.0.1",
+ "parse5-parser-stream": "^6.0.1"
},
"repository": {
"type": "git",
diff --git a/packages/parse5-sax-parser/docs/index.md b/packages/parse5-sax-parser/docs/index.md
index a89b295fd6bc1937540d6d60532e65450332c8c0..a089a954ad96e0416fbb883d084cd9cefa7590e2 100644
--- a/packages/parse5-sax-parser/docs/index.md
+++ b/packages/parse5-sax-parser/docs/index.md
@@ -87,8 +87,8 @@ const fs = require('fs');
const file = fs.createWriteStream('google.com.html');
const parser = new SAXParser();
-parser.on('doctype', (name, publicId, systemId) => {
- // Process doctype info ans stop parsing
+parser.on('doctype', ({ name, publicId, systemId }) => {
+ // Process doctype info and stop parsing
...
parser.stop();
});
diff --git a/packages/parse5-sax-parser/package.json b/packages/parse5-sax-parser/package.json
index f8873fa155eda36d6488f7efdcf8288dd1fae09e..09241523687545fea354d0f304bcdba947780e45 100644
--- a/packages/parse5-sax-parser/package.json
+++ b/packages/parse5-sax-parser/package.json
@@ -1,7 +1,7 @@
{
"name": "parse5-sax-parser",
"description": "Streaming SAX-style HTML parser.",
- "version": "5.1.1",
+ "version": "6.0.1",
"author": "Ivan Nikulin (https://github.com/inikulin)",
"contributors": "https://github.com/inikulin/parse5/graphs/contributors",
"homepage": "https://github.com/inikulin/parse5",
@@ -15,7 +15,7 @@
"license": "MIT",
"main": "./lib/index.js",
"dependencies": {
- "parse5": "^5.1.1"
+ "parse5": "^6.0.1"
},
"repository": {
"type": "git",
diff --git a/packages/parse5-serializer-stream/package.json b/packages/parse5-serializer-stream/package.json
index 00a671514cdf9d167420db2b5514a7adb12aa5fe..2c2948adaef5b48cece599309b1849eea0192fa9 100644
--- a/packages/parse5-serializer-stream/package.json
+++ b/packages/parse5-serializer-stream/package.json
@@ -1,7 +1,7 @@
{
"name": "parse5-serializer-stream",
"description": "Streaming HTML serializer.",
- "version": "5.1.1",
+ "version": "6.0.1",
"author": "Ivan Nikulin (https://github.com/inikulin)",
"contributors": "https://github.com/inikulin/parse5/graphs/contributors",
"homepage": "https://github.com/inikulin/parse5",
@@ -16,7 +16,7 @@
"license": "MIT",
"main": "./lib/index.js",
"dependencies": {
- "parse5": "^5.1.1"
+ "parse5": "^6.0.1"
},
"repository": {
"type": "git",
diff --git a/packages/parse5/docs/source-code-location/end-location.md b/packages/parse5/docs/source-code-location/end-location.md
new file mode 100644
index 0000000000000000000000000000000000000000..8b3bd57db6240842619d927ccd96c92686b81b15
--- /dev/null
+++ b/packages/parse5/docs/source-code-location/end-location.md
@@ -0,0 +1,48 @@
+# Interface: EndLocation
+
+### Properties
+
+* [endCol](#endcol)
+* [endOffset](#endoffset)
+* [endLine](#endline)
+* [endTag](#endtag)
+
+---
+
+## Properties
+
+
+
+### endCol
+
+**● endCol**: *`number`*
+
+One-based column index of the last character
+
+___
+
+
+### endOffset
+
+**● endOffset**: *`number`*
+
+Zero-based last character index
+
+___
+
+
+### endLine
+
+**● endLine**: *`number`*
+
+One-based line index of the last character
+
+___
+
+
+### endTag
+
+**● endTag**: *[Location](location.md)|undefined*
+
+Element's end tag location info.
+This property is undefined, if the element has no closing tag.
\ No newline at end of file
diff --git a/packages/parse5/docs/tree-adapter/interface.md b/packages/parse5/docs/tree-adapter/interface.md
index ac1e565421c2354726566d73a7198018b779674c..238fb190ba02fe611d0c4c0c7d66af156ef9d81b 100644
--- a/packages/parse5/docs/tree-adapter/interface.md
+++ b/packages/parse5/docs/tree-adapter/interface.md
@@ -2,7 +2,7 @@
Tree adapter is a set of utility functions that provides minimal required abstraction layer beetween parser and a specific AST format. Note that `TreeAdapter` is not designed to be a general purpose AST manipulation library. You can build such library on top of existing `TreeAdapter` or use one of the existing libraries from npm.
-*__See__*: [default implementation](https://github.com/inikulin/parse5/blob/master/lib/tree_adapters/default.js)
+*__See__*: [default implementation](https://github.com/inikulin/parse5/blob/master/packages/parse5/lib/tree-adapters/default.js)
### Methods
@@ -38,7 +38,7 @@ Tree adapter is a set of utility functions that provides minimal required abstra
* [setDocumentType](#setdocumenttype)
* [setNodeSourceCodeLocation](#setnodesourcecodelocation)
* [setTemplateContent](#settemplatecontent)
-
+* [updateNodeSourceCodeLocation](#updatenodesourcecodelocation)
---
## Methods
@@ -588,6 +588,21 @@ Sets the `` element content element.
| contentElement | DocumentFragment | Content element. |
**Returns:** `void`
-
___
+
+
+### updateNodeSourceCodeLocation
+
+▸ **updateNodeSourceCodeLocation**(node: *Node*, endLocation: *[EndLocation](../source-code-location/end-location.md)*): `void`
+
+Updates the source code location of nodes.
+
+**Parameters:**
+| Param | Type | Description |
+| ------ | ------ | ------ |
+| node | Node | Node. |
+| endLocation | [EndLocation](../source-code-location/end-location.md) | Source code location information of the end of the node. |
+
+**Returns:** `void`
+___
diff --git a/packages/parse5/lib/extensions/location-info/parser-mixin.js b/packages/parse5/lib/extensions/location-info/parser-mixin.js
index fcf3a40af5367e67b04cf613fbffe1a5866f71ff..e7d3e2da1286336d3405b4dec4b8e14bf97834f7 100644
--- a/packages/parse5/lib/extensions/location-info/parser-mixin.js
+++ b/packages/parse5/lib/extensions/location-info/parser-mixin.js
@@ -43,17 +43,19 @@ class LocationInfoParserMixin extends Mixin {
// NOTE: For cases like
- First 'p' closes without a closing
// tag and for cases like | - 'p' closes without a closing tag.
const isClosingEndTag = closingToken.type === Tokenizer.END_TAG_TOKEN && tn === closingToken.tagName;
-
+ const endLoc = {};
if (isClosingEndTag) {
- loc.endTag = Object.assign({}, ctLoc);
- loc.endLine = ctLoc.endLine;
- loc.endCol = ctLoc.endCol;
- loc.endOffset = ctLoc.endOffset;
+ endLoc.endTag = Object.assign({}, ctLoc);
+ endLoc.endLine = ctLoc.endLine;
+ endLoc.endCol = ctLoc.endCol;
+ endLoc.endOffset = ctLoc.endOffset;
} else {
- loc.endLine = ctLoc.startLine;
- loc.endCol = ctLoc.startCol;
- loc.endOffset = ctLoc.startOffset;
+ endLoc.endLine = ctLoc.startLine;
+ endLoc.endCol = ctLoc.startCol;
+ endLoc.endOffset = ctLoc.startOffset;
}
+
+ this.treeAdapter.updateNodeSourceCodeLocation(element, endLoc);
}
}
}
@@ -208,9 +210,8 @@ class LocationInfoParserMixin extends Mixin {
const tnLoc = this.treeAdapter.getNodeSourceCodeLocation(textNode);
if (tnLoc) {
- tnLoc.endLine = token.location.endLine;
- tnLoc.endCol = token.location.endCol;
- tnLoc.endOffset = token.location.endOffset;
+ const { endLine, endCol, endOffset } = token.location;
+ this.treeAdapter.updateNodeSourceCodeLocation(textNode, { endLine, endCol, endOffset });
} else {
this.treeAdapter.setNodeSourceCodeLocation(textNode, token.location);
}
diff --git a/packages/parse5/lib/parser/index.js b/packages/parse5/lib/parser/index.js
index 5eececdd1fd04939a54e94344ddb3ac060653bf7..8ac84a702e36f12f7871494e1f9443c68a14adc3 100644
--- a/packages/parse5/lib/parser/index.js
+++ b/packages/parse5/lib/parser/index.js
@@ -427,28 +427,34 @@ class Parser {
_runParsingLoop(scriptHandler) {
let lastToken = {};
while (!this.stopped) {
- this._setupTokenizerCDATAMode();
- const token = this.tokenizer.getNextToken();
- if (token.type === Tokenizer.HIBERNATION_TOKEN) {
- break;
- }
- if (token.type !== Tokenizer.EOF_TOKEN && token.type !== Tokenizer.WHITESPACE_CHARACTER_TOKEN) {
- lastToken =token;
- }
- checkselfClosingNode(this, token);
- if (this.skipNextNewLine) {
- this.skipNextNewLine = false;
- if (token.type === Tokenizer.WHITESPACE_CHARACTER_TOKEN && token.chars[0] === '\n') {
- if (token.chars.length === 1) {
- continue;
+ this._setupTokenizerCDATAMode();
+
+ const token = this.tokenizer.getNextToken();
+
+ if (token.type === Tokenizer.HIBERNATION_TOKEN) {
+ break;
+ }
+ if (token.type !== Tokenizer.EOF_TOKEN && token.type !== Tokenizer.WHITESPACE_CHARACTER_TOKEN) {
+ lastToken =token;
}
- token.chars = token.chars.substr(1);
+ checkselfClosingNode(this, token);
+ if (this.skipNextNewLine) {
+ this.skipNextNewLine = false;
+
+ if (token.type === Tokenizer.WHITESPACE_CHARACTER_TOKEN && token.chars[0] === '\n') {
+ if (token.chars.length === 1) {
+ continue;
+ }
+
+ token.chars = token.chars.substr(1);
+ }
+ }
+
+ this._processInputToken(token);
+
+ if (scriptHandler && this.pendingScript) {
+ break;
}
- }
- this._processInputToken(token);
- if (scriptHandler && this.pendingScript) {
- break;
- }
}
checkInvalid(this, lastToken);
}
@@ -895,63 +901,63 @@ class Parser {
* @param {Object} parse parse5 object.
* @param {Object} token Hml text token information.
*/
-function checkselfClosingNode(parse, token) {
- const tagName = (token.tagName || "").toLowerCase();
- const selfClosing = token.selfClosing;
- const flag = parse.validator.isSupportedSelfClosing(tagName);
- if (parse.nodeInfo.tn && tagName && !parse.nodeInfo.sc) {
- const loc =
- String(token.location.startLine) + String(token.location.startCol);
- if (
- !flag ||
- (loc !== parse.nodeInfo.pos && token.type === Tokenizer.START_TAG_TOKEN)
- ) {
+ function checkselfClosingNode(parse, token) {
+ const tagName = (token.tagName || "").toLowerCase();
+ const selfClosing = token.selfClosing;
+ const flag = parse.validator.isSupportedSelfClosing(tagName);
+ if (parse.nodeInfo.tn && tagName && !parse.nodeInfo.sc) {
+ const loc =
+ String(token.location.startLine) + String(token.location.startCol);
+ if (
+ !flag ||
+ (loc !== parse.nodeInfo.pos && token.type === Tokenizer.START_TAG_TOKEN)
+ ) {
+ parse.compileResult.log.push({
+ line: String(token.location.startLine) || 1,
+ column: String(token.location.startCol) || 1,
+ reason: 'ERROR: tag `' + parse.nodeInfo.tn + '` must be closed, please follow norm',
+ });
+ parse.nodeInfo = {};
+ }
+ }
+ if (tagName && flag) {
+ if (token.type === Tokenizer.START_TAG_TOKEN && !selfClosing) {
+ parse.nodeInfo.tn = tagName;
+ parse.nodeInfo.sc = false;
+ parse.nodeInfo.pos =
+ String(token.location.line) + String(token.location.col);
+ }
+ if (
+ token.type === Tokenizer.END_TAG_TOKEN &&
+ tagName === parse.nodeInfo.tn
+ ) {
+ parse.nodeInfo.sc = true;
+ }
+ }
+ if (!flag && selfClosing && token.type === Tokenizer.START_TAG_TOKEN) {
parse.compileResult.log.push({
- line: String(token.location.startLine) || 1,
- column: String(token.location.startCol) || 1,
- reason: 'ERROR: tag `' + parse.nodeInfo.tn + '` must be closed, please follow norm',
+ line: token.location.startLine || 1,
+ column: token.location.startCol || 1,
+ reason: "ERROR: tag `" + tagName + "` can not use selfClosing",
});
- parse.nodeInfo = {};
}
}
- if (tagName && flag) {
- if (token.type === Tokenizer.START_TAG_TOKEN && !selfClosing) {
- parse.nodeInfo.tn = tagName;
- parse.nodeInfo.sc = false;
- parse.nodeInfo.pos =
- String(token.location.line) + String(token.location.col);
- }
+
+ /**
+ * Check if the html text is legal.
+ * @param {Object} lastToken Hml text last token information.
+ */
+ function checkInvalid(lastToken) {
if (
- token.type === Tokenizer.END_TAG_TOKEN &&
- tagName === parse.nodeInfo.tn
+ lastToken.type && lastToken.type !== Tokenizer.END_TAG_TOKEN &&
+ lastToken.type !== Tokenizer.COMMENT_TOKEN
) {
- parse.nodeInfo.sc = true;
+ compileResult.log.push({
+ line: lastToken.location.startLine || 1,
+ column: lastToken.location.startCol || 1,
+ reason: "ERROR: hml content is invalid. Please check it.",
+ });
}
- }
- if (!flag && selfClosing && token.type === Tokenizer.START_TAG_TOKEN) {
- parse.compileResult.log.push({
- line: token.location.startLine || 1,
- column: token.location.startCol || 1,
- reason: "ERROR: tag `" + tagName + "` can not use selfClosing",
- });
- }
-}
-
-/**
- * Check if the html text is legal.
- * @param {Object} lastToken Hml text last token information.
- */
-function checkInvalid(lastToken) {
- if (
- lastToken.type && lastToken.type !== Tokenizer.END_TAG_TOKEN &&
- lastToken.type !== Tokenizer.COMMENT_TOKEN
- ) {
- compileResult.log.push({
- line: lastToken.location.startLine || 1,
- column: lastToken.location.startCol || 1,
- reason: "ERROR: hml content is invalid. Please check it.",
- });
- }
}
module.exports = Parser;
diff --git a/packages/parse5/lib/tree-adapters/default.js b/packages/parse5/lib/tree-adapters/default.js
index 7935a0e0b48b8d48ceb8f6e0d6c9705dc030966e..14d007a1611bdb56fad0b2f30af0727f0fb23e48 100644
--- a/packages/parse5/lib/tree-adapters/default.js
+++ b/packages/parse5/lib/tree-adapters/default.js
@@ -215,3 +215,7 @@ exports.setNodeSourceCodeLocation = function(node, location) {
exports.getNodeSourceCodeLocation = function(node) {
return node.sourceCodeLocation;
};
+
+exports.updateNodeSourceCodeLocation = function(node, endLocation) {
+ node.sourceCodeLocation = Object.assign(node.sourceCodeLocation, endLocation);
+};
diff --git a/packages/parse5/package.json b/packages/parse5/package.json
index 7e9347327a6e95ebb487f391d3e372de4a8f3a42..13a12df243f8d8d049e975b3f2ed8f6177c59f88 100644
--- a/packages/parse5/package.json
+++ b/packages/parse5/package.json
@@ -1,7 +1,7 @@
{
"name": "parse5",
"description": "HTML parser and serializer.",
- "version": "5.1.1",
+ "version": "6.0.1",
"author": "Ivan Nikulin (https://github.com/inikulin)",
"contributors": "https://github.com/inikulin/parse5/graphs/contributors",
"homepage": "https://github.com/inikulin/parse5",
diff --git a/packages/parse5/test/location-info-parser.test.js b/packages/parse5/test/location-info-parser.test.js
index 22f474c865013115f930ab9dab13ae658f0f9ba7..4d4ce68790b66291b7ff040a6a1eaa5d082fc932 100644
--- a/packages/parse5/test/location-info-parser.test.js
+++ b/packages/parse5/test/location-info-parser.test.js
@@ -7,7 +7,7 @@ const {
assertStartTagLocation,
assertNodeLocation
} = require('../../../test/utils/generate-location-info-parser-tests');
-const { generateTestsForEachTreeAdapter } = require('../../../test/utils/common');
+const { generateTestsForEachTreeAdapter, treeAdapters } = require('../../../test/utils/common');
generateLocationInfoParserTests(module.exports, 'Parser', (input, opts) => ({
node: parse5.parse(input, opts)
@@ -127,3 +127,56 @@ generateTestsForEachTreeAdapter(module.exports, (_test, treeAdapter) => {
assert.ok(!location.endTag);
};
});
+
+exports['Updating node source code location (GH-314)'] = function() {
+ const sourceCodeLocationSetter = {
+ setNodeSourceCodeLocation(node, location) {
+ if (location === null) {
+ node.sourceCodeLocation = null;
+ } else {
+ node.sourceCodeLocation = {
+ start: {
+ line: location.startLine,
+ column: location.startCol,
+ offset: location.startOffset
+ },
+ end: {
+ line: location.endLine,
+ column: location.endCol,
+ offset: location.endOffset
+ }
+ };
+ }
+ },
+ updateNodeSourceCodeLocation(node, endLocation) {
+ node.sourceCodeLocation = {
+ start: node.sourceCodeLocation.start,
+ end: {
+ line: endLocation.endLine,
+ column: endLocation.endCol,
+ offset: endLocation.endOffset
+ }
+ };
+ }
+ };
+ const adapter = Object.assign(treeAdapters.default, sourceCodeLocationSetter);
+ const document = parse5.parse('Testing location', { adapter, sourceCodeLocationInfo: true });
+ const [doctype, html] = document.childNodes;
+ const [head, body] = html.childNodes;
+ const [text] = body.childNodes;
+
+ assert.deepEqual(doctype.sourceCodeLocation, {
+ start: { line: 1, column: 1, offset: 0 },
+ end: { line: 1, column: 11, offset: 10 }
+ });
+ assert.strictEqual(html.sourceCodeLocation, null);
+ assert.strictEqual(head.sourceCodeLocation, null);
+ assert.deepEqual(body.sourceCodeLocation, {
+ start: { line: 1, column: 11, offset: 10 },
+ end: { line: 1, column: 40, offset: 39 }
+ });
+ assert.deepEqual(text.sourceCodeLocation, {
+ start: { line: 1, column: 17, offset: 16 },
+ end: { line: 1, column: 33, offset: 32 }
+ });
+};