Skip to content

Commit 280c311

Browse files
committed
Add juice.codeBlocks
+ v4 release + updates deps
1 parent a9b653e commit 280c311

File tree

10 files changed

+83
-32
lines changed

10 files changed

+83
-32
lines changed

History.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
4.0.0 / 2016-11-17
2+
==================
3+
4+
* deps: update web-resource-inliner and cross-spawn
5+
* engine: specify support as node 4.2+
6+
* feat: add option `codeBlocks` to support general exclusion of fenced code, like EJS, HBS, etc.
7+
18
3.0.1 / 2016-10-06
29
==================
310

README.md

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,32 @@ Juice is exposed as a standard module, and from CLI with a smaller set of option
4848

4949
All juice methods take an options object that can contain any of these properties, though not every method uses all of these:
5050

51-
* `extraCss` - extra css to apply to the file. Defaults to `""`.
52-
* `applyStyleTags` - whether to inline styles in `<style></style>` Defaults to `true`.
53-
* `removeStyleTags` - whether to remove the original `<style></style>` tags after (possibly) inlining the css from them. Defaults to `true`.
54-
* `preserveMediaQueries` - preserves all media queries (and contained styles) within `<style></style>` tags as a refinement when `removeStyleTags` is `true`. Other styles are removed. Defaults to `true`.
55-
* `preserveFontFaces` - preserves all `@font-face` within `<style></style>` tags as a refinement when `removeStyleTags` is `true`. Other styles are removed. Defaults to `true`.
56-
* `insertPreservedExtraCss` - whether to insert into the document any preserved `@media` or `@font-face` content from `extraCss` when using `preserveMediaQueries` or `preserveFontFaces`. When `true` order of preference to append the `<style>` element is into `head`, then `body`, then at the end of the document. When a `string` the value is treated as a CSS/jQuery/cheerio selector, and when found, the `<style>` tag will be appended to the end of the first match. Defaults to `true`.
57-
* `applyWidthAttributes` - whether to use any CSS pixel widths to create `width` attributes on elements set in `juice.widthElements`. Defaults to `true`.
58-
* `applyHeightAttributes` - whether to use any CSS pixel heights to create `height` attributes on elements set in `juice.heightElements`. Defaults to `true`.
59-
* `applyAttributesTableElements` - whether to create attributes for styles in `juice.styleToAttribute` on elements set in `juice.tableElements`. Defaults to `true`.
60-
* `webResources` - An options object that will be passed to [web-resource-inliner](https://www.npmjs.com/package/web-resource-inliner) for juice functions that will get remote resources (`juiceResources` and `juiceFile`). Defaults to `{}`.
61-
* `inlinePseudoElements` - Whether to insert pseudo elements (`::before` and `::after`) as `<span>` into the DOM. *Note*: Inserting pseudo elements will modify the DOM and may conflict with CSS selectors elsewhere on the page (e.g., `:last-child`).
62-
* `xmlMode` - whether to output XML/XHTML with all tags closed. Note that the input *must* also be valid XML/XHTML or you will get undesirable results. Defaults to `false`.
63-
* `preserveImportant` - preserves `!important` in values. Defaults to `false`.
51+
* `applyAttributesTableElements` - whether to create attributes for styles in `juice.styleToAttribute` on elements set in `juice.tableElements`. Defaults to `true`.
52+
53+
* `applyHeightAttributes` - whether to use any CSS pixel heights to create `height` attributes on elements set in `juice.heightElements`. Defaults to `true`.
54+
55+
* `applyStyleTags` - whether to inline styles in `<style></style>` Defaults to `true`.
56+
57+
* `applyWidthAttributes` - whether to use any CSS pixel widths to create `width` attributes on elements set in `juice.widthElements`. Defaults to `true`.
58+
59+
* `extraCss` - extra css to apply to the file. Defaults to `""`.
60+
61+
* `insertPreservedExtraCss` - whether to insert into the document any preserved `@media` or `@font-face` content from `extraCss` when using `preserveMediaQueries` or `preserveFontFaces`. When `true` order of preference to append the `<style>` element is into `head`, then `body`, then at the end of the document. When a `string` the value is treated as a CSS/jQuery/cheerio selector, and when found, the `<style>` tag will be appended to the end of the first match. Defaults to `true`.
62+
63+
* `inlinePseudoElements` - Whether to insert pseudo elements (`::before` and `::after`) as `<span>` into the DOM. *Note*: Inserting pseudo elements will modify the DOM and may conflict with CSS selectors elsewhere on the page (e.g., `:last-child`).
64+
65+
* `preserveFontFaces` - preserves all `@font-face` within `<style></style>` tags as a refinement when `removeStyleTags` is `true`. Other styles are removed. Defaults to `true`.
66+
67+
* `preserveImportant` - preserves `!important` in values. Defaults to `false`.
68+
69+
* `preserveMediaQueries` - preserves all media queries (and contained styles) within `<style></style>` tags as a refinement when `removeStyleTags` is `true`. Other styles are removed. Defaults to `true`.
70+
71+
* `removeStyleTags` - whether to remove the original `<style></style>` tags after (possibly) inlining the css from them. Defaults to `true`.
72+
73+
* `webResources` - An options object that will be passed to [web-resource-inliner](https://www.npmjs.com/package/web-resource-inliner) for juice functions that will get remote resources (`juiceResources` and `juiceFile`). Defaults to `{}`.
74+
75+
* `xmlMode` - whether to output XML/XHTML with all tags closed. Note that the input *must* also be valid XML/XHTML or you will get undesirable results. Defaults to `false`.
76+
6477

6578
### Methods
6679

@@ -118,6 +131,10 @@ Given a cheerio instance and css, this modifies the cheerio instance so that the
118131

119132
### Global settings
120133

134+
#### juice.codeBlocks
135+
136+
An object where each value has a `start` and `end` to specify fenced code blocks that should be ignored during parsing and inlining. For example, Handlebars (hbs) templates are `{start: '{{', end: '}}'}`. `codeBlocks` can fix problems where otherwise juice might interpret code like `<=` as HTML, when it is meant to be template language code.
137+
121138
#### juice.ignoredPseudos
122139

123140
Array of ignored pseudo-selectors such as 'hover' and 'active'.

client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var juiceDocument = function(html, options) {
2020
return juiceClient.juiceDocument(html, options);
2121
}
2222

23-
juiceClient.inlineContent = function (html, css, options) {
23+
juiceClient.inlineContent = function(html, css, options) {
2424
return cheerio(html, { xmlMode: options && options.xmlMode}, juiceClient.inlineDocument, [css, options]);
2525
};
2626

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ juice.juiceFile = juiceFile;
3535
juice.juiceResources = juiceResources;
3636
juice.inlineExternal = inlineExternal;
3737

38+
juice.codeBlocks = cheerio.codeBlocks;
39+
3840
function juiceFile(filePath, options, callback) {
3941
// set default options
4042
fs.readFile(filePath, 'utf8', function(err, content) {

lib/cheerio.js

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,34 @@ var cheerioLoad = function(html, options) {
1212
return cheerio.load(html,options);
1313
};
1414

15-
var encodeEJS = function(html) {
16-
return html.replace(/<%((.|\s)*?)%>/g, function(match, subMatch) {
17-
return '<!--EJS <%' + subMatch + '%> -->';
15+
var encodeCodeBlocks = function(html) {
16+
var blocks = module.exports.codeBlocks;
17+
Object.keys(blocks).forEach((key) => {
18+
var re = new RegExp(blocks[key].start + '((.|\\s)*?)' + blocks[key].end, 'g');
19+
html = html.replace(re, function(match, subMatch) {
20+
return '<!--' + key + ' ' + blocks[key].start + subMatch + blocks[key].end + ' -->';
21+
});
1822
});
23+
return html;
1924
};
2025

21-
var decodeEJS = function(html) {
22-
return html.replace(/<!--EJS <%((.|\s)*?)%> -->/g, function(match, subMatch) {
23-
return '<%' + subMatch + '%>';
26+
var decodeCodeBlocks = function(html) {
27+
var blocks = module.exports.codeBlocks;
28+
Object.keys(blocks).forEach((key) => {
29+
var re = new RegExp('<!--' + key + ' ' + blocks[key].start + '((.|\\s)*?)' + blocks[key].end + ' -->', 'g');
30+
html = html.replace(re, function(match, subMatch) {
31+
return blocks[key].start + subMatch + blocks[key].end;
32+
});
2433
});
34+
return html;
2535
};
2636

2737
var encodeEntities = function(html) {
28-
return encodeEJS(html);
38+
return encodeCodeBlocks(html);
2939
};
3040

3141
var decodeEntities = function(html) {
32-
return decodeEJS(html);
42+
return decodeCodeBlocks(html);
3343
};
3444

3545
/**
@@ -52,3 +62,8 @@ module.exports = function(html, options, callback, callbackExtraArguments) {
5262
}
5363
return decodeEntities(doc.html());
5464
};
65+
66+
module.exports.codeBlocks = {
67+
EJS: { start: '<%', end: '%>' },
68+
HBS: { start: '{{', end: '}}' }
69+
};

lib/inline.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function inlineDocument($, css, options) {
2626
var rules = utils.parseCSS(css);
2727
var editedElements = [];
2828
var styleAttributeName = 'style';
29-
29+
3030
if (options.styleAttributeName) {
3131
styleAttributeName = options.styleAttributeName;
3232
}
@@ -149,7 +149,7 @@ function inlineDocument($, css, options) {
149149
var value = style[i].value;
150150
var important = style[i].value.match(/!important$/) !== null;
151151
if (important && !options.preserveImportant) value = value.replace(/\s*!important$/, '');
152-
// adds line number and column number for the properties as "additionalPriority" to the
152+
// adds line number and column number for the properties as "additionalPriority" to the
153153
// properties because in CSS the position directly affect the priority.
154154
var additionalPriority = [style[i].position.start.line, style[i].position.start.col];
155155
var prop = new utils.Property(name, value, selector, important ? 2 : 0, additionalPriority);
@@ -356,4 +356,4 @@ function extractCssFromDocument($, options) {
356356

357357
return juiceClient;
358358

359-
};
359+
};

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "juice",
3-
"version": "3.0.1",
3+
"version": "4.0.0",
44
"description": "Inlines css into html source",
55
"bin": "bin/juice",
66
"main": "index.js",
@@ -34,22 +34,22 @@
3434
}
3535
],
3636
"engines": {
37-
"node": ">=4.0.0"
37+
"node": ">=4.2.0"
3838
},
3939
"types": "juice.d.ts",
4040
"dependencies": {
4141
"cheerio": "^0.22.0",
4242
"commander": "2.9.0",
43-
"cross-spawn": "^4.0.0",
44-
"mensch": "^0.3.3",
43+
"cross-spawn": "^5.0.1",
4544
"deep-extend": "^0.4.0",
45+
"mensch": "^0.3.3",
4646
"slick": "1.12.2",
47-
"web-resource-inliner": "3.0.0"
47+
"web-resource-inliner": "^4.0.0"
4848
},
4949
"devDependencies": {
5050
"batch": "0.5.3",
51-
"mocha": "1.21.4",
52-
"should": "4.0.4",
51+
"mocha": "^3.1.2",
52+
"should": "^11.1.1",
5353
"typescript": "^2.0.3"
5454
},
5555
"repository": {

test/cases/hbs.css

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

test/cases/hbs.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<body>
2+
{{#if `age <= 40`}}<p>Young</p>{{/if}}
3+
</body>

test/cases/hbs.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+
{{#if `age <= 40`}}<p style="color: red;">Young</p>{{/if}}
3+
</body>

0 commit comments

Comments
 (0)