Skip to content

Commit cd94f3a

Browse files
committed
Merge pull request #168 from dantman/inline-height
Implement inlining of height attributes.
2 parents 32806f1 + eac714a commit cd94f3a

File tree

6 files changed

+74
-7
lines changed

6 files changed

+74
-7
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ All juice methods take an options object that can contain any of these propertie
5252
* `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 `false`.
5353
* `preserveFontFaces` - preserves all `@font-face` within `<style></style>` tags as a refinement when `removeStyleTags` is `true`. Other styles are removed. Defaults to `false`.
5454
* `applyWidthAttributes` - whether to use any CSS pixel widths to create `width` attributes on elements set in `juice.widthElements`. Defaults to `false`.
55+
* `applyHeightAttributes` - whether to use any CSS pixel heights to create `height` attributes on elements set in `juice.heightElements`. Defaults to `false`.
5556
* `applyAttributesTableElements` - whether to create attributes for styles in `juice.styleToAttribute` on elements set in `juice.tableElements`. Defaults to `false`.
5657
* `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 `{}`.
5758
* `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`).
@@ -121,6 +122,10 @@ Array of ignored pseudo-selectors such as 'hover' and 'active'.
121122

122123
Array of HTML elements that can receive `width` attributes.
123124

125+
#### juice.heightElements
126+
127+
Array of HTML elements that can receive `height` attributes.
128+
124129
#### juice.styleToAttribute
125130

126131
Object of style property names (key) to their respective attribute names (value).

lib/juice.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ juice.utils = require('./utils');
6161

6262
juice.ignoredPseudos = ['hover', 'active', 'focus', 'visited', 'link'];
6363
juice.widthElements = ['TABLE', 'TD', 'IMG'];
64+
juice.heightElements = ['TABLE', 'TD', 'IMG'];
6465
juice.tableElements = ['TABLE', 'TD', 'TH', 'TR', 'TD', 'CAPTION', 'COLGROUP', 'COL', 'THEAD', 'TBODY', 'TFOOT'];
6566
juice.nonVisualElements = [ "HEAD", "TITLE", "BASE", "LINK", "STYLE", "META", "SCRIPT", "NOSCRIPT" ];
6667
juice.styleToAttribute = {
@@ -89,7 +90,15 @@ function inlineDocument($, css, options) {
8990
}
9091

9192
if (options && options.applyWidthAttributes) {
92-
editedElements.forEach(setWidthAttrs);
93+
editedElements.forEach(function(el) {
94+
setDimensionAttrs(el, 'width');
95+
});
96+
}
97+
98+
if (options && options.applyHeightAttributes) {
99+
editedElements.forEach(function(el) {
100+
setDimensionAttrs(el, 'height');
101+
});
93102
}
94103

95104
if (options && options.applyAttributesTableElements) {
@@ -221,18 +230,18 @@ function inlineDocument($, css, options) {
221230
}
222231
}
223232

224-
function setWidthAttrs(el) {
233+
function setDimensionAttrs(el, dimension) {
225234
var elName = el.name.toUpperCase();
226-
if (juice.widthElements.indexOf(elName) > -1) {
235+
if (juice[dimension + 'Elements'].indexOf(elName) > -1) {
227236
for (var i in el.styleProps) {
228-
if (el.styleProps[i].prop === 'width') {
237+
if (el.styleProps[i].prop === dimension) {
229238
if (el.styleProps[i].value.match(/px/)) {
230-
var pxWidth = el.styleProps[i].value.replace('px', '');
231-
$(el).attr('width', pxWidth);
239+
var pxSize = el.styleProps[i].value.replace('px', '');
240+
$(el).attr(dimension, pxSize);
232241
return;
233242
}
234243
if (juice.tableElements.indexOf(elName) > -1 && el.styleProps[i].value.match(/\%/)) {
235-
$(el).attr('width', el.styleProps[i].value);
244+
$(el).attr(dimension, el.styleProps[i].value);
236245
return;
237246
}
238247
}
@@ -334,6 +343,7 @@ function getDefaultOptions(options) {
334343
preserveMediaQueries: false,
335344
preserveFontFaces: false,
336345
applyWidthAttributes: false,
346+
applyHeightAttributes: false,
337347
applyAttributesTableElements: false,
338348
url: ""
339349
}, options);

test/cases/juice-content/height-attr.css

Whitespace-only changes.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<html>
2+
<head>
3+
<style>
4+
p, td.px, img.px {
5+
height: 200px;
6+
}
7+
td.percentage, img.percentage {
8+
height: 50%;
9+
}
10+
</style>
11+
</head>
12+
<body>
13+
<p>none</p>
14+
<table>
15+
<tr>
16+
<td class="px">
17+
high
18+
</td>
19+
<td class="percentage">
20+
high
21+
</td>
22+
</tr>
23+
</table>
24+
<img src="" alt="high" class="px">
25+
<img src="" alt="high" class="percentage">
26+
</body>
27+
</html>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"url": "./",
3+
"removeStyleTags": true,
4+
"applyHeightAttributes": true
5+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<html>
2+
<head>
3+
4+
</head>
5+
<body>
6+
<p style="height: 200px;">none</p>
7+
<table>
8+
<tr>
9+
<td class="px" style="height: 200px;" height="200">
10+
high
11+
</td>
12+
<td class="percentage" style="height: 50%;" height="50%">
13+
high
14+
</td>
15+
</tr>
16+
</table>
17+
<img src="" alt="high" class="px" style="height: 200px;" height="200">
18+
<img src="" alt="high" class="percentage" style="height: 50%;">
19+
</body>
20+
</html>

0 commit comments

Comments
 (0)