Skip to content

Commit

Permalink
fix: add handling for invalid data-uri calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
bingnz committed Apr 13, 2016
1 parent f0a4d1c commit e03b5c2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/data-uri-visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ module dataUriVisitor {
return ruleNode;
}

let importedFile: string;
if (ruleNode.args.length === 0) {
return ruleNode;
}

let argument: any;

if (ruleNode.args.length === 2) { // specifying MIME type.
importedFile = (<any>ruleNode.args[1]).value[0].value;
argument = ruleNode.args[1];
} else {
importedFile = (<any>ruleNode.args[0]).value[0].value;
argument = ruleNode.args[0];
}

let importedFile = argument.value[0].value;
var importPath = path.normalize(path.join(ruleNode.currentFileInfo.entryPath, importedFile));

this._imports.push(importPath);
Expand Down
32 changes: 32 additions & 0 deletions test/import-lister.js
Original file line number Diff line number Diff line change
Expand Up @@ -475,4 +475,36 @@ describe('import-lister', () => {
.then(() => expect(options).to.deep.equal({ a: 'b', c: 'd' }));
});
});

describe('when a data-uri call is invalid', () => {
it('should throw an error', () => {
let resolverFunction = {
resolve: function(file) {
return Promise.resolve(file);
}
};

let pathResolver = {
PathResolver: function()
{
return resolverFunction;
}
};
let importBufferStub = {
'ImportBuffer': function (lister) {
return {
'listImports': inputFile =>
lister(inputFile).then(files =>
Promise.map(files, file =>
Promise.resolve({ path: file, stat: { mtime: new Date() } })))}}};

sinon.spy(resolverFunction, 'resolve');
importLister = new (getImportLister({ pathResolver: pathResolver, importBuffer: importBufferStub }));

return importLister.listImports(new File({ path: 'x', contents: new Buffer('@a: data-uri();') }))
.catch(error => {
expect(error.message).to.contain('Failed to process imports');
});
});
});
});

0 comments on commit e03b5c2

Please sign in to comment.