-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathmain.js
301 lines (255 loc) · 11.9 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* eslint-disable */
/* global describe, it */
const should = require('should');
const fs = require('fs');
const path = require('path');
const Vinyl = require('vinyl');
const beautify = require('js-beautify').html;
const inlineCss = require('../index');
function getFile(filePath) {
return new Vinyl({
path: path.resolve(filePath),
cwd: './test/',
base: path.dirname(filePath),
contents: Buffer.from(String(fs.readFileSync(filePath)))
});
}
function compare(fixturePath, expectedPath, options, done) {
const file = getFile(fixturePath);
options.url = `file://${file.path}`;
inlineCss(file.contents.toString('utf8'), options)
.then(html => {
const expected = beautify(String(fs.readFileSync(expectedPath)), {
"preserve-newlines": false
});
beautify(html, {
"preserve-newlines": false
}).should.be.equal(expected);
})
.then(() => {
done()
})
.catch(err => {
done(err)
});
}
describe('inline-css', () => {
it('Should convert linked css to inline css', done => {
const options = {};
compare(path.join('test', 'fixtures', 'in.html'), path.join('test', 'expected', 'out.html'), options, done);
});
it('Should inline css in multiple HTML files', done => {
const options = {};
compare(path.join('test', 'fixtures', 'multiple', 'one', 'in.html'), path.join('test', 'expected', 'multiple', 'one', 'out.html'), options, () => {});
compare(path.join('test', 'fixtures', 'multiple', 'two', 'in.html'), path.join('test', 'expected', 'multiple', 'two', 'out.html'), options, done);
});
it('Should inline css in edge case (alpha)', done => {
const options = {};
compare(path.join('test', 'fixtures', 'alpha.html'), path.join('test', 'expected', 'alpha.html'), options, done);
});
it('Should inline css in edge case (cascading)', done => {
const options = {};
compare(path.join('test', 'fixtures', 'cascading.html'), path.join('test', 'expected', 'cascading.html'), options, done);
});
it('Should inline css in edge case (class)', done => {
const options = {};
compare(path.join('test', 'fixtures', 'class.html'), path.join('test', 'expected', 'class.html'), options, done);
});
it('Should inline css in edge case (class+id)', done => {
const options = {};
compare(path.join('test', 'fixtures', 'class+id.html'), path.join('test', 'expected', 'class+id.html'), options, done);
});
it('Should inline css in edge case (css-quotes)', done => {
const options = {};
compare(path.join('test', 'fixtures', 'css-quotes.html'), path.join('test', 'expected', 'css-quotes.html'), options, done);
});
it('Should inline css in edge case (direct-descendents)', done => {
const options = {};
compare(path.join('test', 'fixtures', 'direct-descendents.html'), path.join('test', 'expected', 'direct-descendents.html'), options, done);
});
it('Should inline css in edge case (empty)', done => {
const options = {};
compare(path.join('test', 'fixtures', 'empty.html'), path.join('test', 'expected', 'empty.html'), options, done);
});
it('Should inline css in edge case (id)', done => {
const options = {};
compare(path.join('test', 'fixtures', 'id.html'), path.join('test', 'expected', 'id.html'), options, done);
});
it('Should inline last rule if identical rules use important', done => {
const options = {};
compare(path.join('test', 'fixtures', 'identical-important.html'), path.join('test', 'expected', 'identical-important.html'), options, done);
});
it('Should ignore pseudo selectors', done => {
const options = {};
compare(path.join('test', 'fixtures', 'ignore-pseudos.html'), path.join('test', 'expected', 'ignore-pseudos.html'), options, done);
});
it('Should inline css in edge case (important)', done => {
const options = {};
compare(path.join('test', 'fixtures', 'important.html'), path.join('test', 'expected', 'important.html'), options, done);
});
it('Should inline css in edge case (media)', done => {
const options = {};
compare(path.join('test', 'fixtures', 'media.html'), path.join('test', 'expected', 'media.html'), options, done);
});
it('Should inline css in edge case (normalize)', done => {
const options = {};
compare(path.join('test', 'fixtures', 'normalize.html'), path.join('test', 'expected', 'normalize.html'), options, done);
});
it('Should inline css in edge case (preserve-events)', done => {
const options = {};
compare(path.join('test', 'fixtures', 'preserve-events.html'), path.join('test', 'expected', 'preserve-events.html'), options, done);
});
it('Should inline css in edge case (media)', done => {
const options = {};
compare(path.join('test', 'fixtures', 'regression-media.html'), path.join('test', 'expected', 'regression-media.html'), options, done);
});
it('Should inline css in edge case (selector-newline)', done => {
const options = {};
compare(path.join('test', 'fixtures', 'regression-selector-newline.html'), path.join('test', 'expected', 'regression-selector-newline.html'), options, done);
});
it('Should compare properties and inline the most specific', done => {
const options = {};
compare(path.join('test', 'fixtures', 'specificity.html'), path.join('test', 'expected', 'specificity.html'), options, done);
});
it('Should preserve existing inline styles ', done => {
const options = {};
compare(path.join('test', 'fixtures', 'style-preservation.html'), path.join('test', 'expected', 'style-preservation.html'), options, done);
});
it('Should inline css in edge case (tag)', done => {
const options = {};
compare(path.join('test', 'fixtures', 'tag.html'), path.join('test', 'expected', 'tag.html'), options, done);
});
it('Should inline css in edge case (yui-reset)', done => {
const options = {};
compare(path.join('test', 'fixtures', 'yui-reset.html'), path.join('test', 'expected', 'yui-reset.html'), options, done);
});
it('Should inline css with doctype', done => {
const options = {};
compare(path.join('test', 'fixtures', 'doctype.html'), path.join('test', 'expected', 'doctype.html'), options, done);
});
it('Should inline css with no css', done => {
const options = {};
compare(path.join('test', 'fixtures', 'no_css.html'), path.join('test', 'expected', 'no_css.html'), options, done);
});
it('Should inline css with remote url', function(done) {
this.timeout(10000);
const options = {};
compare(path.join('test', 'fixtures', 'remote_url.html'), path.join('test', 'expected', 'remote_url.html'), options, done);
});
it('Should inline css in with spaces in path', done => {
const options = {};
compare(path.join('test', 'fixtures', 'spaces_in_path.html'), path.join('test', 'expected', 'spaces_in_path.html'), options, done);
});
it('Should inline css with two styles', done => {
const options = {};
compare(path.join('test', 'fixtures', 'two_styles.html'), path.join('test', 'expected', 'two_styles.html'), options, done);
});
it('Should inline css with font quotes', done => {
const options = {
url: './',
removeStyleTags: true
};
compare(path.join('test', 'fixtures', 'font-quotes.html'), path.join('test', 'expected', 'font-quotes.html'), options, done);
});
it('Should inline css with two styles', done => {
const options = {};
compare(path.join('test', 'fixtures', 'two_styles.html'), path.join('test', 'expected', 'two_styles.html'), options, done);
});
it('Should inline css and preserve media queries', done => {
const options = {
url: './',
removeStyleTags: true,
preserveMediaQueries: true
};
compare(
path.join('test', 'fixtures', 'media-preserve.html'),
path.join('test', 'expected', 'media-preserve.html'),
options,
done
);
});
it('Should inline css and create width attributes on elements', done => {
const options = {
url: './',
removeStyleTags: true,
applyWidthAttributes: true
};
compare(path.join('test', 'fixtures', 'width-attr.html'), path.join('test', 'expected', 'width-attr.html'), options, done);
});
it('Should inline css and create table attributes on table elements', done => {
const options = {
url: './',
removeStyleTags: true,
applyTableAttributes: true
};
compare(path.join('test', 'fixtures', 'table-attr.html'), path.join('test', 'expected', 'table-attr.html'), options, done);
});
it('Should inline css in HTML templates', done => {
const options = {
url: './'
};
compare(path.join('test', 'fixtures', 'template.ejs'), path.join('test', 'fixtures', 'template.ejs'), options, done);
});
it('Should inline css in edge case and remove html selectors', done => {
const options = {
removeHtmlSelectors: true
};
compare(path.join('test', 'fixtures', 'remove-html-selectors.html'), path.join('test', 'expected', 'remove-html-selectors.html'), options, done);
});
it('Should error when passed malformed CSS', done => {
const file = getFile(path.join('test', 'fixtures', 'malformed.html'));
const options = {
url: `file://${file.path}`
};
inlineCss(file.contents.toString('utf8'), options)
.then(html => {
done(new Error('test should error when passed malformed CSS'));
})
.catch(({message}) => {
message.should.be.equal('Error: Unexpected } (line 3, char 1)');
done();
});
});
it('Should handle html character entities correctly', done => {
const options = {};
compare(path.join('test', 'fixtures', 'character-entities.html'), path.join('test', 'expected', 'character-entities.html'), options, done);
});
it('Should error when options.url is not set', done => {
const options = {};
const file = getFile(path.join('test', 'fixtures', 'template.ejs'));
inlineCss(file.contents.toString('utf8'), options)
.then(html => {
done(new Error('test should error when options.url is not set'));
})
.catch(err => {
done();
});
});
it('Should handle xhtml documents correctly', done => {
const options = {
xmlMode: true
};
compare(path.join('test', 'fixtures', 'xhtml.html'), path.join('test', 'expected', 'xhtml.html'), options, done);
});
it('Should ignore hbs code blocks', done => {
const options = {
xmlMode: true
};
compare(path.join('test', 'fixtures', 'codeblocks.html'), path.join('test', 'expected', 'codeblocks.html'), options, done);
});
it('Should ignore ejs code blocks', done => {
const options = {
xmlMode: false
};
compare(path.join('test', 'fixtures', 'ejs.html'), path.join('test', 'expected', 'ejs.html'), options, done);
});
it('Should ignore user defined code blocks', done => {
const options = {
xmlMode: true,
codeBlocks: {
craze: { start: '<<', end: '>>' }
}
};
compare(path.join('test', 'fixtures', 'codeblocks-external.html'), path.join('test', 'expected', 'codeblocks-external.html'), options, done);
});
});