95 lines
2.6 KiB
Python
Raw Permalink Normal View History

2012-04-03 13:36:08 +00:00
# -*- coding: utf8 -*-
2012-01-28 14:52:09 +00:00
"""
2012-04-03 13:36:08 +00:00
.. module:: lesscpy.plib.property
:synopsis: Property node.
2013-07-19 11:21:51 +02:00
2012-04-03 13:36:08 +00:00
Copyright (c)
See LICENSE for details.
2012-07-18 17:28:04 -04:00
.. moduleauthor:: Johann T. Mariusson <jtm@robot.is>
2012-01-28 14:52:09 +00:00
"""
import re
2012-02-19 20:38:19 +00:00
from .node import Node
2012-02-27 19:10:22 +00:00
2013-07-19 11:21:51 +02:00
2012-02-19 20:38:19 +00:00
class Property(Node):
2013-07-19 11:21:51 +02:00
2012-04-03 13:36:08 +00:00
"""Represents CSS property declaration.
"""
2013-07-19 11:21:51 +02:00
2012-01-28 14:52:09 +00:00
def parse(self, scope):
2012-04-03 13:36:08 +00:00
"""Parse node
args:
scope (Scope): current scope
raises:
SyntaxError
returns:
self
2012-02-26 16:36:13 +00:00
"""
if not self.parsed:
if len(self.tokens) > 2:
property, style, _ = self.tokens
self.important = True
else:
property, style = self.tokens
self.important = False
self.property = ''.join(property)
self.parsed = []
if style:
style = self.preprocess(style)
self.parsed = self.process(style, scope)
2012-02-25 17:08:08 +00:00
return self
2013-07-19 11:21:51 +02:00
2012-02-26 11:19:10 +00:00
def preprocess(self, style):
2012-04-03 13:36:08 +00:00
"""Hackish preprocessing from font shorthand tags.
Skips expression parse on certain tags.
2013-07-19 11:21:51 +02:00
args:
2012-04-03 13:36:08 +00:00
style (list): .
returns:
list
2012-02-26 11:19:10 +00:00
"""
if self.property == 'font':
2013-07-19 11:21:51 +02:00
style = [''.join(u.expression())
2012-02-26 11:19:10 +00:00
if hasattr(u, 'expression')
else u
for u in style]
2012-02-27 19:10:22 +00:00
else:
style = [(u, ' ')
if hasattr(u, 'expression')
else u
for u in style]
2012-02-26 11:19:10 +00:00
return style
2013-07-19 11:21:51 +02:00
2012-03-03 09:58:47 +00:00
def fmt(self, fills):
2012-04-03 13:36:08 +00:00
""" Format node
args:
fills (dict): replacements
returns:
str
2012-02-25 17:08:08 +00:00
"""
2012-02-26 10:59:21 +00:00
f = "%(tab)s%(property)s:%(ws)s%(style)s%(important)s;%(nl)s"
imp = ' !important' if self.important else ''
2012-02-26 16:36:13 +00:00
if fills['nl']:
self.parsed = [',%s' % fills['ws']
if p == ','
2013-07-19 11:21:51 +02:00
else p
2012-02-26 16:36:13 +00:00
for p in self.parsed]
2013-07-19 11:21:51 +02:00
style = ''.join([p.fmt(fills)
if hasattr(p, 'fmt')
2012-02-26 16:36:13 +00:00
else str(p)
for p in self.parsed])
# IE cannot handle no space after url()
style = re.sub("(url\([^\)]*\))([^\s,])", "\\1 \\2", style)
2012-02-25 17:08:08 +00:00
fills.update({
'property': self.property,
2012-02-27 19:10:22 +00:00
'style': style.strip(),
2012-02-26 10:59:21 +00:00
'important': imp
2012-02-25 17:08:08 +00:00
})
return f % fills
2013-07-19 11:21:51 +02:00
def copy(self):
2012-05-05 17:20:03 +00:00
""" Return a full copy of self
Returns:
Property object
"""
return Property([t for t in self.tokens], 0)