From 53bee3d1e3b744e7978237ea7536f1e8e68298e3 Mon Sep 17 00:00:00 2001 From: Ivan Demidov Date: Wed, 28 Jul 2021 10:47:08 +0300 Subject: [PATCH 01/13] docs: update import --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 0257988..1a01cb2 100644 --- a/readme.md +++ b/readme.md @@ -22,7 +22,7 @@ $ npm install posthtml-parser ``` ```js -import parser from 'posthtml-parser' +import { parser } from 'posthtml-parser' import fs from 'fs' const html = fs.readFileSync('path/to/input.html', 'utf-8') From 4f4ab7e47823e17e30963a0d9a2accfbc368d827 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 12 Aug 2021 20:52:23 +0000 Subject: [PATCH 02/13] build(deps): bump path-parse from 1.0.6 to 1.0.7 Bumps [path-parse](https://github.com/jbgutierrez/path-parse) from 1.0.6 to 1.0.7. - [Release notes](https://github.com/jbgutierrez/path-parse/releases) - [Commits](https://github.com/jbgutierrez/path-parse/commits/v1.0.7) --- updated-dependencies: - dependency-name: path-parse dependency-type: indirect ... Signed-off-by: dependabot[bot] --- package-lock.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index f6a9465..7ee3da5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9677,9 +9677,9 @@ "dev": true }, "path-parse": { - "version": "1.0.6", - "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.6.tgz", - "integrity": "sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==", + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.7.tgz", + "integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==", "dev": true }, "path-type": { From 74c165ccbdbb6eba0c6b9e325163f9e1e5d3692f Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Fri, 20 Aug 2021 09:20:33 +0100 Subject: [PATCH 03/13] test: break test to demonstrate bug --- test/test-core.spec.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/test/test-core.spec.ts b/test/test-core.spec.ts index 17f889b..535acd3 100644 --- a/test/test-core.spec.ts +++ b/test/test-core.spec.ts @@ -243,7 +243,7 @@ test('should be not converting html entity name', t => { }); test('should parse with source locations', t => { - const html = '

Test

\n

Foo

'; + const html = '

Test

\n

Foo


'; const tree = parser(html, { sourceLocations: true }); const expected = [ { @@ -284,12 +284,25 @@ test('should parse with source locations', t => { line: 2, column: 1 }, + end: { + line: 2, + column: 13 + } + } + }, + { + tag: 'hr', + location: { + start: { + line: 2, + column: 14 + }, end: { line: 2, column: 17 } } - } + }, ]; t.deepEqual(tree, expected); }); From 1bd66334eeb681440db14b9ab2ef4056c5a34ff4 Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Fri, 20 Aug 2021 19:57:37 +0100 Subject: [PATCH 04/13] Use patched version --- package-lock.json | 42 +++++++++++++++++++++++++++++++++++++----- package.json | 2 +- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7ee3da5..5189964 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4447,6 +4447,7 @@ "version": "2.4.4", "resolved": "https://registry.npmjs.org/domutils/-/domutils-2.4.4.tgz", "integrity": "sha512-jBC0vOsECI4OMdD0GC9mGn7NXPLb+Qt6KW1YDQzeQYRUFKmNG8lh7mO5HiELfr+lLQE7loDVI4QcAxV80HS+RA==", + "dev": true, "requires": { "dom-serializer": "^1.0.1", "domelementtype": "^2.0.1", @@ -7314,14 +7315,45 @@ "dev": true }, "htmlparser2": { - "version": "6.0.0", - "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-6.0.0.tgz", - "integrity": "sha512-numTQtDZMoh78zJpaNdJ9MXb2cv5G3jwUoe3dMQODubZvLoGvTE/Ofp6sHvH8OGKcN/8A47pGLi/k58xHP/Tfw==", + "version": "github:fb55/htmlparser2#5ab080e67833e97e58a8b3d6dc7364aceb7dd5cb", + "from": "github:fb55/htmlparser2#5ab080e67833e97e58a8b3d6dc7364aceb7dd5cb", "requires": { "domelementtype": "^2.0.1", "domhandler": "^4.0.0", - "domutils": "^2.4.4", - "entities": "^2.0.0" + "domutils": "^2.5.2", + "entities": "^3.0.1" + }, + "dependencies": { + "domutils": { + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/domutils/-/domutils-2.7.0.tgz", + "integrity": "sha512-8eaHa17IwJUPAiB+SoTYBo5mCdeMgdcAoXJ59m6DT1vw+5iLS3gNoqYaRowaBKtGVrOF1Jz4yDTgYKLK2kvfJg==", + "requires": { + "dom-serializer": "^1.0.1", + "domelementtype": "^2.2.0", + "domhandler": "^4.2.0" + }, + "dependencies": { + "domelementtype": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-2.2.0.tgz", + "integrity": "sha512-DtBMo82pv1dFtUmHyr48beiuq792Sxohr+8Hm9zoxklYPfa6n0Z3Byjj2IV7bmr2IyqClnqEQhfgHJJ5QF0R5A==" + }, + "domhandler": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/domhandler/-/domhandler-4.2.0.tgz", + "integrity": "sha512-zk7sgt970kzPks2Bf+dwT/PLzghLnsivb9CcxkvR8Mzr66Olr0Ofd8neSbglHJHaHa2MadfoSdNlKYAaafmWfA==", + "requires": { + "domelementtype": "^2.2.0" + } + } + } + }, + "entities": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/entities/-/entities-3.0.1.tgz", + "integrity": "sha512-WiyBqoomrwMdFG1e0kqvASYfnlb0lp8M5o5Fw2OFq1hNZxxcNk8Ik0Xm7LxzBhuidnZB/UtBqVCgUz3kBOP51Q==" + } } }, "http-cache-semantics": { diff --git a/package.json b/package.json index 4c71c52..697f2cd 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "posthtmltree" ], "dependencies": { - "htmlparser2": "^6.0.0" + "htmlparser2": "github:fb55/htmlparser2#5ab080e67833e97e58a8b3d6dc7364aceb7dd5cb" }, "devDependencies": { "@antfu/eslint-config-ts": "^0.4.3", From fb96d68e00df4d3d0f54340d23c3acaf0e1e8d0d Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Fri, 20 Aug 2021 20:30:02 +0100 Subject: [PATCH 05/13] Force in something that works --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index d9bdd82..9df4f5b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -143,7 +143,7 @@ export const parser = (html: string, options: Options = {}): Node[] => { const buf: Node | undefined = bufArray.pop(); if (buf && typeof buf === 'object' && buf.location && parser.endIndex !== null) { - buf.location.end = locationTracker.getPosition(parser.endIndex); + buf.location.end = locationTracker.getPosition((!parser.tagname || parser.tagname === buf.tag) ? parser.endIndex : parser.startIndex - 1); } if (buf) { From 4f00e5c06df3f856b2d2126963cf69704181995c Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Fri, 20 Aug 2021 21:19:53 +0100 Subject: [PATCH 06/13] Use release --- package-lock.json | 5 +++-- package.json | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5189964..6a2cbf2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7315,8 +7315,9 @@ "dev": true }, "htmlparser2": { - "version": "github:fb55/htmlparser2#5ab080e67833e97e58a8b3d6dc7364aceb7dd5cb", - "from": "github:fb55/htmlparser2#5ab080e67833e97e58a8b3d6dc7364aceb7dd5cb", + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-7.0.0.tgz", + "integrity": "sha512-IhdltX9BWhYQft4UPA92jFasNajskja0om6vU0DaIEL4OseCg5zE+mHAMr51AT89TbzzECrQWJ4CZ5NVYTPlKw==", "requires": { "domelementtype": "^2.0.1", "domhandler": "^4.0.0", diff --git a/package.json b/package.json index 697f2cd..bc19848 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "posthtmltree" ], "dependencies": { - "htmlparser2": "github:fb55/htmlparser2#5ab080e67833e97e58a8b3d6dc7364aceb7dd5cb" + "htmlparser2": "^7.0.0" }, "devDependencies": { "@antfu/eslint-config-ts": "^0.4.3", From 667ccf6da7aaefbb76ba529b850836aeef78b246 Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Sun, 22 Aug 2021 20:35:30 +0100 Subject: [PATCH 07/13] Try and record previous indicies and rewrite if needed --- src/index.ts | 26 +++++++++++++++++++----- src/location-tracker.ts | 7 ++++++- test/test-core.spec.ts | 44 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 70 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index 9df4f5b..a922c78 100644 --- a/src/index.ts +++ b/src/index.ts @@ -44,11 +44,16 @@ export const parser = (html: string, options: Options = {}): Node[] => { const locationTracker = new LocationTracker(html); const bufArray: Node[] = []; const results: Node[] = []; + let lastIndices: [number, number]; - function bufferArrayLast(): Node { + function bufferArrayLast(): Node | undefined { return bufArray[bufArray.length - 1]; } + function resultsLast(): Node | undefined { + return results[results.length - 1]; + } + function isDirective(directive: Directive, tag: string): boolean { if (directive.name instanceof RegExp) { const regex = new RegExp(directive.name.source, 'i'); @@ -122,10 +127,20 @@ export const parser = (html: string, options: Options = {}): Node[] => { } function onopentag(tag: string, attrs: Attributes) { - const start = locationTracker.getPosition(parser.startIndex); const buf: NodeTag = { tag }; if (options.sourceLocations) { + if (lastIndices?.[0] === parser.startIndex && lastIndices?.[1] === parser.endIndex) { + // The last closing tag was inferred, so we need to update its end location + const last = bufferArrayLast() || resultsLast(); + + if (typeof last === 'object' && Array.isArray(last.content) && last.location) { + last.location.end = locationTracker.getPosition(parser.startIndex - 1) + } + } + + const start = locationTracker.getPosition(parser.startIndex); + buf.location = { start, end: start @@ -142,8 +157,9 @@ export const parser = (html: string, options: Options = {}): Node[] => { function onclosetag() { const buf: Node | undefined = bufArray.pop(); - if (buf && typeof buf === 'object' && buf.location && parser.endIndex !== null) { - buf.location.end = locationTracker.getPosition((!parser.tagname || parser.tagname === buf.tag) ? parser.endIndex : parser.startIndex - 1); + if (buf && typeof buf === 'object' && buf.location && buf.location.end === buf.location.start && parser.endIndex !== null) { + lastIndices = [parser.startIndex, parser.endIndex]; + buf.location.end = locationTracker.getPosition(parser.endIndex); } if (buf) { @@ -167,7 +183,7 @@ export const parser = (html: string, options: Options = {}): Node[] => { } function ontext(text: string) { - const last: Node = bufferArrayLast(); + const last = bufferArrayLast(); if (last === undefined) { results.push(text); diff --git a/src/location-tracker.ts b/src/location-tracker.ts index 033959c..bbcc81c 100644 --- a/src/location-tracker.ts +++ b/src/location-tracker.ts @@ -25,7 +25,12 @@ export class LocationTracker { getPosition(index: number): Position { if (index < this.lastIndex) { - throw new Error('Source indices must be monotonic'); + this.lastPosition = { + line: 1, + column: 1 + }; + + this.lastIndex = 0; } while (this.lastIndex < index) { diff --git a/test/test-core.spec.ts b/test/test-core.spec.ts index 535acd3..18c036b 100644 --- a/test/test-core.spec.ts +++ b/test/test-core.spec.ts @@ -243,7 +243,7 @@ test('should be not converting html entity name', t => { }); test('should parse with source locations', t => { - const html = '

Test

\n

Foo


'; + const html = '

Test

\n

Foo


Bar\n


'; const tree = parser(html, { sourceLocations: true }); const expected = [ { @@ -303,6 +303,48 @@ test('should parse with source locations', t => { } } }, + { + tag: 'p', + location: { + start: { + line: 2, + column: 18, + }, + end: { + line: 2, + column: 21, + }, + }, + }, + { + tag: 'p', + content: [ + 'Bar\n', + ], + location: { + start: { + line: 2, + column: 22 + }, + end: { + line: 2, + column: 28 + } + } + }, + { + tag: 'hr', + location: { + start: { + line: 3, + column: 1 + }, + end: { + line: 3, + column: 4 + } + } + }, ]; t.deepEqual(tree, expected); }); From c9fde6390551e8dcabd87da9d08b242747f23292 Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Mon, 23 Aug 2021 14:01:05 +0100 Subject: [PATCH 08/13] Use patched version --- package-lock.json | 5 ++--- package.json | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6a2cbf2..86ba362 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7315,9 +7315,8 @@ "dev": true }, "htmlparser2": { - "version": "7.0.0", - "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-7.0.0.tgz", - "integrity": "sha512-IhdltX9BWhYQft4UPA92jFasNajskja0om6vU0DaIEL4OseCg5zE+mHAMr51AT89TbzzECrQWJ4CZ5NVYTPlKw==", + "version": "github:fb55/htmlparser2#da67eba77cbb938e489373181ff40245d422d1c7", + "from": "github:fb55/htmlparser2#da67eba77cbb938e489373181ff40245d422d1c7", "requires": { "domelementtype": "^2.0.1", "domhandler": "^4.0.0", diff --git a/package.json b/package.json index bc19848..5bdfa64 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "posthtmltree" ], "dependencies": { - "htmlparser2": "^7.0.0" + "htmlparser2": "github:fb55/htmlparser2#da67eba77cbb938e489373181ff40245d422d1c7" }, "devDependencies": { "@antfu/eslint-config-ts": "^0.4.3", From 41845391201256ec5e676c2d98ef292a80924b5c Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Tue, 24 Aug 2021 09:04:16 +0100 Subject: [PATCH 09/13] Use updated patched version --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 86ba362..c73568a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7315,8 +7315,8 @@ "dev": true }, "htmlparser2": { - "version": "github:fb55/htmlparser2#da67eba77cbb938e489373181ff40245d422d1c7", - "from": "github:fb55/htmlparser2#da67eba77cbb938e489373181ff40245d422d1c7", + "version": "github:fb55/htmlparser2#a2b0bf3fd8332c36575973c77e75d491e34ad30e", + "from": "github:fb55/htmlparser2#a2b0bf3fd8332c36575973c77e75d491e34ad30e", "requires": { "domelementtype": "^2.0.1", "domhandler": "^4.0.0", diff --git a/package.json b/package.json index 5bdfa64..3ee1f68 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "posthtmltree" ], "dependencies": { - "htmlparser2": "github:fb55/htmlparser2#da67eba77cbb938e489373181ff40245d422d1c7" + "htmlparser2": "github:fb55/htmlparser2#a2b0bf3fd8332c36575973c77e75d491e34ad30e" }, "devDependencies": { "@antfu/eslint-config-ts": "^0.4.3", From 2c5332156e6f45c2a1734592a3980a6f8d7f39e6 Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Sat, 28 Aug 2021 19:33:40 +0100 Subject: [PATCH 10/13] Use release --- package-lock.json | 13 +++++++------ package.json | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/package-lock.json b/package-lock.json index c73568a..b111dc8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7315,19 +7315,20 @@ "dev": true }, "htmlparser2": { - "version": "github:fb55/htmlparser2#a2b0bf3fd8332c36575973c77e75d491e34ad30e", - "from": "github:fb55/htmlparser2#a2b0bf3fd8332c36575973c77e75d491e34ad30e", + "version": "7.1.0", + "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-7.1.0.tgz", + "integrity": "sha512-C5W4WAg8imoK0gklCyLSaRR4aL8oyBmaMsYDXtQsIEIpTTJq9bv4ewHQpxaTaGK2kqwyZnsu34tqGlRG23LW/Q==", "requires": { "domelementtype": "^2.0.1", "domhandler": "^4.0.0", - "domutils": "^2.5.2", + "domutils": "^2.8.0", "entities": "^3.0.1" }, "dependencies": { "domutils": { - "version": "2.7.0", - "resolved": "https://registry.npmjs.org/domutils/-/domutils-2.7.0.tgz", - "integrity": "sha512-8eaHa17IwJUPAiB+SoTYBo5mCdeMgdcAoXJ59m6DT1vw+5iLS3gNoqYaRowaBKtGVrOF1Jz4yDTgYKLK2kvfJg==", + "version": "2.8.0", + "resolved": "https://registry.npmjs.org/domutils/-/domutils-2.8.0.tgz", + "integrity": "sha512-w96Cjofp72M5IIhpjgobBimYEfoPjx1Vx0BSX9P30WBdZW2WIKU0T1Bd0kz2eNZ9ikjKgHbEyKx8BB6H1L3h3A==", "requires": { "dom-serializer": "^1.0.1", "domelementtype": "^2.2.0", diff --git a/package.json b/package.json index 3ee1f68..10769e4 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "posthtmltree" ], "dependencies": { - "htmlparser2": "github:fb55/htmlparser2#a2b0bf3fd8332c36575973c77e75d491e34ad30e" + "htmlparser2": "^7.1.0" }, "devDependencies": { "@antfu/eslint-config-ts": "^0.4.3", From 4847a0899347758cd96829518fa6059aa7b13f6b Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Sun, 29 Aug 2021 22:13:39 +0100 Subject: [PATCH 11/13] Use isImplied --- package-lock.json | 12 ++++++------ package.json | 2 +- src/index.ts | 37 +++++++++++++------------------------ src/location-tracker.ts | 7 +------ 4 files changed, 21 insertions(+), 37 deletions(-) diff --git a/package-lock.json b/package-lock.json index b111dc8..e6c8811 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7315,9 +7315,9 @@ "dev": true }, "htmlparser2": { - "version": "7.1.0", - "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-7.1.0.tgz", - "integrity": "sha512-C5W4WAg8imoK0gklCyLSaRR4aL8oyBmaMsYDXtQsIEIpTTJq9bv4ewHQpxaTaGK2kqwyZnsu34tqGlRG23LW/Q==", + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/htmlparser2/-/htmlparser2-7.1.1.tgz", + "integrity": "sha512-hZb0lfG0hbhR/hB879zbBr8Opv0Be9Zp+JYHgqTw5epF++aotu/zmMTPLy/60iJyR1MaD/3pYRp7xYteXsZMEA==", "requires": { "domelementtype": "^2.0.1", "domhandler": "^4.0.0", @@ -7341,9 +7341,9 @@ "integrity": "sha512-DtBMo82pv1dFtUmHyr48beiuq792Sxohr+8Hm9zoxklYPfa6n0Z3Byjj2IV7bmr2IyqClnqEQhfgHJJ5QF0R5A==" }, "domhandler": { - "version": "4.2.0", - "resolved": "https://registry.npmjs.org/domhandler/-/domhandler-4.2.0.tgz", - "integrity": "sha512-zk7sgt970kzPks2Bf+dwT/PLzghLnsivb9CcxkvR8Mzr66Olr0Ofd8neSbglHJHaHa2MadfoSdNlKYAaafmWfA==", + "version": "4.2.2", + "resolved": "https://registry.npmjs.org/domhandler/-/domhandler-4.2.2.tgz", + "integrity": "sha512-PzE9aBMsdZO8TK4BnuJwH0QT41wgMbRzuZrHUcpYncEjmQazq8QEaBWgLG7ZyC/DAZKEgglpIA6j4Qn/HmxS3w==", "requires": { "domelementtype": "^2.2.0" } diff --git a/package.json b/package.json index 10769e4..20b8423 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "posthtmltree" ], "dependencies": { - "htmlparser2": "^7.1.0" + "htmlparser2": "^7.1.1" }, "devDependencies": { "@antfu/eslint-config-ts": "^0.4.3", diff --git a/src/index.ts b/src/index.ts index a922c78..1f7e689 100644 --- a/src/index.ts +++ b/src/index.ts @@ -44,16 +44,12 @@ export const parser = (html: string, options: Options = {}): Node[] => { const locationTracker = new LocationTracker(html); const bufArray: Node[] = []; const results: Node[] = []; - let lastIndices: [number, number]; + let lastOpenTagEndIndex = 0; - function bufferArrayLast(): Node | undefined { + function bufferArrayLast(): Node { return bufArray[bufArray.length - 1]; } - function resultsLast(): Node | undefined { - return results[results.length - 1]; - } - function isDirective(directive: Directive, tag: string): boolean { if (directive.name instanceof RegExp) { const regex = new RegExp(directive.name.source, 'i'); @@ -130,21 +126,11 @@ export const parser = (html: string, options: Options = {}): Node[] => { const buf: NodeTag = { tag }; if (options.sourceLocations) { - if (lastIndices?.[0] === parser.startIndex && lastIndices?.[1] === parser.endIndex) { - // The last closing tag was inferred, so we need to update its end location - const last = bufferArrayLast() || resultsLast(); - - if (typeof last === 'object' && Array.isArray(last.content) && last.location) { - last.location.end = locationTracker.getPosition(parser.startIndex - 1) - } - } - - const start = locationTracker.getPosition(parser.startIndex); - buf.location = { - start, - end: start + start: locationTracker.getPosition(parser.startIndex), + end: locationTracker.getPosition(parser.endIndex), }; + lastOpenTagEndIndex = parser.endIndex; } if (Object.keys(attrs).length > 0) { @@ -154,12 +140,15 @@ export const parser = (html: string, options: Options = {}): Node[] => { bufArray.push(buf); } - function onclosetag() { + function onclosetag(name: string, isImplied: boolean) { const buf: Node | undefined = bufArray.pop(); - if (buf && typeof buf === 'object' && buf.location && buf.location.end === buf.location.start && parser.endIndex !== null) { - lastIndices = [parser.startIndex, parser.endIndex]; - buf.location.end = locationTracker.getPosition(parser.endIndex); + if (buf && typeof buf === 'object' && buf.location && parser.endIndex !== null) { + if (!isImplied) { + buf.location.end = locationTracker.getPosition(parser.endIndex); + } else if (lastOpenTagEndIndex < parser.startIndex) { + buf.location.end = locationTracker.getPosition(parser.startIndex - 1); + } } if (buf) { @@ -183,7 +172,7 @@ export const parser = (html: string, options: Options = {}): Node[] => { } function ontext(text: string) { - const last = bufferArrayLast(); + const last: Node = bufferArrayLast(); if (last === undefined) { results.push(text); diff --git a/src/location-tracker.ts b/src/location-tracker.ts index bbcc81c..033959c 100644 --- a/src/location-tracker.ts +++ b/src/location-tracker.ts @@ -25,12 +25,7 @@ export class LocationTracker { getPosition(index: number): Position { if (index < this.lastIndex) { - this.lastPosition = { - line: 1, - column: 1 - }; - - this.lastIndex = 0; + throw new Error('Source indices must be monotonic'); } while (this.lastIndex < index) { From 5166475c953917c6aa3d9c76931f78011fdc54cc Mon Sep 17 00:00:00 2001 From: Chris Wilkinson Date: Sun, 29 Aug 2021 22:19:47 +0100 Subject: [PATCH 12/13] Linting --- src/index.ts | 2 +- test/test-core.spec.ts | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/index.ts b/src/index.ts index 1f7e689..a2e447b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -128,7 +128,7 @@ export const parser = (html: string, options: Options = {}): Node[] => { if (options.sourceLocations) { buf.location = { start: locationTracker.getPosition(parser.startIndex), - end: locationTracker.getPosition(parser.endIndex), + end: locationTracker.getPosition(parser.endIndex) }; lastOpenTagEndIndex = parser.endIndex; } diff --git a/test/test-core.spec.ts b/test/test-core.spec.ts index 18c036b..aebac75 100644 --- a/test/test-core.spec.ts +++ b/test/test-core.spec.ts @@ -308,18 +308,18 @@ test('should parse with source locations', t => { location: { start: { line: 2, - column: 18, + column: 18 }, end: { line: 2, - column: 21, - }, - }, + column: 21 + } + } }, { tag: 'p', content: [ - 'Bar\n', + 'Bar\n' ], location: { start: { @@ -344,7 +344,7 @@ test('should parse with source locations', t => { column: 4 } } - }, + } ]; t.deepEqual(tree, expected); }); From c6fff56de4c794470ae64be7e4e724969769de48 Mon Sep 17 00:00:00 2001 From: Ivan Demidov Date: Mon, 30 Aug 2021 10:11:27 +0300 Subject: [PATCH 13/13] 0.10.1 --- changelog.md | 18 ++++++++++++++++++ package-lock.json | 2 +- package.json | 2 +- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/changelog.md b/changelog.md index 190a474..747a4ef 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,23 @@ +## 0.10.1 (2021-08-30) + +* Force in something that works ([fb96d68](https://github.com/posthtml/posthtml-parser/commit/fb96d68)) +* Linting ([5166475](https://github.com/posthtml/posthtml-parser/commit/5166475)) +* Try and record previous indicies and rewrite if needed ([667ccf6](https://github.com/posthtml/posthtml-parser/commit/667ccf6)) +* Use isImplied ([4847a08](https://github.com/posthtml/posthtml-parser/commit/4847a08)) +* Use patched version ([c9fde63](https://github.com/posthtml/posthtml-parser/commit/c9fde63)) +* Use patched version ([1bd6633](https://github.com/posthtml/posthtml-parser/commit/1bd6633)) +* Use release ([2c53321](https://github.com/posthtml/posthtml-parser/commit/2c53321)) +* Use release ([4f00e5c](https://github.com/posthtml/posthtml-parser/commit/4f00e5c)) +* Use updated patched version ([4184539](https://github.com/posthtml/posthtml-parser/commit/4184539)) +* test: break test to demonstrate bug ([74c165c](https://github.com/posthtml/posthtml-parser/commit/74c165c)) +* build(deps): bump path-parse from 1.0.6 to 1.0.7 ([4f4ab7e](https://github.com/posthtml/posthtml-parser/commit/4f4ab7e)) +* docs: update import ([53bee3d](https://github.com/posthtml/posthtml-parser/commit/53bee3d)) + + + ## 0.10.0 (2021-07-28) +* 0.10.0 ([b569e1a](https://github.com/posthtml/posthtml-parser/commit/b569e1a)) * refactor: parse now export not default ([f8ae42d](https://github.com/posthtml/posthtml-parser/commit/f8ae42d)) * perf: resolve types ([9ed9bd0](https://github.com/posthtml/posthtml-parser/commit/9ed9bd0)) diff --git a/package-lock.json b/package-lock.json index e6c8811..4ee4f85 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "posthtml-parser", - "version": "0.10.0", + "version": "0.10.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 20b8423..14f4352 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "posthtml-parser", - "version": "0.10.0", + "version": "0.10.1", "description": "Parse HTML/XML to PostHTMLTree", "license": "MIT", "repository": "posthtml/posthtml-parser",