From d9d832a1c636b9ca0a75f31352a5a58a02d7c1f7 Mon Sep 17 00:00:00 2001 From: Donald Mull Jr Date: Fri, 13 Mar 2015 23:35:11 -0400 Subject: [PATCH 1/3] support for xml --- lib/juice.js | 26 +++++++++++++++++++------- lib/utils.js | 5 +++-- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/lib/juice.js b/lib/juice.js index 52bff52..c4a1d18 100644 --- a/lib/juice.js +++ b/lib/juice.js @@ -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; @@ -180,9 +185,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 @@ -218,9 +227,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) { @@ -256,4 +269,3 @@ function extractCssFromDocument($, options) { var css = results.join("\n"); return css; } - diff --git a/lib/utils.js b/lib/utils.js index 62239d2..319d0b1 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -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){ From 9166574bfda3ecfdf72dd37e73bc57bf1c1f992b Mon Sep 17 00:00:00 2001 From: Jarrett Widman Date: Thu, 21 May 2015 07:53:50 -0500 Subject: [PATCH 2/3] test case confirm not converting to XHTML --- test/cases/not-xhtml.css | 1 + test/cases/not-xhtml.html | 1 + test/cases/not-xhtml.out | 1 + 3 files changed, 3 insertions(+) create mode 100644 test/cases/not-xhtml.css create mode 100644 test/cases/not-xhtml.html create mode 100644 test/cases/not-xhtml.out diff --git a/test/cases/not-xhtml.css b/test/cases/not-xhtml.css new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/test/cases/not-xhtml.css @@ -0,0 +1 @@ + diff --git a/test/cases/not-xhtml.html b/test/cases/not-xhtml.html new file mode 100644 index 0000000..0ca25d9 --- /dev/null +++ b/test/cases/not-xhtml.html @@ -0,0 +1 @@ +
diff --git a/test/cases/not-xhtml.out b/test/cases/not-xhtml.out new file mode 100644 index 0000000..0ca25d9 --- /dev/null +++ b/test/cases/not-xhtml.out @@ -0,0 +1 @@ +
From d3dfb483160d4d4a3a1200bf822b27e3c7efb788 Mon Sep 17 00:00:00 2001 From: Jarrett Widman Date: Thu, 21 May 2015 08:09:57 -0500 Subject: [PATCH 3/3] tests and docs for xmlMode, bump version --- History.md | 5 +++++ README.md | 1 + package.json | 2 +- test/cases/juice-content/xml.css | 0 test/cases/juice-content/xml.html | 9 +++++++++ test/cases/juice-content/xml.json | 4 ++++ test/cases/juice-content/xml.out | 9 +++++++++ 7 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 test/cases/juice-content/xml.css create mode 100644 test/cases/juice-content/xml.html create mode 100644 test/cases/juice-content/xml.json create mode 100644 test/cases/juice-content/xml.out diff --git a/History.md b/History.md index 2e5079f..f35c6e3 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,8 @@ +1.2.0 / 2015-05-21 +================== + +* Add `xmlMode` option + 1.1.2 / 2015-05-08 ================== diff --git a/README.md b/README.md index 45ed335..018b16c 100644 --- a/README.md +++ b/README.md @@ -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 `` 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 diff --git a/package.json b/package.json index 5b86f59..1041933 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/cases/juice-content/xml.css b/test/cases/juice-content/xml.css new file mode 100644 index 0000000..e69de29 diff --git a/test/cases/juice-content/xml.html b/test/cases/juice-content/xml.html new file mode 100644 index 0000000..c0f236c --- /dev/null +++ b/test/cases/juice-content/xml.html @@ -0,0 +1,9 @@ + + + + + + hello
+ + + diff --git a/test/cases/juice-content/xml.json b/test/cases/juice-content/xml.json new file mode 100644 index 0000000..0f3ac2e --- /dev/null +++ b/test/cases/juice-content/xml.json @@ -0,0 +1,4 @@ +{ + "url": "./", + "xmlMode": true +} \ No newline at end of file diff --git a/test/cases/juice-content/xml.out b/test/cases/juice-content/xml.out new file mode 100644 index 0000000..ae08613 --- /dev/null +++ b/test/cases/juice-content/xml.out @@ -0,0 +1,9 @@ + + + + + + hello
+ + +