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 f6a9465..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": { @@ -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,46 @@ "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": "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", - "domutils": "^2.4.4", - "entities": "^2.0.0" + "domutils": "^2.8.0", + "entities": "^3.0.1" + }, + "dependencies": { + "domutils": { + "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", + "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.2", + "resolved": "https://registry.npmjs.org/domhandler/-/domhandler-4.2.2.tgz", + "integrity": "sha512-PzE9aBMsdZO8TK4BnuJwH0QT41wgMbRzuZrHUcpYncEjmQazq8QEaBWgLG7ZyC/DAZKEgglpIA6j4Qn/HmxS3w==", + "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": { @@ -9677,9 +9710,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": { diff --git a/package.json b/package.json index 4c71c52..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", @@ -39,7 +39,7 @@ "posthtmltree" ], "dependencies": { - "htmlparser2": "^6.0.0" + "htmlparser2": "^7.1.1" }, "devDependencies": { "@antfu/eslint-config-ts": "^0.4.3", 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') diff --git a/src/index.ts b/src/index.ts index d9bdd82..a2e447b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -44,6 +44,7 @@ export const parser = (html: string, options: Options = {}): Node[] => { const locationTracker = new LocationTracker(html); const bufArray: Node[] = []; const results: Node[] = []; + let lastOpenTagEndIndex = 0; function bufferArrayLast(): Node { return bufArray[bufArray.length - 1]; @@ -122,14 +123,14 @@ 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) { buf.location = { - start, - end: start + start: locationTracker.getPosition(parser.startIndex), + end: locationTracker.getPosition(parser.endIndex) }; + lastOpenTagEndIndex = parser.endIndex; } if (Object.keys(attrs).length > 0) { @@ -139,11 +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 && parser.endIndex !== null) { - buf.location.end = locationTracker.getPosition(parser.endIndex); + if (!isImplied) { + buf.location.end = locationTracker.getPosition(parser.endIndex); + } else if (lastOpenTagEndIndex < parser.startIndex) { + buf.location.end = locationTracker.getPosition(parser.startIndex - 1); + } } if (buf) { diff --git a/test/test-core.spec.ts b/test/test-core.spec.ts index 17f889b..aebac75 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 = '
Foo
'; + const html = 'Foo
Bar\n