mirror of
https://github.com/KevinMidboe/inline-html.git
synced 2025-12-08 20:29:06 +00:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4e761d12a5 | ||
|
|
c9b71e782d | ||
|
|
5d0351585e | ||
|
|
c30350d011 | ||
|
|
22ce5e3f25 | ||
|
|
897d87436a | ||
|
|
bde7bc9a0b | ||
|
|
b3d479224e |
24
README.md
24
README.md
@@ -11,9 +11,11 @@ The following HTML elements and CSS data types are inlined:
|
|||||||
|
|
||||||
- Scripts - The source path is read and inlined.
|
- Scripts - The source path is read and inlined.
|
||||||
|
|
||||||
- Images - The source path is replaced with a datauri.
|
- Linked CSS stylesheets - The stylesheet is read and inlined within a `<style>` element. Note that nested `@import`'s are also inlined.
|
||||||
|
|
||||||
- Linked LESS stylesheets - The LESS is compiled and the result is inlined within a `<style>` element. Note that `@imports` are processed as well.
|
- Linked LESS stylesheets - The LESS is compiled and the output is inlined within a `<style>` element. Note that `@import`'s are also inlined.
|
||||||
|
|
||||||
|
- Images - The source path is replaced with a datauri.
|
||||||
|
|
||||||
- CSS url data types - The reference path is replaced with a datauri. These can be used in linked stylesheets, style elements, and element style attributes.
|
- CSS url data types - The reference path is replaced with a datauri. These can be used in linked stylesheets, style elements, and element style attributes.
|
||||||
|
|
||||||
@@ -32,7 +34,6 @@ Assuming ...
|
|||||||
- `main.less`
|
- `main.less`
|
||||||
|
|
||||||
```css
|
```css
|
||||||
@import (less) 'main.css';
|
|
||||||
div { background-image: url('path/to/file'); }
|
div { background-image: url('path/to/file'); }
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -51,6 +52,7 @@ var inline = require('inline-html');
|
|||||||
co(function * () {
|
co(function * () {
|
||||||
var html = `
|
var html = `
|
||||||
<script src="main.js"></script>
|
<script src="main.js"></script>
|
||||||
|
<link rel="stylesheet" href="main.css"/>
|
||||||
<link rel="stylesheet/less" href="main.less"/>
|
<link rel="stylesheet/less" href="main.less"/>
|
||||||
<style> div { background-image: url('path/to/file'); } </style>
|
<style> div { background-image: url('path/to/file'); } </style>
|
||||||
<div style="background-image: url('path/to/file');"></div>
|
<div style="background-image: url('path/to/file');"></div>
|
||||||
@@ -59,13 +61,9 @@ co(function * () {
|
|||||||
html = yield inline.html(html);
|
html = yield inline.html(html);
|
||||||
console.log(html);
|
console.log(html);
|
||||||
/**
|
/**
|
||||||
<script>
|
<script> var a = 1; </script>
|
||||||
var a = 1;
|
<style> @font-face { src: url('data:...'); } </style>
|
||||||
</script>
|
<style> div { background-image: url('data:...'); } </style>
|
||||||
<style>
|
|
||||||
@font-face { src: url('data:...'); }
|
|
||||||
div { background-image: url('data:...'); }
|
|
||||||
</style>
|
|
||||||
<style> div { background-image: url('data:...'); } </style>
|
<style> div { background-image: url('data:...'); } </style>
|
||||||
<div style="background-image: url('data:...');"></div>
|
<div style="background-image: url('data:...');"></div>
|
||||||
<img src="data:..."/>
|
<img src="data:..."/>
|
||||||
@@ -87,7 +85,7 @@ npm install inline-html
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
<a name="html"/>
|
<a name="html"></a>
|
||||||
### inline.html ( html [, options] )
|
### inline.html ( html [, options] )
|
||||||
|
|
||||||
Parses an HTML string and embeds referenced local assets into the HTML.
|
Parses an HTML string and embeds referenced local assets into the HTML.
|
||||||
@@ -117,7 +115,7 @@ co(function * () {
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
<a name="file"/>
|
<a name="file"></a>
|
||||||
### inline.file ( filename [, options] )
|
### inline.file ( filename [, options] )
|
||||||
|
|
||||||
Reads an HTML file and embeds referenced local assets into the HTML.
|
Reads an HTML file and embeds referenced local assets into the HTML.
|
||||||
@@ -146,7 +144,7 @@ co(function * () {
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
<a name="results"/>
|
<a name="results"></a>
|
||||||
### Results
|
### Results
|
||||||
|
|
||||||
The `Promise` returned by these functions is optionally fulfilled with a `results` object that has the following properties:
|
The `Promise` returned by these functions is optionally fulfilled with a `results` object that has the following properties:
|
||||||
|
|||||||
@@ -10,8 +10,6 @@ const url = require('url');
|
|||||||
|
|
||||||
const collapseWhitespace = str => string(str).collapseWhitespace().toString();
|
const collapseWhitespace = str => string(str).collapseWhitespace().toString();
|
||||||
const forEachIndexed = R.addIndex(R.forEach);
|
const forEachIndexed = R.addIndex(R.forEach);
|
||||||
const format = (path) => url.format(path);
|
|
||||||
const parse = (path) => url.parse(path);
|
|
||||||
const resolve = path.resolve;
|
const resolve = path.resolve;
|
||||||
|
|
||||||
const augmentError = (error, filename, files) => {
|
const augmentError = (error, filename, files) => {
|
||||||
@@ -24,13 +22,12 @@ const augmentError = (error, filename, files) => {
|
|||||||
* Returns url path without query string and hash if present.
|
* Returns url path without query string and hash if present.
|
||||||
*
|
*
|
||||||
* @param path
|
* @param path
|
||||||
*
|
|
||||||
* @returns path
|
* @returns path
|
||||||
*/
|
*/
|
||||||
const cleanUrl = R.pipe(
|
const cleanUrl = R.pipe(
|
||||||
parse,
|
url.parse,
|
||||||
R.pick(['protocol', 'host', 'pathname']),
|
R.pick(['protocol', 'host', 'pathname']),
|
||||||
format,
|
url.format,
|
||||||
decodeURI
|
decodeURI
|
||||||
);
|
);
|
||||||
/**
|
/**
|
||||||
@@ -68,16 +65,14 @@ const inlineStyles = ($, filename) => {
|
|||||||
try {
|
try {
|
||||||
const styles = $('style')
|
const styles = $('style')
|
||||||
.toArray();
|
.toArray();
|
||||||
|
|
||||||
const contents = R.map(style => {
|
const contents = R.map(style => {
|
||||||
const css = $(style).html();
|
const css = $(style).html();
|
||||||
const result = inlineUrl(filename, css);
|
const result = inlineUrl(filename, css);
|
||||||
files = R.concat(files, result.files);
|
files = R.concat(files, result.files);
|
||||||
return result.css;
|
return result.css;
|
||||||
}, styles);
|
}, styles);
|
||||||
|
const replaceStyle = (style, index) => $(style).html(contents[index]);
|
||||||
forEachIndexed((style, index) => $(style).html(contents[index]), styles);
|
forEachIndexed(replaceStyle, styles);
|
||||||
|
|
||||||
return { $, files };
|
return { $, files };
|
||||||
}
|
}
|
||||||
catch (error) { throw augmentError(error, filename, files); }
|
catch (error) { throw augmentError(error, filename, files); }
|
||||||
@@ -95,7 +90,6 @@ const inlineStyleAttributes = ($, filename) => {
|
|||||||
const elements = $('*')
|
const elements = $('*')
|
||||||
.filter('[style]')
|
.filter('[style]')
|
||||||
.toArray();
|
.toArray();
|
||||||
|
|
||||||
const styles = R.map(element => {
|
const styles = R.map(element => {
|
||||||
var style = $(element).attr('style');
|
var style = $(element).attr('style');
|
||||||
const rule = wrap(style);
|
const rule = wrap(style);
|
||||||
@@ -104,9 +98,8 @@ const inlineStyleAttributes = ($, filename) => {
|
|||||||
style = R.pipe( collapseWhitespace, unwrap )(result.css);
|
style = R.pipe( collapseWhitespace, unwrap )(result.css);
|
||||||
return style;
|
return style;
|
||||||
}, elements);
|
}, elements);
|
||||||
|
const replaceElementStyle = (element, index) => $(element).attr('style', styles[index]);
|
||||||
forEachIndexed((element, index) => $(element).attr('style', styles[index]), elements);
|
forEachIndexed(replaceElementStyle, elements);
|
||||||
|
|
||||||
return { $, files };
|
return { $, files };
|
||||||
}
|
}
|
||||||
catch (error) { throw augmentError(error, filename, files); }
|
catch (error) { throw augmentError(error, filename, files); }
|
||||||
|
|||||||
13
lib/img.js
13
lib/img.js
@@ -6,7 +6,6 @@ const path = require('path');
|
|||||||
const R = require('ramda');
|
const R = require('ramda');
|
||||||
|
|
||||||
const forEachIndexed = R.addIndex(R.forEach);
|
const forEachIndexed = R.addIndex(R.forEach);
|
||||||
const resolve = R.curry((a,b) => path.resolve(a,b));
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inline sourced image files
|
* Inline sourced image files
|
||||||
@@ -17,11 +16,8 @@ const resolve = R.curry((a,b) => path.resolve(a,b));
|
|||||||
* Filename used to resolve relative sources being inlined
|
* Filename used to resolve relative sources being inlined
|
||||||
*/
|
*/
|
||||||
const inlineImg = co.wrap(function * ($, filename) {
|
const inlineImg = co.wrap(function * ($, filename) {
|
||||||
var files;
|
|
||||||
const basedir = path.dirname(filename);
|
const basedir = path.dirname(filename);
|
||||||
const getAttr = R.curry((attr, element) => $(element).attr(attr));
|
var files;
|
||||||
const setAttr = R.curry((attr, element, value) => $(element).attr(attr, value));
|
|
||||||
const getFilename = R.pipe(getAttr('src'), resolve(basedir));
|
|
||||||
try {
|
try {
|
||||||
const images = $('img')
|
const images = $('img')
|
||||||
.filter((index, element) => {
|
.filter((index, element) => {
|
||||||
@@ -29,13 +25,12 @@ const inlineImg = co.wrap(function * ($, filename) {
|
|||||||
return isLocalPath(source) && !isTemplateExpression(source);
|
return isLocalPath(source) && !isTemplateExpression(source);
|
||||||
})
|
})
|
||||||
.toArray();
|
.toArray();
|
||||||
|
const getFilename = element => path.resolve(basedir, $(element).attr('src'));
|
||||||
const filenames = R.map(getFilename, images);
|
const filenames = R.map(getFilename, images);
|
||||||
files = R.uniq(filenames);
|
files = R.uniq(filenames);
|
||||||
const uris = yield R.map(datauri, filenames);
|
const uris = yield R.map(datauri, filenames);
|
||||||
|
const replaceImageSource = (image, index) => $(image).attr('src', uris[index]);
|
||||||
forEachIndexed((image, index) => setAttr('src', image, uris[index]), images);
|
forEachIndexed(replaceImageSource, images);
|
||||||
|
|
||||||
return { $, files };
|
return { $, files };
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ const fs = require('mz/fs');
|
|||||||
const inlineCssUrl = require('./css-url');
|
const inlineCssUrl = require('./css-url');
|
||||||
const inlineImg = require('./img');
|
const inlineImg = require('./img');
|
||||||
const inlineLess = require('./link-less');
|
const inlineLess = require('./link-less');
|
||||||
|
const inlineLinkCss = require('./link-css');
|
||||||
const inlineScript = require('./script');
|
const inlineScript = require('./script');
|
||||||
const R = require('ramda');
|
const R = require('ramda');
|
||||||
const Ru = require('@panosoft/ramda-utils');
|
const Ru = require('@panosoft/ramda-utils');
|
||||||
@@ -19,7 +20,7 @@ var inline = {};
|
|||||||
*/
|
*/
|
||||||
inline.html = co.wrap(function * (html, options) {
|
inline.html = co.wrap(function * (html, options) {
|
||||||
options = Ru.defaults({
|
options = Ru.defaults({
|
||||||
filename: null,
|
filename: '.',
|
||||||
less: {},
|
less: {},
|
||||||
verbose: false
|
verbose: false
|
||||||
}, options || {});
|
}, options || {});
|
||||||
@@ -34,6 +35,10 @@ inline.html = co.wrap(function * (html, options) {
|
|||||||
$ = result.$;
|
$ = result.$;
|
||||||
files = R.concat(files, result.files);
|
files = R.concat(files, result.files);
|
||||||
|
|
||||||
|
result = yield inlineLinkCss($, filename, options);
|
||||||
|
$ = result.$;
|
||||||
|
files = R.concat(files, result.files);
|
||||||
|
|
||||||
result = inlineCssUrl($, filename, options);
|
result = inlineCssUrl($, filename, options);
|
||||||
$ = result.$;
|
$ = result.$;
|
||||||
files = R.concat(files, result.files);
|
files = R.concat(files, result.files);
|
||||||
|
|||||||
70
lib/link-css.js
Normal file
70
lib/link-css.js
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
const co = require('co');
|
||||||
|
const fs = require('mz/fs');
|
||||||
|
const isLocalPath = require('is-local-path');
|
||||||
|
const isTemplateExpression = require('./is-template-expression');
|
||||||
|
const path = require('path');
|
||||||
|
const postcss = require('postcss');
|
||||||
|
const postcssImport = require('postcss-import');
|
||||||
|
const postcssUrl = require('postcss-url');
|
||||||
|
const R = require('ramda');
|
||||||
|
|
||||||
|
const forEachIndexed = R.addIndex(R.forEach);
|
||||||
|
const render = R.curryN(2, co.wrap(function * (filename, href) {
|
||||||
|
var imports;
|
||||||
|
const processor = postcss()
|
||||||
|
.use(postcssImport({ async: true, onImport: files => imports = files }))
|
||||||
|
.use(postcssUrl({ url: 'rebase' }));
|
||||||
|
try {
|
||||||
|
const css = yield fs.readFile(href, 'utf8');
|
||||||
|
const result = yield processor.process(css, { from: href, to: filename });
|
||||||
|
const output = {css: result.css, imports};
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
// process uses error.file = href
|
||||||
|
// import uses error.fileName
|
||||||
|
// readFile uses nothing => use filename
|
||||||
|
error.filename = error.file || error.fileName || filename;
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
/**
|
||||||
|
* Inline liked CSS stylesheets by replacing link elements with
|
||||||
|
* style elements that contain the css file contents.
|
||||||
|
* @param {Object} $
|
||||||
|
* Parsed HTML source to inline
|
||||||
|
* @param {String} [filename='.']
|
||||||
|
* Filename of the HTML document contained within $
|
||||||
|
* @return {Promise}
|
||||||
|
*/
|
||||||
|
const inlineLinkCss = co.wrap(function * ($, filename) {
|
||||||
|
// TODO consider: explicitly default filename = '.'?
|
||||||
|
// path.dirname(null || undefined) -> '.'
|
||||||
|
const basedir = path.dirname(filename);
|
||||||
|
var files = [];
|
||||||
|
try {
|
||||||
|
const links = $('link[rel="stylesheet"]')
|
||||||
|
.filter((index, link) => {
|
||||||
|
const href = $(link).attr('href');
|
||||||
|
return isLocalPath(href) && !isTemplateExpression(href);
|
||||||
|
})
|
||||||
|
.toArray();
|
||||||
|
const getHref = element => path.resolve(basedir, $(element).attr('href'));
|
||||||
|
const hrefs = R.map(getHref, links);
|
||||||
|
files = R.concat(files, hrefs);
|
||||||
|
const outputs = yield R.map(render(filename), hrefs);
|
||||||
|
const imports = R.flatten(R.map(R.prop('imports'), outputs));
|
||||||
|
files = R.concat(files, imports);
|
||||||
|
const styles = R.map(output => $('<style>').html(output.css), outputs);
|
||||||
|
const replaceLink = (link, index) => $(link).replaceWith(styles[index]);
|
||||||
|
forEachIndexed(replaceLink, links);
|
||||||
|
files = R.uniq(files);
|
||||||
|
return { $, files };
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
if (!error.filename) error.filename = filename;
|
||||||
|
error.files = R.uniq(files);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
module.exports = inlineLinkCss;
|
||||||
@@ -1,17 +1,24 @@
|
|||||||
const co = require('co');
|
const co = require('co');
|
||||||
const fs = require('mz/fs');
|
const fs = require('mz/fs');
|
||||||
const isLocalPath = require('is-local-path');
|
const isLocalPath = require('is-local-path');
|
||||||
|
const isTemplateExpression = require('./is-template-expression');
|
||||||
const less = require('less');
|
const less = require('less');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const R = require('ramda');
|
const R = require('ramda');
|
||||||
const Ru = require('@panosoft/ramda-utils');
|
const Ru = require('@panosoft/ramda-utils');
|
||||||
|
|
||||||
const forEachIndexed = R.addIndex(R.forEach);
|
const forEachIndexed = R.addIndex(R.forEach);
|
||||||
const resolve = R.curry((a,b) => path.resolve(a,b));
|
|
||||||
|
|
||||||
const render = R.curryN(2, co.wrap(function * (options, filename) {
|
const render = R.curryN(3, co.wrap(function * (options, basedir, href) {
|
||||||
options = R.merge(options || {}, { filename });
|
const relativePath = path.relative(basedir, path.dirname(href));
|
||||||
const contents = yield fs.readFile(filename, 'utf8');
|
const rootpath = relativePath ? `${relativePath}/` : false;
|
||||||
|
options = R.merge(options || {}, {
|
||||||
|
async: true,
|
||||||
|
relativeUrls: true,
|
||||||
|
filename: href,
|
||||||
|
rootpath
|
||||||
|
});
|
||||||
|
const contents = yield fs.readFile(href, 'utf8');
|
||||||
return yield less.render(contents, options);
|
return yield less.render(contents, options);
|
||||||
}));
|
}));
|
||||||
/**
|
/**
|
||||||
@@ -27,25 +34,25 @@ const render = R.curryN(2, co.wrap(function * (options, filename) {
|
|||||||
*/
|
*/
|
||||||
const inlineLess = co.wrap(function * ($, filename, options) {
|
const inlineLess = co.wrap(function * ($, filename, options) {
|
||||||
options = Ru.defaults({ less: {} }, options || {});
|
options = Ru.defaults({ less: {} }, options || {});
|
||||||
options = Ru.defaults({ relativeUrls: true }, options.less);
|
options = options.less;
|
||||||
var files = [];
|
|
||||||
const basedir = path.dirname(filename);
|
const basedir = path.dirname(filename);
|
||||||
const getAttr = R.curry((attr, element) => $(element).attr(attr));
|
var files = [];
|
||||||
const getStylesheet = R.pipe(getAttr('href'), resolve(basedir));
|
|
||||||
try {
|
try {
|
||||||
const links = $('link[rel="stylesheet/less"]')
|
const links = $('link[rel="stylesheet/less"]')
|
||||||
.filter((index, link) => isLocalPath($(link).attr('href')))
|
.filter((index, link) => {
|
||||||
|
const href = $(link).attr('href');
|
||||||
|
return isLocalPath(href) && !isTemplateExpression(href);
|
||||||
|
})
|
||||||
.toArray();
|
.toArray();
|
||||||
|
const getHref = element => path.resolve(basedir, $(element).attr('href'));
|
||||||
const stylesheets = R.map(getStylesheet, links);
|
const hrefs = R.map(getHref, links);
|
||||||
files = R.concat(files, stylesheets);
|
files = R.concat(files, hrefs);
|
||||||
const outputs = yield R.map(render(options), stylesheets);
|
const outputs = yield R.map(render(options, basedir), hrefs);
|
||||||
const imports = R.flatten(R.map(R.prop('imports'), outputs));
|
const imports = R.flatten(R.map(R.prop('imports'), outputs));
|
||||||
files = R.concat(files, imports);
|
files = R.concat(files, imports);
|
||||||
const styles = R.map(output => $('<style>').html(output.css), outputs);
|
const styles = R.map(output => $('<style>').html(output.css), outputs);
|
||||||
|
const replaceLink = (link, index) => $(link).replaceWith(styles[index]);
|
||||||
forEachIndexed((link, index) => $(link).replaceWith(styles[index]), links);
|
forEachIndexed(replaceLink, links);
|
||||||
|
|
||||||
files = R.uniq(files);
|
files = R.uniq(files);
|
||||||
return { $, files };
|
return { $, files };
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
{
|
{
|
||||||
"name": "inline-html",
|
"name": "inline-html",
|
||||||
"version": "0.2.2",
|
"version": "0.2.4",
|
||||||
"description": "Inline local assets referenced in an HTML document.",
|
"description": "Inline local assets referenced in an HTML document.",
|
||||||
"repository": "panosoft/inline-html",
|
"repository": "panosoft/inline-html",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=4.0.0"
|
||||||
|
},
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "mocha",
|
"test": "mocha",
|
||||||
@@ -19,6 +22,7 @@
|
|||||||
"less": "^2.5.1",
|
"less": "^2.5.1",
|
||||||
"mz": "^2.0.0",
|
"mz": "^2.0.0",
|
||||||
"postcss": "^5.0.12",
|
"postcss": "^5.0.12",
|
||||||
|
"postcss-import": "^7.1.3",
|
||||||
"postcss-url": "^5.0.0",
|
"postcss-url": "^5.0.0",
|
||||||
"ramda": "^0.18.0",
|
"ramda": "^0.18.0",
|
||||||
"string": "^3.3.0"
|
"string": "^3.3.0"
|
||||||
|
|||||||
1
test/fixtures/basic.css
vendored
1
test/fixtures/basic.css
vendored
@@ -1 +1,2 @@
|
|||||||
|
/* basic.css */
|
||||||
div { color: blue; }
|
div { color: blue; }
|
||||||
|
|||||||
1
test/fixtures/basic.less
vendored
1
test/fixtures/basic.less
vendored
@@ -1 +1,2 @@
|
|||||||
|
/* basic.less */
|
||||||
div { color: blue; }
|
div { color: blue; }
|
||||||
|
|||||||
1
test/fixtures/import.css
vendored
Normal file
1
test/fixtures/import.css
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
@import 'basic.css';
|
||||||
1
test/fixtures/invalid-import.css
vendored
Normal file
1
test/fixtures/invalid-import.css
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
@import 'missing.css';
|
||||||
1
test/fixtures/invalid-import.less
vendored
Normal file
1
test/fixtures/invalid-import.less
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
@import 'missing.less';
|
||||||
1
test/fixtures/invalid-syntax.less
vendored
Normal file
1
test/fixtures/invalid-syntax.less
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
div {
|
||||||
1
test/fixtures/invalid-url.css
vendored
Normal file
1
test/fixtures/invalid-url.css
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
div { background-image: url('missing.png'); }
|
||||||
1
test/fixtures/invalid-url.less
vendored
Normal file
1
test/fixtures/invalid-url.less
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
div { background-image: url('missing.png'); }
|
||||||
1
test/fixtures/invalidImport.less
vendored
1
test/fixtures/invalidImport.less
vendored
@@ -1 +0,0 @@
|
|||||||
@import (less) 'missing.css';
|
|
||||||
3
test/fixtures/invalidUrl.less
vendored
3
test/fixtures/invalidUrl.less
vendored
@@ -1,3 +0,0 @@
|
|||||||
div {
|
|
||||||
background-image: url('missing.png');
|
|
||||||
}
|
|
||||||
1
test/fixtures/nested-import.css
vendored
Normal file
1
test/fixtures/nested-import.css
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
@import 'import.css';
|
||||||
1
test/fixtures/nested-import.less
vendored
Normal file
1
test/fixtures/nested-import.less
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
@import 'import.less';
|
||||||
1
test/fixtures/url.css
vendored
Normal file
1
test/fixtures/url.css
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
div { background-image: url('file.txt'); }
|
||||||
1
test/fixtures/url.less
vendored
Normal file
1
test/fixtures/url.less
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
div { background-image: url('file.txt'); }
|
||||||
409
test/index.js
409
test/index.js
@@ -1,11 +1,11 @@
|
|||||||
var co = require('co');
|
const co = require('co');
|
||||||
var datauri = require('datauri');
|
const datauri = require('datauri');
|
||||||
var expect = require('chai')
|
const expect = require('chai')
|
||||||
.use(require('chai-as-promised'))
|
.use(require('chai-as-promised'))
|
||||||
.expect;
|
.expect;
|
||||||
var fs = require('fs');
|
const fs = require('fs');
|
||||||
var inline = require('../lib');
|
const inline = require('../lib');
|
||||||
var path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
describe('inline-html', () => {
|
describe('inline-html', () => {
|
||||||
|
|
||||||
@@ -13,19 +13,19 @@ describe('inline-html', () => {
|
|||||||
it('exists', () => expect(inline).to.have.property('file').that.is.a('function'));
|
it('exists', () => expect(inline).to.have.property('file').that.is.a('function'));
|
||||||
|
|
||||||
it('accept html filename', () => {
|
it('accept html filename', () => {
|
||||||
var filename = path.resolve(__dirname, './fixtures/file.txt');
|
const filename = path.resolve(__dirname, './fixtures/file.txt');
|
||||||
var content = fs.readFileSync(filename, 'utf8');
|
const content = fs.readFileSync(filename, 'utf8');
|
||||||
return expect(inline.file(filename)).to.eventually.equal(content);
|
return expect(inline.file(filename)).to.eventually.equal(content);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('options.filename: always reset to current filename and used for css url path resolution', () => {
|
it('options.filename: always reset to current filename and used for css url path resolution', () => {
|
||||||
var filename = path.resolve(__dirname, 'fixtures/css-url.html'); // file contains url path relative to itself
|
const filename = path.resolve(__dirname, 'fixtures/css-url.html'); // file contains url path relative to itself
|
||||||
var options = { filename: __filename }; // sets filename relative to this test file
|
const options = { filename: __filename }; // sets filename relative to this test file
|
||||||
return expect(inline.file(filename, options)).to.eventually.match(/"data:.*,.*"/);
|
return expect(inline.file(filename, options)).to.eventually.match(/"data:.*,.*"/);
|
||||||
});
|
});
|
||||||
it('options.filename: always reset to current filename and used for img src path resolution', () => {
|
it('options.filename: always reset to current filename and used for img src path resolution', () => {
|
||||||
var filename = path.resolve(__dirname, 'fixtures/img.html'); // file contains src relative to itself
|
const filename = path.resolve(__dirname, 'fixtures/img.html'); // file contains src relative to itself
|
||||||
var options = { filename: __filename }; // sets filename relative to this test file
|
const options = { filename: __filename }; // sets filename relative to this test file
|
||||||
return expect(inline.file(filename, options)).to.eventually.match(/"data:.*,.*"/);
|
return expect(inline.file(filename, options)).to.eventually.match(/"data:.*,.*"/);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -36,72 +36,72 @@ describe('inline-html', () => {
|
|||||||
it('exists', () => expect(inline).to.have.property('html').that.is.a('function'));
|
it('exists', () => expect(inline).to.have.property('html').that.is.a('function'));
|
||||||
|
|
||||||
it('accept html string', () => {
|
it('accept html string', () => {
|
||||||
var html = 'HTML';
|
const html = 'HTML';
|
||||||
return expect(inline.html(html)).to.eventually.equal(html);
|
return expect(inline.html(html)).to.eventually.equal(html);
|
||||||
});
|
});
|
||||||
it('preserve self closing tags', () => {
|
it('preserve self closing tags', () => {
|
||||||
var html = '<br/>';
|
const html = '<br/>';
|
||||||
return expect(inline.html(html)).to.eventually.equal(html);
|
return expect(inline.html(html)).to.eventually.equal(html);
|
||||||
});
|
});
|
||||||
it('preserve partials', () => {
|
it('preserve partials', () => {
|
||||||
var html = '{{> partial}}';
|
const html = '{{> partial}}';
|
||||||
return expect(inline.html(html)).to.eventually.equal(html);
|
return expect(inline.html(html)).to.eventually.equal(html);
|
||||||
});
|
});
|
||||||
it('preserve helpers', () => {
|
it('preserve helpers', () => {
|
||||||
var html = '{{helper}}';
|
const html = '{{helper}}';
|
||||||
return expect(inline.html(html)).to.eventually.equal(html);
|
return expect(inline.html(html)).to.eventually.equal(html);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('options.filename: default to cwd for css url path resolution', () => {
|
it('options.filename: default to cwd for css url path resolution', () => {
|
||||||
var url = 'test/fixtures/file.txt'; // Note: this is relative to cwd
|
const url = 'test/fixtures/file.txt'; // Note: this is relative to cwd
|
||||||
var uri = datauri(url);
|
const uri = datauri(url);
|
||||||
|
|
||||||
var html = (path) => `<style>div { background-image: url('${path}'); }</style>`;
|
const html = (path) => `<style>div { background-image: url('${path}'); }</style>`;
|
||||||
return expect(inline.html(html(url))).to.eventually.equal(html(uri));
|
return expect(inline.html(html(url))).to.eventually.equal(html(uri));
|
||||||
});
|
});
|
||||||
it('options.filename: set basepath for css url path resolution', () => {
|
it('options.filename: set basepath for css url path resolution', () => {
|
||||||
var filename = path.resolve(__dirname, 'fixtures/fake.html');
|
const filename = path.resolve(__dirname, 'fixtures/fake.html');
|
||||||
var dirname = path.dirname(filename);
|
const dirname = path.dirname(filename);
|
||||||
|
|
||||||
var url = 'file.txt'; // Note: path relative to filename's dirname
|
const url = 'file.txt'; // Note: path relative to filename's dirname
|
||||||
var uri = datauri(path.resolve(dirname, url));
|
const uri = datauri(path.resolve(dirname, url));
|
||||||
|
|
||||||
var html = (path) => `<style>div { background-image: url('${path}'); }</style>`;
|
const html = (path) => `<style>div { background-image: url('${path}'); }</style>`;
|
||||||
var options = { filename: filename };
|
const options = { filename: filename };
|
||||||
return expect(inline.html(html(url), options)).to.eventually.equal(html(uri));
|
return expect(inline.html(html(url), options)).to.eventually.equal(html(uri));
|
||||||
});
|
});
|
||||||
|
|
||||||
it('options.filename: default to cwd for img src path resolution', () => {
|
it('options.filename: default to cwd for img src path resolution', () => {
|
||||||
var url = 'test/fixtures/file.txt'; // Note: path relative to cwd
|
const url = 'test/fixtures/file.txt'; // Note: path relative to cwd
|
||||||
var uri = datauri(url);
|
const uri = datauri(url);
|
||||||
|
|
||||||
var html = (path) => `<img src="${path}"/>`;
|
const html = (path) => `<img src="${path}"/>`;
|
||||||
return expect(inline.html(html(url))).to.eventually.equal(html(uri));
|
return expect(inline.html(html(url))).to.eventually.equal(html(uri));
|
||||||
});
|
});
|
||||||
it('options.filename: set basepath for img src path resolution', () => {
|
it('options.filename: set basepath for img src path resolution', () => {
|
||||||
var filename = path.resolve(__dirname, 'fixtures/fake.html');
|
const filename = path.resolve(__dirname, 'fixtures/fake.html');
|
||||||
var dirname = path.dirname(filename);
|
const dirname = path.dirname(filename);
|
||||||
|
|
||||||
var url = 'file.txt'; // Note: path relative to filename's dirname
|
const url = 'file.txt'; // Note: path relative to filename's dirname
|
||||||
var uri = datauri(path.resolve(dirname, url));
|
const uri = datauri(path.resolve(dirname, url));
|
||||||
|
|
||||||
var html = (path) => `<img src="${path}"/>`;
|
const html = (path) => `<img src="${path}"/>`;
|
||||||
var options = { filename: filename };
|
const options = { filename: filename };
|
||||||
return expect(inline.html(html(url), options)).to.eventually.equal(html(uri));
|
return expect(inline.html(html(url), options)).to.eventually.equal(html(uri));
|
||||||
});
|
});
|
||||||
|
|
||||||
it('options.filename: included in results.files for img src if options.verbose true', () => {
|
it('options.filename: included in results.files for img src if options.verbose true', () => {
|
||||||
var filename = path.resolve(__dirname, 'fixtures/file.txt');
|
const filename = path.resolve(__dirname, 'fixtures/file.txt');
|
||||||
var html = `<img src="${filename}"/>`;
|
const html = `<img src="${filename}"/>`;
|
||||||
var options = { verbose: true };
|
const options = { verbose: true };
|
||||||
return expect(inline.html(html, options)).to.eventually.have.property('files')
|
return expect(inline.html(html, options)).to.eventually.have.property('files')
|
||||||
.that.is.an('array')
|
.that.is.an('array')
|
||||||
.that.contains(filename);
|
.that.contains(filename);
|
||||||
});
|
});
|
||||||
it('options.filename: included in results.files for css url path if options.verbose true', () => {
|
it('options.filename: included in results.files for css url path if options.verbose true', () => {
|
||||||
var filename = path.resolve(__dirname, 'fixtures/file.txt');
|
const filename = path.resolve(__dirname, 'fixtures/file.txt');
|
||||||
var html = `<style>div { background-image: url('${filename}'); }</style>`;
|
const html = `<style>div { background-image: url('${filename}'); }</style>`;
|
||||||
var options = { verbose: true };
|
const options = { verbose: true };
|
||||||
return expect(inline.html(html, options)).to.eventually.have.property('files')
|
return expect(inline.html(html, options)).to.eventually.have.property('files')
|
||||||
.that.is.an('array')
|
.that.is.an('array')
|
||||||
.that.contains(filename);
|
.that.contains(filename);
|
||||||
@@ -109,60 +109,60 @@ describe('inline-html', () => {
|
|||||||
|
|
||||||
|
|
||||||
it('options.verbose: return results object if true', () => {
|
it('options.verbose: return results object if true', () => {
|
||||||
var filename = path.resolve(__dirname, 'fixtures/file.txt');
|
const filename = path.resolve(__dirname, 'fixtures/file.txt');
|
||||||
var html = `<img src="${filename}"/>`;
|
const html = `<img src="${filename}"/>`;
|
||||||
var options = { verbose: true };
|
const options = { verbose: true };
|
||||||
return expect(inline.html(html, options)).to.eventually.be.an('object')
|
return expect(inline.html(html, options)).to.eventually.be.an('object')
|
||||||
.that.contains.keys(['html', 'files']);
|
.that.contains.keys(['html', 'files']);
|
||||||
});
|
});
|
||||||
it('options.verbose: return html if false', () => {
|
it('options.verbose: return html if false', () => {
|
||||||
var filename = path.resolve(__dirname, 'fixtures/file.txt');
|
const filename = path.resolve(__dirname, 'fixtures/file.txt');
|
||||||
var html = `<img src="${filename}"/>`;
|
const html = `<img src="${filename}"/>`;
|
||||||
var options = { verbose: false };
|
const options = { verbose: false };
|
||||||
return expect(inline.html(html, options)).to.eventually.be.a('string');
|
return expect(inline.html(html, options)).to.eventually.be.a('string');
|
||||||
});
|
});
|
||||||
it('options.verbose: default false', () => {
|
it('options.verbose: default false', () => {
|
||||||
var filename = path.resolve(__dirname, 'fixtures/file.txt');
|
const filename = path.resolve(__dirname, 'fixtures/file.txt');
|
||||||
var html = `<img src="${filename}"/>`;
|
const html = `<img src="${filename}"/>`;
|
||||||
return expect(inline.html(html)).to.eventually.be.a('string');
|
return expect(inline.html(html)).to.eventually.be.a('string');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
describe('css-url', () => {
|
describe('css-url', () => {
|
||||||
it('inline local url in style element', () => {
|
it('inline local url in style element', () => {
|
||||||
var filename = path.resolve(__dirname, 'fixtures/file.txt');
|
const filename = path.resolve(__dirname, 'fixtures/file.txt');
|
||||||
var html = `<style>div {background-image: url("${filename}");}</style>`;
|
const html = `<style>div {background-image: url("${filename}");}</style>`;
|
||||||
return expect(inline.html(html)).to.eventually.match(/data:.*,.*/);
|
return expect(inline.html(html)).to.eventually.match(/data:.*,.*/);
|
||||||
});
|
});
|
||||||
it('inline local url in style attribute', () => {
|
it('inline local url in style attribute', () => {
|
||||||
var filename = path.resolve(__dirname, 'fixtures/file.txt');
|
const filename = path.resolve(__dirname, 'fixtures/file.txt');
|
||||||
var html = `<div style="background-image: url('${filename}');"></div>`;
|
const html = `<div style="background-image: url('${filename}');"></div>`;
|
||||||
return expect(inline.html(html)).to.eventually.match(/data:.*,.*/);
|
return expect(inline.html(html)).to.eventually.match(/data:.*,.*/);
|
||||||
});
|
});
|
||||||
it('ignore remote url', () => {
|
it('ignore remote url', () => {
|
||||||
var html = `<style> div { background-image: url('http://test.com/file.txt?query=string#hash'); }</style>`;
|
const html = `<style> div { background-image: url('http://test.com/file.txt?query=string#hash'); }</style>`;
|
||||||
return expect(inline.html(html)).to.eventually.equal(html);
|
return expect(inline.html(html)).to.eventually.equal(html);
|
||||||
});
|
});
|
||||||
it('ignore template expression url', () => {
|
it('ignore template expression url', () => {
|
||||||
var html = `<style> div { background-image: url({{path}}); }</style>`;
|
const html = `<style> div { background-image: url({{path}}); }</style>`;
|
||||||
return expect(inline.html(html)).to.eventually.equal(html);
|
return expect(inline.html(html)).to.eventually.equal(html);
|
||||||
});
|
});
|
||||||
it('ignore url query strings and hashes', () => {
|
it('ignore url query strings and hashes', () => {
|
||||||
var filename = path.resolve(__dirname, 'fixtures/file.txt');
|
const filename = path.resolve(__dirname, 'fixtures/file.txt');
|
||||||
var url = `${filename}?query=string#hash`;
|
const url = `${filename}?query=string#hash`;
|
||||||
var uri = datauri(filename);
|
const uri = datauri(filename);
|
||||||
var html = (source) => `<style> div { background-image: url('${source}'); }</style>`;
|
const html = (source) => `<style> div { background-image: url('${source}'); }</style>`;
|
||||||
return expect(inline.html(html(url))).to.eventually.equal(html(uri));
|
return expect(inline.html(html(url))).to.eventually.equal(html(uri));
|
||||||
});
|
});
|
||||||
it('handle url with spaces', () => {
|
it('handle url with spaces', () => {
|
||||||
var filename = path.resolve(__dirname, 'fixtures/file space.txt');
|
const filename = path.resolve(__dirname, 'fixtures/file space.txt');
|
||||||
var uri = datauri(filename);
|
const uri = datauri(filename);
|
||||||
var html = (source) => `<style> div { background-image: url('${source}'); }</style>`;
|
const html = (source) => `<style> div { background-image: url('${source}'); }</style>`;
|
||||||
return expect(inline.html(html(filename))).to.eventually.equal(html(uri));
|
return expect(inline.html(html(filename))).to.eventually.equal(html(uri));
|
||||||
});
|
});
|
||||||
it('throw when syntax invalid in style element', () => co(function * () {
|
it('throw when syntax invalid in style element', () => co(function * () {
|
||||||
var filename = path.resolve(__dirname, 'index.html');
|
const filename = path.resolve(__dirname, 'index.html');
|
||||||
var html = `<style>div {</style>`;
|
const html = `<style>div {</style>`;
|
||||||
try {
|
try {
|
||||||
yield inline.html(html, {filename});
|
yield inline.html(html, {filename});
|
||||||
throw new Error('No error thrown');
|
throw new Error('No error thrown');
|
||||||
@@ -173,8 +173,8 @@ describe('inline-html', () => {
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
it('throw when syntax invalid in style attribute', () => co(function * () {
|
it('throw when syntax invalid in style attribute', () => co(function * () {
|
||||||
var filename = path.resolve(__dirname, 'index.html');
|
const filename = path.resolve(__dirname, 'index.html');
|
||||||
var html = `<div style="background url()"></div>`;
|
const html = `<div style="background url()"></div>`;
|
||||||
try {
|
try {
|
||||||
yield inline.html(html, {filename});
|
yield inline.html(html, {filename});
|
||||||
throw new Error('No error thrown');
|
throw new Error('No error thrown');
|
||||||
@@ -185,10 +185,10 @@ describe('inline-html', () => {
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
it('throw when url invalid in style element', () => co(function * () {
|
it('throw when url invalid in style element', () => co(function * () {
|
||||||
var filename = path.resolve(__dirname, 'index.html');
|
const filename = path.resolve(__dirname, 'index.html');
|
||||||
var url = 'missing.png';
|
const url = 'missing.png';
|
||||||
var resolvedUrl = path.resolve(path.dirname(filename), url);
|
const resolvedUrl = path.resolve(path.dirname(filename), url);
|
||||||
var html = `<style>div { background-image: url('${url}'); }</style>`;
|
const html = `<style>div { background-image: url('${url}'); }</style>`;
|
||||||
try {
|
try {
|
||||||
yield inline.html(html, {filename});
|
yield inline.html(html, {filename});
|
||||||
throw new Error('No error thrown');
|
throw new Error('No error thrown');
|
||||||
@@ -199,10 +199,10 @@ describe('inline-html', () => {
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
it('throw when url invalid in style attribute ', () => co(function * () {
|
it('throw when url invalid in style attribute ', () => co(function * () {
|
||||||
var filename = path.resolve(__dirname, 'index.html');
|
const filename = path.resolve(__dirname, 'index.html');
|
||||||
var url = 'missing.png';
|
const url = 'missing.png';
|
||||||
var resolvedUrl = path.resolve(path.dirname(filename), url);
|
const resolvedUrl = path.resolve(path.dirname(filename), url);
|
||||||
var html = `<div style="background-image: url('${url}')"></div>`;
|
const html = `<div style="background-image: url('${url}')"></div>`;
|
||||||
try {
|
try {
|
||||||
yield inline.html(html, {filename});
|
yield inline.html(html, {filename});
|
||||||
throw new Error('No error thrown');
|
throw new Error('No error thrown');
|
||||||
@@ -213,12 +213,12 @@ describe('inline-html', () => {
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
it('include all urls in error.files up until and including invalid url in style element', () => co(function * () {
|
it('include all urls in error.files up until and including invalid url in style element', () => co(function * () {
|
||||||
var filename = path.resolve(__dirname, 'index.html');
|
const filename = path.resolve(__dirname, 'index.html');
|
||||||
var validUrl = 'fixtures/file.txt';
|
const validUrl = 'fixtures/file.txt';
|
||||||
var invalidUrl = 'missing.png';
|
const invalidUrl = 'missing.png';
|
||||||
var resolvedInvalidUrl = path.resolve(path.dirname(filename), invalidUrl);
|
const resolvedInvalidUrl = path.resolve(path.dirname(filename), invalidUrl);
|
||||||
var resolvedValidUrl = path.resolve(path.dirname(filename), validUrl);
|
const resolvedValidUrl = path.resolve(path.dirname(filename), validUrl);
|
||||||
var html = `
|
const html = `
|
||||||
<style>div {background-image: url('${validUrl}');}</style>
|
<style>div {background-image: url('${validUrl}');}</style>
|
||||||
<style>div {background-image: url('${invalidUrl}');}</style>
|
<style>div {background-image: url('${invalidUrl}');}</style>
|
||||||
`;
|
`;
|
||||||
@@ -233,12 +233,12 @@ describe('inline-html', () => {
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
it('include all urls in error.files up until and including invalid url in style attribute', () => co(function * () {
|
it('include all urls in error.files up until and including invalid url in style attribute', () => co(function * () {
|
||||||
var filename = path.resolve(__dirname, 'index.html');
|
const filename = path.resolve(__dirname, 'index.html');
|
||||||
var validUrl = 'fixtures/file.txt';
|
const validUrl = 'fixtures/file.txt';
|
||||||
var invalidUrl = 'missing.png';
|
const invalidUrl = 'missing.png';
|
||||||
var resolvedInvalidUrl = path.resolve(path.dirname(filename), invalidUrl);
|
const resolvedInvalidUrl = path.resolve(path.dirname(filename), invalidUrl);
|
||||||
var resolvedValidUrl = path.resolve(path.dirname(filename), validUrl);
|
const resolvedValidUrl = path.resolve(path.dirname(filename), validUrl);
|
||||||
var html = `
|
const html = `
|
||||||
<div style="background-image: url('${validUrl}')"></div>
|
<div style="background-image: url('${validUrl}')"></div>
|
||||||
<div style="background-image: url('${invalidUrl}')"></div>
|
<div style="background-image: url('${invalidUrl}')"></div>
|
||||||
`;
|
`;
|
||||||
@@ -253,12 +253,12 @@ describe('inline-html', () => {
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
it('include all urls in error.files up until and including invalid url when style element valid and style attribute invalid', () => co(function * () {
|
it('include all urls in error.files up until and including invalid url when style element valid and style attribute invalid', () => co(function * () {
|
||||||
var filename = path.resolve(__dirname, 'index.html');
|
const filename = path.resolve(__dirname, 'index.html');
|
||||||
var validUrl = 'fixtures/file.txt';
|
const validUrl = 'fixtures/file.txt';
|
||||||
var invalidUrl = 'missing.png';
|
const invalidUrl = 'missing.png';
|
||||||
var resolvedInvalidUrl = path.resolve(path.dirname(filename), invalidUrl);
|
const resolvedInvalidUrl = path.resolve(path.dirname(filename), invalidUrl);
|
||||||
var resolvedValidUrl = path.resolve(path.dirname(filename), validUrl);
|
const resolvedValidUrl = path.resolve(path.dirname(filename), validUrl);
|
||||||
var html = `
|
const html = `
|
||||||
<style>div {background-image: url("${validUrl}");}</style>
|
<style>div {background-image: url("${validUrl}");}</style>
|
||||||
<div style="background-image: url('${invalidUrl}')"></div>
|
<div style="background-image: url('${invalidUrl}')"></div>
|
||||||
`;
|
`;
|
||||||
@@ -275,24 +275,24 @@ describe('inline-html', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('img', () => {
|
describe('img', () => {
|
||||||
it('inline img src', () => {
|
it('inline local source', () => {
|
||||||
var filename = path.resolve(__dirname, 'fixtures/file.txt');
|
const filename = path.resolve(__dirname, 'fixtures/file.txt');
|
||||||
var html = `<img src="${filename}"/>`;
|
const html = `<img src="${filename}"/>`;
|
||||||
return expect(inline.html(html)).to.eventually.match(/data:.*,.*/);
|
return expect(inline.html(html)).to.eventually.match(/data:.*,.*/);
|
||||||
});
|
});
|
||||||
it('ignore img src remote paths', () => {
|
it('ignore remote source', () => {
|
||||||
var html = `<img src="http://test.com/file.txt?query=string#hash"/>`;
|
const html = `<img src="http://test.com/file.txt?query=string#hash"/>`;
|
||||||
return expect(inline.html(html)).to.eventually.equal(html);
|
return expect(inline.html(html)).to.eventually.equal(html);
|
||||||
});
|
});
|
||||||
it('ignore img src template expression paths', () => {
|
it('ignore template expression source', () => {
|
||||||
var html = `<img src="{{path}}"/>`;
|
const html = `<img src="{{path}}"/>`;
|
||||||
return expect(inline.html(html)).to.eventually.equal(html);
|
return expect(inline.html(html)).to.eventually.equal(html);
|
||||||
});
|
});
|
||||||
it('throw when src invalid', () => co(function * () {
|
it('throw when src invalid', () => co(function * () {
|
||||||
var filename = path.resolve(__dirname, 'index.html');
|
const filename = path.resolve(__dirname, 'index.html');
|
||||||
var source = 'missing.png';
|
const source = 'missing.png';
|
||||||
var html = `<img src="${source}" >`;
|
const html = `<img src="${source}" >`;
|
||||||
var resolvedSource = path.resolve(path.dirname(filename), source);
|
const resolvedSource = path.resolve(path.dirname(filename), source);
|
||||||
try {
|
try {
|
||||||
yield inline.html(html, {filename});
|
yield inline.html(html, {filename});
|
||||||
throw new Error('No error thrown');
|
throw new Error('No error thrown');
|
||||||
@@ -303,12 +303,12 @@ describe('inline-html', () => {
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
it('include all sources in error.files up until and including invalid source', () => co(function * () {
|
it('include all sources in error.files up until and including invalid source', () => co(function * () {
|
||||||
var filename = path.resolve(__dirname, 'index.html');
|
const filename = path.resolve(__dirname, 'index.html');
|
||||||
var valid = 'fixtures/file.txt';
|
const valid = 'fixtures/file.txt';
|
||||||
var invalid = 'missing.png';
|
const invalid = 'missing.png';
|
||||||
var resolvedInvalid = path.resolve(path.dirname(filename), invalid);
|
const resolvedInvalid = path.resolve(path.dirname(filename), invalid);
|
||||||
var resolvedValid = path.resolve(path.dirname(filename), valid);
|
const resolvedValid = path.resolve(path.dirname(filename), valid);
|
||||||
var html = `
|
const html = `
|
||||||
<img src="${valid}">
|
<img src="${valid}">
|
||||||
<img src="${invalid}">
|
<img src="${invalid}">
|
||||||
`;
|
`;
|
||||||
@@ -324,23 +324,126 @@ describe('inline-html', () => {
|
|||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('link-css', () => {
|
||||||
|
const filename = path.resolve(__dirname, 'index.html');
|
||||||
|
const link = (href) => `<link rel="stylesheet" href="${href}"/>`;
|
||||||
|
it('inline local href', () => co(function * () {
|
||||||
|
const href = path.resolve(__dirname, 'fixtures/basic.css');
|
||||||
|
const html = link(href);
|
||||||
|
const result = yield inline.html(html);
|
||||||
|
expect(result).to.match(/<style>[^]*basic.css[^]*<\/style>/);
|
||||||
|
}));
|
||||||
|
it('ignore remote href', () => co(function * () {
|
||||||
|
const html = link('http://test.com/main.less');
|
||||||
|
const result = yield inline.html(html);
|
||||||
|
expect(result).to.equal(html);
|
||||||
|
}));
|
||||||
|
it('ignore template expression href', () => co(function * () {
|
||||||
|
const html = link('{{href}}');
|
||||||
|
const result = yield inline.html(html);
|
||||||
|
expect(result).to.equal(html);
|
||||||
|
}));
|
||||||
|
it('inline local nested imports', () => co(function * () {
|
||||||
|
const href = path.resolve(__dirname, 'fixtures/nested-import.css');
|
||||||
|
const html = link(href);
|
||||||
|
const result = yield inline.html(html);
|
||||||
|
return expect(result).to.match(/<style>[^]*basic.css[^]*<\/style>/)
|
||||||
|
.and.not.match(/@import/);
|
||||||
|
}));
|
||||||
|
it('rebase urls relative to html filename', () => co(function * () {
|
||||||
|
const href = 'fixtures/url.css';
|
||||||
|
const html = link(href);
|
||||||
|
const result = yield inline.html(html, { filename });
|
||||||
|
expect(result).to.match(/<style>[^]*url\('data:.*,.*'\)[^]*<\/style>/);
|
||||||
|
}));
|
||||||
|
it('throw error when href invalid', () => co(function * () {
|
||||||
|
const invalid = 'fixtures/missing.css';
|
||||||
|
const invalidResolved = path.resolve(path.dirname(filename), invalid);
|
||||||
|
const html = link(invalid);
|
||||||
|
try {
|
||||||
|
yield inline.html(html, { filename });
|
||||||
|
throw new Error('No error thrown');
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
expect(error).to.have.property('filename').that.equals(filename);
|
||||||
|
expect(error).to.have.property('files').that.contains(invalidResolved);
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
it('throw error when import path invalid', () => co(function * () {
|
||||||
|
const invalid = 'fixtures/invalid-import.css';
|
||||||
|
const invalidResolved = path.resolve(path.dirname(filename), invalid);
|
||||||
|
const html = link(invalid);
|
||||||
|
try {
|
||||||
|
yield inline.html(html, { filename });
|
||||||
|
throw new Error('No error thrown');
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
expect(error).to.have.property('filename').that.equals(invalidResolved);
|
||||||
|
expect(error).to.have.property('files').that.contains(invalidResolved);
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
it('throw error when css syntax invalid', () => co(function * () {
|
||||||
|
const invalid = 'fixtures/invalid-syntax.css';
|
||||||
|
const invalidResolved = path.resolve(path.dirname(filename), invalid);
|
||||||
|
const html = link(invalid);
|
||||||
|
try {
|
||||||
|
yield inline.html(html, { filename });
|
||||||
|
throw new Error('No error thrown');
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
expect(error).to.have.property('filename').that.equals(invalidResolved);
|
||||||
|
expect(error).to.have.property('files').that.contains(invalidResolved);
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
it('include all local hrefs in error.files when error encountered', () => co(function * () {
|
||||||
|
const valid = 'fixtures/basic.css';
|
||||||
|
const invalid = 'fixtures/missing.css';
|
||||||
|
const validResolved = path.resolve(path.dirname(filename), valid);
|
||||||
|
const invalidResolved = path.resolve(path.dirname(filename), invalid);
|
||||||
|
const html = `${link(invalid)}${link(valid)}`;
|
||||||
|
try {
|
||||||
|
yield inline.html(html, { filename });
|
||||||
|
throw new Error('No error thrown');
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
expect(error).to.have.property('filename').that.equals(filename);
|
||||||
|
expect(error).to.have.property('files').that.contains(validResolved);
|
||||||
|
expect(error).to.have.property('files').that.contains(invalidResolved);
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
describe('link-less', () => {
|
describe('link-less', () => {
|
||||||
it('inline link less', () => {
|
it('inline local href', () => {
|
||||||
var filename = path.resolve(__dirname, 'fixtures/basic.less');
|
const filename = path.resolve(__dirname, 'fixtures/basic.less');
|
||||||
var html = `<link rel="stylesheet/less" href="${filename}"/>`;
|
const html = `<link rel="stylesheet/less" href="${filename}"/>`;
|
||||||
return expect(inline.html(html)).to.eventually.match(/<style>[^]*<\/style>/);
|
return expect(inline.html(html)).to.eventually.match(/<style>[^]*basic.less[^]*<\/style>/);
|
||||||
});
|
});
|
||||||
it('inline link less imports', () => {
|
it('ignore remote href', () => {
|
||||||
var filename = path.resolve(__dirname, 'fixtures/import.less');
|
const html = `<link rel="stylesheet/less" href="http://test.com/main.less"/>`;
|
||||||
var html = `<link rel="stylesheet/less" href="${filename}"/>`;
|
return expect(inline.html(html)).to.eventually.equal(html);
|
||||||
return expect(inline.html(html)).to.eventually.match(/<style>[^]*<\/style>/)
|
});
|
||||||
|
it('ignore template expression href', () => {
|
||||||
|
const html = `<link rel="stylesheet/less" href="{{href}}"/>`;
|
||||||
|
return expect(inline.html(html)).to.eventually.equal(html);
|
||||||
|
});
|
||||||
|
it('inline local nested imports', () => {
|
||||||
|
const filename = path.resolve(__dirname, 'fixtures/nested-import.less');
|
||||||
|
const html = `<link rel="stylesheet/less" href="${filename}"/>`;
|
||||||
|
return expect(inline.html(html)).to.eventually.match(/<style>[^]*basic.less[^]*basic.css[^]*<\/style>/)
|
||||||
.and.not.match(/@import/);
|
.and.not.match(/@import/);
|
||||||
});
|
});
|
||||||
|
it('rebase urls relative to html filename', () => {
|
||||||
|
const filename = path.resolve(__dirname, 'index.html');
|
||||||
|
const href = 'fixtures/url.less';
|
||||||
|
const html = `<link rel="stylesheet/less" href="${href}"/>`;
|
||||||
|
return expect(inline.html(html, { filename })).to.eventually.match(/<style>[^]*url\('data:.*,.*'\)[^]*<\/style>/);
|
||||||
|
});
|
||||||
it('throw error when link href invalid', () => co(function * () {
|
it('throw error when link href invalid', () => co(function * () {
|
||||||
var filename = path.resolve(__dirname, 'index.html');
|
const filename = path.resolve(__dirname, 'index.html');
|
||||||
var href = 'missing.less';
|
const href = 'missing.less';
|
||||||
var resolvedHref = path.resolve(path.dirname(filename), href);
|
const resolvedHref = path.resolve(path.dirname(filename), href);
|
||||||
var html = `<link rel="stylesheet/less" href="${href}">`;
|
const html = `<link rel="stylesheet/less" href="${href}">`;
|
||||||
try {
|
try {
|
||||||
yield inline.html(html, {filename});
|
yield inline.html(html, {filename});
|
||||||
throw new Error('No error thrown');
|
throw new Error('No error thrown');
|
||||||
@@ -351,10 +454,10 @@ describe('inline-html', () => {
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
it('throw error when less import invalid', () => co(function * () {
|
it('throw error when less import invalid', () => co(function * () {
|
||||||
var filename = path.resolve(__dirname, 'fixtures/index.html');
|
const filename = path.resolve(__dirname, 'fixtures/index.html');
|
||||||
var lessBasename = 'invalidImport.less';
|
const lessBasename = 'invalid-import.less';
|
||||||
var lessFilename = path.resolve(path.dirname(filename), lessBasename);
|
const lessFilename = path.resolve(path.dirname(filename), lessBasename);
|
||||||
var html = `<link rel="stylesheet/less" href="${lessBasename}">`;
|
const html = `<link rel="stylesheet/less" href="${lessBasename}">`;
|
||||||
try {
|
try {
|
||||||
yield inline.html(html, {filename});
|
yield inline.html(html, {filename});
|
||||||
throw new Error('No error thrown');
|
throw new Error('No error thrown');
|
||||||
@@ -365,10 +468,10 @@ describe('inline-html', () => {
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
it('throw error when less syntax invalid', () => co(function * () {
|
it('throw error when less syntax invalid', () => co(function * () {
|
||||||
var filename = path.resolve(__dirname, 'fixtures/index.html');
|
const filename = path.resolve(__dirname, 'fixtures/index.html');
|
||||||
var lessBasename = 'invalidSyntax.less';
|
const lessBasename = 'invalid-syntax.less';
|
||||||
var lessFilename = path.resolve(path.dirname(filename), lessBasename);
|
const lessFilename = path.resolve(path.dirname(filename), lessBasename);
|
||||||
var html = `<link rel="stylesheet/less" href="${lessBasename}">`;
|
const html = `<link rel="stylesheet/less" href="${lessBasename}">`;
|
||||||
try {
|
try {
|
||||||
yield inline.html(html, {filename});
|
yield inline.html(html, {filename});
|
||||||
throw new Error('No error thrown');
|
throw new Error('No error thrown');
|
||||||
@@ -378,44 +481,48 @@ describe('inline-html', () => {
|
|||||||
expect(error).to.have.property('files').that.contains(lessFilename);
|
expect(error).to.have.property('files').that.contains(lessFilename);
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
it('throw error when less url invalid', () => co(function * () {
|
it('include all local hrefs in error.files when error encountered', () => co(function * () {
|
||||||
var filename = path.resolve(__dirname, 'fixtures/index.html');
|
const filename = path.resolve(__dirname, 'index.html');
|
||||||
var lessBasename = 'invalidUrl.less';
|
const valid = 'fixtures/basic.css';
|
||||||
var badUrl = path.resolve(path.dirname(filename), 'missing.png');
|
const invalid = 'fixtures/missing.css';
|
||||||
var html = `<link rel="stylesheet/less" href="${lessBasename}">`;
|
const validResolved = path.resolve(path.dirname(filename), valid);
|
||||||
|
const invalidResolved = path.resolve(path.dirname(filename), invalid);
|
||||||
|
const html = `
|
||||||
|
<link rel="stylesheet/less" href="${invalid}">
|
||||||
|
<link rel="stylesheet/less" href="${valid}">
|
||||||
|
`;
|
||||||
try {
|
try {
|
||||||
yield inline.html(html, {filename});
|
yield inline.html(html, {filename});
|
||||||
throw new Error('No error thrown');
|
throw new Error('No error thrown');
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
// expect error.filename to be html file, not less file, since images
|
|
||||||
// aren't inlined until after the compiled less has been inlined into the html.
|
|
||||||
expect(error).to.have.property('filename').that.equals(filename);
|
expect(error).to.have.property('filename').that.equals(filename);
|
||||||
expect(error).to.have.property('files').that.contains(badUrl);
|
expect(error).to.have.property('files').that.contains(validResolved);
|
||||||
|
expect(error).to.have.property('files').that.contains(invalidResolved);
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('script', () => {
|
describe('script', () => {
|
||||||
it('inline local src', () => {
|
it('inline local src', () => {
|
||||||
var source = path.resolve(__dirname, 'fixtures/file.txt');
|
const source = path.resolve(__dirname, 'fixtures/file.txt');
|
||||||
var html = `<script src="${source}"></script>`;
|
const html = `<script src="${source}"></script>`;
|
||||||
var contents = fs.readFileSync(source, 'utf8');
|
const contents = fs.readFileSync(source, 'utf8');
|
||||||
return expect(inline.html(html)).to.eventually.equal(`<script>${contents}</script>`);
|
return expect(inline.html(html)).to.eventually.equal(`<script>${contents}</script>`);
|
||||||
});
|
});
|
||||||
it('ignore remote src', () => {
|
it('ignore remote src', () => {
|
||||||
var html = `<script src="http://test.com/file.txt?query=string#hash"/>`;
|
const html = `<script src="http://test.com/file.txt?query=string#hash"/>`;
|
||||||
return expect(inline.html(html)).to.eventually.equal(html);
|
return expect(inline.html(html)).to.eventually.equal(html);
|
||||||
});
|
});
|
||||||
it('ignore template expression src', () => {
|
it('ignore template expression src', () => {
|
||||||
var html = `<script src="{{path}}"/>`;
|
const html = `<script src="{{path}}"/>`;
|
||||||
return expect(inline.html(html)).to.eventually.equal(html);
|
return expect(inline.html(html)).to.eventually.equal(html);
|
||||||
});
|
});
|
||||||
it('throw when source invalid', () => co(function * () {
|
it('throw when source invalid', () => co(function * () {
|
||||||
var filename = path.resolve(__dirname, 'index.html');
|
const filename = path.resolve(__dirname, 'index.html');
|
||||||
var source = 'missing.js';
|
const source = 'missing.js';
|
||||||
var html = `<script src="${source}"></script>`;
|
const html = `<script src="${source}"></script>`;
|
||||||
var resolvedSource = path.resolve(path.dirname(filename), source);
|
const resolvedSource = path.resolve(path.dirname(filename), source);
|
||||||
try {
|
try {
|
||||||
yield inline.html(html, {filename});
|
yield inline.html(html, {filename});
|
||||||
throw new Error('No error thrown');
|
throw new Error('No error thrown');
|
||||||
@@ -428,12 +535,12 @@ describe('inline-html', () => {
|
|||||||
);
|
);
|
||||||
it('include all sources in error.files up until and including invalid source', () => {
|
it('include all sources in error.files up until and including invalid source', () => {
|
||||||
return co(function * () {
|
return co(function * () {
|
||||||
var filename = path.resolve(__dirname, 'index.html');
|
const filename = path.resolve(__dirname, 'index.html');
|
||||||
var valid = 'fixtures/file.txt';
|
const valid = 'fixtures/file.txt';
|
||||||
var invalid = 'missing.js';
|
const invalid = 'missing.js';
|
||||||
var resolvedInvalid = path.resolve(path.dirname(filename), invalid);
|
const resolvedInvalid = path.resolve(path.dirname(filename), invalid);
|
||||||
var resolvedValid = path.resolve(path.dirname(filename), valid);
|
const resolvedValid = path.resolve(path.dirname(filename), valid);
|
||||||
var html = `
|
const html = `
|
||||||
<script src="${valid}"></script>
|
<script src="${valid}"></script>
|
||||||
<script src="${invalid}"></script>
|
<script src="${invalid}"></script>
|
||||||
`;
|
`;
|
||||||
|
|||||||
Reference in New Issue
Block a user