Get correct property value for special tags img, area, abbr, data and input tags are all consumed differently.
2 files changed, 25 insertions(+), 1 deletion(-)
M src/microformats/parser.clj → src/microformats/parser.clj
@@ -34,12 +34,23 @@ :attrs :class split-classes)) +(defn get-property + "Get the p-x property value of an element" + [el] + (case (:tag el) + :img (-> el :attrs :alt) + :area (-> el :attrs :alt) + :abbr (-> el :attrs :title) + :data (-> el :attrs :value) + :input (-> el :attrs :value) + (first (:content el)))) + (defn parse-p "Parse p-* classes within HTML element." [element] (let [el (first (html/select element [(html/attr-starts :class "p-")])) props (into [] ((classes-to-props "p-") (element-to-classes el)))] - (hash-map :properties (apply hash-map (first props) (repeat (count props) (first (:content el))))))) + (hash-map :properties (apply hash-map (first props) (repeat (count props) (get-property el)))))) (defn parse-h "Parse h-* classes within a HTML document."
M test/microformats/parser_test.clj → test/microformats/parser_test.clj
@@ -16,3 +16,16 @@ {:items [{:properties {:nickname "exuser"}}] :rels {}} "<div class=\"h-card\"><p class=\"p-nickname\">exuser</p></div>" )) + +(deftest parse-p-special-elements + (testing "img, abbr and data elements should be parsed differently" + (are [ex in] (= ex (parse in)) + {:items [{:properties {:name "Example User"}}] :rels {}} + "<div class=\"h-card\"><img class=\"p-name\" alt=\"Example User\"></div>" + + {:items [{:properties {:name "Example User"}}] :rels {}} + "<div class=\"h-card h-person\"><abbr class=\"p-name\" title=\"Example User\">@example</abbr></div>" + + {:items [{:properties {:name "Example User"}}] :rels {}} + "<div class=\"h-card\"><data class=\"p-name\" value=\"Example User\"></data></div>" + )))