Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This pull request updates parsing of base64-encoded data URI and adds support of base64 TTF fonts. This may close #153 and #682.
Data URI parsing
Current implementation of data URI parser is too simple and doesn't allow mediatype to contain additional params, like charset or such. RFC 2397 says:
Most solutions from web recommend using URI format like
data:font/ttf;charset=utf8;base64,etc
for fonts. I don't really know how these additional params must be used, but I guess it is better to allow parser to work with them properly. Current regex-based parser breaks on them. New implementation just stores them inside wrapper object for further use.I've decided to drop regex in favor of simpler parsing solution because it isn't easy to get repeated capture groups in one go. Proper regex would also be not very readable I think.
Fonts support
Fonts in base64 actually could've been supported even without changing data URI parsing. When
font-family
CSS rule contains src with data URI it's already properly parsed by current implementation andB64InlineURI
file-like object is created. Only thing that prevents fonts from being used is font-adding code that expects real file and checks file extensions.Proposed implementation checks that font file is a
B64InlineURI
class and uses parsed mime type to detect font type.One issue that remains is that only TTF fonts are supported in base64 form (using
font/ttf
mimetype). Support ofafm
andpfb
doesn't work without heavy modification of code. Current implementation expects bothafm
andpfb
to be located somewhere with same basename, and this couldn't happen when font is provided in base64 form in single URL.