def test_parse(self): "cssutils.parse()" # temp css for tests name = '__cssutils_temptestfile__.css' css = u'a:after { content: "äu\u2020" }' if os.path.exists(name): raise IOError('skipping test as file "%s" exists' % name) css = u'a:after { content: "äu\u2020" }' t = codecs.open(name, 'w', encoding='utf-8') t.write(css) t.close() self.assertRaises( UnicodeDecodeError, cssutils.parse, name, 'ascii') s = cssutils.parse(name, 'iso-8859-1') #??? s = cssutils.parse(name, 'utf-8') self.assertEqual(cssutils.css.CSSStyleSheet, type(s)) css = u'a:after { content: "ä" }' t = codecs.open(name, 'w', 'iso-8859-1') t.write(css) t.close() self.assertRaises( UnicodeDecodeError, cssutils.parse, name, 'ascii') s = cssutils.parse(name, 'iso-8859-1') self.assertEqual(cssutils.css.CSSStyleSheet, type(s)) # clean up os.remove(name)
def combine(proxy, srcenc='css', tarenc='utf-8', minified=True): """ TODO: - encoding - read conf """ src = cssutils.parse(proxy, encoding=srcenc) sys.stderr.write('COMBINING %s\n' % proxy) srcpath = os.path.dirname(proxy) r = cssutils.css.CSSStyleSheet() for rule in src.cssRules: if rule.type == rule.IMPORT_RULE: fn = os.path.join(srcpath, rule.href) sys.stderr.write('* PROCESSING @import %s\n' % fn) importsheet = cssutils.parse(fn, encoding=srcenc) importsheet.encoding = None # remove @charset r.insertRule( cssutils.css.CSSComment(cssText=u'/* %s */' % rule.cssText)) for x in importsheet.cssRules: if x.type == x.IMPORT_RULE: sys.stderr.write('WARN\tNo nested @imports: %s\n' % x.cssText) # TODO: too simple if prefixes different in sheets! # elif x.type == x.NAMESPACE_RULE: # print 'INFO\tMoved to begin of sheet', x.cssText # r.insertRule(x, 0) else: r.insertRule(x) #r.insertRule(importsheet.cssRules) # elif rule.type == rule.NAMESPACE_RULE: # print 'INFO\tMoved to begin of sheet', rule.cssText # r.insertRule(rule, 0) else: r.insertRule(rule) r.encoding = tarenc if minified: cssutils.ser.prefs.useMinified() return r.cssText
def combine(proxy, srcenc='css', tarenc='utf-8', minified=True): """ TODO: - encoding - read conf """ src = cssutils.parse(proxy, encoding=srcenc) sys.stderr.write('COMBINING %s\n' % proxy) srcpath = os.path.dirname(proxy) r = cssutils.css.CSSStyleSheet() for rule in src.cssRules: if rule.type == rule.IMPORT_RULE: fn = os.path.join(srcpath, rule.href) sys.stderr.write('* PROCESSING @import %s\n' % fn) importsheet = cssutils.parse(fn, encoding=srcenc) importsheet.encoding = None # remove @charset r.insertRule(cssutils.css.CSSComment(cssText=u'/* %s */' % rule.cssText)) for x in importsheet.cssRules: if x.type == x.IMPORT_RULE: sys.stderr.write('WARN\tNo nested @imports: %s\n' % x.cssText) # TODO: too simple if prefixes different in sheets! # elif x.type == x.NAMESPACE_RULE: # print 'INFO\tMoved to begin of sheet', x.cssText # r.insertRule(x, 0) else: r.insertRule(x) #r.insertRule(importsheet.cssRules) # elif rule.type == rule.NAMESPACE_RULE: # print 'INFO\tMoved to begin of sheet', rule.cssText # r.insertRule(rule, 0) else: r.insertRule(rule) r.encoding = tarenc if minified: cssutils.ser.prefs.useMinified() return r.cssText