Skip to content

Commit

Permalink
Resolve #2 content escape in assemble context
Browse files Browse the repository at this point in the history
  • Loading branch information
ain committed Apr 28, 2016
1 parent 8d16dc7 commit a10dc9c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
12 changes: 5 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/**
* Handlebars Helper: {{rawinclude}}
* Handlebars Helper: {{{rawinclude}}}
* Copyright © 2014-2016 Ain Tohvri
* Licensed under GPL-3.0
*/

'use strict';

/**
* {{rawinclude}}
* {{{rawinclude}}}
* Like {{ include }} but without context.
*
* @param {String} path Path of the file to include.
* @return {String} Returns raw content of the file at path.
* @example: {{rawinclude '/path/to/compund.svg'}}
* @example: {{{rawinclude '/path/to/compund.svg'}}}
* @todo support for Array input, minimatch.
*/
exports.rawinclude = function (path, options) {
Expand All @@ -24,9 +24,7 @@ exports.rawinclude = function (path, options) {
options = options || {};
options.hash = options.hash || {};

var Handlebars = require('handlebars');
var matter = require('gray-matter');
var fs = require('fs')

var result = matter.read(path);
return new Handlebars.SafeString(result.content);
return fs.readFileSync(path, 'utf8');
};
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
"scripts": {
"test": "mocha"
},
"dependencies": {
"gray-matter": "~2.0.2"
},
"peerDependencies": {
"handlebars": "~4.0.5"
},
Expand Down
24 changes: 12 additions & 12 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
var should = require('should');
var Handlebars = require('handlebars');
var helpers = require('./index');
var matter = require('gray-matter');
var fs = require('fs');

Handlebars.registerHelper("rawinclude", helpers.rawinclude);

describe('parsing', function () {
it('should parse fixtures/GPLv3_Logo.svg into template', function () {
var template = Handlebars.compile('{{rawinclude "fixtures/GPLv3_Logo.svg"}}');
var context = matter.read('fixtures/GPLv3_Logo.svg');
template({}).should.eql(context.content);
var template = Handlebars.compile('{{{rawinclude "fixtures/GPLv3_Logo.svg"}}}');
var content = fs.readFileSync('fixtures/GPLv3_Logo.svg', 'utf8');
template({}).should.eql(content);
});

it('should parse fixtures/circle.svg into template', function () {
var template = Handlebars.compile('{{rawinclude "fixtures/circle.svg"}}');
var context = matter.read('fixtures/circle.svg');
template({}).should.eql(context.content);
var template = Handlebars.compile('{{{rawinclude "fixtures/circle.svg"}}}');
var content = fs.readFileSync('fixtures/circle.svg', 'utf8');
template({}).should.eql(content);
});

it('should parse fixtures/simple.json into template', function () {
var template = Handlebars.compile('{{rawinclude "fixtures/simple.json"}}');
var context = matter.read('fixtures/simple.json');
template({}).should.eql(context.content);
var template = Handlebars.compile('{{{rawinclude "fixtures/simple.json"}}}');
var content = fs.readFileSync('fixtures/simple.json', 'utf8');
template({}).should.eql(content);
});

it('should parse fixtures/circle.svg with correctly using gray-matter', function () {
var template = Handlebars.compile('{{rawinclude "fixtures/circle.svg"}}');
var template = Handlebars.compile('{{{rawinclude "fixtures/circle.svg"}}}');
template({}).should.eql('<?xml version="1.0" standalone="no"?>\n<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n<svg width="4960px" height="7015px" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:1.41421;">\n <g>\n <g id="Layer1">\n <g>\n <path d="M2410.17,2875.22C2858,2875.22 3221.03,3242.25 3221.03,3695C3221.03,4147.75 2858,4514.77 2410.17,4514.77C1962.34,4514.77 1599.31,4147.75 1599.31,3695C1599.31,3242.25 1962.34,2875.22 2410.17,2875.22Z" style="fill:rgb(227,229,228);"/>\n </g>\n </g>\n </g>\n</svg>\n');
});
});

describe('errorhandling', function () {
it('should throw TypeError on non-string key', function () {
Handlebars.compile('{{rawinclude foo}}').should.throw(TypeError);
Handlebars.compile('{{{rawinclude foo}}}').should.throw(TypeError);
});
});

0 comments on commit a10dc9c

Please sign in to comment.