Changeset 2858:84dbaa7b0b6c in livinglogic.python.xist

Show
Ignore:
Timestamp:
08/07/07 17:30:47 (12 years ago)
Author:
Walter Doerwald <walter@…>
Branch:
default
Message:

Fix media handling for html.applycss().

Add a method hasmedia() to html.style.Attrs.media and html.link.Attrs.media
(by putting it into their common baseclass), that checks whether a given
media type is in the attribute. Add checks for media type in the topmost
style loop, so that the media attributes on style and link elements are honored.

Files:
2 modified

Legend:

Unmodified
Added
Removed
  • NEWS.xml

    r2790 r2858  
    5656<item>The <class>Attrs</class> methods <method>updatenew</method> and 
    5757<method>updatexisting</method> have been removed.</item> 
     58 
     59<item>The <lit>media</lit> attribute of <class>html.link</class> and 
     60<class>html.style</class> now has a method <method>hasmedia</method>.</item> 
    5861</ulist> 
    5962</section> 
  • src/ll/xist/ns/html.py

    r2857 r2858  
    4141class CharacterAttr(xsc.TextAttr): "a single character, as per section 2.2 of [XML]" 
    4242class LinkTypesAttr(xsc.TextAttr): "space-separated list of link types" 
    43 class MediaDescAttr(xsc.TextAttr): "single or comma-separated list of media descriptors" 
     43 
     44 
     45class MediaDescAttr(xsc.TextAttr): 
     46    "single or comma-separated list of media descriptors" 
     47 
     48    def hasmedia(self, media): 
     49        """ 
     50        Return whether <self> contains the media type <arg>media</arg>. Returns 
     51        <lit>True</lit> if <arg>media</arg> is <lit>None</lit> or <self/> is 
     52        empty. 
     53        """ 
     54        if media is not None and self: 
     55            return media in set(m.strip() for m in unicode(self).split(",")) 
     56        return True 
     57 
     58 
    4459class URIListAttr(xsc.TextAttr): "a space separated list of Uniform Resource Identifiers" 
    4560class DatetimeAttr(xsc.TextAttr): "date and time information. ISO date format" 
     
    14121427def _doimport(wantmedia, parentsheet, base): 
    14131428    havemedia = _getmedia(parentsheet) 
    1414     if wantmedia is None or havemedia is None or wantmedia in havemedia: 
     1429    if wantmedia is None or not havemedia or wantmedia in havemedia: 
    14151430        for rule in parentsheet.cssRules: 
    14161431            if rule.type == css.CSSRule.IMPORT_RULE: 
     
    14441459            if isinstance(cssnode, style): 
    14451460                href = str(self.base) if base is not None else None 
    1446                 media = unicode(cssnode.attrs["media"]) if "media" in cssnode.attrs else None 
    1447                 stylesheet = cssutils.parseString(unicode(cssnode.content), href=href, media=media) 
    1448                 for rule in _doimport(media, stylesheet, base): 
    1449                     yield rule 
     1461                if cssnode.attrs.media.hasmedia(media): 
     1462                    stylesheet = cssutils.parseString(unicode(cssnode.content), href=href, media=unicode(cssnode.attrs.media)) 
     1463                    for rule in _doimport(media, stylesheet, base): 
     1464                        yield rule 
    14501465            else: # link 
    14511466                if "href" in cssnode.attrs: 
     
    14531468                    if base is not None: 
    14541469                        href = self.base/href 
    1455                     media = unicode(cssnode.attrs["media"]) if "media" in cssnode.attrs else None 
    1456                     with contextlib.closing(href.open("rb")) as r: 
    1457                         s = r.read() 
    1458                     stylesheet = cssutils.parseString(unicode(s), href=str(href), media=media) 
    1459                     for rule in _doimport(media, stylesheet, href): 
    1460                         yield rule 
     1470                    if cssnode.attrs.media.hasmedia(media): 
     1471                        with contextlib.closing(href.open("rb")) as r: 
     1472                            s = r.read() 
     1473                        stylesheet = cssutils.parseString(unicode(s), href=str(href), media=unicode(cssnode.attrs.media)) 
     1474                        for rule in _doimport(media, stylesheet, href): 
     1475                            yield rule 
    14611476    return misc.Iterator(doiter(node, base, media)) 
    14621477