Commits

Tom Morris committed 2f6937e

Created a function that retrieves text from an element for the ‘value’ code.

Comments (0)

Files changed (3)

mf2py/dom_addins.py

                 return True
         return False
 
+def getText(self):
+    out = ''
+    if self.hasChildNodes():
+        for child in self.childNodes:
+            if 'wholeText' in dir(child):
+                if child.wholeText is not None:
+                    out = out + child.wholeText
+            elif 'childNodes' in dir(child):
+                out = out + child.getText()
+    else:
+        if self.nodeValue is not None:
+            out = out + self.nodeValue
+    return out
 
 Element.__dict__.update({'getElementsByClassName': getElementsByClassName})
 Element.__dict__.update({'addClassName': addClassName})
 Element.__dict__.update({'hasClassName': hasClassName})
+Element.__dict__.update({'getText': getText})
             """Returns value-class-pattern. This may be either a string a dict or None."""
             return [x for x in el.getElementsByClassName("value")]
 
-        def handle_microformat(microformat_name, el):
-            properties = parse_props(el, {})
-            if microformat_name == "h-card" and 'name' not in properties:
-                properties["name"] = [el.firstChild.nodeValue]
-                # TODO: replace with proper name-implied
-            microformat = {"type": [microformat_name],
-                           "properties": properties}
-            return microformat
-
         parsed = set()
         
         def property_classnames(classes):
                     properties["name"] = [el.getElementsByTagName("abbr")[0].getAttribute("abbr")]
                 # TODO: implement the rest of http://microformats.org/wiki/microformats2-parsing#parsing_for_implied_properties
                 else:
-                    properties["name"] = [el.firstChild.nodeValue]
+                    properties["name"] = [el.getText()]
             if "photo" not in properties:
                 if el.nodeName == 'img' and el.hasAttribute("src"):
                     properties["photo"] = [el.getAttribute("src")]
             if len(children) > 0:
                 microformat["children"] = children
             if is_nested:
-                microformat["value"] = el.firstChild.nodeValue
+                microformat["value"] = str(el)
             return microformat
         
         def parse_props(el, is_root_element=False):

test/test_parser.py

     assert len(result["items"]) == 1
     assert "children" in result["items"][0]
     assert len(result["items"][0]["children"]) == 1
-    assert result["items"][0]["children"]["properties"]["name"][0] == "Some Citation"
+    assert result["items"][0]["children"][0]["properties"]["name"][0] == "Some Citation"
 
 def test_implied_name():
     result = parse_fixture("implied_properties.html")