"Fossies" - the Fresh Open Source Software Archive

Member "xhtml2pdf-0.2.5/xhtml2pdf/w3c/cssDOMElementInterface.py" (25 Sep 2020, 4180 Bytes) of package /linux/www/xhtml2pdf-0.2.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "cssDOMElementInterface.py" see the Fossies "Dox" file reference documentation.

    1 #!/usr/bin/env python
    2 ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    3 ##~ Copyright (C) 2002-2004  TechGame Networks, LLC.
    4 ##~
    5 ##~ This library is free software; you can redistribute it and/or
    6 ##~ modify it under the terms of the BSD style License as found in the
    7 ##~ LICENSE file included with this distribution.
    8 ##
    9 ##  Modified by Dirk Holtwick <holtwick@web.de>, 2007-2008
   10 ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   11 from __future__ import absolute_import
   12 
   13 from . import css #python 3
   14 
   15 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   16 #~ Definitions
   17 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   18 
   19 class CSSDOMElementInterface(css.CSSElementInterfaceAbstract):
   20     """An implementation of css.CSSElementInterfaceAbstract for xml.dom Element Nodes"""
   21 
   22     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   23     #~ Constants / Variables / Etc.
   24     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   25 
   26     style = None
   27 
   28     _pseudoStateHandlerLookup = {
   29         'first-child':
   30             lambda self: not bool(self.getPreviousSibling()),
   31         'not-first-child':
   32             lambda self: bool(self.getPreviousSibling()),
   33 
   34         'last-child':
   35             lambda self: not bool(self.getNextSibling()),
   36         'not-last-child':
   37             lambda self: bool(self.getNextSibling()),
   38 
   39         'middle-child':
   40             lambda self: not bool(self.getPreviousSibling()) and not bool(self.getNextSibling()),
   41         'not-middle-child':
   42             lambda self: bool(self.getPreviousSibling()) or bool(self.getNextSibling()),
   43 
   44         # XXX 'first-line':
   45 
   46     }
   47 
   48     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   49     #~ Definitions
   50     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   51 
   52     def __init__(self, domElement, cssParser=None):
   53         self.domElement = domElement
   54         # print self.domElement.attributes
   55         if cssParser is not None:
   56             self.onCSSParserVisit(cssParser)
   57 
   58 
   59     def onCSSParserVisit(self, cssParser):
   60         styleSrc = self.getStyleAttr()
   61         if styleSrc:
   62             style = cssParser.parseInline(styleSrc)
   63             self.setInlineStyle(style)
   64 
   65 
   66     #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   67 
   68     def matchesNode(self, namespace_tagName):
   69         namespace,tagName = namespace_tagName
   70         if tagName not in ('*', self.domElement.tagName):
   71             return False
   72         if namespace in (None, '', '*'):
   73             # matches any namespace
   74             return True
   75         else: # full compare
   76             return namespace == self.domElement.namespaceURI
   77 
   78 
   79     def getAttr(self, name, default=NotImplemented):
   80         attrValue = self.domElement.attributes.get(name)
   81         if attrValue is not None:
   82             return attrValue.value
   83         else:
   84             return default
   85 
   86 
   87     def getIdAttr(self):
   88         return self.getAttr('id', '')
   89 
   90 
   91     def getClassAttr(self):
   92         return self.getAttr('class', '')
   93 
   94 
   95     def getStyleAttr(self):
   96         return self.getAttr('style', None)
   97 
   98 
   99     def inPseudoState(self, name, params=()):
  100         handler = self._pseudoStateHandlerLookup.get(name, lambda self: False)
  101         return handler(self)
  102 
  103 
  104     def iterXMLParents(self, includeSelf=False):
  105         klass = self.__class__
  106         current = self.domElement
  107         if not includeSelf:
  108             current = current.parentNode
  109         while (current is not None) and (current.nodeType == current.ELEMENT_NODE):
  110             yield klass(current)
  111             current = current.parentNode
  112 
  113 
  114     def getPreviousSibling(self):
  115         sibling = self.domElement.previousSibling
  116         while sibling:
  117             if sibling.nodeType == sibling.ELEMENT_NODE:
  118                 return sibling
  119             else:
  120                 sibling = sibling.previousSibling
  121         return None
  122 
  123 
  124     def getNextSibling(self):
  125         sibling = self.domElement.nextSibling
  126         while sibling:
  127             if sibling.nodeType == sibling.ELEMENT_NODE:
  128                 return sibling
  129             else:
  130                 sibling = sibling.nextSibling
  131         return None
  132 
  133 
  134     def getInlineStyle(self):
  135         return self.style
  136 
  137 
  138     def setInlineStyle(self, style):
  139         self.style = style
  140