Package detail

juice

Automattic5.2mMIT11.0.1

Inlines css into html source

readme

Build Status Dependency Status

Juice

Given HTML, juice will inline your CSS properties into the style attribute.

Some projects using Juice

How to use

Juice has a number of functions based on whether you want to process a file, HTML string, or a cheerio document, and whether you want juice to automatically get remote stylesheets, scripts and image dataURIs to inline.

To inline HTML without getting remote resources, using default options:

var juice = require('juice');
var result = juice("<style>div{color:red;}</style><div/>");

result will be:

<div style="color: red;"></div>

Try out the web client version

What is this useful for ?

  • HTML emails. For a comprehensive list of supported selectors see here
  • Embedding HTML in 3rd-party websites.

Documentation

Juice is exposed as a standard module, and from CLI with a smaller set of options.

Options

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

  • applyAttributesTableElements - whether to create attributes for styles in juice.styleToAttribute on elements set in juice.tableElements. Defaults to true.

  • applyHeightAttributes - whether to use any CSS pixel heights to create height attributes on elements set in juice.heightElements. Defaults to true.

  • applyStyleTags - whether to inline styles in <style></style> Defaults to true.

  • applyWidthAttributes - whether to use any CSS pixel widths to create width attributes on elements set in juice.widthElements. Defaults to true.

  • decodeStyleAttributes - whether to decode the value of style= attributes. Defaults to false.

  • extraCss - extra css to apply to the file. Defaults to "".

  • insertPreservedExtraCss - whether to insert into the document any preserved @media or @font-face content from extraCss when using preserveMediaQueries, preserveFontFaces or preserveKeyFrames. 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.

  • 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).

  • preserveFontFaces - preserves all @font-face within <style></style> tags as a refinement when removeStyleTags is true. Other styles are removed. Defaults to true.

  • preserveImportant - preserves !important in values. Defaults to false.

  • 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.

  • preserveKeyFrames - preserves all key frames within <style></style> tags as a refinement when removeStyleTags is true. Other styles are removed. Defaults to true.

  • preservePseudos - preserves all rules containing pseudo selectors defined in ignoredPseudos within <style></style> tags as a refinement when removeStyleTags is true. Other styles are removed. Defaults to true.

  • removeStyleTags - whether to remove the original <style></style> tags after (possibly) inlining the css from them. Defaults to true.

  • resolveCSSVariables - whether to resolve CSS variables. Defaults to true.

  • webResources - An options object that will be passed to web-resource-inliner for juice functions that will get remote resources (juiceResources and juiceFile). Defaults to {}.

  • 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.

Methods

juice(html [, options])

Returns string containing inlined HTML. Does not fetch remote resources.

  • html - html string, accepts complete documents as well as fragments
  • options - optional, see Options above

juice.juiceResources(html, options, callback)

Callback returns string containing inlined HTML. Fetches remote resources.

  • html - html string
  • options - see Options above
  • callback(err, html)
    • err - Error object or null
    • html - inlined HTML

juice.juiceFile(filePath, options, callback)

Callback returns string containing inlined HTML. Fetches remote resources.

  • filePath - path to the html file to be juiced
  • options - see Options above
  • callback(err, html)
    • err - Error object or null
    • html - inlined HTML

juice.juiceDocument($ [, options])

