Skip to content

Commit c1454ef

Browse files
committed
Add more thorough testing
1 parent cbed25e commit c1454ef

File tree

6 files changed

+88070
-7
lines changed

6 files changed

+88070
-7
lines changed

src/result.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,19 @@ function missingTag(tag) {
227227
);
228228
}
229229

230+
function addClass(html, regex, newClass) {
231+
return html.replace(regex, function(_, tag, attributes) {
232+
if (/class="([^"]*)"/i.test(attributes)) {
233+
attributes = attributes.replace(/class="([^"]*)"/i, function(_, klass) {
234+
return `class="${klass} ${newClass}"`;
235+
});
236+
} else {
237+
attributes += ' class="' + newClass + '"';
238+
}
239+
return `<${tag}${attributes}>`;
240+
});
241+
}
242+
230243
async function insertIntoIndexHTML(
231244
html,
232245
htmlAttributes,
@@ -254,9 +267,7 @@ async function insertIntoIndexHTML(
254267
});
255268

256269
if (htmlClass) {
257-
html = html.replace(/(<html.*)class="([^"]*)"([^>]*)/i, function(_, prefix, klass, suffix) {
258-
return prefix + `class="${klass + ' ' + htmlClass.value}"` + suffix;
259-
});
270+
html = addClass(html, /<(html)(.*)>/i, htmlClass.value);
260271
}
261272
if (htmlAttributes) {
262273
html = html.replace(/<html[^>]*/i, function(match) {
@@ -265,9 +276,7 @@ async function insertIntoIndexHTML(
265276
}
266277

267278
if (bodyClass) {
268-
html = html.replace(/(<body.*)class="([^"]*)"([^>]*)/i, function(_, prefix, klass, suffix) {
269-
return prefix + `class="${klass + ' ' + bodyClass.value}"` + suffix;
270-
});
279+
html = addClass(html, /<(body)(.*)>/i, bodyClass.value);
271280
}
272281
if (bodyAttributes) {
273282
html = html.replace(/<body[^>]*/i, function(match) {

test/fastboot-test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,22 @@ describe('FastBoot', function() {
138138
.visit('/')
139139
.then(r => r.html())
140140
.then(html => {
141-
expect(html).to.match(/<body data-before=1 +class="no-js default-class it-works" data-after=2/);
141+
expect(html).to.match(
142+
/<body data-before=1 +class="no-js default-class it-works" data-after=2/
143+
);
144+
});
145+
});
146+
147+
it('appends classes correctly even when there was no classes in the original html', function() {
148+
var fastboot = new FastBoot({
149+
distPath: fixture('custom-body-attrs-with-no-default-classes'),
150+
});
151+
152+
return fastboot
153+
.visit('/')
154+
.then(r => r.html())
155+
.then(html => {
156+
expect(html).to.match(/<body data-before=1 data-after=2 +class="it-works"/);
142157
});
143158
});
144159

0 commit comments

Comments
 (0)