Skip to content

Commit cdd7dbd

Browse files
committed
style: 2 space, close issue #33
1 parent 8e64082 commit cdd7dbd

File tree

3 files changed

+340
-339
lines changed

3 files changed

+340
-339
lines changed

.editorconfig

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
# This file is for unifying the coding style for different editors and IDEs.
2-
# More information at http://EditorConfig.org
3-
4-
# No .editorconfig files above the root directory
51
root = true
62

73
[*]
8-
charset = utf-8
4+
indent_style = space
95
indent_size = 4
106
end_of_line = lf
11-
indent_style = space
7+
charset = utf-8
8+
quote_type = single
129
trim_trailing_whitespace = true
1310
insert_final_newline = true
1411

15-
[*.{bemjson.js,deps.js}]
16-
indent_size = 4
17-
18-
[{bower,package}.json]
12+
[{package.json,*.yml,*.jade,*.pss,*.css,*.js,*.md,.*,*.ts}]
1913
indent_size = 2
2014

15+
[{changelog.md,.*}]
16+
insert_final_newline = false
17+
2118
[*.md]
2219
trim_trailing_whitespace = false

index.js

+119-119
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { Parser } = require('htmlparser2');
1+
const {Parser} = require('htmlparser2');
22

33
/**
44
* @see https://github.com/fb55/htmlparser2/wiki/Parser-options
@@ -14,141 +14,141 @@ const defaultDirectives = [{name: '!doctype', start: '<', end: '>'}];
1414
* @return {PostHTMLTree}
1515
*/
1616
function postHTMLParser(html, options) {
17-
const bufArray = [];
18-
const results = [];
17+
const bufArray = [];
18+
const results = [];
1919

20-
bufArray.last = function() {
21-
return this[this.length - 1];
22-
};
20+
bufArray.last = function () {
21+
return this[this.length - 1];
22+
};
2323

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

28-
return regex.test(tag);
29-
}
30-
31-
if (tag !== name) {
32-
return false;
33-
}
28+
return regex.test(tag);
29+
}
3430

35-
return true;
31+
if (tag !== name) {
32+
return false;
3633
}
3734

38-
function parserDirective(name, data) {
39-
const directives = [].concat(defaultDirectives, options.directives || []);
40-
const last = bufArray.last();
35+
return true;
36+
}
4137

