Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 286 lines (241 sloc) 10.607 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
# CSSPrefixer
# Copyright 2010-2012 Greg V. <floatboth@me.com>

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import re
import cssutils

prefixRegex = re.compile('^(-o-|-ms-|-moz-|-webkit-)')


class BaseReplacementRule(object):
    vendor_prefixes = ['moz', 'webkit']

    def __init__(self, prop):
        self.prop = prop

    def get_prefixed_props(self, filt):
        for prefix in [p for p in self.vendor_prefixes if p in filt]:
            yield cssutils.css.Property(
                    name='-%s-%s' % (prefix, self.prop.name),
                    value=self.prop.value,
                    priority=self.prop.priority
                    )

    @staticmethod
    def should_prefix():
        return True


class FullReplacementRule(BaseReplacementRule):
    vendor_prefixes = sorted(BaseReplacementRule.vendor_prefixes + ['o', 'ms'])


class BaseAndIEReplacementRule(BaseReplacementRule):
    vendor_prefixes = sorted(BaseReplacementRule.vendor_prefixes + ['ms'])


class BaseAndOperaReplacementRule(BaseReplacementRule):
    vendor_prefixes = sorted(BaseReplacementRule.vendor_prefixes + ['o'])


class WebkitReplacementRule(BaseReplacementRule):
    vendor_prefixes = ['webkit']


class OperaAndIEReplacementRule(BaseReplacementRule):
    vendor_prefixes = ['ms', 'o']


class MozReplacementRule(BaseReplacementRule):
    vendor_prefixes = ['moz']


class BorderRadiusReplacementRule(BaseReplacementRule):
    """
Mozilla's Gecko engine uses different syntax for rounded corners.
"""
    vendor_prefixes = ['webkit']

    def get_prefixed_props(self, filt):
        for prop in BaseReplacementRule.get_prefixed_props(self, filt):
            yield prop
        if 'moz' in filt:
            name = '-moz-' + self.prop.name.replace('top-left-radius', 'radius-topleft') \
                   .replace('top-right-radius', 'radius-topright') \
                   .replace('bottom-right-radius', 'radius-bottomright') \
                   .replace('bottom-left-radius', 'radius-bottomleft')
            yield cssutils.css.Property(
                    name=name,
                    value=self.prop.value,
                    priority=self.prop.priority
                    )


class CursorReplacementRule(BaseReplacementRule):
    """
Experimental cursor values.
"""
    def get_prefixed_props(self, filt):
        if self.prop.value in ('zoom-in', 'zoom-out'):
            for prefix in [p for p in self.vendor_prefixes if p in filt]:
                yield cssutils.css.Property(
                        name='cursor',
                        value='-%s-%s' % (prefix, self.prop.value),
                        priority=self.prop.priority
                        )


class DisplayReplacementRule(BaseReplacementRule):
    """
Flexible Box Model stuff.
CSSUtils parser doesn't support duplicate properties, so that's dirty.
"""
    def get_prefixed_props(self, filt):
        if self.prop.value == 'box': # only add prefixes if the value is box
            for prefix in [p for p in self.vendor_prefixes if p in filt]:
                yield cssutils.css.Property(
                        name='display',
                        value='-%s-box' % prefix,
                        priority=self.prop.priority
                        )


class TransitionReplacementRule(BaseReplacementRule):
    vendor_prefixes = ['moz', 'o', 'webkit']

    def __get_prefixed_prop(self, prefix=None):
        name = self.prop.name
        if prefix:
            name = '-%s-%s' % (prefix, self.prop.name)
        newValues = []
        for value in self.prop.value.split(','):
            parts = value.strip().split(' ')
            parts[0] = prefixRegex.sub('', parts[0])
            if parts[0] in rules and prefix and rules[parts[0]].should_prefix():
                parts[0] = '-%s-%s' % (prefix, parts[0])
            newValues.append(' '.join(parts))
        return cssutils.css.Property(
                name=name,
                value=', '.join(newValues),
                priority=self.prop.priority
                )

    def get_prefixed_props(self, filt):
        for prefix in [p for p in self.vendor_prefixes if p in filt]:
            yield self.__get_prefixed_prop(prefix)

    def get_base_prop(self):
        return self.__get_prefixed_prop()


