Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Unicode Support by CSS Specifications #381
I came around this whole topic pretty recently, as I'm currently refactoring our FE Asset Manager Tool (Webmerge). Therefore I wanted to document my findings here.
This explains how the character encoding of a css file is determined. Since we are only dealing with local files, we never have a HTTP header. So the precedence should be 'charset' rule, byte-order mark (BOM) or auto-detection (finally falling back to system default/UTF-8). This may not sound too hard to implement, but what about import rules? The CSS specs do not forbid the mixing of different encodings! I solved that by converting all files to UTF-8 internally. On writing there is an option to tell the tool what encoding it should be (UTF-8 by default). One can also define if it should write a BOM or not and if it should add the charset declaration.
Since my tool is written in perl, I have a lot of utilities at hand to deal with different unicode charsets. I'm pretty sure that most OSS uses libiconv to convert between different encodings. But I have now idea how easy/hard this would be to integrate platform independent (it seems doable).
Current status on libsass unicode support
Currently libsass seems to handle the common UTF-8 case pretty well. I believe it should correctly support all ASCII compatible encodings (like UTF-8 or Latin-1). If all includes use the same encoding, the output should be correct (in the same encoding). It should also handle unicode chars in selectors, variable names and other identifiers. This is true for all ASCII compatible encodings. So the main incompatible encodings (I'm aware of) are UTF-16/UTF-32 (which could be converted to UTF-8 with libiconv).
Current encoding auto detection
Libsass currently reads all kind of BOMs and will error out if it finds something it doesn't know how to handle! It seems that it throws away the optional UTF-8 BOM (if any is found). IMO it would be nice if users could configure that (also if a charset rule should be added to the output).
What is currently not supported
What is missing to support the above cases
Low priority feature
I guess the current implementation should handle more than 99% of all real world use cases.
I guess the biggest Problem is to have libiconv (or some other) library as a dependency. Since it contains a lot of rules for the conversions, I see it as the only way to handle this correctly. Once that is sorted out it should be pretty much straight forward to implement the missing pieces (in parser.cpp - Parser::parse should return encoding and add Parser::sniff_charset, then convert the source byte stream to UTF-8).
I hope the statements above all hold true. Unicode is really not the easiest topic to wrap your head around. But since I did all the above recently in Perl, I wanted to document it here. Feel free to extend or criticize.
Have a nice day
changed the title from
Unicode Support by CSS Specifications [discussion]
Unicode Support by CSS Specifications
Jun 5, 2014
My knee-jerk reaction is that I'd rather not make libiconv a dependency of LibSass itself ... it might be appropriate to put it in SassC instead, as a conversion step before invoking LibSass. If libiconv needs to be invoked on each import in a Sass project, then maybe this could be solved with custom importers (i.e., SassC could provide a callback that would pass each import through libiconv). Or, since the command-line version of iconv is installed on most Unix-like systems, we could even simply pipe each import through that before compiling (not what I would actually recommend).
referenced this issue
Dec 12, 2014
I'm not sure if this is related but it was recently reported to be that our str-slice specs are disabled, presumable due to utf-8 tests in the spec
str-slice("øáéíóúüñ¿éàŤǅǂɊɱʭʬѪ҈ݓ", -80, -200);
Is this related to utf-8 handling or a bug with string-functions and utf-8?