From 8662f28be5446c37e20d93183d36398015b418a8 Mon Sep 17 00:00:00 2001 From: Hans Otto Wirtz Date: Mon, 16 Mar 2020 14:55:24 +0800 Subject: [PATCH 1/2] Add support for htmlparser2 --- lib/inline.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/inline.js b/lib/inline.js index 2899c04..b4d6b42 100644 --- a/lib/inline.js +++ b/lib/inline.js @@ -339,7 +339,10 @@ function getStylesData($, options) { var styleDataList, styleData, styleElement; stylesList.each(function() { styleElement = this; - styleDataList = styleElement.childNodes; + // the API for Cheerio using parse5 (default) and htmlparser2 are slightly different + // detect this by checking if .childNodes exist (as opposed to .children) + var usingParse5 = !!styleElement.childNodes; + styleDataList = usingParse5 ? styleElement.childNodes : styleElement.children; if (styleDataList.length !== 1) { return; } @@ -348,14 +351,19 @@ function getStylesData($, options) { results.push(styleData); } if (options.removeStyleTags && $(styleElement).attr('data-embed') === undefined) { - var preservedText = utils.getPreservedText(styleElement.childNodes[0].nodeValue, { + var text = usingParse5 ? styleElement.childNodes[0].nodeValue : styleElement.children[0].data; + var preservedText = utils.getPreservedText(text, { mediaQueries: options.preserveMediaQueries, fontFaces: options.preserveFontFaces, keyFrames: options.preserveKeyFrames, pseudos: options.preservePseudos }, juiceClient.ignoredPseudos); if (preservedText) { - styleElement.childNodes[0].nodeValue = preservedText; + if (usingParse5) { + styleElement.childNodes[0].nodeValue = preservedText; + } else { + styleElement.children[0].data = preservedText; + } } else { $(styleElement).remove(); } From 4be7b6454b5780f962a16c807023382c78eef47f Mon Sep 17 00:00:00 2001 From: Hans Otto Wirtz Date: Mon, 16 Mar 2020 15:21:24 +0800 Subject: [PATCH 2/2] Add test for htmlparser2 --- test/run.js | 12 ++++++++++++ test/typescript/juice-tests.ts | 13 ++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/test/run.js b/test/run.js index 768545d..e5a5648 100644 --- a/test/run.js +++ b/test/run.js @@ -5,6 +5,8 @@ var utils = require('../lib/utils'); var basename = require('path').basename; var fs = require('fs'); var assert = require('assert'); +var cheerio = require('cheerio'); +var htmlparser2 = require('htmlparser2'); /** @@ -25,6 +27,16 @@ it('juice(html)', function() { assert.equal(utils.normalizeLineEndings(actual.trim()), utils.normalizeLineEndings(expected.trim())); }); +it('juice(document) with htmlparser2', function() { + var dom = htmlparser2.parseDOM('
'); + var $ = cheerio.load(dom); + + var expected = '
'; + juice.juiceDocument($); + var actual = $.html(); + assert.equal(utils.normalizeLineEndings(actual.trim()), utils.normalizeLineEndings(expected.trim())); +}); + var optionFiles = fs.readdirSync(__dirname + '/cases/juice-content'); optionFiles.forEach(function(file) { diff --git a/test/typescript/juice-tests.ts b/test/typescript/juice-tests.ts index 13b6e85..92bcff2 100644 --- a/test/typescript/juice-tests.ts +++ b/test/typescript/juice-tests.ts @@ -4,6 +4,7 @@ import juice = require('../../'); import cheerio = require('cheerio'); +import htmlparser2 = require('htmlparser2'); const sample = '
'; const someHtml = '

yo

'; @@ -47,7 +48,10 @@ const allWebResourceOptions = { }, }; -const cheerio$ = cheerio.load('

test

'); +const someMoreHtml = '

test

'; +const cheerio$ = cheerio.load(someMoreHtml); +const htmlparser2dom = htmlparser2.parseDOM(someMoreHtml); +const cheerioHtmlparser2$ = cheerio.load(htmlparser2dom); const x: string = juice(someHtml, {}); @@ -146,6 +150,11 @@ const c6 = juice.juiceDocument( cheerio$, ); +const c7 = juice.juiceDocument( + cheerioHtmlparser2$, + noOptions +); + juice.inlineContent(someHtml, someCss, noOptions); juice.inlineContent(someHtml, someCss, mostOptions); @@ -166,6 +175,8 @@ juice.inlineDocument(cheerio$, someCss, someWebResourceOptions); juice.inlineDocument(cheerio$, someCss, allWebResourceOptions); +juice.inlineDocument(cheerioHtmlparser2$, someCss, noOptions); + // Global settings juice.codeBlocks['HBS'] = { start: '{{', end: '}}' };