Skip to content

Commit 23dd2a0

Browse files
committed
style: after lint
1 parent 5233a43 commit 23dd2a0

3 files changed

Lines changed: 95 additions & 88 deletions

File tree

index.d.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,96 @@
11
declare namespace parser {
22
type DefaultOptions = {
3-
/**
3+
/**
44
* @default false
55
*/
6-
lowerCaseTags: boolean;
6+
lowerCaseTags: boolean;
77

8-
/**
8+
/**
99
* @default false
1010
*/
11-
lowerCaseAttributeNames: boolean;
11+
lowerCaseAttributeNames: boolean;
1212
};
1313

1414
type Directive = {
15-
name: string;
16-
start: string;
17-
end: string;
15+
name: string;
16+
start: string;
17+
end: string;
1818
};
1919

2020
type Options = {
21-
/**
21+
/**
2222
* Adds processing of custom directives.
2323
* Note: The property `name` in custom directives can be `String` or `RegExp` type.
2424
*
2525
* @default
2626
* [{name: '!doctype', start: '<', end: '>'}]
2727
*/
28-
directives?: Directive[];
28+
directives?: Directive[];
2929

30-
/**
30+
/**
3131
* Indicates whether special tags (`<script>` and `<style>`) should get special treatment and if "empty" tags (eg. `<br>`) can have children.
3232
* If `false`, the content of special tags will be text only.
3333
* For feeds and other XML content (documents that don't consist of HTML), set this to true.
3434
*
3535
* @default false
3636
*/
37-
xmlMode?: boolean;
37+
xmlMode?: boolean;
3838

39-
/**
39+
/**
4040
* If set to `true`, entities within the document will be decoded.
4141
*
4242
* @default false
4343
*/
44-
decodeEntities?: boolean;
44+
decodeEntities?: boolean;
4545

46-
/**
46+
/**
4747
* If set to `true`, all tags will be lowercased. If `xmlMode` is disabled.
4848
*
4949
* @default false
5050
*/
51-
lowerCaseTags?: boolean;
51+
lowerCaseTags?: boolean;
5252

53-
/**
53+
/**
5454
* If set to `true`, all attribute names will be lowercased. This has noticeable impact on speed.
5555
*
5656
* @default false
5757
*/
58-
lowerCaseAttributeNames?: boolean;
58+
lowerCaseAttributeNames?: boolean;
5959

60-
/**
60+
/**
6161
* If set to true, CDATA sections will be recognized as text even if the `xmlMode` option is not enabled.
6262
* NOTE: If `xmlMode` is set to `true` then CDATA sections will always be recognized as text.
6363
*
6464
* @default false
6565
*/
66-
recognizeCDATA?: boolean;
66+
recognizeCDATA?: boolean;
6767

68-
/**
68+
/**
6969
* If set to `true`, self-closing tags will trigger the `onclosetag` event even if `xmlMode` is not set to `true`.
7070
* NOTE: If `xmlMode` is set to true then self-closing tags will always be recognized.
7171
*
7272
* @default false
7373
*/
74-
recognizeSelfClosing?: boolean;
74+
recognizeSelfClosing?: boolean;
7575
};
7676

7777
type Tree = Node[];
7878
type Node = NodeText | NodeTag;
7979
type NodeText = string;
8080
type NodeTag = {
81-
tag: string;
82-
attrs?: Attributes;
83-
content?: Node[];
81+
tag: string;
82+
attrs?: Attributes;
83+
content?: Node[];
8484
};
8585

8686
type Attributes = Record<string, string>;
8787
}
8888

8989
declare const parser: {
90-
defaultOptions: parser.DefaultOptions;
91-
defaultDirectives: parser.Directive[];
90+
defaultOptions: parser.DefaultOptions;
91+
defaultDirectives: parser.Directive[];
9292

93-
(content: string, options?: parser.Options): parser.Tree;
93+
(content: string, options?: parser.Options): parser.Tree;
9494
};
9595

9696
export = parser;

index.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function postHTMLParser(html, options) {
2323

2424
function isDirective({name}, tag) {
2525
if (name instanceof RegExp) {
26-
const regex = RegExp(name.source, 'i');
26+
const regex = new RegExp(name.source, 'i');
2727

2828
return regex.test(tag);
2929
}
@@ -39,8 +39,7 @@ function postHTMLParser(html, options) {
3939
const directives = [].concat(defaultDirectives, options.directives || []);
4040
const last = bufArray.last();
4141

42-
for (let i = 0; i < directives.length; i++) {
43-
const directive = directives[i];
42+
for (const directive of directives) {
4443
const directiveText = directive.start + data + directive.end;
4544

4645
name = name.toLowerCase();
@@ -50,7 +49,10 @@ function postHTMLParser(html, options) {
5049
return;
5150
}
5251

53-
last.content || (last.content = []);
52+
if (last.content === undefined) {
53+
last.content = [];
54+
}
55+
5456
last.content.push(directiveText);
5557
}
5658
}
@@ -59,9 +61,9 @@ function postHTMLParser(html, options) {
5961
function normalizeArributes(attrs) {
6062
const result = {};
6163
Object.keys(attrs).forEach(key => {
62-
const obj = {};
63-
obj[key] = attrs[key].replace(/&quot;/g, '"');
64-
Object.assign(result, obj);
64+
const object = {};
65+
object[key] = attrs[key].replace(/&quot;/g, '"');
66+
Object.assign(result, object);
6567
});
6668

6769
return result;
@@ -78,13 +80,16 @@ function postHTMLParser(html, options) {
7880
return;
7981
}
8082

81-
last.content || (last.content = []);
83+
if (last.content === undefined) {
84+
last.content = [];
85+
}
86+
8287
last.content.push(comment);
8388
},
8489
onopentag(tag, attrs) {
8590
const buf = {tag};
8691

87-
if (Object.keys(attrs).length) {
92+
if (Object.keys(attrs).length > 0) {
8893
buf.attrs = normalizeArributes(attrs);
8994
}
9095

@@ -93,7 +98,7 @@ function postHTMLParser(html, options) {
9398
onclosetag() {
9499
const buf = bufArray.pop();
95100

96-
if (!bufArray.length) {
101+
if (!bufArray.length > 0) {
97102
results.push(buf);
98103
return;
99104
}
@@ -113,13 +118,15 @@ function postHTMLParser(html, options) {
113118
return;
114119
}
115120

116-
if (last.content && last.content.length && typeof last.content[last.content.length - 1] === 'string') {
117-
last.content[last.content.length - 1] = `${last.content[last.content.length - 1]}${text}`
118-
return
121+
if (last.content && last.content.length > 0 && typeof last.content[last.content.length - 1] === 'string') {
122+
last.content[last.content.length - 1] = `${last.content[last.content.length - 1]}${text}`;
123+
return;
119124
}
120125

126+
if (last.content === undefined) {
127+
last.content = [];
128+
}
121129

122-
last.content || (last.content = []);
123130
last.content.push(text);
124131
}
125132
}, options || defaultOptions);
@@ -134,7 +141,7 @@ function parserWrapper(...args) {
134141
let option;
135142

136143
function parser(html) {
137-
const opt = Object.assign({}, defaultOptions, option);
144+
const opt = {...defaultOptions, ...option};
138145
return postHTMLParser(html, opt);
139146
}
140147

0 commit comments

Comments
 (0)