chore: Merge branch 'master' of github.com:remy/inliner

* 'master' of github.com:remy/inliner:
  fix: don't corrupt whitespace in scripts
This commit is contained in:
Remy Sharp 2017-04-05 10:58:14 +01:00
commit 40845eeb3a
10 changed files with 71 additions and 6 deletions

View File

@ -190,7 +190,7 @@ function main() {
inliner.url = url; // this is a hack for the `resolve` function later on
return inliner.get(this.filename || url, { encoding: 'binary' });
})
.catch(function isUrl(error) {
.catch(function isUrl() {
// make the best guess as to whether we're working with a url
if (inliner.url || url.indexOf('<') === -1) {
url = inliner.url || inliner.source;
@ -312,12 +312,19 @@ function main() {
// collapse the white space
if (inliner.options.collapseWhitespace) {
debug('collapsing whitespace');
$('pre, textarea').each(function () {
$(this).html($(this).html()
.replace(/\n/g, '~~nl~~')
.replace(/\s/g, '~~s~~'));
});
$('script').each(function () {
$(this).text($(this).text()
.replace(/\n/g, '~~nl~~')
.replace(/\s/g, '~~s~~'));
});
html = $.html()
.replace(/\s+/g, ' ')
.replace(/~~nl~~/g, '\n')

View File

@ -16,6 +16,7 @@ function resolve(inliner, todo, $) {
var isMinified = false;
if (type && type.toLowerCase() !== 'text/javascript') {
debug('skipping %s', type);
return false;
}

20
test/README.md Normal file
View File

@ -0,0 +1,20 @@
# Tests
When filing bugs, if it's an inlining issue, ensure you include full test source to work against.
This can be via a [PR](https://github.com/remy/inliner/pulls) or by linking to a [gist](https://gist.github.com) that include at least **two** (first) files:
- `<issue>.src.html`
- `<issue>.result.html`
When these are put in the [fixtures](https://github.com/remy/inliner/tree/master/test/fixtures) directory, they are automatically tested against.
If there are any external assets the example needs, please also include these and name them with the same root as your example, i.e. `<issue>.css` or `<issue>.min.js` etc.
In addition `<issue>.opts.json` can be loaded to help specify runtime options during the test.
**To test a single fixture you can use:**:
```bash
$ FILTER=<issue> npm test
```

View File

@ -0,0 +1,7 @@
<!doctype html> <html> <body> <script type="text/template">
First paragraph
Second paragraph
# Heading
</script> <script>var foo=function(){console.log("foo")};foo();var bar=10;</script> </body></html>

View File

@ -0,0 +1,18 @@
<!doctype html>
<html>
<body>
<script type="text/template">
First paragraph
Second paragraph
# Heading
</script>
<script>
var foo = function () {
console.log('foo');
}
foo();
var bar = 10;
</script>

View File

@ -0,0 +1,5 @@
<script>const string = `This
is a multline
string`;</script>

View File

@ -0,0 +1,7 @@
<script>
const string = `This
is a multline
string`;
</script>

View File

@ -1 +1 @@
<script>{ "user-agent": "inliner" }</script>
<script>"function"==typeof callback&&callback({"user-agent":"inliner"});</script>

View File

@ -1 +1 @@
<script src="https://httpbin.org/user-agent"></script>
<script src="https://httpbin.now.sh/user-agent?callback=callback"></script>

View File

@ -8,7 +8,7 @@ var st = require('st');
var server;
test('setup mock server', function (t) {
server = http.createServer(function(req, res) {
server = http.createServer(function (req, res) {
if (isASCII(req.url)) {
st(path.resolve(__dirname, 'fixtures'))(req, res);
}
@ -39,7 +39,7 @@ test('inliner core functions', function coreTests(t) {
t.ok(inliner, 'inline is instantiated');
var roundtripHTML = '<!DOCTYPE html><html></html>';
new Inliner(roundtripHTML, function(error, html) {
new Inliner(roundtripHTML, function (error, html) {
t.equal(html, roundtripHTML, 'recognizes HTML as main input');
});
});
@ -50,7 +50,7 @@ test('inliner handles given source as local', function sourcedTests(t) {
t.plan(1);
var content = fs.readFileSync(__dirname + '/fixtures/css-ext-import.src.html', 'utf8');
new Inliner(content, function (error, content) {
new Inliner(content, function (error) {
t.equal(error, null, 'treats local content as file');
});
});