Advertisement
brandizzi

Implementing get_css_class_dict()

Feb 26th, 2018
832
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | None | 0 0
  1. def get_css_class_dict(node):
  2.     """
  3.    Given an xml.dom.minidom.Node, returns a map from every "class" attribute
  4.    from it to a list of nodes with this class.
  5.  
  6.    For example, for the document below:
  7.  
  8.    >>> doc = xml.dom.minidom.parseString(
  9.    ...     '''
  10.    ...     <html>
  11.    ...         <body>
  12.    ...             <div class="a b"><span class="a"></span></div>
  13.    ...         </body>
  14.    ...     </html>
  15.    ...     ''')
  16.  
  17.    ...we will get this:
  18.  
  19.    >>> d = get_css_class_dict(doc)
  20.    >>> d['a']  # doctest: +ELLIPSIS
  21.    [<DOM Element: div at ...>, <DOM Element: span at ...>]
  22.    >>> d['b']  # doctest: +ELLIPSIS
  23.    [<DOM Element: span at ...>]
  24.    """
  25.     css_class_dict = {}
  26.  
  27.     if node.attributes is not None and 'class' in node.attributes:
  28.         css_classes = node.attributes['class'].value
  29.  
  30.         for css_class in css_classes.split():
  31.             css_class_list = css_class_dict.get(css_class, [])
  32.             css_class_list.append(node)
  33.             css_class_dict[css_class] = css_class_list
  34.  
  35.     childNodes = getattr(node, 'childNodes', [])
  36.  
  37.     for cn in childNodes:
  38.         ccd = get_css_class_dict(cn)
  39.         for css_class, nodes_list in ccd.items():
  40.             css_class_list = css_class_dict.get(css_class, [])
  41.             css_class_list.extend(nodes_list)
  42.             css_class_dict[css_class] = css_class_list  
  43.  
  44.     return css_class_dict
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement