Skip to content

Commit

Permalink
Merge pull request #127 from jrit/cheerio-node-12
Browse files Browse the repository at this point in the history
XML support
  • Loading branch information
jrit committed May 21, 2015
2 parents 73296c2 + d3dfb48 commit e0ac5d6
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 10 deletions.
5 changes: 5 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
1.2.0 / 2015-05-21
==================

* Add `xmlMode` option

1.1.2 / 2015-05-08
==================

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ All juice methods take an options object that can contain any of these propertie
* `applyAttributesTableElements` - whether to create attributes for styles in `juice.styleToAttribute` on elements set in `juice.tableElements`. Defaults to `false`.
* `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 `{}`.
* `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`).
* `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

Expand Down
26 changes: 19 additions & 7 deletions lib/juice.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@
*/

var juice = function (html,options) {
var $ = utils.cheerio(html);
return juiceDocument($,options).html();
var $ = utils.cheerio(html, { xmlMode: options && options.xmlMode});
doc = juiceDocument($,options)

if (options && options.xmlMode)
return doc.xml();
else
return doc.html();
};

module.exports = juice;
Expand Down Expand Up @@ -295,9 +300,13 @@ function juiceResources(html, options, callback) {
return callback(err);
}

var $ = utils.cheerio(html);
var $ = utils.cheerio(html, { xmlMode: options && options.xmlMode});
juiceDocument($, options);
callback(null, $.html());

if (options.xmlMode)
callback(null, $.xml());
else
callback(null, $.html());
};

options.webResources.relativeTo = options.webResources.relativeTo || options.url; //legacy support
Expand Down Expand Up @@ -334,9 +343,13 @@ function juiceFile(filePath, options, callback) {
}

function inlineContent(html, css, options) {
var $ = utils.cheerio(html);
var $ = utils.cheerio(html, { xmlMode: options && options.xmlMode});
inlineDocument($, css, options);
return $.html();

if (options && options.xmlMode)
return $.xml();
else
return $.html();
}

function getStylesData($, options) {
Expand Down Expand Up @@ -372,4 +385,3 @@ function extractCssFromDocument($, options) {
var css = results.join("\n");
return css;
}

5 changes: 3 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ exports.getMediaQueryText = function ( css )
* api public
*/

exports.cheerio = function (html) {
return cheerio.load(html,{decodeEntities: false});
exports.cheerio = function (html, options) {
options = exports.extend({decodeEntities: false}, options || {});
return cheerio.load(html,options);
};

exports.inlineExternal = function (html, inlineOptions, callback){
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "juice",
"version": "1.1.2",
"version": "1.2.0",
"description": "Inlines css into html source",
"bin": "./bin/juice",
"main": "./lib/juice",
Expand Down
Empty file.
9 changes: 9 additions & 0 deletions test/cases/juice-content/xml.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head>
<meta name="name" content="value" />
</head>
<body>
hello<br />
<img src="" />
</body>
</html>
4 changes: 4 additions & 0 deletions test/cases/juice-content/xml.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"url": "./",
"xmlMode": true
}
9 changes: 9 additions & 0 deletions test/cases/juice-content/xml.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head>
<meta name="name" content="value"/>
</head>
<body>
hello<br/>
<img src=""/>
</body>
</html>
1 change: 1 addition & 0 deletions test/cases/not-xhtml.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

1 change: 1 addition & 0 deletions test/cases/not-xhtml.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<br>
1 change: 1 addition & 0 deletions test/cases/not-xhtml.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<br>

0 comments on commit e0ac5d6

Please sign in to comment.