Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/keep-html-fragments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"inline-css": patch
"style-data": patch
"list-stylesheets": patch
---

Return an HTML fragment as a fragment. Since the move to cheerio 1.x, a fragment came back wrapped in `<html><head></head><body>` tags.
8 changes: 6 additions & 2 deletions lib/inline-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const removeClassId = require('./removeClassId');
const setTableAttrs = require('./setTableAttrs');
const pick = require('pick-util');

const isDocument = html => /<(?:!doctype|html|head|body)[\s>]/i.test(html);

function replaceCodeBlock(html, re, block) {
return html.replace(re, () => block);
}
Expand Down Expand Up @@ -50,14 +52,16 @@ module.exports = (html, css, options) => {
const decodeEntities = _html => decodeCodeBlocks(_html);
let $;

$ = cheerio.load(encodeEntities(html), pick(opts, [
const encoded = encodeEntities(html);

$ = cheerio.load(encoded, pick(opts, [
'xmlMode',
'decodeEntities',
'lowerCaseTags',
'lowerCaseAttributeNames',
'recognizeCDATA',
'recognizeSelfClosing'
]));
]), isDocument(encoded));

try {
rules = parseCSS(css);
Expand Down
8 changes: 6 additions & 2 deletions packages/list-stylesheets/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const cheerio = require('cheerio');
const pick = require('pick-util');

const isDocument = html => /<(?:!doctype|html|head|body)[\s>]/i.test(html);

function replaceCodeBlock(html, re, block) {
return html.replace(re, () => block);
}
Expand Down Expand Up @@ -41,7 +43,9 @@ module.exports = (html, options) => {
const decodeEntities = _html => decodeCodeBlocks(_html);
let $;

$ = cheerio.load(encodeEntities(html), Object.assign({
const encoded = encodeEntities(html);

$ = cheerio.load(encoded, Object.assign({
decodeEntities: false
}, pick(options, [
'xmlMode',
Expand All @@ -50,7 +54,7 @@ module.exports = (html, options) => {
'lowerCaseAttributeNames',
'recognizeCDATA',
'recognizeSelfClosing'
])));
])), isDocument(encoded));

results.hrefs = [];

Expand Down
7 changes: 7 additions & 0 deletions packages/list-stylesheets/test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,11 @@ describe('list-stylesheets', () => {
done
);
});

it('Should leave an html fragment without html, head and body tags', () => {
const data = getStylesheetList('<link rel="stylesheet" href="file.css"><p>Hi</p>', { applyLinkTags: true, removeLinkTags: true });

data.hrefs.should.be.eql(['file.css']);
data.html.should.be.equal('<p>Hi</p>');
});
});
8 changes: 6 additions & 2 deletions packages/style-data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const mediaQueryText = require('mediaquery-text');
const cheerio = require('cheerio');
const pick = require('pick-util');

const isDocument = html => /<(?:!doctype|html|head|body)[\s>]/i.test(html);

function replaceCodeBlock(html, re, block) {
return html.replace(re, () => block);
}
Expand Down Expand Up @@ -49,7 +51,9 @@ module.exports = (html, options, callback) => {
let styleDataList;
let styleData;

$ = cheerio.load(encodeEntities(html), Object.assign({
const encoded = encodeEntities(html);

$ = cheerio.load(encoded, Object.assign({
decodeEntities: false
}, pick(options, [
'xmlMode',
Expand All @@ -58,7 +62,7 @@ module.exports = (html, options, callback) => {
'lowerCaseAttributeNames',
'recognizeCDATA',
'recognizeSelfClosing'
])));
])), isDocument(encoded));

results.css = [];

Expand Down
8 changes: 8 additions & 0 deletions packages/style-data/test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,12 @@ describe('style-data', () => {
done
);
});

it('Should leave an html fragment without html, head and body tags', done => {
getStylesData('<style>.a { color: red; }</style><p class="a">Hi</p>', { applyStyleTags: true, removeStyleTags: true }, (err, results) => {
results.html.should.be.equal('<p class="a">Hi</p>');
should.deepEqual(results.css, ['.a { color: red; }']);
done();
});
});
});
9 changes: 9 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,4 +298,13 @@ describe('inline-css', () => {
};
compare(path.join('test', 'fixtures', 'codeblocks-external.html'), path.join('test', 'expected', 'codeblocks-external.html'), options, done);
});

it('Should leave an html fragment without html, head and body tags', done => {
inlineCss('<div class="a">Hi</div><style>.a { color: red; }</style>', { url: `file://${path.resolve('test')}/` })
.then(html => {
html.should.be.equal('<div class="a" style="color: red;">Hi</div>');
done();
})
.catch(done);
});
});