Skip to content

Commit a53a947

Browse files
committed
Merge pull request #141 from jrit/cheerio-node-12
special purpose encode/decode for EJS
2 parents 051449d + 32b9e0f commit a53a947

File tree

7 files changed

+39
-4
lines changed

7 files changed

+39
-4
lines changed

History.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
1.3.3 / 2015-07-14
2+
==================
3+
4+
* Prevent mangling of EJS tags
5+
16
1.3.0 / 2015-07-02
27
==================
38

lib/juice.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var juice = function (html,options) {
2828
return doc.xml();
2929
}
3030
else {
31-
return doc.html();
31+
return utils.decodeEntities(doc.html());
3232
}
3333
};
3434

@@ -307,7 +307,7 @@ function juiceResources(html, options, callback) {
307307
return callback(null, $.xml());
308308
}
309309
else {
310-
return callback(null, $.html());
310+
return callback(null, utils.decodeEntities($.html()));
311311
}
312312
};
313313

@@ -354,7 +354,7 @@ function inlineContent(html, css, options) {
354354
return $.xml();
355355
}
356356
else {
357-
return $.html();
357+
return utils.decodeEntities($.html());
358358
}
359359
}
360360

lib/utils.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ exports.getMediaQueryText = function ( css )
140140

141141
exports.cheerio = function (html, options) {
142142
options = exports.extend({decodeEntities: false}, options || {});
143+
html = exports.encodeEntities(html);
143144
return cheerio.load(html,options);
144145
};
145146

@@ -152,6 +153,26 @@ exports.normalizeLineEndings = function (text){
152153
return text.replace(/\r\n/g, '\n').replace(/\n/g, '\r\n');
153154
};
154155

156+
exports.encodeEJS = function (html){
157+
return html.replace(/<%((.|\s)*?)%>/g, function(match, subMatch){
158+
return "<!--EJS <%"+subMatch+"%> -->";
159+
});
160+
};
161+
162+
exports.decodeEJS = function (html){
163+
return html.replace(/<!--EJS <%((.|\s)*?)%> -->/g, function (match, subMatch){
164+
return "<%"+subMatch+"%>";
165+
});
166+
};
167+
168+
exports.encodeEntities = function(html){
169+
return exports.encodeEJS(html);
170+
};
171+
172+
exports.decodeEntities = function(html){
173+
return exports.decodeEJS(html);
174+
};
175+
155176
/**
156177
* Converts to array
157178
*

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "juice",
3-
"version": "1.3.2",
3+
"version": "1.3.3",
44
"description": "Inlines css into html source",
55
"bin": "./bin/juice",
66
"main": "./lib/juice",

test/cases/ejs.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
color: red;
3+
}

test/cases/ejs.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<body>
2+
<%= config.sitePrefix %>verify?grant=<%= grant._id %>&secret=<%= grant.secret %>
3+
</body>

test/cases/ejs.out

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<body style="color: red;">
2+
<%= config.sitePrefix %>verify?grant=<%= grant._id %>&secret=<%= grant.secret %>
3+
</body>

0 commit comments

Comments
 (0)