Skip to content

Commit

Permalink
Add support for border-width shorthand property. (#21)
Browse files Browse the repository at this point in the history
References GH-19.
  • Loading branch information
ted kaemming authored and BYK committed Jul 26, 2016
1 parent 691fc37 commit 40cbd39
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 39 deletions.
10 changes: 6 additions & 4 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,30 @@ class TestCase(Exam, unittest.TestCase):


def test_expand_shorthand_box_property():
assert expand_shorthand_box_property('margin', '1px') == {
expand = expand_shorthand_box_property('margin-{}')

assert expand('1px') == {
'margin-top': '1px',
'margin-right': '1px',
'margin-bottom': '1px',
'margin-left': '1px',
}

assert expand_shorthand_box_property('margin', '1px 2px') == {
assert expand('1px 2px') == {
'margin-top': '1px',
'margin-right': '2px',
'margin-bottom': '1px',
'margin-left': '2px',
}

assert expand_shorthand_box_property('margin', '1px 2px 3px') == {
assert expand('1px 2px 3px') == {
'margin-top': '1px',
'margin-right': '2px',
'margin-bottom': '3px',
'margin-left': '2px',
}

assert expand_shorthand_box_property('margin', '1px 2px 3px 4px') == {
assert expand('1px 2px 3px 4px') == {
'margin-top': '1px',
'margin-right': '2px',
'margin-bottom': '3px',
Expand Down
69 changes: 34 additions & 35 deletions toronado/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,46 +27,46 @@
logger = logging.getLogger(__name__)


def expand_shorthand_box_property(name, value):
bits = value.split()
size = len(bits)
if size == 1:
result = (bits[0],) * 4
elif size == 2:
result = (bits[0], bits[1],) * 2
elif size == 3:
result = (bits[0], bits[1], bits[2], bits[1])
elif size == 4:
result = tuple(bits)
else:
raise ValueError('incorrect number of values for box rule: %s' % size)

def expand_shorthand_box_property(template):
sides = ('top', 'right', 'bottom', 'left')
return {'%s-%s' % (name, side): value for side, value in zip(sides, result)}

def expand_property(value):
bits = value.split()
size = len(bits)
if size == 1:
result = (bits[0],) * 4
elif size == 2:
result = (bits[0], bits[1],) * 2
elif size == 3:
result = (bits[0], bits[1], bits[2], bits[1])
elif size == 4:
result = tuple(bits)
else:
raise ValueError('incorrect number of values for box rule: %s' % size)

def rewrite_margin_property_value(value):
return expand_box_rule('margin', value)
return {template.format(side): value for side, value in zip(sides, result)}


def rewrite_padding_property_value(value):
return expand_box_rule('padding', value)
return expand_property


rewrite_map = {
'margin': expand_shorthand_box_property,
'padding': expand_shorthand_box_property,
'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(name, value):
logger.warning(
"CSS shorthand syntax expansion is not supported for %r. Mixing "
"shorthand and specific property values (e.g. `font` and `font-size`) "
"may lead to unexpected results.",
name,
)
return {name: value}
def warn_unsupported_shorthand_property(property):
def expand_property(value):
logger.warning(
"CSS shorthand syntax expansion is not supported for %r. Mixing "
"shorthand and specific property values (e.g. `font` and `font-size`) "
"may lead to unexpected results.",
name,
)
return {name: value}

return expand_property


unsupported_shorthand_properties = (
Expand All @@ -80,7 +80,6 @@ def warn_unsupported_shorthand_property(name, value):
'border-right',
'border-style',
'border-top',
'border-width',
'font',
'list-style',
'transform',
Expand All @@ -89,16 +88,16 @@ def warn_unsupported_shorthand_property(name, value):


for property in unsupported_shorthand_properties:
rewrite_map[property] = warn_unsupported_shorthand_property
rewrite_map[property] = warn_unsupported_shorthand_property(property)


def rewrite_property(property):
result = rewrite_map.get(
property.name,
lambda name, value: {
name: value,
lambda value: {
property.name: value,
}
)(property.name, property.value)
)(property.value)

if property.priority:
for key, value in result.items():
Expand Down

0 comments on commit 40cbd39

Please sign in to comment.