mirror of
https://github.com/KevinMidboe/inline-html.git
synced 2025-12-08 20:29:06 +00:00
Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f143065dbf | ||
|
|
4501132051 | ||
|
|
006d059e1b | ||
|
|
0a743f6b2e | ||
|
|
7bad6838e7 | ||
|
|
0bd64b1308 | ||
|
|
251431a110 | ||
|
|
e8545de30a | ||
|
|
4a3b6f9d50 | ||
|
|
5b88484888 | ||
|
|
dff5f1160b | ||
|
|
abb3dd95a7 | ||
|
|
8f27f9d04f | ||
|
|
76ff6ca703 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,2 +1,4 @@
|
|||||||
node_modules
|
node_modules
|
||||||
|
.idea
|
||||||
|
*.log
|
||||||
test/test.html
|
test/test.html
|
||||||
@@ -1,3 +1,7 @@
|
|||||||
language: node_js
|
language: node_js
|
||||||
node_js:
|
node_js:
|
||||||
- "node"
|
- "iojs"
|
||||||
|
notifications:
|
||||||
|
email:
|
||||||
|
on_success: change
|
||||||
|
on_failure: always
|
||||||
|
|||||||
110
README.md
110
README.md
@@ -1,90 +1,106 @@
|
|||||||
# inline-html
|
# inline-html
|
||||||
|
|
||||||
Embed local assets in an HTML document.
|
Inline local assets referenced in an HTML document.
|
||||||
|
|
||||||
[]()
|
[](https://www.npmjs.com/package/inline-html)
|
||||||
[]()
|
[](https://www.npmjs.com/package/inline-html)
|
||||||
[]()
|
[](https://travis-ci.org/panosoft/inline-html)
|
||||||
[]()
|
[](https://david-dm.org/panosoft/inline-html)
|
||||||
[]()
|
[](https://www.npmjs.com/package/inline-html)
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
npm install inline-html
|
```sh
|
||||||
|
npm install inline-html
|
||||||
|
```
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
This:
|
Reads an HTML file, embeds the contents of local assets that are referenced within that file, and returns the inlined `html` string.
|
||||||
|
|
||||||
var inlineHtml = require('inline-html');
|
The following elements and data types are inlined:
|
||||||
inlineHtml('path/to/file.html').then(function (html) {
|
|
||||||
...
|
|
||||||
});
|
|
||||||
|
|
||||||
Turns this:
|
- LESS stylesheets - The LESS is compiled and the result is inlined within a `<style>` element.
|
||||||
|
- 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.
|
||||||
|
- Images - The source path is replaced with a datauri.
|
||||||
|
|
||||||
|
Assuming we have the following files:
|
||||||
|
|
||||||
|
- `index.html`
|
||||||
|
|
||||||
|
```html
|
||||||
<link rel="stylesheet/less" href="main.less"/>
|
<link rel="stylesheet/less" href="main.less"/>
|
||||||
<style>
|
<style>
|
||||||
div { background-image: url('path/to/file'); }
|
div { background-image: url('path/to/file'); }
|
||||||
</style>
|
</style>
|
||||||
<div style="background-image: url('path/to/file');"></div>
|
<div style="background-image: url('path/to/file');"></div>
|
||||||
<img src="path/to/file"/>
|
<img src="path/to/file"/>
|
||||||
|
```
|
||||||
Into this:
|
|
||||||
|
|
||||||
<style>
|
|
||||||
@font-face { src: url('data:...'); }
|
|
||||||
div { background-image: url('data:...'); }
|
|
||||||
</style>
|
|
||||||
<style>
|
|
||||||
div { background-image: url('data:...'); }
|
|
||||||
</style>
|
|
||||||
<div style="background-image: url('data:...');"></div>
|
|
||||||
<img src="data:..."/>
|
|
||||||
|
|
||||||
Where:
|
|
||||||
|
|
||||||
- `main.less`
|
- `main.less`
|
||||||
|
|
||||||
|
```css
|
||||||
@import (inline) 'main.css';
|
@import (inline) 'main.css';
|
||||||
div { background-image: url('path/to/file'); }
|
div { background-image: url('path/to/file'); }
|
||||||
|
```
|
||||||
|
|
||||||
- `main.css`
|
- `main.css`
|
||||||
|
|
||||||
|
```css
|
||||||
@font-face { src: url('path/to/file'); }
|
@font-face { src: url('path/to/file'); }
|
||||||
|
```
|
||||||
|
|
||||||
|
We can use `inline-html`:
|
||||||
|
|
||||||
|
```js
|
||||||
|
var inlineHtml = require('inline-html');
|
||||||
|
|
||||||
|
inlineHtml('index.html').then(function (html) {
|
||||||
|
// ...
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
To create the following `html` string:
|
||||||
|
|
||||||
|
```html
|
||||||
|
<style>
|
||||||
|
@font-face { src: url('data:...'); }
|
||||||
|
div { background-image: url('data:...'); }
|
||||||
|
</style>
|
||||||
|
<style>
|
||||||
|
div { background-image: url('data:...'); }
|
||||||
|
</style>
|
||||||
|
<div style="background-image: url('data:...');"></div>
|
||||||
|
<img src="data:..."/>
|
||||||
|
```
|
||||||
|
|
||||||
## API
|
## API
|
||||||
|
|
||||||
### inlineHtml( filename [, options] )
|
- [`inlineHtml`](#inlineHtml)
|
||||||
|
|
||||||
Reads an HTML file and embeds the contents of local assets referenced by the following elements and data types:
|
---
|
||||||
|
|
||||||
- LESS stylesheets - The LESS is compiled and the result is inlined within a `<style>` element.
|
<a name="inlineHtml"/>
|
||||||
|
### inlineHtml ( html [, options] )
|
||||||
|
|
||||||
<link rel="stylesheet/less" href="main.less"/> -> <style>...</style>
|
Reads an HTML file and embeds referenced local assets into the HTML.
|
||||||
|
|
||||||
- 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.
|
Returns a `Promise` that is fulfilled with an `html` string or an instance of [`Results`](#Results) depending on the value of `options.verbose`.
|
||||||
|
|
||||||
url('file.ext') -> url('data:...')
|
__Arguments__
|
||||||
|
|
||||||
- Images - The source path is replaced with a datauri.
|
- `html` - An HTML string or a filename of an HTML file to inline. Relative file paths are resolved relative to `options.filename` if a string or the filename's directory if a filename.
|
||||||
|
|
||||||
<img src="file.ext"/> -> <img src="data:..."/>
|
|
||||||
|
|
||||||
Returns a `Promise` that is fulfilled with an `html` string or a `results` object depending on the value of `options.verbose`.
|
|
||||||
|
|
||||||
#### Arguments
|
|
||||||
|
|
||||||
- `filename` - The filename of the HTML file to be inlined. Relative file paths are resolved relative to the filename directory.
|
|
||||||
- `options`
|
- `options`
|
||||||
|
- `filename` - The filename used to resolve relative paths when `html` is a string. If `html` is a string and this option is not provided, relative paths will be resolved relative to the process's current working directory.
|
||||||
- `less` - An object containing LESS options to pass to the less compiler. Defaults to `{}`.
|
- `less` - An object containing LESS options to pass to the less compiler. Defaults to `{}`.
|
||||||
- `verbose` - A boolean that determines the promises fulfillment value. Defaults to `false`.
|
- `verbose` - A boolean that determines the promises fulfillment value. Supported values are:
|
||||||
- `true`: promise is resolved with an instance of `Results`
|
- `true`: An instance of [`Results`](#Results).
|
||||||
- `false`: promise is resolved with `html`
|
- `false`: An `html` string. (_default_)
|
||||||
|
|
||||||
#### Results object
|
<a name="Results"/>
|
||||||
|
__Results__
|
||||||
|
|
||||||
The `Promise` returned by this function is optionally fulfilled with a `results` object that has the following properties:
|
The `Promise` returned by this function is optionally fulfilled with a `results` object that has the following properties:
|
||||||
|
|
||||||
- `html` - The inlined html
|
- `html` - The inlined html
|
||||||
- `files` - An array of filenames of the inlined local assets.
|
- `files` - An array of filenames for the local assets that were inlined.
|
||||||
|
|||||||
17
lib/index.js
17
lib/index.js
@@ -6,12 +6,25 @@ var inlineStyle = require('./inline-style');
|
|||||||
var inlineImg = require('./inline-img');
|
var inlineImg = require('./inline-img');
|
||||||
var inlineLinkLess = require('./inline-link-less');
|
var inlineLinkLess = require('./inline-link-less');
|
||||||
|
|
||||||
var inline = co.wrap(function * (filename, options) {
|
var inline = co.wrap(function * (html, options) {
|
||||||
options = _.defaults(options || {}, {
|
options = _.defaults(options || {}, {
|
||||||
|
filename: null,
|
||||||
less: {},
|
less: {},
|
||||||
verbose: false
|
verbose: false
|
||||||
});
|
});
|
||||||
var html = yield fs.readFile(filename, 'utf8');
|
var filename;
|
||||||
|
try {
|
||||||
|
filename = html;
|
||||||
|
html = yield fs.readFile(filename, 'utf8');
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
if (error.code === 'ENOENT') {
|
||||||
|
filename = options.filename;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
var files = [filename];
|
var files = [filename];
|
||||||
|
|
||||||
// Inline links
|
// Inline links
|
||||||
|
|||||||
@@ -1,21 +1,43 @@
|
|||||||
|
var _ = require('lodash');
|
||||||
var datauri = require('datauri');
|
var datauri = require('datauri');
|
||||||
var isLocalPath = require('is-local-path');
|
var isLocalPath = require('is-local-path');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
var postcss = require('postcss');
|
var postcss = require('postcss');
|
||||||
var url = require('postcss-url');
|
var postcssUrl = require('postcss-url');
|
||||||
|
var url = require('url');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns url path without query string and hash if present.
|
||||||
|
*
|
||||||
|
* @param path
|
||||||
|
*
|
||||||
|
* @returns path
|
||||||
|
*/
|
||||||
|
var clean = function (path) {
|
||||||
|
path = url.parse(path);
|
||||||
|
path = _.pick(path, ['protocol', 'host', 'pathname']);
|
||||||
|
return url.format(path);
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* Convert local url data type paths to datauris.
|
||||||
|
*
|
||||||
|
* @param css
|
||||||
|
* @param filename
|
||||||
|
* @returns {{css: (css|any), files: Array}}
|
||||||
|
*/
|
||||||
var inline = function (css, filename) {
|
var inline = function (css, filename) {
|
||||||
var files = [];
|
var files = [];
|
||||||
var basePath = path.dirname(filename);
|
var basePath = path.dirname(filename);
|
||||||
var result = postcss()
|
var result = postcss()
|
||||||
.use(url({
|
.use(postcssUrl({
|
||||||
url: function (url) {
|
url: function (urlPath) {
|
||||||
if (isLocalPath(url)) {
|
if (isLocalPath(urlPath)) {
|
||||||
url = path.resolve(basePath, url);
|
urlPath = clean(urlPath);
|
||||||
files.push(url);
|
urlPath = path.resolve(basePath, urlPath);
|
||||||
url = datauri(url);
|
files.push(urlPath);
|
||||||
|
urlPath = datauri(urlPath);
|
||||||
}
|
}
|
||||||
return url;
|
return urlPath;
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
.process(css);
|
.process(css);
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ var inline = function (html, filename) {
|
|||||||
$(element).attr('src', src);
|
$(element).attr('src', src);
|
||||||
});
|
});
|
||||||
return {
|
return {
|
||||||
html: $.html(),
|
html: $.xml(),
|
||||||
files: files
|
files: files
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ var inline = co.wrap(function * (html, filename, options) {
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
return {
|
return {
|
||||||
html: $.html(),
|
html: $.xml(),
|
||||||
files: files
|
files: files
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ var inline = function (html, filename) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
html: $.html(),
|
html: $.xml(),
|
||||||
files: files
|
files: files
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
13
package.json
13
package.json
@@ -1,11 +1,11 @@
|
|||||||
{
|
{
|
||||||
"name": "inline-html",
|
"name": "inline-html",
|
||||||
"version": "0.1.4",
|
"version": "0.1.9",
|
||||||
"description": "Embed local assets in an HTML document.",
|
"description": "Inline local assets referenced in an HTML document.",
|
||||||
"repository": "panosoft/inline-html",
|
"repository": "panosoft/inline-html",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "mocha --harmony_arrow_functions"
|
||||||
},
|
},
|
||||||
"author": "",
|
"author": "",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
@@ -17,8 +17,13 @@
|
|||||||
"less": "^2.5.1",
|
"less": "^2.5.1",
|
||||||
"lodash": "^3.10.0",
|
"lodash": "^3.10.0",
|
||||||
"mz": "^2.0.0",
|
"mz": "^2.0.0",
|
||||||
"postcss": "^4.1.16",
|
"postcss": "^5.0.0",
|
||||||
"postcss-url": "^4.0.0",
|
"postcss-url": "^4.0.0",
|
||||||
"string": "^3.3.0"
|
"string": "^3.3.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"chai": "^3.2.0",
|
||||||
|
"chai-as-promised": "^5.1.0",
|
||||||
|
"mocha": "^2.2.5"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
8
test/fixtures/assets/imported.css
vendored
8
test/fixtures/assets/imported.css
vendored
@@ -1,8 +0,0 @@
|
|||||||
body {
|
|
||||||
border: solid 4px green;
|
|
||||||
}
|
|
||||||
#import {
|
|
||||||
background-image: url('./person.png');
|
|
||||||
background-size: contain;
|
|
||||||
background-repeat: no-repeat;
|
|
||||||
}
|
|
||||||
BIN
test/fixtures/assets/person.png
vendored
BIN
test/fixtures/assets/person.png
vendored
Binary file not shown.
|
Before Width: | Height: | Size: 5.7 KiB |
1
test/fixtures/basic.css
vendored
Normal file
1
test/fixtures/basic.css
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
div { color: blue; }
|
||||||
1
test/fixtures/basic.less
vendored
Normal file
1
test/fixtures/basic.less
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
div { color: blue; }
|
||||||
1
test/fixtures/css-url.html
vendored
Normal file
1
test/fixtures/css-url.html
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<style>div { background-image: url("file.txt"); }</style>
|
||||||
1
test/fixtures/file.txt
vendored
Normal file
1
test/fixtures/file.txt
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
Test.
|
||||||
1
test/fixtures/img.html
vendored
Normal file
1
test/fixtures/img.html
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
<img src="file.txt"/>
|
||||||
2
test/fixtures/import.less
vendored
Normal file
2
test/fixtures/import.less
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
@import 'basic.less';
|
||||||
|
@import (inline) 'basic.css';
|
||||||
25
test/fixtures/index.html
vendored
25
test/fixtures/index.html
vendored
@@ -1,25 +0,0 @@
|
|||||||
<!doctype html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<link rel="stylesheet/less" href="main.less"/>
|
|
||||||
<style>
|
|
||||||
div {
|
|
||||||
padding-left: 2em;
|
|
||||||
}
|
|
||||||
#style {
|
|
||||||
background-image: url('./assets/person.png');
|
|
||||||
background-size: contain;
|
|
||||||
background-repeat: no-repeat;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<div id="link">Link</div>
|
|
||||||
<div id="import">Import</div>
|
|
||||||
<div id="style">Style</div>
|
|
||||||
<div id="attribute" style="background-image: url('./assets/person.png'); background-size: contain; background-repeat: no-repeat;">Attribute</div>
|
|
||||||
<img height="32px" width="32px" src="./assets/person.png"/>Image
|
|
||||||
{{> partial}}
|
|
||||||
{{helper}}
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
7
test/fixtures/main.less
vendored
7
test/fixtures/main.less
vendored
@@ -1,7 +0,0 @@
|
|||||||
@import (less) './assets/imported.css';
|
|
||||||
|
|
||||||
#link {
|
|
||||||
background-image: url('assets/person.png');
|
|
||||||
background-size: contain;
|
|
||||||
background-repeat: no-repeat;
|
|
||||||
}
|
|
||||||
168
test/index.js
168
test/index.js
@@ -1,13 +1,163 @@
|
|||||||
var inline = require('../lib');
|
var inline = require('../lib');
|
||||||
var path = require('path');
|
var expect = require('chai')
|
||||||
|
.use(require('chai-as-promised'))
|
||||||
|
.expect;
|
||||||
|
var datauri = require('datauri');
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
|
var path = require('path');
|
||||||
|
|
||||||
var filepath = path.resolve(__dirname, './fixtures/index.html');
|
describe('inlineHtml', function () {
|
||||||
var options = {verbose: true};
|
|
||||||
|
|
||||||
inline(filepath, options)
|
it('html: filename', function () {
|
||||||
.then(function (html) {
|
var filename = path.resolve(__dirname, './fixtures/file.txt');
|
||||||
console.log(html);
|
var content = fs.readFileSync(filename, 'utf8');
|
||||||
fs.writeFileSync('./test.html', html);
|
return expect(inline(filename)).to.eventually.equal(content);
|
||||||
})
|
});
|
||||||
.catch(console.error);
|
it('html: string', function () {
|
||||||
|
var html = 'HTML';
|
||||||
|
return expect(inline(html)).to.eventually.equal(html);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('inline link less', function () {
|
||||||
|
var filename = path.resolve(__dirname, 'fixtures/basic.less');
|
||||||
|
var html = `<link rel="stylesheet/less" href="${filename}"/>`;
|
||||||
|
return expect(inline(html)).to.eventually.match(/<style>[^]*<\/style>/);
|
||||||
|
});
|
||||||
|
it('inline link less imports', function () {
|
||||||
|
var filename = path.resolve(__dirname, 'fixtures/import.less');
|
||||||
|
var html = `<link rel="stylesheet/less" href="${filename}"/>`;
|
||||||
|
return expect(inline(html)).to.eventually.match(/<style>[^]*<\/style>/)
|
||||||
|
.and.not.match(/@import/);
|
||||||
|
});
|
||||||
|
it('inline css url path in style element', function () {
|
||||||
|
var filename = path.resolve(__dirname, 'fixtures/file.txt');
|
||||||
|
var html = `<style>div {background-image: url("${filename}");}</style>`;
|
||||||
|
return expect(inline(html)).to.eventually.match(/data:.*,.*/);
|
||||||
|
});
|
||||||
|
it('inline css url path in element style attribute', function () {
|
||||||
|
var filename = path.resolve(__dirname, 'fixtures/file.txt');
|
||||||
|
var html = `<div style="background-image: url('${filename}');"></div>`;
|
||||||
|
return expect(inline(html)).to.eventually.match(/data:.*,.*/);
|
||||||
|
});
|
||||||
|
it('inline img src', function () {
|
||||||
|
var filename = path.resolve(__dirname, 'fixtures/file.txt');
|
||||||
|
var html = `<img src="${filename}"/>`;
|
||||||
|
return expect(inline(html)).to.eventually.match(/data:.*,.*/);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('options.verbose: return results object if true', function () {
|
||||||
|
var filename = path.resolve(__dirname, 'fixtures/file.txt');
|
||||||
|
var html = `<img src="${filename}"/>`;
|
||||||
|
var options = { verbose: true };
|
||||||
|
return expect(inline(html, options)).to.eventually.be.an('object')
|
||||||
|
.that.contains.keys(['html', 'files']);
|
||||||
|
});
|
||||||
|
it('options.verbose: return html if false', function () {
|
||||||
|
var filename = path.resolve(__dirname, 'fixtures/file.txt');
|
||||||
|
var html = `<img src="${filename}"/>`;
|
||||||
|
var options = { verbose: false };
|
||||||
|
return expect(inline(html, options)).to.eventually.be.a('string');
|
||||||
|
});
|
||||||
|
it('options.verbose: default false', function () {
|
||||||
|
var filename = path.resolve(__dirname, 'fixtures/file.txt');
|
||||||
|
var html = `<img src="${filename}"/>`;
|
||||||
|
return expect(inline(html)).to.eventually.be.a('string');
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('options.filename: ignored for css url path resolution if html filename', function () {
|
||||||
|
var html = 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
|
||||||
|
return expect(inline(html, options)).to.eventually.match(/"data:.*,.*"/);
|
||||||
|
});
|
||||||
|
it('options.filename: ignored for img src path resolution if html filename', function () {
|
||||||
|
var html = path.resolve(__dirname, 'fixtures/img.html'); // file contains src relative to itself
|
||||||
|
var options = { filename: __filename }; // sets filename relative to this test file
|
||||||
|
return expect(inline(html, options)).to.eventually.match(/"data:.*,.*"/);
|
||||||
|
});
|
||||||
|
it('options.filename: set basepath for css url path resolution if html string', function () {
|
||||||
|
var filename = path.resolve(__dirname, 'fixtures/fake.html');
|
||||||
|
var dirname = path.dirname(filename);
|
||||||
|
|
||||||
|
var url = 'file.txt'; // Note: path relative to filename's dirname
|
||||||
|
var uri = datauri(path.resolve(dirname, url));
|
||||||
|
|
||||||
|
var html = (path) => `<style>div { background-image: url('${path}'); }</style>`;
|
||||||
|
var options = { filename: filename };
|
||||||
|
return expect(inline(html(url), options)).to.eventually.equal(html(uri));
|
||||||
|
});
|
||||||
|
it('options.filename: set basepath for img src path resolution if html string', function () {
|
||||||
|
var filename = path.resolve(__dirname, 'fixtures/fake.html');
|
||||||
|
var dirname = path.dirname(filename);
|
||||||
|
|
||||||
|
var url = 'file.txt'; // Note: path relative to filename's dirname
|
||||||
|
var uri = datauri(path.resolve(dirname, url));
|
||||||
|
|
||||||
|
var html = (path) => `<img src="${path}"/>`;
|
||||||
|
var options = { filename: filename };
|
||||||
|
return expect(inline(html(url), options)).to.eventually.equal(html(uri));
|
||||||
|
});
|
||||||
|
it('options.filename: default to cwd for css url path resolution if html string', function () {
|
||||||
|
var url = 'test/fixtures/file.txt'; // Note: this is relative to cwd
|
||||||
|
var uri = datauri(url);
|
||||||
|
|
||||||
|
var html = (path) => `<style>div { background-image: url('${path}'); }</style>`;
|
||||||
|
return expect(inline(html(url))).to.eventually.equal(html(uri));
|
||||||
|
});
|
||||||
|
it('options.filename: default to cwd for img src path resolution if html string', function () {
|
||||||
|
var url = 'test/fixtures/file.txt'; // Note: path relative to cwd
|
||||||
|
var uri = datauri(url);
|
||||||
|
|
||||||
|
var html = (path) => `<img src="${path}"/>`;
|
||||||
|
return expect(inline(html(url))).to.eventually.equal(html(uri));
|
||||||
|
});
|
||||||
|
it('options.filename: included in results.files for img src if html string and options.verbose true', function () {
|
||||||
|
var filename = path.resolve(__dirname, 'fixtures/file.txt');
|
||||||
|
var html = `<img src="${filename}"/>`;
|
||||||
|
var options = { verbose: true };
|
||||||
|
return expect(inline(html, options)).to.eventually.have.property('files')
|
||||||
|
.that.is.an('array')
|
||||||
|
.that.contains(filename);
|
||||||
|
});
|
||||||
|
it('options.filename: included in results.files for css url path if html string and options.verbose true', function () {
|
||||||
|
var filename = path.resolve(__dirname, 'fixtures/file.txt');
|
||||||
|
var html = `<style>div { background-image: url('${filename}'); }</style>`;
|
||||||
|
var options = { verbose: true };
|
||||||
|
return expect(inline(html, options)).to.eventually.have.property('files')
|
||||||
|
.that.is.an('array')
|
||||||
|
.that.contains(filename);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
it('preserve self closing tags', function () {
|
||||||
|
var html = '<br/>';
|
||||||
|
return expect(inline(html)).to.eventually.equal(html);
|
||||||
|
});
|
||||||
|
it('preserve partials', function () {
|
||||||
|
var html = '{{> partial}}';
|
||||||
|
return expect(inline(html)).to.eventually.equal(html);
|
||||||
|
});
|
||||||
|
it('preserve helpers', function () {
|
||||||
|
var html = '{{helper}}';
|
||||||
|
return expect(inline(html)).to.eventually.equal(html);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('ignore css url remote paths', function () {
|
||||||
|
var html = `<style> div { background-image: url('http://test.com/file.txt?query=string#hash'); }</style>`;
|
||||||
|
return expect(inline(html)).to.eventually.equal(html);
|
||||||
|
});
|
||||||
|
it('ignore img src remote paths', function () {
|
||||||
|
var html = `<img src="http://test.com/file.txt?query=string#hash"/>`;
|
||||||
|
return expect(inline(html)).to.eventually.equal(html);
|
||||||
|
});
|
||||||
|
it('ignore query strings and hashes on local paths', function () {
|
||||||
|
var filename = path.resolve(__dirname, 'fixtures/file.txt');
|
||||||
|
var url = `${filename}?query=string#hash`;
|
||||||
|
var uri = datauri(filename);
|
||||||
|
var html = (source) => `<style> div { background-image: url('${source}'); }</style>`;
|
||||||
|
return expect(inline(html(url))).to.eventually.equal(html(uri));
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
var inline = require('../lib/inline-css-url');
|
|
||||||
var fs = require('fs');
|
|
||||||
|
|
||||||
var filePath = './fixtures/assets/imported.css';
|
|
||||||
var css = fs.readFileSync(filePath, 'utf8');
|
|
||||||
var result = inline(css, filePath);
|
|
||||||
console.log(result.css);
|
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
var inline = require('../lib/inline-link-less');
|
|
||||||
var fs = require('fs');
|
|
||||||
var path = require('path');
|
|
||||||
|
|
||||||
var filepath = path.resolve(__dirname, './fixtures/index.html');
|
|
||||||
var html = fs.readFileSync(filepath, 'utf8');
|
|
||||||
|
|
||||||
inline(html, filepath, {})
|
|
||||||
.then(console.log)
|
|
||||||
.catch(console.error);
|
|
||||||
Reference in New Issue
Block a user