diff --git a/pyquery/pyquery.py b/pyquery/pyquery.py index 628a32d..4f9890f 100644 --- a/pyquery/pyquery.py +++ b/pyquery/pyquery.py @@ -12,6 +12,7 @@ import types import sys +from collections import OrderedDict PY3k = sys.version_info >= (3,) @@ -883,44 +884,40 @@ def toggle_class(self, value): tag.set('class', ' '.join(classes)) return self - def css(self, *args, **kwargs): - """css attributes manipulation - """ - - attr = value = no_default - length = len(args) - if length == 1: - attr = args[0] - elif length == 2: - attr, value = args - elif kwargs: - attr = kwargs + def css(self, propertyName, value=no_default): + """css attributes manipulation""" + def clean_css(styles): + return OrderedDict((key.strip().replace('_', '-'), value.strip()) + for key, value in styles.items()) + + if isinstance(propertyName, basestring): + getter = value is no_default + attrs = {propertyName: not getter and value or ''} + elif isinstance(propertyName, dict): + attrs = propertyName + getter = False + elif isinstance(propertyName, list): + attrs = OrderedDict((key, '') for key in propertyName) + getter = True else: - raise ValueError('Invalid arguments %s %s' % (args, kwargs)) + raise ValueError('Invalid arguments %s' % str(propertyName)) + attrs = clean_css(attrs) - if isinstance(attr, dict): - for tag in self: - stripped_keys = [key.strip().replace('_', '-') - for key in attr.keys()] - current = [el.strip() - for el in (tag.get('style') or '').split(';') - if el.strip() - and not el.split(':')[0].strip() in stripped_keys] - for key, value in attr.items(): - key = key.replace('_', '-') - current.append('%s: %s' % (key, value)) - tag.set('style', '; '.join(current)) - elif isinstance(value, basestring): - attr = attr.replace('_', '-') - for tag in self: - current = [ - el.strip() - for el in (tag.get('style') or '').split(';') - if (el.strip() and - not el.split(':')[0].strip() == attr.strip())] - current.append('%s: %s' % (attr, value)) - tag.set('style', '; '.join(current)) - return self + for tag in self: + style = (tag.get('style') or '').split(';') + styles = OrderedDict(e.split(':', 1) for e in style if ':' in e) + styles = clean_css(styles) + + if getter: + # The getter always returns for the first element in jquery + ret = [ styles.get(key, no_default) for key in attrs.keys() ] + return isinstance(propertyName, list) and ret or ''.join(ret[:1]) + + styles.update(attrs) + styles = [ ': '.join(e) for e in styles.items() if all(e) ] + tag.set('style', '; '.join(styles) + ';') + # Return's self on write (but not on read) + return self css = FlexibleElement(pget=css, pset=css) diff --git a/tests/test_css.html b/tests/test_css.html new file mode 100644 index 0000000..d2f47f6 --- /dev/null +++ b/tests/test_css.html @@ -0,0 +1,10 @@ + +
+Hello world !
+ ++hello python ! +
+