-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support shorthand property compression. #24
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,8 +27,12 @@ | |
logger = logging.getLogger(__name__) | ||
|
||
|
||
def expand_box_property_names(template): | ||
return list(map(template.format, ('top', 'right', 'bottom', 'left'))) | ||
|
||
|
||
def expand_shorthand_box_property(template): | ||
sides = ('top', 'right', 'bottom', 'left') | ||
names = expand_box_property_names(template) | ||
|
||
def expand_property(value): | ||
bits = value.split() | ||
|
@@ -44,18 +48,11 @@ def expand_property(value): | |
else: | ||
raise ValueError('incorrect number of values for box rule: %s' % size) | ||
|
||
return {template.format(side): value for side, value in zip(sides, result)} | ||
return {name: value for name, value in zip(names, result)} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this basically There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, oops. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed here: 8d6bc29 |
||
|
||
return expand_property | ||
|
||
|
||
rewrite_map = { | ||
'margin': expand_shorthand_box_property('margin-{}'), | ||
'padding': expand_shorthand_box_property('padding-{}'), | ||
'border-width': expand_shorthand_box_property('border-{}-width'), | ||
} | ||
|
||
|
||
def warn_unsupported_shorthand_property(property): | ||
def expand_property(value): | ||
logger.warning( | ||
|
@@ -69,6 +66,35 @@ def expand_property(value): | |
return expand_property | ||
|
||
|
||
def compress_box_property(shorthand, template): | ||
names = expand_box_property_names(template) | ||
|
||
def compress_property(value): | ||
if not set(value).issuperset(set(names)): | ||
return value | ||
|
||
top, right, bottom, left = map(value.pop, names) | ||
|
||
if top == right == bottom == left: | ||
value[shorthand] = top | ||
elif top == bottom and right == left: | ||
value[shorthand] = '{} {}'.format(top, right) | ||
elif right == left: | ||
value[shorthand] = '{} {} {}'.format(top, right, bottom) | ||
else: | ||
value[shorthand] = '{} {} {} {}'.format(top, right, bottom, left) | ||
|
||
return value | ||
|
||
return compress_property | ||
|
||
|
||
shorthand_box_properties = { | ||
'margin': 'margin-{}', | ||
'padding': 'padding-{}', | ||
'border-width': 'border-{}-width', | ||
} | ||
|
||
unsupported_shorthand_properties = ( | ||
'animation', | ||
'background', | ||
|
@@ -87,12 +113,19 @@ def expand_property(value): | |
) | ||
|
||
|
||
expansion_rewrite_map = {} | ||
property_processors = [] | ||
|
||
for property, template in shorthand_box_properties.items(): | ||
expansion_rewrite_map[property] = expand_shorthand_box_property(template) | ||
property_processors.append(compress_box_property(property, template)) | ||
|
||
for property in unsupported_shorthand_properties: | ||
rewrite_map[property] = warn_unsupported_shorthand_property(property) | ||
expansion_rewrite_map[property] = warn_unsupported_shorthand_property(property) | ||
|
||
|
||
def rewrite_property(property): | ||
result = rewrite_map.get( | ||
def expand_property(property): | ||
result = expansion_rewrite_map.get( | ||
property.name, | ||
lambda value: { | ||
property.name: value, | ||
|
@@ -122,13 +155,17 @@ def __unicode__(self): | |
Renders the properties as a string suitable for inclusion as a HTML tag | ||
attribute. | ||
""" | ||
return '; '.join(map(': '.join, self.items())) | ||
value = self.copy() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is probably unnecessary, but having a |
||
for processor in property_processors: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a cool way to do this: http://stackoverflow.com/a/11736942/90297 (don't think it is better than yours, was just curious and wanted to share what I found) |
||
value = processor(value) | ||
|
||
return '; '.join(map(': '.join, value.items())) | ||
|
||
@classmethod | ||
def from_string(cls, value): | ||
values = {} | ||
for property in CSSStyleDeclaration(value).getProperties(): | ||
values.update(rewrite_property(property)) | ||
values.update(expand_property(property)) | ||
return cls(values) | ||
|
||
|
||
|
@@ -218,7 +255,7 @@ def inline(tree): | |
for rule in ifilter(is_style_rule, stylesheet_parser.parseString(stylesheet.text)): | ||
properties = {} | ||
for property in rule.style: | ||
properties.update(rewrite_property(property)) | ||
properties.update(expand_property(property)) | ||
|
||
# XXX: This doesn't handle selectors with odd multiple whitespace. | ||
for selector in map(text_type.strip, rule.selectorText.split(',')): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You may wanna have another test case to cove
Basically when the value is
0
, you should still collapse that regardless of the unit.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, will add this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a little tricky, because to be reliably implemented it'd need to support:
<number>
value that is equal to 0 (such as0
,+0.0
,-.0e10
, etc.): https://developer.mozilla.org/en-US/docs/Web/CSS/number<length>
properties: : https://developer.mozilla.org/en-US/docs/Web/CSS/lengthI opened another ticket to take care of this later: GH-25