42-
for (let i = 0; i < directives.length; i++) {
43-
const directive = directives[i];
44-
const directiveText = directive.start + data + directive.end;
38+
function parserDirective(name, data) {
39+
const directives = [].concat(defaultDirectives, options.directives || []);
40+
const last = bufArray.last();
4541

46-
name = name.toLowerCase();
47-
if (isDirective(directive, name)) {
48-
if (!last) {
49-
results.push(directiveText);
50-
return;
51-
}
42+
for (let i = 0; i < directives.length; i++) {
43+
const directive = directives[i];
44+
const directiveText = directive.start + data + directive.end;
5245

53-
last.content || (last.content = []);
54-
last.content.push(directiveText);
55-
}
46+
name = name.toLowerCase();
47+
if (isDirective(directive, name)) {
48+
if (!last) {
49+
results.push(directiveText);
50+
return;
5651
}
57-
}
58-
59-
function normalizeArributes(attrs) {
60-
const result = {};
61-
Object.keys(attrs).forEach(key => {
62-
const obj = {};
63-
obj[key] = attrs[key].replace(/&quot;/g, '"');
64-
Object.assign(result, obj);
65-
});
6652

67-
return result;
53+
last.content || (last.content = []);
54+
last.content.push(directiveText);
55+
}
6856
}
57+
}
58+
59+
function normalizeArributes(attrs) {
60+
const result = {};
61+
Object.keys(attrs).forEach(key => {
62+
const obj = {};
63+
obj[key] = attrs[key].replace(/&quot;/g, '"');
64+
Object.assign(result, obj);
65+
});
66+
67+
return result;
68+
}
69+
70+
const parser = new Parser({
71+
onprocessinginstruction: parserDirective,
72+
oncomment(data) {
73+
const comment = `<!--${data}-->`;
74+
const last = bufArray.last();
75+
76+
if (!last) {
77+
results.push(comment);
78+
return;
79+
}
80+
81+
last.content || (last.content = []);
82+
last.content.push(comment);
83+
},
84+
onopentag(tag, attrs) {
85+
const buf = {tag};
86+
87+
if (Object.keys(attrs).length) {
88+
buf.attrs = normalizeArributes(attrs);
89+
}
90+
91+
bufArray.push(buf);
92+
},
93+
onclosetag() {
94+
const buf = bufArray.pop();
95+
96+
if (!bufArray.length) {
97+
results.push(buf);
98+
return;
99+
}
100+
101+
const last = bufArray.last();
102+
if (!Array.isArray(last.content)) {
103+
last.content = [];
104+
}
105+
106+
last.content.push(buf);
107+
},
108+
ontext(text) {
109+
const last = bufArray.last();
110+
111+
if (!last) {
112+
results.push(text);
113+
return;
114+
}
115+
116+
if (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
119+
}
120+
121+
122+
last.content || (last.content = []);
123+
last.content.push(text);
124+
}
125+
}, options || defaultOptions);
69126

70-
const parser = new Parser({
71-
onprocessinginstruction: parserDirective,
72-
oncomment(data) {
73-
const comment = `<!--${data}-->`;
74-
const last = bufArray.last();
75-
76-
if (!last) {
77-
results.push(comment);
78-
return;
79-
}
80-
81-
last.content || (last.content = []);
82-
last.content.push(comment);
83-
},
84-
onopentag(tag, attrs) {
85-
const buf = { tag };
86-
87-
if (Object.keys(attrs).length) {
88-
buf.attrs = normalizeArributes(attrs);
89-
}
90-
91-
bufArray.push(buf);
92-
},
93-
onclosetag() {
94-
const buf = bufArray.pop();
95-
96-
if (!bufArray.length) {
97-
results.push(buf);
98-
return;
99-
}
100-
101-
const last = bufArray.last();
102-
if (!Array.isArray(last.content)) {
103-
last.content = [];
104-
}
105-
106-
last.content.push(buf);
107-
},
108-
ontext(text) {
109-
const last = bufArray.last();
110-
111-
if (!last) {
112-
results.push(text);
113-
return;
114-
}
115-
116-
if (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
119-
}
120-
121-
122-
last.content || (last.content = []);
123-
last.content.push(text);
124-
}
125-
}, options || defaultOptions);
126-
127-
parser.write(html);
128-
parser.end();
127+
parser.write(html);
128+
parser.end();
129129

130-
return results;
130+
return results;
131131
}
132132

133133
function parserWrapper(...args) {
134-
let option;
135-
136-
function parser(html) {
137-
const opt = Object.assign({}, defaultOptions, option);
138-
return postHTMLParser(html, opt);
139-
}
140-
141-
if (
142-
args.length === 1 &&
143-
Boolean(args[0]) &&
144-
args[0].constructor.name === 'Object'
145-
) {
146-
option = args[0];
147-
return parser;
148-
}
149-
150-
option = args[1];
151-
return parser(args[0]);
134+
let option;
135+
136+
function parser(html) {
137+
const opt = Object.assign({}, defaultOptions, option);
138+
return postHTMLParser(html, opt);
139+
}
140+
141+
if (
142+
args.length === 1 &&
143+
Boolean(args[0]) &&
144+
args[0].constructor.name === 'Object'
145+
) {
146+
option = args[0];
147+
return parser;
148+
}
149+
150+
option = args[1];
151+
return parser(args[0]);
152152
}
153153

154154
module.exports = parserWrapper;

0 commit comments

Comments
 (0)