Ignore template expression in link href.

This commit is contained in:
Alexandre Gigliotti
2015-12-02 11:40:47 -08:00
parent 897d87436a
commit 22ce5e3f25
3 changed files with 21 additions and 8 deletions

View File

@@ -1,6 +1,7 @@
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');
@@ -38,7 +39,10 @@ const inlineLess = co.wrap(function * ($, filename, options) {
var files = []; var files = [];
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 getHref = element => path.resolve(basedir, $(element).attr('href'));
const hrefs = R.map(getHref, links); const hrefs = R.map(getHref, links);

1
test/fixtures/nested-import.less vendored Normal file
View File

@@ -0,0 +1 @@
@import 'import.less';

View File

@@ -275,16 +275,16 @@ describe('inline-html', () => {
}); });
describe('img', () => { describe('img', () => {
it('inline img src', () => { it('inline local source', () => {
const filename = path.resolve(__dirname, 'fixtures/file.txt'); const filename = path.resolve(__dirname, 'fixtures/file.txt');
const 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', () => {
const 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', () => {
const 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);
}); });
@@ -325,20 +325,28 @@ describe('inline-html', () => {
}); });
describe('link-less', () => { describe('link-less', () => {
it('inline link less', () => { it('inline local href', () => {
const filename = path.resolve(__dirname, 'fixtures/basic.less'); const filename = path.resolve(__dirname, 'fixtures/basic.less');
const 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>[^]*<\/style>/);
}); });
it('inline link less imports', () => { it('ignore remote href', () => {
const filename = path.resolve(__dirname, 'fixtures/import.less'); const html = `<link rel="stylesheet/less" href="http://test.com/main.less"/>`;
return expect(inline.html(html)).to.eventually.equal(html);
});
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}"/>`; 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>[^]*<\/style>/)
.and.not.match(/@import/); .and.not.match(/@import/);
}); });
it('rebase urls relative to html filename', () => { it('rebase urls relative to html filename', () => {
const filename = path.resolve(__dirname, 'index.html'); const filename = path.resolve(__dirname, 'index.html');
const href = 'fixtures/import.less'; const href = 'fixtures/basic.less';
const html = `<link rel="stylesheet/less" href="${href}"/>`; const html = `<link rel="stylesheet/less" href="${href}"/>`;
return expect(inline.html(html, { filename })).to.eventually.match(/<style>[^]*<\/style>/); return expect(inline.html(html, { filename })).to.eventually.match(/<style>[^]*<\/style>/);
}); });