This takes a cheerio instance and performs inlining in-place. Returns the same cheerio instance. Does not fetch remote resources.

  • $ - a cheerio instance, be sure to use the same cheerio version that juice uses
  • options - optional, see Options above`

juice.inlineContent(html, css [, options])

This takes html and css and returns new html with the provided css inlined. It does not look at <style> or <link rel="stylesheet"> elements at all.

  • html - html string
  • css - css string
  • options - optional, see Options above

juice.inlineDocument($, css [, options])

Given a cheerio instance and css, this modifies the cheerio instance so that the provided css is inlined. It does not look at <style> or <link rel="stylesheet"> elements at all.

  • $ - a cheerio instance, be sure to use the same cheerio version that juice uses
  • css - css string
  • options - optional, see Options above

Global settings

juice.codeBlocks

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 juice.codeBlocks.HBS = {start: '{{', end: '}}'}. codeBlocks can fix problems where otherwise juice might interpret code like <= as HTML, when it is meant to be template language code. Note that codeBlocks is a dictionary which can contain many different code blocks, so don't do juice.codeBlocks = {...} do juice.codeBlocks.myBlock = {...}

juice.ignoredPseudos

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

juice.widthElements

Array of HTML elements that can receive width attributes.

juice.heightElements

Array of HTML elements that can receive height attributes.

juice.styleToAttribute

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

juice.tableElements

Array of table HTML elements that can receive attributes defined in juice.styleToAttribute.

juice.nonVisualElements

Array of elements that will not have styles inlined because they are not intended to render.

juiceClient.excludedProperties

Array of css properties that won't be inlined.

Special markup

data-embed

When a data-embed attribute is present on a stylesheet <link> that has been inlined into the document as a <style></style> tag by the web-resource-inliner juice will not inline the styles and will not remove the <style></style> tags.

This can be used to embed email client support hacks that rely on css selectors into your email templates.

CLI Options

To use Juice from CLI, run juice [options] input.html output.html

For a listing of all available options, just type juice -h.

Note that if you want to just type juice from the command line, you should npm install juice -g so it is globally available.

CLI Options:

The CLI should have all the above options with the names changed from camel case to hyphen-delimited, so for example extraCss becomes extra-css and webResources.scripts becomes web-resources-scripts.

These are additional options not included in the standard juice options listed above:

  • --css [filepath] will load and inject CSS into extraCss.
  • --options-file [filepath] will load and inject options from a JSON file. Options from the CLI will be given priority over options in the file when there is a conflict.
  • codeBlocks is optionally supported in the options file if you include it. This will allow you to support different template languages in a build process.

Running Juice in the Browser

Attempting to Browserify require('juice') fails because portions of Juice and its dependencies interact with the file system using the standard require('fs'). However, you can require('juice/client') via Browserify which has support for juiceDocument, inlineDocument, and inlineContent, but not juiceFile, juiceResources, or inlineExternal. Note that automated tests are not running in the browser yet.

License

MIT Licensed, see License.md

3rd-party

changelog

10.0.0 / 2023-12-03

  • Resolve css variables

9.1.0 / 2023-07-03

  • Fix: An error is thrown in resetCounter when adding an counter-reset inline style to an element in combination with a different style (e.g., in head).

8.0.0 / 2021-22-21

  • pseudo Update
  • !important table attribute update

7.0.0 / 2020-07-10

  • Breaking: requires node 10+
  • Please see changes in web-resource-inliner@5 for other details

6.0.0 / 2019-11-27

  • Drop support for Node.js 4

5.2.0 / 2019-03-18

  • Fix bug with getPreservedText (mixed up preservePseudos and ignorePseudos)

5.1.0 / 2018-12-18

  • Add codeBlocks support in CLI --options-file

5.0.0 / 2018-10-02

  • Fix gradient CSS being inlined into the background attribute

5.0.0 / 2018-10-02

  • Adds preservePseudos option with default true
  • Adds th to list of elements that can receive a width attribute

4.3.0 / 2018-06-01

  • Adds preserveKeyFrames option

4.2.3 / 2018-03-08

  • fix help typo
  • fix memory leak in cheerio code block encode/decode
  • update TS definition to allow for optional fileContent option

4.2.2 / 2017-10-22

  • Fix code blocks encode/decode so cheerio doesn't mulch them if they are tag attributes

4.2.1 / 2017-10-20

  • Fix regex catastrophic backtracking issue with unclosed code blocks

4.2.0 / 2017-10-05

  • Adds support for preserveImportant to CLI

4.0.1 / 2016-11-24

  • Updated typescript definitions

4.0.0 / 2016-11-17

  • deps: update web-resource-inliner and cross-spawn
  • engine: specify support as node 4.2+
  • feat: add option codeBlocks to support general exclusion of fenced code, like EJS, HBS, etc.

3.0.1 / 2016-10-06

  • deps: install mensch from npm instead of github

3.0.0 / 2016-06-01

  • deps: upgrade web-resource-inliner, change cross-spawn-async to cross-spawn, move batch to devDeps

2.0.0 / 2016-05-24

  • fix: specificity bugs
  • deps: upgrade cheerio, cssom, web-resource-inliner
  • major: most options default to true instead of false now
  • major: remove deprecated toArray

1.11.0 / 2016-05-11

  • feat: exclude css properties from inliner with juiceClient.excludedProperties
  • fix: specificity of consecutive !important rules

1.10.0 / 2016-02-25

  • cli: correctly handle absolute path for optionsFile
  • cli: convert all numbers and booleans from default string values
  • Replace deprecated dep win-spawn with cross-spawn-async

1.9.0 / 2016-01-04

  • Add option insertPreservedExtraCss

1.8.1 / 2015-12-01

  • Switch xtend to deep-extend to fix issue loading webResources settings when using --options-file

1.8.0 / 2015-11-23

  • Make all options available through CLI
  • Add --options-file to CLI for loading JSON options
  • Make CLI tests work on Windows
  • Deprecate utils.toArray()
  • Fix handling of :not() specificity

1.7.0 / 2015-11-03

  • Refactor to provide browser support at juice/client
  • Add option applyHeightAttributes
  • Bump dep web-resource-inliner

1.6.0 / 2015-10-26

  • Add feature data-embed attribute
  • update deps: web-resource-inliner, batch, commander, slick

1.5.0 / 2015-09-25

  • Exclude non-visual tags from inlining
  • Add option preserveFontFaces
  • update dep: web-resource-inliner

1.4.0 / 2015-08-19

  • Add extra CSS option to CLI
  • CLI has test coverage now

1.3.3 / 2015-07-14

  • Prevent mangling of EJS tags

1.3.0 / 2015-07-02

  • Add option preserveImportant
  • Make lib use_strict compliant and lint files

1.2.0 / 2015-05-21

  • Add xmlMode option

1.1.2 / 2015-05-08

  • remove index.js and point main in package.json to /lib/juice.js

1.1.1

  • publish with line endings fixed in /bin

1.1.0 / 2015-05-04

  • Fix order of inlined style properties. Now sorted by selector specificity, resulting in the same computed styles that the original CSS would have had.
  • Add option to inline pseudo elements as elements

1.0.2 / 2015-04-27

  • added option applyAttributesTableElements
  • bump version on web-resource-inliner to 1.1.1
  • fix bin/juice so it works as documented

1.0.1 / 2015-02-22

  • legacy support for url option
  • bump version on web-resource-inliner to use relativeTo with a url and remote paths starting //
  • update skipped tests to mocha so they will run with npm test and on travis
  • bump web-resource-inliner version to expose strict option, which is now false by default

1.0.0 / 2015-02-12

  • add support for node 0.12
  • drop support for node 0.8
  • use cheerio instead of jsdom
  • move remote resource fetching to external library web-resource-inliner
  • adjust public methods as needed to support other changes
  • rename juiceContent to juiceResources
  • maintain CSS single quotes
  • ability to inline css pixel widths to width attribute with applyWidthAttributes
  • alphabetize styles for improved specificity
  • ability to keep media query styles with option preserveMediaQueries
  • clean up testing setup, including removing old dependency on expresso
  • istanbul put in place to show test coverage
  • remove now unused options applyLinkTags and removeLinkTags

0.5.0 / 2014-09-08

  • update dependencies [binarykitchen]
  • handle errors in loading external css
  • add repository field to readme

0.4.0 / 2013-04-15

  • update jsdom dependency to 0.6.0

0.3.3 / 2013-04-08

  • fix resolving file:// paths on windows (thanks Mirco Zeiss)
  • fix crash during cleanup. (thanks Ger Hobbelt)
  • update superagent to 0.14.0

0.3.2 / 2013-03-26

  • fix regression: not ignoring pseudos

0.3.1 / 2013-03-26

  • do not crash on ::selectors (covered by normalize.css test case)

0.3.0 / 2013-03-26

  • update jsdom dependency to 0.5.4
  • support node v0.10
  • switch dependency to slick instead of mootools which was rudely unpublished

0.2.0 / 2013-02-13

  • update jsdom dependency to 0.5.0

0.1.3 / 2013-02-12

  • fix specificity test. all test cases passed now.
  • add a command line juice program

0.1.2 / 2013-02-11

  • fix incorrectly lowercasing <link> href

0.1.1 / 2013-02-11

  • explicitly document which node versions are supported with engines and travis-ci.
  • expose juice.inlineDocument and juice.inlineContent

0.1.0 / 2013-02-07

  • fix / test case for @media queries
  • merge boost into juice
  • legacy juice function still works as is
  • add juice(filePath, [options], callback)
  • add juice.juiceDocument(document, options, callback)
  • add juice.juiceContent(html, options, callback)
  • remove juice.juiceDom

0.0.9 / 2013-02-07

  • update jsdom dependency to 0.4.0
  • update cssom dependency to 0.2.5

0.0.8 / 2013-02-06

  • expose a lower level export so you can operate on a jsdom document [superjoe30]
  • fix exports not working [superjoe30]
  • fix jshint problems [superjoe30]

0.0.7 / 2013-02-06

  • fixed test case expected outputs to have starting and ending <html> and <body> tags as jsdom appends them in its html() function if they do not exist
  • regression test for previous fix for media queries. note i had to wrap my test .out content in <html><body> tags in order to pass tests, it looks like they are appended at some point for partial html content which just guessing is new behavior from when these tests were written
  • make sure the css rule has selectorText to prevent parsing exception. hit this on @media rules which do not have selector text. afaik this means media queries will not be inlined. however everything else is.
  • bump jsdom
  • Added note regarding node-email-templates to README

0.0.6 / 2011-12-20

  • Corrected juice unit tests for latest cssom.
  • Fixed presence of \n in selectors.
  • Fixed unneeded removal of inline event handlers in html.
  • Bumped jsdom.

0.0.5 / 2011-10-10

  • Added whitelist of pseudos to ignore (fixes :first-child etc)
  • Fine-tuned jsdom for speed (disabled unneded features).
  • Added caching of parsed selectors.

0.0.4 / 2011-10-10

  • Fixed :hover.

0.0.3 / 2011-10-09

  • Fixed specificity :not recursion.

0.0.2 / 2011-10-09

  • Fixed specificity calculation for not() and pseudos. [arian]

0.0.1 / 2011-10-09

  • Initial release.