goog.getCssName with the class name as a variable

597 views
Skip to first unread message

Hendrik Söbbing

unread,
Nov 22, 2011, 7:54:16 AM11/22/11
to closure-style...@googlegroups.com
Hi everybody,

i tried to minify a CSS class name, generated by an algorithm:

var classNormal = 'size-' + Math.round(Math.random()*10);
var classMinified = goog.getCssName(classNormal);

The Closure compiler throws an error, insists on a string literal:

ERROR - goog.getCssName called with invalid argument, string literal expected. Was "NAME".

I tried adding type declaration, but no effect.

Am I missing something? Isn't this a valid use case? Or does the compiler do the class replacement in compile time? What could be a workaround?

Michael Bolin

unread,
Nov 22, 2011, 2:54:46 PM11/22/11
to closure-style...@googlegroups.com
Correct -- the compiler does class replacement at compile time, which is why this is an error.

Did you read both of these?


If part of your application relies on the use of dynamically generated class names, then you should exclude those class names from CSS renaming. This entails using the --excluded_classes_from_renaming flag, which also means that those classes should not be wrapped with goog.getCssName().

Hendrik Söbbing

unread,
Nov 23, 2011, 6:36:25 AM11/23/11
to closure-style...@googlegroups.com
Thank you very much, Michael, for pointing that compile-time fact out! 

I read those articles, of course, but obviously not throughly enough. :) Maybe that fact should be emphasized a bit more, I could think of more people not realizing this.

Anyway, thank you again for your help!
Hendrik

m.gad...@zerodo.it

unread,
Mar 2, 2012, 7:02:23 AM3/2/12
to closure-style...@googlegroups.com
This is exactly my case.
I'm using plovr for compilation and i don't know were put "--excluded_classes_from_renaming" flag.
Can you help me?

Markus

unread,
Mar 2, 2012, 7:03:28 AM3/2/12
to closure-style...@googlegroups.com
This is exactly my case.
I'm using plovr for compilation and i don't know were put "--excluded_classes_from_renaming" flag.
Can you help me?

 

On Tuesday, November 22, 2011 8:54:46 PM UTC+1, Michael Bolin wrote:
Reply all
Reply to author
Forward
0 new messages