mirror of
				https://github.com/KevinMidboe/inline-html.git
				synced 2025-10-29 17:40:29 +00:00 
			
		
		
		
	Fix bug where template expressions were interpreted as paths.
This commit is contained in:
		@@ -1,5 +1,6 @@
 | 
				
			|||||||
var datauri = require('datauri');
 | 
					var datauri = require('datauri');
 | 
				
			||||||
var isLocalPath = require('is-local-path');
 | 
					var isLocalPath = require('is-local-path');
 | 
				
			||||||
 | 
					var isTemplateExpression = require('./is-template-expression');
 | 
				
			||||||
var path = require('path');
 | 
					var path = require('path');
 | 
				
			||||||
var postcss = require('postcss');
 | 
					var postcss = require('postcss');
 | 
				
			||||||
var postcssUrl = require('postcss-url');
 | 
					var postcssUrl = require('postcss-url');
 | 
				
			||||||
@@ -33,7 +34,7 @@ var inline = function (css, filename) {
 | 
				
			|||||||
	var result = postcss()
 | 
						var result = postcss()
 | 
				
			||||||
		.use(postcssUrl({
 | 
							.use(postcssUrl({
 | 
				
			||||||
			url: function (urlPath) {
 | 
								url: function (urlPath) {
 | 
				
			||||||
				if (isLocalPath(urlPath)) {
 | 
									if (isLocalPath(urlPath) && !isTemplateExpression(urlPath)) {
 | 
				
			||||||
					urlPath = clean(urlPath);
 | 
										urlPath = clean(urlPath);
 | 
				
			||||||
					urlPath = path.resolve(basePath, urlPath);
 | 
										urlPath = path.resolve(basePath, urlPath);
 | 
				
			||||||
					files.push(urlPath);
 | 
										files.push(urlPath);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,7 @@
 | 
				
			|||||||
var cheerio = require('cheerio');
 | 
					var cheerio = require('cheerio');
 | 
				
			||||||
var datauri = require('datauri');
 | 
					var datauri = require('datauri');
 | 
				
			||||||
var isLocalPath = require('is-local-path');
 | 
					var isLocalPath = require('is-local-path');
 | 
				
			||||||
 | 
					var isTemplateExpression = require('./is-template-expression');
 | 
				
			||||||
var path = require('path');
 | 
					var path = require('path');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var inline = function (html, filename) {
 | 
					var inline = function (html, filename) {
 | 
				
			||||||
@@ -8,7 +9,8 @@ var inline = function (html, filename) {
 | 
				
			|||||||
	var basedir = path.dirname(filename);
 | 
						var basedir = path.dirname(filename);
 | 
				
			||||||
	var $ = cheerio.load(html, {decodeEntities: false});
 | 
						var $ = cheerio.load(html, {decodeEntities: false});
 | 
				
			||||||
	var images = $('img').filter(function (index, element) {
 | 
						var images = $('img').filter(function (index, element) {
 | 
				
			||||||
		return isLocalPath($(element).attr('src'));
 | 
							var path = $(element).attr('src');
 | 
				
			||||||
 | 
							return isLocalPath(path) && !isTemplateExpression(path);
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
	images.each(function (index, element) {
 | 
						images.each(function (index, element) {
 | 
				
			||||||
		var src = $(element).attr('src');
 | 
							var src = $(element).attr('src');
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										14
									
								
								lib/is-template-expression.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								lib/is-template-expression.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
				
			|||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * Tests whether a path is a mustache template expression.
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * Note: would be best if this was file extension specific
 | 
				
			||||||
 | 
					 * (i.e. *.hbs => test for `{{ }}` )
 | 
				
			||||||
 | 
					 *
 | 
				
			||||||
 | 
					 * @param {String} path
 | 
				
			||||||
 | 
					 * @return {Boolean}
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					var isTemplateExpression = function (path) {
 | 
				
			||||||
 | 
						return /^{{.*}}$/.test(path);
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					module.exports = isTemplateExpression;
 | 
				
			||||||
@@ -152,6 +152,14 @@ describe('inlineHtml', function () {
 | 
				
			|||||||
		var html = `<img src="http://test.com/file.txt?query=string#hash"/>`;
 | 
							var html = `<img src="http://test.com/file.txt?query=string#hash"/>`;
 | 
				
			||||||
		return expect(inline(html)).to.eventually.equal(html);
 | 
							return expect(inline(html)).to.eventually.equal(html);
 | 
				
			||||||
	});
 | 
						});
 | 
				
			||||||
 | 
						it('ignore css url template expression paths', function () {
 | 
				
			||||||
 | 
							var html = `<style> div { background-image: url({{path}}); }</style>`;
 | 
				
			||||||
 | 
							return expect(inline(html)).to.eventually.equal(html);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
						it('ignore img src template expression paths', function () {
 | 
				
			||||||
 | 
							var html = `<img src="{{path}}"/>`;
 | 
				
			||||||
 | 
							return expect(inline(html)).to.eventually.equal(html);
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
	it('ignore query strings and hashes on local paths', function () {
 | 
						it('ignore query strings and hashes on local paths', function () {
 | 
				
			||||||
		var filename = path.resolve(__dirname, 'fixtures/file.txt');
 | 
							var filename = path.resolve(__dirname, 'fixtures/file.txt');
 | 
				
			||||||
		var url = `${filename}?query=string#hash`;
 | 
							var url = `${filename}?query=string#hash`;
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user