css-font-face-src
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -1,82 +0,17 @@ | ||
var util = require('./util'); | ||
var grammar = require('./grammar'); | ||
var sliceFontFaceSrcReferences = function (fontFaceSrc) { | ||
var functionParamRegexS = "\\s*(?:\"[^\"]*\"|'[^']*'|[^\\(]+)\\s*", | ||
referenceRegexS = "(local\\(" + functionParamRegexS + "\\))" + "|" + | ||
"(url\\(" + functionParamRegexS + "\\))" + "(?:\\s+(format\\(" + functionParamRegexS + "\\)))?", | ||
simpleFontFaceSrcRegexS = "^\\s*(" + referenceRegexS + ")" + | ||
"(?:\\s*,\\s*(" + referenceRegexS + "))*" + | ||
"\\s*$", | ||
referenceRegex = new RegExp(referenceRegexS, "g"), | ||
repeatedMatch, | ||
fontFaceSrcReferences = [], | ||
getReferences = function (match) { | ||
var references = []; | ||
match.slice(1).forEach(function (elem) { | ||
if (elem) { | ||
references.push(elem); | ||
} | ||
}); | ||
return references; | ||
}; | ||
if (!fontFaceSrc) { | ||
return []; | ||
} | ||
if (fontFaceSrc.match(new RegExp(simpleFontFaceSrcRegexS))) { | ||
repeatedMatch = referenceRegex.exec(fontFaceSrc); | ||
while (repeatedMatch) { | ||
fontFaceSrcReferences.push(getReferences(repeatedMatch)); | ||
repeatedMatch = referenceRegex.exec(fontFaceSrc); | ||
} | ||
return fontFaceSrcReferences; | ||
} | ||
throw new Error("Invalid value"); | ||
exports.SyntaxError = function (message, offset) { | ||
this.message = message; | ||
this.offset = offset; | ||
}; | ||
var findFontFaceFormat = function (value) { | ||
var fontFaceFormatRegex = /^format\(([^\)]+)\)/, | ||
quotedFormat; | ||
if (!fontFaceFormatRegex.test(value)) { | ||
return null; | ||
exports.parse = function (fontFaceSourceValue) { | ||
try { | ||
return grammar.parse(fontFaceSourceValue); | ||
} catch (e) { | ||
throw new exports.SyntaxError(e.message, e.offset); | ||
} | ||
quotedFormat = fontFaceFormatRegex.exec(value)[1]; | ||
return util.unquoteString(quotedFormat); | ||
}; | ||
var extractFontFaceSrcUrl = function (reference) { | ||
var url, format; | ||
try { | ||
url = util.extractCssUrl(reference[0]); | ||
if (reference[1]) { | ||
format = findFontFaceFormat(reference[1]); | ||
} | ||
return { | ||
url: url, | ||
format: format | ||
}; | ||
} catch (e) {} | ||
}; | ||
exports.parse = function (fontFaceSourceValue) { | ||
var fontReferences = sliceFontFaceSrcReferences(fontFaceSourceValue); | ||
return fontReferences.map(function (reference) { | ||
var fontSrc = extractFontFaceSrcUrl(reference); | ||
if (fontSrc) { | ||
return fontSrc; | ||
} else { | ||
return { | ||
local: util.extractLocalFontName(reference) | ||
}; | ||
} | ||
}); | ||
}; | ||
exports.serialize = function (parsedFontFaceSources) { | ||
@@ -83,0 +18,0 @@ return parsedFontFaceSources.map(function (sourceItem) { |
@@ -7,3 +7,3 @@ var trimCSSWhitespace = function (value) { | ||
exports.unquoteString = function (quotedUrl) { | ||
var unquoteString = function (quotedUrl) { | ||
var doubleQuoteRegex = /^"(.*)"$/, | ||
@@ -23,24 +23,4 @@ singleQuoteRegex = /^'(.*)'$/; | ||
exports.extractCssUrl = function (cssUrl) { | ||
var urlRegex = /^url\(([^\)]+)\)/, | ||
quotedUrl; | ||
if (!urlRegex.test(cssUrl)) { | ||
throw new Error("Invalid value"); | ||
} | ||
quotedUrl = urlRegex.exec(cssUrl)[1]; | ||
return exports.unquoteString(trimCSSWhitespace(quotedUrl)); | ||
exports.extractValue = function (value) { | ||
return unquoteString(trimCSSWhitespace(value)); | ||
}; | ||
exports.extractLocalFontName = function (fontValue) { | ||
var localFontRegex = /^local\(([^\)]+)\)/, | ||
quotedLocalFont; | ||
if (!localFontRegex.test(fontValue)) { | ||
throw new Error("Invalid value"); | ||
} | ||
quotedLocalFont = localFontRegex.exec(fontValue)[1]; | ||
return exports.unquoteString(trimCSSWhitespace(quotedLocalFont)); | ||
}; |
{ | ||
"name": "css-font-face-src", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "A CSS @font-face src property value parser", | ||
"main": "lib/index.js", | ||
"scripts": { | ||
"test": "jshint lib/ test/ && jasmine-node test/ --captureExceptions" | ||
"test": "jshint lib/ --exclude lib/grammar.js test/ && jasmine-node test/ --captureExceptions", | ||
"prepublish": "pegjs lib/grammar.peg lib/grammar.js" | ||
}, | ||
@@ -30,4 +31,5 @@ "repository": { | ||
"jasmine-node": "~1.14.3", | ||
"jshint": "~2.5.0" | ||
"jshint": "~2.5.0", | ||
"pegjs": "~0.8.0" | ||
} | ||
} |
@@ -25,3 +25,3 @@ css-font-face-src | ||
{ url: 'font.otf', format: 'opentype' }, | ||
{ url: 'font.woff', format: undefined }, | ||
{ url: 'font.woff' }, | ||
{ local: 'Another Font' } ] | ||
@@ -28,0 +28,0 @@ ``` |
@@ -8,3 +8,3 @@ var parser = require('../lib/index'); | ||
parser.parse("invalid text"); | ||
}).toThrow(new Error("Invalid value")); | ||
}).toThrow(new parser.SyntaxError('Expected "local(", "url(" or end of input but "i" found.', 1)); | ||
}); | ||
@@ -15,3 +15,3 @@ | ||
parser.parse('"invalid url", url("font.woff")'); | ||
}).toThrow(new Error("Invalid value")); | ||
}).toThrow(new parser.SyntaxError('Expected "local(", "url(" or end of input but "\\"" found.', 1)); | ||
}); | ||
@@ -22,3 +22,3 @@ | ||
parser.parse('url("font.woff"), "invalid url"'); | ||
}).toThrow(new Error("Invalid value")); | ||
}).toThrow(new parser.SyntaxError('Expected "local(", "url(" or [ \\t\\r\\n\\f] but "\\"" found.', 18)); | ||
}); | ||
@@ -52,3 +52,3 @@ | ||
it("should parse a mix of multiple values", function () { | ||
var parse = parser.parse("local('The Font'), url('font.otf') format('opentype'), url('font.woff'), local(\"Another Font\")"); | ||
var parse = parser.parse("local('The Font'), url( 'font.otf') format('opentype'), url(font.woff), local(\"Another Font\")"); | ||
@@ -67,2 +67,21 @@ expect(parse).toEqual([{ | ||
it("should parse white space between values", function () { | ||
var parse = parser.parse('url("font.woff") \t\r\n\f, \t\r\n\furl("font.eot")'); | ||
expect(parse).toEqual([{ | ||
url: 'font.woff' | ||
}, { | ||
url: 'font.eot' | ||
}]); | ||
}); | ||
it("should parse white space between a format", function () { | ||
var parse = parser.parse('url("font.woff") \t\r\n\fformat("woff")'); | ||
expect(parse).toEqual([{ | ||
url: 'font.woff', | ||
format: 'woff' | ||
}]); | ||
}); | ||
it("should handle an empty string", function () { | ||
@@ -69,0 +88,0 @@ var parse = parser.parse(""); |
var util = require('../lib/util'); | ||
describe("Util", function () { | ||
describe("extractCssUrl", function () { | ||
it("should extract a CSS URL", function () { | ||
var url = util.extractCssUrl('url(path/file.png)'); | ||
expect(url).toEqual("path/file.png"); | ||
}); | ||
it("should handle double quotes", function () { | ||
var url = util.extractCssUrl('url("path/file.png")'); | ||
expect(url).toEqual("path/file.png"); | ||
}); | ||
it("should handle single quotes", function () { | ||
var url = util.extractCssUrl("url('path/file.png')"); | ||
expect(url).toEqual("path/file.png"); | ||
}); | ||
it("should handle whitespace", function () { | ||
var url = util.extractCssUrl('url( path/file.png )'); | ||
expect(url).toEqual("path/file.png"); | ||
}); | ||
it("should also handle tab, line feed, carriage return and form feed", function () { | ||
var url = util.extractCssUrl('url(\t\r\f\npath/file.png\t\r\f\n)'); | ||
expect(url).toEqual("path/file.png"); | ||
}); | ||
it("should keep any other whitspace", function () { | ||
var url = util.extractCssUrl('url(\u2003\u3000path/file.png)'); | ||
expect(url).toEqual("\u2003\u3000path/file.png"); | ||
}); | ||
it("should handle whitespace with double quotes", function () { | ||
var url = util.extractCssUrl('url( "path/file.png" )'); | ||
expect(url).toEqual("path/file.png"); | ||
}); | ||
it("should handle whitespace with single quotes", function () { | ||
var url = util.extractCssUrl("url( 'path/file.png' )"); | ||
expect(url).toEqual("path/file.png"); | ||
}); | ||
it("should extract a data URI", function () { | ||
var url = util.extractCssUrl('url("data:image/png;base64,soMEfAkebASE64=")'); | ||
expect(url).toEqual("data:image/png;base64,soMEfAkebASE64="); | ||
}); | ||
it("should throw an exception on invalid CSS URL", function () { | ||
expect(function () { | ||
util.extractCssUrl('invalid_stuff'); | ||
}).toThrow(new Error("Invalid value")); | ||
}); | ||
}); | ||
describe("extractLocalFontName", function () { | ||
it("should extract a local font name", function () { | ||
var fontName = util.extractLocalFontName('local(the font)'); | ||
describe("extractValue", function () { | ||
it("should handle string without quotes", function () { | ||
var fontName = util.extractValue('the font'); | ||
expect(fontName).toEqual("the font"); | ||
@@ -64,3 +11,3 @@ }); | ||
it("should handle double quotes", function () { | ||
var fontName = util.extractLocalFontName('local("the font")'); | ||
var fontName = util.extractValue('"the font"'); | ||
expect(fontName).toEqual("the font"); | ||
@@ -70,3 +17,3 @@ }); | ||
it("should handle single quotes", function () { | ||
var fontName = util.extractLocalFontName("local('the font')"); | ||
var fontName = util.extractValue("'the font'"); | ||
expect(fontName).toEqual("the font"); | ||
@@ -76,3 +23,3 @@ }); | ||
it("should handle whitespace", function () { | ||
var fontName = util.extractLocalFontName('local( the font )'); | ||
var fontName = util.extractValue(' the font '); | ||
expect(fontName).toEqual("the font"); | ||
@@ -82,3 +29,3 @@ }); | ||
it("should also handle tab, line feed, carriage return and form feed", function () { | ||
var fontName = util.extractLocalFontName('local(\t\r\f\nthe font\t\r\f\n)'); | ||
var fontName = util.extractValue('\t\r\f\nthe font\t\r\f\n'); | ||
expect(fontName).toEqual("the font"); | ||
@@ -88,3 +35,3 @@ }); | ||
it("should keep any other whitspace", function () { | ||
var fontName = util.extractLocalFontName('local(\u2003the font)'); | ||
var fontName = util.extractValue('\u2003the font'); | ||
expect(fontName).toEqual("\u2003the font"); | ||
@@ -94,3 +41,3 @@ }); | ||
it("should handle whitespace with double quotes", function () { | ||
var fontName = util.extractLocalFontName('local( "the font" )'); | ||
var fontName = util.extractValue(' "the font" '); | ||
expect(fontName).toEqual("the font"); | ||
@@ -100,29 +47,6 @@ }); | ||
it("should handle whitespace with single quotes", function () { | ||
var fontName = util.extractLocalFontName("local( 'the font' )"); | ||
var fontName = util.extractValue(" 'the font' "); | ||
expect(fontName).toEqual("the font"); | ||
}); | ||
it("should throw an exception on invalid local font", function () { | ||
expect(function () { | ||
util.extractLocalFontName('invalid_stuff'); | ||
}).toThrow(new Error("Invalid value")); | ||
}); | ||
}); | ||
describe("unquoteString", function () { | ||
it("should unquote a double quoted string", function () { | ||
var url = util.unquoteString('"a string"'); | ||
expect(url).toEqual('a string'); | ||
}); | ||
it("should unquote a single quoted string", function () { | ||
var url = util.unquoteString("'a string'"); | ||
expect(url).toEqual('a string'); | ||
}); | ||
it("should pass a string that is not quoted", function () { | ||
var url = util.unquoteString("a string"); | ||
expect(url).toEqual('a string'); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
10
10862
3
176