Changeset 2840:d41fb886d154 in livinglogic.python.xist

Show
Ignore:
Timestamp:
08/02/07 15:37:19 (12 years ago)
Author:
Walter Doerwald <walter@…>
Branch:
default
Message:

Respect style attributes on nodes for html.applycss().

style attribute will be parsed an put in the specificity order in the right
spot when iterating through the rules.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • src/ll/xist/ns/html.py

    r2839 r2840  
    2020 
    2121 
    22 import os, cgi, contextlib 
     22import os, cgi, contextlib, operator 
    2323 
    2424from ll import url, misc 
     
    14661466 
    14671467def applycss(node, base=None, media=None): 
     1468    def iterstyles(node, rules): 
     1469        if "style" in node.attrs: 
     1470            style = node.attrs.style 
     1471            if not style.isfancy(): 
     1472                # parse the style out of the style attribute 
     1473                stylerule = cssutils.parseString(u"*{%s}" % style).cssRules[0] 
     1474                styleselector = xfind.IsSelector(node) 
     1475                styleweight = xfind.CSSWeight(1, 0, 0) 
     1476                # put the style attribute into the order as the last of the selectors with ID weight (see http://www.w3.org/TR/REC-CSS1#cascading-order) 
     1477                done = False 
     1478                for (weight, selector, rule) in rules: 
     1479                    if weight > styleweight and not done: 
     1480                        yield (styleweight, styleselector, stylerule) 
     1481                        done = True 
     1482                    yield (weight, selector, rule) 
     1483                if not done: 
     1484                    yield (styleweight, styleselector, stylerule) 
     1485                return 
     1486        for x in rules: 
     1487            yield x 
     1488     
    14681489    rules = [] 
    14691490    for (i, rule) in enumerate(itercssrules(node, base=base, media=media)): 
    14701491        for selector in rule.selectorList: 
    14711492            selector = xfind.css(selector) 
    1472             rules.append((selector, rule)) 
    1473     rules.sort(key=lambda (selector, rule): selector.cssweight()) 
     1493            rules.append((selector.cssweight(), selector, rule)) 
     1494    rules.sort(key=operator.itemgetter(0)) 
    14741495    for path in node.walk(xsc.Element): 
    14751496        if path[-1].Attrs.isallowed("style"): 
    14761497            styles = {} 
    14771498            count = 0 
    1478             for (selector, rule) in rules: 
     1499            for (weight, selector, rule) in iterstyles(path[-1], rules): 
    14791500                if selector.match(path): 
    14801501                    for prop in rule.style.seq: