';
+ const tree = parser(html, {sourceLocations: true});
+ const expected = [
+ {
+ tag: 'h1',
+ content: ['Test'],
+ loc: {
+ start: {
+ line: 1,
+ column: 1
+ },
+ end: {
+ line: 1,
+ column: 13
+ }
+ }
+ },
+ '\n',
+ {
+ tag: 'p',
+ content: [
+ {
+ tag: 'b',
+ content: ['Foo'],
+ loc: {
+ start: {
+ line: 2,
+ column: 4
+ },
+ end: {
+ line: 2,
+ column: 13
+ }
+ }
+ }
+ ],
+ loc: {
+ start: {
+ line: 2,
+ column: 1
+ },
+ end: {
+ line: 2,
+ column: 17
+ }
+ }
+ }
+ ];
+ t.deepEqual(tree, expected);
+});
diff --git a/types/index.d.ts b/types/index.d.ts
index 7bb532f..0518569 100644
--- a/types/index.d.ts
+++ b/types/index.d.ts
@@ -12,6 +12,7 @@ export type Directive = {
export type Options = {
directives?: Directive[];
+ sourceLocations?: boolean;
} & ParserOptions;
export type Node = NodeText | NodeTag;
@@ -20,6 +21,16 @@ export type NodeTag = {
tag?: string | boolean;
attrs?: Attributes;
content?: Node[];
+ loc?: SourceLocation;
};
export type Attributes = Record;
+export type SourceLocation = {
+ start: Position;
+ end: Position;
+};
+
+export type Position = {
+ line: number;
+ column: number;
+};
From 7d6f76b3db9228bfc01f553fda4884a2f8b712aa Mon Sep 17 00:00:00 2001
From: Devon Govett
Date: Wed, 5 May 2021 20:12:12 -0700
Subject: [PATCH 02/59] Move location tracker to a separate file and refactor
into class
---
src/index.ts | 38 ++++++--------------------------------
src/location-tracker.ts | 39 +++++++++++++++++++++++++++++++++++++++
test/test-core.spec.ts | 6 +++---
types/index.d.ts | 2 +-
4 files changed, 49 insertions(+), 36 deletions(-)
create mode 100644 src/location-tracker.ts
diff --git a/src/index.ts b/src/index.ts
index 256638d..9e60796 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,5 +1,6 @@
import {Parser, ParserOptions} from 'htmlparser2';
import {Directive, Node, NodeTag, Options, Attributes} from '../types/index.d';
+import {LocationTracker} from './location-tracker';
const defaultOptions: ParserOptions = {
lowerCaseTags: false,
@@ -16,6 +17,7 @@ const defaultDirectives: Directive[] = [
];
const parser = (html: string, options: Options = {}): Node[] => {
+ const locationTracker = new LocationTracker(html);
const bufArray: Node[] = [];
const results: Node[] = [];
@@ -49,34 +51,6 @@ const parser = (html: string, options: Options = {}): Node[] => {
return result;
}
- const lastLoc = {
- line: 1,
- column: 1
- };
-
- let lastIndex = 0;
- function getLoc(index: number) {
- if (index < lastIndex) {
- throw new Error('Source indices must be monotonic');
- }
-
- while (lastIndex < index) {
- if (html.charCodeAt(lastIndex) === /* \n */ 10) {
- lastLoc.line++;
- lastLoc.column = 1;
- } else {
- lastLoc.column++;
- }
-
- lastIndex++;
- }
-
- return {
- line: lastLoc.line,
- column: lastLoc.column
- };
- }
-
function onprocessinginstruction(name: string, data: string) {
const directives = defaultDirectives.concat(options.directives ?? []);
const last: Node = bufferArrayLast();
@@ -120,11 +94,11 @@ const parser = (html: string, options: Options = {}): Node[] => {
}
function onopentag(tag: string, attrs: Attributes) {
- const start = getLoc(parser.startIndex);
+ const start = locationTracker.getPosition(parser.startIndex);
const buf: NodeTag = {tag};
if (options.sourceLocations) {
- buf.loc = {
+ buf.location = {
start,
end: start
};
@@ -140,8 +114,8 @@ const parser = (html: string, options: Options = {}): Node[] => {
function onclosetag() {
const buf: Node | undefined = bufArray.pop();
- if (buf && typeof buf === 'object' && buf.loc && parser.endIndex !== null) {
- buf.loc.end = getLoc(parser.endIndex);
+ if (buf && typeof buf === 'object' && buf.location && parser.endIndex !== null) {
+ buf.location.end = locationTracker.getPosition(parser.endIndex);
}
if (buf) {
diff --git a/src/location-tracker.ts b/src/location-tracker.ts
new file mode 100644
index 0000000..95d5ba2
--- /dev/null
+++ b/src/location-tracker.ts
@@ -0,0 +1,39 @@
+import {Position} from '../types/index.d';
+
+export class LocationTracker {
+ private readonly source: string;
+ private lastPosition: Position;
+ private lastIndex: number;
+
+ constructor(source: string) {
+ this.source = source;
+ this.lastPosition = {
+ line: 1,
+ column: 1
+ };
+
+ this.lastIndex = 0;
+ }
+
+ getPosition(index: number): Position {
+ if (index < this.lastIndex) {
+ throw new Error('Source indices must be monotonic');
+ }
+
+ while (this.lastIndex < index) {
+ if (this.source.charCodeAt(this.lastIndex) === /* \n */ 10) {
+ this.lastPosition.line++;
+ this.lastPosition.column = 1;
+ } else {
+ this.lastPosition.column++;
+ }
+
+ this.lastIndex++;
+ }
+
+ return {
+ line: this.lastPosition.line,
+ column: this.lastPosition.column
+ };
+ }
+}
diff --git a/test/test-core.spec.ts b/test/test-core.spec.ts
index 92177af..e1f8673 100644
--- a/test/test-core.spec.ts
+++ b/test/test-core.spec.ts
@@ -248,7 +248,7 @@ test('should parse with source locations', t => {
{
tag: 'h1',
content: ['Test'],
- loc: {
+ location: {
start: {
line: 1,
column: 1
@@ -266,7 +266,7 @@ test('should parse with source locations', t => {
{
tag: 'b',
content: ['Foo'],
- loc: {
+ location: {
start: {
line: 2,
column: 4
@@ -278,7 +278,7 @@ test('should parse with source locations', t => {
}
}
],
- loc: {
+ location: {
start: {
line: 2,
column: 1
diff --git a/types/index.d.ts b/types/index.d.ts
index 0518569..b3ece55 100644
--- a/types/index.d.ts
+++ b/types/index.d.ts
@@ -21,7 +21,7 @@ export type NodeTag = {
tag?: string | boolean;
attrs?: Attributes;
content?: Node[];
- loc?: SourceLocation;
+ location?: SourceLocation;
};
export type Attributes = Record;
From 8373816269efbd943ea84e12dda9620a3eccc52b Mon Sep 17 00:00:00 2001
From: Ivan Demidov
Date: Thu, 6 May 2021 14:51:59 +0300
Subject: [PATCH 03/59] perf: content two dimensional array
---
types/index.d.ts | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/types/index.d.ts b/types/index.d.ts
index dba2073..d269117 100644
--- a/types/index.d.ts
+++ b/types/index.d.ts
@@ -16,7 +16,7 @@ export type Options = {
export type Tag = string | boolean;
export type Attributes = Record;
-export type Content = NodeText | Node[];
+export type Content = NodeText | Node[] | Node[][];
export type NodeText = string | number;
export type NodeTag = {
From 14d353ef97f7290a292e7378c76c1c074e7a5826 Mon Sep 17 00:00:00 2001
From: Ivan Demidov
Date: Thu, 6 May 2021 14:53:58 +0300
Subject: [PATCH 04/59] 0.8.5
---
changelog.md | 7 +++++++
package-lock.json | 2 +-
package.json | 2 +-
3 files changed, 9 insertions(+), 2 deletions(-)
diff --git a/changelog.md b/changelog.md
index 376f5fc..279e148 100644
--- a/changelog.md
+++ b/changelog.md
@@ -1,5 +1,12 @@
+## 0.8.5 (2021-05-06)
+
+* perf: content two dimensional array ([8373816](https://github.com/posthtml/posthtml-parser/commit/8373816))
+
+
+
## 0.8.4 (2021-05-06)
+* 0.8.4 ([aaa0b3e](https://github.com/posthtml/posthtml-parser/commit/aaa0b3e))
* perf: attr value as number ([3d08ec5](https://github.com/posthtml/posthtml-parser/commit/3d08ec5))
diff --git a/package-lock.json b/package-lock.json
index 040db20..3d064f0 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,6 +1,6 @@
{
"name": "posthtml-parser",
- "version": "0.8.4",
+ "version": "0.8.5",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
diff --git a/package.json b/package.json
index 459dd10..5632730 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "posthtml-parser",
- "version": "0.8.4",
+ "version": "0.8.5",
"description": "Parse HTML/XML to PostHTMLTree",
"license": "MIT",
"repository": "posthtml/posthtml-parser",
From e6bc6cdf5d246fcf8a7ece12a49a4d1c617f415d Mon Sep 17 00:00:00 2001
From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com>
Date: Thu, 6 May 2021 17:24:19 +0000
Subject: [PATCH 05/59] build(deps): bump handlebars from 4.7.6 to 4.7.7
Bumps [handlebars](https://github.com/wycats/handlebars.js) from 4.7.6 to 4.7.7.
- [Release notes](https://github.com/wycats/handlebars.js/releases)
- [Changelog](https://github.com/handlebars-lang/handlebars.js/blob/master/release-notes.md)
- [Commits](https://github.com/wycats/handlebars.js/compare/v4.7.6...v4.7.7)
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 3d064f0..2058556 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -7155,9 +7155,9 @@
}
},
"handlebars": {
- "version": "4.7.6",
- "resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.7.6.tgz",
- "integrity": "sha512-1f2BACcBfiwAfStCKZNrUCgqNZkGsAT7UM3kkYtXuLo0KnaVfjKOyf7PRzB6++aK9STyT1Pd2ZCPe3EGOXleXA==",
+ "version": "4.7.7",
+ "resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.7.7.tgz",
+ "integrity": "sha512-aAcXm5OAfE/8IXkcZvCepKU3VzW1/39Fb5ZuqMtgI/hT8X2YgoMvBY5dLhq/cpOvw7Lk1nK/UF71aLG/ZnVYRA==",
"dev": true,
"requires": {
"minimist": "^1.2.5",
From e9b8d0452f5a288bf7c98e1b950a134e245306b7 Mon Sep 17 00:00:00 2001
From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com>
Date: Thu, 6 May 2021 20:42:36 +0000
Subject: [PATCH 06/59] build(deps): bump lodash from 4.17.20 to 4.17.21
Bumps [lodash](https://github.com/lodash/lodash) from 4.17.20 to 4.17.21.
- [Release notes](https://github.com/lodash/lodash/releases)
- [Commits](https://github.com/lodash/lodash/compare/4.17.20...4.17.21)
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 3d064f0..886b1c3 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -8405,9 +8405,9 @@
}
},
"lodash": {
- "version": "4.17.20",
- "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz",
- "integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==",
+ "version": "4.17.21",
+ "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
+ "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==",
"dev": true
},
"lodash._reinterpolate": {
From d61007bb6b535d4414124b4b93194d63b6673fa1 Mon Sep 17 00:00:00 2001
From: Devon Govett
Date: Fri, 7 May 2021 22:21:19 -0700
Subject: [PATCH 07/59] Add description to docs
---
readme.md | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/readme.md b/readme.md
index 7a6775f..0a58363 100644
--- a/readme.md
+++ b/readme.md
@@ -115,6 +115,11 @@ Type: `Boolean`
Default: `false`
Description: *If set to true, self-closing tags will trigger the `onclosetag` event even if `xmlMode` is not set to `true`. NOTE: If `xmlMode` is set to `true` then self-closing tags will always be recognized.*
+### `sourceLocations`
+Type: `Boolean`
+Default: `false`
+Description: *If set to true, AST nodes will have a `location` property containing the `start` and `end` line and column position of the node.*
+
## License
[MIT](LICENSE)
From d725e837d606621927e2b2978a6162da896e9c9c Mon Sep 17 00:00:00 2001
From: Ivan Demidov
Date: Tue, 11 May 2021 09:47:31 +0300
Subject: [PATCH 08/59] 0.9.0
---
changelog.md | 9 +++++++++
package-lock.json | 2 +-
package.json | 2 +-
3 files changed, 11 insertions(+), 2 deletions(-)
diff --git a/changelog.md b/changelog.md
index 279e148..e5013a3 100644
--- a/changelog.md
+++ b/changelog.md
@@ -1,5 +1,14 @@
+## 0.9.0 (2021-05-11)
+
+* Add description to docs ([d61007b](https://github.com/posthtml/posthtml-parser/commit/d61007b))
+* Move location tracker to a separate file and refactor into class ([7d6f76b](https://github.com/posthtml/posthtml-parser/commit/7d6f76b))
+* feat: add optional support for source locations ([6faf50d](https://github.com/posthtml/posthtml-parser/commit/6faf50d))
+
+
+
## 0.8.5 (2021-05-06)
+* 0.8.5 ([14d353e](https://github.com/posthtml/posthtml-parser/commit/14d353e))
* perf: content two dimensional array ([8373816](https://github.com/posthtml/posthtml-parser/commit/8373816))
diff --git a/package-lock.json b/package-lock.json
index 3d064f0..bfeae47 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,6 +1,6 @@
{
"name": "posthtml-parser",
- "version": "0.8.5",
+ "version": "0.9.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
diff --git a/package.json b/package.json
index 5632730..32cb246 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "posthtml-parser",
- "version": "0.8.5",
+ "version": "0.9.0",
"description": "Parse HTML/XML to PostHTMLTree",
"license": "MIT",
"repository": "posthtml/posthtml-parser",
From 213759175dcd7c74d6484303cbc73bfd988bcacf Mon Sep 17 00:00:00 2001
From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com>
Date: Tue, 11 May 2021 22:14:25 +0000
Subject: [PATCH 09/59] build(deps): bump hosted-git-info from 2.8.8 to 2.8.9
Bumps [hosted-git-info](https://github.com/npm/hosted-git-info) from 2.8.8 to 2.8.9.
- [Release notes](https://github.com/npm/hosted-git-info/releases)
- [Changelog](https://github.com/npm/hosted-git-info/blob/v2.8.9/CHANGELOG.md)
- [Commits](https://github.com/npm/hosted-git-info/compare/v2.8.8...v2.8.9)
Signed-off-by: dependabot[bot]
---
package-lock.json | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/package-lock.json b/package-lock.json
index 29a7328..eca212d 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -7302,9 +7302,9 @@
}
},
"hosted-git-info": {
- "version": "2.8.8",
- "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.8.8.tgz",
- "integrity": "sha512-f/wzC2QaWBs7t9IYqB4T3sR1xviIViXJRJTWBlx2Gf3g0Xi5vI7Yy4koXQ1c9OYDGHN9sBy1DQ2AB8fqZBWhUg==",
+ "version": "2.8.9",
+ "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.8.9.tgz",
+ "integrity": "sha512-mxIDAb9Lsm6DoOJ7xH+5+X4y1LU/4Hi50L9C5sIswK3JzULS4bwk1FvjdBgvYR4bzT4tuUQiC15FE2f5HbLvYw==",
"dev": true
},
"html-escaper": {
@@ -9116,9 +9116,9 @@
},
"dependencies": {
"hosted-git-info": {
- "version": "3.0.7",
- "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-3.0.7.tgz",
- "integrity": "sha512-fWqc0IcuXs+BmE9orLDyVykAG9GJtGLGuZAAqgcckPgv5xad4AcXGIv8galtQvlwutxSlaMcdw7BUtq2EIvqCQ==",
+ "version": "3.0.8",
+ "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-3.0.8.tgz",
+ "integrity": "sha512-aXpmwoOhRBrw6X3j0h5RloK4x1OzsxMPyxqIHyNfSe2pypkVTZFpEiRoSipPEPlMrh0HW/XsjkJ5WgnCirpNUw==",
"dev": true,
"requires": {
"lru-cache": "^6.0.0"
From 7081062588fedff14fb3b89a1952325c91fd2a38 Mon Sep 17 00:00:00 2001
From: Ivan Demidov
Date: Thu, 3 Jun 2021 10:00:47 +0300
Subject: [PATCH 10/59] Update readme.md
---
readme.md | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/readme.md b/readme.md
index 0a58363..0257988 100644
--- a/readme.md
+++ b/readme.md
@@ -115,9 +115,9 @@ Type: `Boolean`
Default: `false`
Description: *If set to true, self-closing tags will trigger the `onclosetag` event even if `xmlMode` is not set to `true`. NOTE: If `xmlMode` is set to `true` then self-closing tags will always be recognized.*
-### `sourceLocations`
-Type: `Boolean`
-Default: `false`
+### `sourceLocations`
+Type: `Boolean`
+Default: `false`
Description: *If set to true, AST nodes will have a `location` property containing the `start` and `end` line and column position of the node.*
## License
From 9e0d1f9203c3fbbbea8d31396bafee36749bbecf Mon Sep 17 00:00:00 2001
From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com>
Date: Sat, 12 Jun 2021 02:31:51 +0000
Subject: [PATCH 11/59] build(deps): bump normalize-url from 4.5.0 to 4.5.1
Bumps [normalize-url](https://github.com/sindresorhus/normalize-url) from 4.5.0 to 4.5.1.
- [Release notes](https://github.com/sindresorhus/normalize-url/releases)
- [Commits](https://github.com/sindresorhus/normalize-url/commits)
---
updated-dependencies:
- dependency-name: normalize-url
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 eca212d..f3c390a 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -9133,9 +9133,9 @@
"dev": true
},
"normalize-url": {
- "version": "4.5.0",
- "resolved": "https://registry.npmjs.org/normalize-url/-/normalize-url-4.5.0.tgz",
- "integrity": "sha512-2s47yzUxdexf1OhyRi4Em83iQk0aPvwTddtFz4hnSSw9dCEsLEGf6SwIO8ss/19S9iBb5sJaOuTvTGDeZI00BQ==",
+ "version": "4.5.1",
+ "resolved": "https://registry.npmjs.org/normalize-url/-/normalize-url-4.5.1.tgz",
+ "integrity": "sha512-9UZCFRHQdNrfTpGg8+1INIg93B6zE0aXMVFkw1WFwvO4SlZywU6aLg5Of0Ap/PgcbSw4LNxvMWXMeugwMCX0AA==",
"dev": true
},
"npm-run-path": {
From 94ebb8df0374050cf5d8041d9f5c7675563f7cf7 Mon Sep 17 00:00:00 2001
From: Ivan Demidov
Date: Mon, 26 Jul 2021 10:38:12 +0300
Subject: [PATCH 12/59] perf: type for replace
---
src/index.ts | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/index.ts b/src/index.ts
index 9e60796..99fb493 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -44,7 +44,7 @@ const parser = (html: string, options: Options = {}): Node[] => {
Object.keys(attrs).forEach((key: string) => {
const object: Attributes = {};
- object[key] = attrs[key].replace(/"/g, '"');
+ object[key] = String(attrs[key]).replace(/"/g, '"');
Object.assign(result, object);
});
From 72f209247ab88411cb04a87837cd2e28c728ff30 Mon Sep 17 00:00:00 2001
From: Ivan Demidov
Date: Mon, 26 Jul 2021 11:56:44 +0300
Subject: [PATCH 13/59] style: after prettier
---
src/index.ts | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/src/index.ts b/src/index.ts
index 99fb493..68ecb7d 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,6 +1,6 @@
-import {Parser, ParserOptions} from 'htmlparser2';
-import {Directive, Node, NodeTag, Options, Attributes} from '../types/index.d';
-import {LocationTracker} from './location-tracker';
+import { Parser, ParserOptions } from 'htmlparser2';
+import { Directive, Node, NodeTag, Options, Attributes } from '../types/index.d';
+import { LocationTracker } from './location-tracker';
const defaultOptions: ParserOptions = {
lowerCaseTags: false,
@@ -95,7 +95,7 @@ const parser = (html: string, options: Options = {}): Node[] => {
function onopentag(tag: string, attrs: Attributes) {
const start = locationTracker.getPosition(parser.startIndex);
- const buf: NodeTag = {tag};
+ const buf: NodeTag = { tag };
if (options.sourceLocations) {
buf.location = {
@@ -167,7 +167,7 @@ const parser = (html: string, options: Options = {}): Node[] => {
onopentag,
onclosetag,
ontext
- }, {...defaultOptions, ...options});
+ }, { ...defaultOptions, ...options });
parser.write(html);
parser.end();
From fdfe82e36a0bf847189e0593a6d25c8135b4e6b7 Mon Sep 17 00:00:00 2001
From: Ivan Demidov
Date: Mon, 26 Jul 2021 12:02:15 +0300
Subject: [PATCH 14/59] test: dom nest parsing issues #76
---
test/test-core.spec.ts | 23 +++++++++++++++++++++++
1 file changed, 23 insertions(+)
diff --git a/test/test-core.spec.ts b/test/test-core.spec.ts
index e1f8673..7e39ec3 100644
--- a/test/test-core.spec.ts
+++ b/test/test-core.spec.ts
@@ -292,3 +292,26 @@ test('should parse with source locations', t => {
];
t.deepEqual(tree, expected);
});
+
+test.only('should parse with input in button', t => {
+ const html = '';
+ const tree = parser(html);
+ const expected = [
+ {
+ tag: 'button',
+ content: [
+ 'Hello ',
+ {
+ tag: 'input',
+ attrs: {
+ type: 'file',
+ 'ng-hide': true
+ }
+ },
+ 'PostHtml'
+ ]
+ }
+ ];
+ console.log({tree});
+ t.deepEqual(tree, expected);
+});
From 47dceb6610544a1a5537a864448f5a207c0f04b6 Mon Sep 17 00:00:00 2001
From: Ivan Demidov
Date: Mon, 26 Jul 2021 12:02:50 +0300
Subject: [PATCH 15/59] build: set object-curly-spacing always
---
xo.config.js | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/xo.config.js b/xo.config.js
index da99e0a..f8a0390 100644
--- a/xo.config.js
+++ b/xo.config.js
@@ -3,6 +3,7 @@ module.exports = {
rules: {
'@typescript-eslint/prefer-readonly-parameter-types': 'off',
'ava/no-skip-test': 'off',
- 'ava/no-only-test': 'off'
+ 'ava/no-only-test': 'off',
+ 'object-curly-spacing': ['error','always']
}
};
From ccbfd69d2ac80a709588622b482958b8122c6c2c Mon Sep 17 00:00:00 2001
From: Ivan Demidov
Date: Mon, 26 Jul 2021 12:03:37 +0300
Subject: [PATCH 16/59] style: after prettier
---
src/location-tracker.ts | 2 +-
test/test-core.spec.ts | 47 +++++++++++++++++++++--------------------
types/index.d.ts | 2 +-
xo.config.js | 2 +-
4 files changed, 27 insertions(+), 26 deletions(-)
diff --git a/src/location-tracker.ts b/src/location-tracker.ts
index 95d5ba2..e5e82a2 100644
--- a/src/location-tracker.ts
+++ b/src/location-tracker.ts
@@ -1,4 +1,4 @@
-import {Position} from '../types/index.d';
+import { Position } from '../types/index.d';
export class LocationTracker {
private readonly source: string;
diff --git a/test/test-core.spec.ts b/test/test-core.spec.ts
index 7e39ec3..7f99920 100644
--- a/test/test-core.spec.ts
+++ b/test/test-core.spec.ts
@@ -14,8 +14,8 @@ test('should be parse comment', t => {
});
test('should be parse CDATA', t => {
- const tree = parser('', {xmlMode: true});
- const expected = [{tag: 'script', content: ['console.log(1);']}];
+ const tree = parser('', { xmlMode: true });
+ const expected = [{ tag: 'script', content: ['console.log(1);'] }];
t.deepEqual(tree, expected);
});
@@ -52,7 +52,7 @@ test.skip('should be parse tag with object in attribute data witchout escape', t
});
test.skip('should be parse tag with object in attribute data escape', t => {
- const json = JSON.stringify({button: {checkedView: 'extra'}});
+ const json = JSON.stringify({ button: { checkedView: 'extra' } });
const html = '';
const tree = parser(html);
@@ -70,25 +70,25 @@ test.skip('should be parse tag with object in attribute data escape', t => {
test('should be parse isolated comment', t => {
const tree = parser('');
- const expected = [{tag: 'div', content: ['']}];
+ const expected = [{ tag: 'div', content: [''] }];
t.deepEqual(tree, expected);
});
test('should be parse comment before text content', t => {
const tree = parser('
Text after comment
');
- const expected = [{tag: 'div', content: ['', 'Text after comment']}];
+ const expected = [{ tag: 'div', content: ['', 'Text after comment'] }];
t.deepEqual(tree, expected);
});
test('should be parse comment after text content', t => {
const tree = parser('
Text before comment.
');
- const expected = [{tag: 'div', content: ['Text before comment.', '']}];
+ const expected = [{ tag: 'div', content: ['Text before comment.', ''] }];
t.deepEqual(tree, expected);
});
test('should be parse comment in the middle of text content', t => {
const tree = parser('
Text surrounding a comment.
');
- const expected = [{tag: 'div', content: ['Text surrounding ', '', ' a comment.']}];
+ const expected = [{ tag: 'div', content: ['Text surrounding ', '', ' a comment.'] }];
t.deepEqual(tree, expected);
});
@@ -101,7 +101,7 @@ test('should be parse doctype', t => {
test('should be parse directive', t => {
const options = {
directives: [
- {name: '?php', start: '<', end: '>'}
+ { name: '?php', start: '<', end: '>' }
]
};
const tree = parser('', options);
@@ -112,7 +112,7 @@ test('should be parse directive', t => {
test('should be parse regular expression directive', t => {
const options = {
directives: [
- {name: /\?(php|=).*/, start: '<', end: '>'}
+ { name: /\?(php|=).*/, start: '<', end: '>' }
]
};
const tree1 = parser('', options);
@@ -127,8 +127,8 @@ test('should be parse regular expression directive', t => {
test('should be parse directives and tag', t => {
const options = {
directives: [
- {name: '!doctype', start: '<', end: '>'},
- {name: '?php', start: '<', end: '>'}
+ { name: '!doctype', start: '<', end: '>' },
+ { name: '?php', start: '<', end: '>' }
]
};
const html = '{{%njk test %}}';
@@ -149,20 +149,20 @@ test('should be parse directives and tag', t => {
test('should be parse tag', t => {
const tree = parser('');
- const expected = [{tag: 'html'}];
+ const expected = [{ tag: 'html' }];
t.deepEqual(tree, expected);
});
test('should be parse doctype and tag', t => {
const tree = parser('');
- const expected = ['', {tag: 'html'}];
+ const expected = ['', { tag: 'html' }];
t.deepEqual(tree, expected);
});
test('should be parse tag attrs', t => {
const tree = parser('');
const expected = [{
- tag: 'div', attrs: {id: 'id', class: 'class'}
+ tag: 'div', attrs: { id: 'id', class: 'class' }
}];
t.deepEqual(tree, expected);
});
@@ -175,14 +175,14 @@ test('should be parse text', t => {
test('should be parse text in content', t => {
const tree = parser('
Text
');
- const expected = [{tag: 'div', content: ['Text']}];
+ const expected = [{ tag: 'div', content: ['Text'] }];
t.deepEqual(tree, expected);
});
test('should be parse not a single node in tree', t => {
const tree = parser('Text1Text2Text3');
const expected = [
- {tag: 'span', content: ['Text1']}, {tag: 'span', content: ['Text2']}, 'Text3'
+ { tag: 'span', content: ['Text1'] }, { tag: 'span', content: ['Text2'] }, 'Text3'
];
t.deepEqual(tree, expected);
});
@@ -190,7 +190,7 @@ test('should be parse not a single node in tree', t => {
test('should be parse not a single node in parent content', t => {
const tree = parser('
Text1Text2Text3
');
const expected = [
- {tag: 'div', content: [{tag: 'span', content: ['Text1']}, {tag: 'span', content: ['Text2']}, 'Text3']}
+ { tag: 'div', content: [{ tag: 'span', content: ['Text1'] }, { tag: 'span', content: ['Text2'] }, 'Text3'] }
];
t.deepEqual(tree, expected);
});
@@ -198,7 +198,7 @@ test('should be parse not a single node in parent content', t => {
test('should be parse camelCase tag name', t => {
const tree = parser('');
const expected = [
- {tag: 'mySuperTag'}
+ { tag: 'mySuperTag' }
];
t.deepEqual(tree, expected);
});
@@ -207,7 +207,7 @@ test('should be parse simple contents are split with "<" in comment', t => {
const html = ' /* width < 800px */ test';
const tree = parser(html);
const expected = [
- {tag: 'a', content: [' /* width < 800px */ ', {tag: 'hr'}, ' test']}
+ { tag: 'a', content: [' /* width < 800px */ ', { tag: 'hr' }, ' test'] }
];
t.deepEqual(tree, expected);
});
@@ -216,7 +216,7 @@ test('should be parse style contents are split with "<" in comment', t => {
const html = '';
const tree = parser(html);
const expected = [
- {tag: 'style', content: [' /* width < 800px */ @media (max-width: 800px) { /* selectors */} ']}
+ { tag: 'style', content: [' /* width < 800px */ @media (max-width: 800px) { /* selectors */} '] }
];
t.deepEqual(tree, expected);
});
@@ -229,7 +229,8 @@ test('should be parse script contents are split with "<" in comment', t => {
tag: 'script',
content: [
' var str = \'hey