Skip to content

Commit

Permalink
Fix #2 and modify test
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximilian Brandau committed Feb 25, 2018
1 parent 0aa1583 commit c894277
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,17 @@ CssShortener.prototype.cssStream = CssShortener.prototype.stream = function(call
}
CssShortener.prototype.htmlStream = function(callback) {
const t = this;
return replaceStream(HTML_CLASS_REGEX, function(match) {
const classes = match.trim().split(' ');
return replaceStream(HTML_CLASS_REGEX, function(match, capturingGroup) {
if (!capturingGroup) return match;
const classes = capturingGroup.trim().split(' ');
const classCount = classes.length;
var result = '';
for (var i = 0; i < classCount; i++) {
// Check if class is mapped and add it to the result
result += (t._classNameMap[classes[i]] != null ? t._classNameMap[classes[i]] : classes[i]);
if (i < classCount - 1) result += ' ';
}
return result;
return `class="${result}"`;
});
}

Expand Down
11 changes: 10 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,19 @@ describe('CssShortener', function() {
'test-class': 'a'
};
c.importMap(map);

const stream = str('<div class="abc test-class 15a"></div>').pipe(c.htmlStream());
toString(stream).then(function(msg) {
msg.should.equal('<div class="abc a 15a"></div>');
done();
const stream = str('<div class="test-class"></div>').pipe(c.htmlStream());
toString(stream).then(function(msg) {
msg.should.equal('<div class="a"></div>');
const stream = str('<div class="myclass"></div>').pipe(c.htmlStream());
toString(stream).then(function(msg) {
msg.should.equal('<div class="myclass"></div>');
done();
});
});
});
});
});
Expand Down

0 comments on commit c894277

Please sign in to comment.