Skip to content

Commit

Permalink
Added support for removing HTML selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
dvprz committed Feb 8, 2015
1 parent 179f4b9 commit 3faf40f
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ module.exports = function (html, options, callback) {
removeLinkTags: true,
preserveMediaQueries: false,
applyWidthAttributes: false,
removeHtmlSelectors: false,
}, options);

inlineContent(html, opt, function (err, content) {
Expand Down
16 changes: 16 additions & 0 deletions lib/inline-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,28 @@ module.exports = function (html, css, options) {
}
}

function removeHtmlSelectors(el) {
var selectors = ['class', 'id'];

selectors.forEach(function(selector) {
var attribute = $(el).attr(selector);

if (typeof attribute !== 'undefined') {
$(el).removeAttr(selector);
}
});
}

rules.forEach(handleRule);
editedElements.forEach(setStyleAttrs);

if (options && options.applyWidthAttributes) {
editedElements.forEach(setWidthAttrs);
}

if (options && options.removeHtmlSelectors) {
editedElements.forEach(removeHtmlSelectors);
}

return $.html();
};
4 changes: 4 additions & 0 deletions test/expected/remove-html-selectors.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<html><body>
<div style="color: red; display: inline-block; font-size: 12px; width: 100%;">Test</div>
<div style="color: red; display: inline-block; font-size: 12px; width: 100%;">Test</div>
</body></html>
9 changes: 9 additions & 0 deletions test/fixtures/remove-html-selectors.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.myClass {
color: red;
font-size: 12px;
}

#myId {
display: inline-block;
width: 100%;
}
4 changes: 4 additions & 0 deletions test/fixtures/remove-html-selectors.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<html><link rel="stylesheet" href="remove-html-selectors.css"/><body>
<div id="myId" class="myClass">Test</div>
<div id="myId" class="myClass">Test</div>
</body></html>
8 changes: 8 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,12 @@ describe('inline-css', function() {
};
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', function(done) {
var options = {
removeHtmlSelectors: true
};
compare(path.join('test', 'fixtures', 'remove-html-selectors.html'), path.join('test', 'expected', 'remove-html-selectors.html'), options, done);
});

});

0 comments on commit 3faf40f

Please sign in to comment.