Skip to content

Commit 8078cd9

Browse files
committed
fix: don't remove wanted whitespace when removing tags (closes #177)
1 parent b4fd0d6 commit 8078cd9

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

src/inject/expected/issue177.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>gulp-inject</title>
5+
<link rel="import" href="/fixtures/component.html">
6+
<link rel="stylesheet" href="/fixtures/styles.css">
7+
<link rel="stylesheet" href="/fixtures/morestyles.css">
8+
<link rel="stylesheet" href="/fixtures/andevenmore.css">
9+
</head>
10+
<body>
11+
<img src="/fixtures/image.png">
12+
13+
<script src="/fixtures/lib.js"></script>
14+
15+
<script type="text/jsx" src="/fixtures/lib.jsx"></script>
16+
</body>
17+
</html>

src/inject/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ function getNewContent(target, collection, opt) {
130130
endTag: endTag,
131131
tagsToInject: tagsToInject,
132132
removeTags: opt.removeTags,
133+
empty: opt.empty,
133134
willInject: function (filesToInject) {
134135
injectedFilesCount += filesToInject.length;
135136
},
@@ -151,6 +152,7 @@ function getNewContent(target, collection, opt) {
151152
endTag: endTag,
152153
tagsToInject: [],
153154
removeTags: opt.removeTags,
155+
empty: opt.empty,
154156
shouldAbort: function (match) {
155157
return matches.indexOf(match[0]) !== -1;
156158
}
@@ -207,8 +209,10 @@ function inject(content, opt) {
207209
var newContents = content.slice(0, startMatch.index);
208210

209211
if (opt.removeTags) {
210-
// Take care of content length change:
211-
startTag.lastIndex -= startMatch[0].length;
212+
if (opt.empty) {
213+
// Take care of content length change:
214+
startTag.lastIndex -= startMatch[0].length;
215+
}
212216
} else {
213217
// <startMatch> + <endMatch>
214218
toInject.unshift(startMatch[0]);

src/inject/inject_test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,23 @@ describe('gulp-inject', function () {
471471
streamShouldContain(stream, ['removeTags.html'], done);
472472
});
473473

474+
it('should be able to remove tags without removing whitespace (issue #177)', function (done) {
475+
var target = src(['template.html'], {read: true});
476+
var sources = src([
477+
'lib.js',
478+
'component.html',
479+
'styles.css',
480+
'morestyles.css',
481+
'andevenmore.css',
482+
'image.png',
483+
'lib.jsx'
484+
]);
485+
486+
var stream = target.pipe(inject(sources, {removeTags: true}));
487+
488+
streamShouldContain(stream, ['issue177.html'], done);
489+
});
490+
474491
it('should not produce log output if quiet option is set', function (done) {
475492
var logOutput = [];
476493
gutil.log = function () {

0 commit comments

Comments
 (0)