class GradientReplacementRule(BaseReplacementRule):
    vendor_prefixes = ['moz', 'o', 'webkit']

    def __iter_values(self):
        valueSplit = self.prop.value.split(',')
        index = 0
        # currentString = ''
        while(True):
            if index >= len(valueSplit):
                break
            rawValue = valueSplit[index].strip()
            snip = prefixRegex.sub('', rawValue)
            if snip.startswith('linear-gradient'):
                values = [re.sub('^linear-gradient\(', '', snip)]
                if valueSplit[index + 1].strip().endswith(')'):
                    values.append(re.sub('\)+$', '', valueSplit[index + 1].strip()))
                else:
                    values.append(valueSplit[index + 1].strip())
                    values.append(re.sub('\)+$', '', valueSplit[index + 2].strip()))
                if len(values) == 2:
                    yield {
                        'start': values[0],
                        'end': values[1]
                        }
                else:
                    yield {
                        'pos': values[0],
                        'start': values[1],
                        'end': values[2]
                        }
                index += len(values)
            elif snip.startswith('gradient'):
                yield {
                    'start': re.sub('\)+$', '', valueSplit[index + 4].strip()),
                    'end': re.sub('\)+$', '', valueSplit[index + 6].strip()),
                    }
                index += 7
            else:
                # not a gradient so just yield the raw string
                yield rawValue
                index += 1

    def __get_prefixed_prop(self, values, prefix=None):
        gradientName = 'linear-gradient'
        if prefix:
            gradientName = '-%s-%s' % (prefix, gradientName)
        newValues = []
        for value in values:
            if isinstance(value, dict):
                if 'pos' in value:
                    newValues.append(gradientName + '(%(pos)s, %(start)s, %(end)s)' % value)
                else:
                    newValues.append(gradientName + '(%(start)s, %(end)s)' % value)
            else:
                newValues.append(value)
        return cssutils.css.Property(
                name=self.prop.name,
                value=', '.join(newValues),
                priority=self.prop.priority
                )

    def get_prefixed_props(self, filt):
        values = list(self.__iter_values())
        needPrefix = False
        for value in values: # check if there are any gradients
            if isinstance(value, dict):
                needPrefix = True
                break
        if needPrefix:
            for prefix in [p for p in self.vendor_prefixes if p in filt]:
                yield self.__get_prefixed_prop(values, prefix)
                if prefix == 'webkit':
                    newValues = []
                    for value in values:
                        if isinstance(value, dict):
                            newValues.append('-webkit-gradient(linear, left top, left bottom, color-stop(0, %(start)s), color-stop(1, %(end)s))' % value)
                        else:
                            newValues.append(value)
                    yield cssutils.css.Property(
                            name=self.prop.name,
                            value=', '.join(newValues),
                            priority=self.prop.priority
                            )
        else:
            yield None

    def get_base_prop(self):
        values = self.__iter_values()
        return self.__get_prefixed_prop(values)

rules = {
    'border-radius': BaseReplacementRule,
    'border-top-left-radius': BorderRadiusReplacementRule,
    'border-top-right-radius': BorderRadiusReplacementRule,
    'border-bottom-right-radius': BorderRadiusReplacementRule,
    'border-bottom-left-radius': BorderRadiusReplacementRule,
    'border-image': FullReplacementRule,
    'box-shadow': BaseReplacementRule,
    'box-sizing': MozReplacementRule,
    'box-orient': BaseAndIEReplacementRule,
    'box-direction': BaseAndIEReplacementRule,
    'box-ordinal-group': BaseAndIEReplacementRule,
    'box-align': BaseAndIEReplacementRule,
    'box-flex': BaseAndIEReplacementRule,
    'box-flex-group': BaseReplacementRule,
    'box-pack': BaseAndIEReplacementRule,
    'box-lines': BaseAndIEReplacementRule,
    'user-select': BaseReplacementRule,
    'user-modify': BaseReplacementRule,
    'margin-start': BaseReplacementRule,
    'margin-end': BaseReplacementRule,
    'padding-start': BaseReplacementRule,
    'padding-end': BaseReplacementRule,
    'column-count': BaseReplacementRule,
    'column-gap': BaseReplacementRule,
    'column-rule': BaseReplacementRule,
    'column-rule-color': BaseReplacementRule,
    'column-rule-style': BaseReplacementRule,
    'column-rule-width': BaseReplacementRule,
    'column-span': WebkitReplacementRule,
    'column-width': BaseReplacementRule,
    'columns': WebkitReplacementRule,

    'background-clip': WebkitReplacementRule,
    'background-origin': WebkitReplacementRule,
    'background-size': WebkitReplacementRule,
    'background-image': GradientReplacementRule,
    'background': GradientReplacementRule,

    'text-overflow': OperaAndIEReplacementRule,

    'transition': TransitionReplacementRule,
    'transition-delay': BaseAndOperaReplacementRule,
    'transition-duration': BaseAndOperaReplacementRule,
    'transition-property': TransitionReplacementRule,
    'transition-timing-function': BaseAndOperaReplacementRule,
    'transform': FullReplacementRule,
    'transform-origin': FullReplacementRule,

    'cursor': CursorReplacementRule,
    'display': DisplayReplacementRule,
    'appearance': WebkitReplacementRule,
    'hyphens': BaseReplacementRule,
}
Something went wrong with that request. Please try again.