Skip to content

Commit

Permalink
FIX cgrand#90
Browse files Browse the repository at this point in the history
- upgrade jsoup to 1.8.3
- test html-resource with jsoup (jsoup always adds a <head>, not
tagsoup)
- add jsoup/tagsoup fixtures to existing tests
- pass tests
  • Loading branch information
JustinIAC committed Oct 1, 2015
1 parent 00aa3ee commit 5658fc2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
13 changes: 7 additions & 6 deletions src/net/cgrand/enlive_html.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
(ns net.cgrand.enlive-html
"enlive-html is a selector-based transformation and extraction engine."
(:refer-clojure :exclude [flatmap])
(:require [net.cgrand.tagsoup :as tagsoup])
(:require [net.cgrand.tagsoup :as tagsoup]
[net.cgrand.jsoup :as jsoup])
(:require [net.cgrand.xml :as xml])
(:require [clojure.string :as str])
(:require [clojure.zip :as z]))
Expand Down Expand Up @@ -106,7 +107,7 @@
[stream loader]
(loader stream))

(defmethod register-resource! java.net.URL
(defmethod register-resource! java.net.URL
[^java.net.URL url]
(alter-meta! *ns* update-in [:net.cgrand.reload/deps] (fnil conj #{}) url))

Expand Down Expand Up @@ -142,7 +143,7 @@
([t a b c d e] (-> t (conj! a) (conj! b) (conj! c) (conj! d) (conj! e)))
([t a b c d e f] (-> t (conj! a) (conj! b) (conj! c) (conj! d) (conj! e) (conj! f)))
([t a b c d e f g] (-> t (conj! a) (conj! b) (conj! c) (conj! d) (conj! e) (conj! f) (conj! g)))
([t a b c d e f g & more]
([t a b c d e f g & more]
(reduce conj! (-> t (conj! a) (conj! b) (conj! c) (conj! d) (conj! e) (conj! f) (conj! g))
more)))

Expand Down Expand Up @@ -684,12 +685,12 @@
:else node)))))

(defn replace-words
"Takes a map of words to replacement strings and replaces
"Takes a map of words to replacement strings and replaces
all occurences. Does not recurse, you have to pair it with an appropriate
selector."
[words-to-replacements]
[words-to-replacements]
(replace-vars
(java.util.regex.Pattern/compile (str "\\b(" (str/join "|" (map #(java.util.regex.Pattern/quote %) (keys words-to-replacements))) ")\\b"))
(java.util.regex.Pattern/compile (str "\\b(" (str/join "|" (map #(java.util.regex.Pattern/quote %) (keys words-to-replacements))) ")\\b"))
words-to-replacements
identity))

Expand Down
25 changes: 24 additions & 1 deletion test/net/cgrand/enlive_html/test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
(:use net.cgrand.enlive-html)
(:require [net.cgrand.xml :as xml])
(:require [clojure.zip :as z])
(:use [clojure.test :only [deftest is are run-all-tests]]))
(:use [clojure.test :only [deftest is are run-all-tests use-fixtures]])
(:require [net.cgrand.tagsoup :as tagsoup]
[net.cgrand.jsoup :as jsoup]))

;; test utilities
(defn- normalize [x]
Expand Down Expand Up @@ -314,3 +316,24 @@
"A B"))
(is (= ((replace-words {"Donald" "Mickey" "Duck" "Mouse"}) "Donald Duckling Duck")
"Mickey Duckling Mouse")))

(deftest jsoup-snippet
;; Jsoup always generates a head
(is (= (html-resource (-> "<html><head></head><body><h1>Hi, cgrand!</h1></body></html>"
(.getBytes "UTF-8")
java.io.ByteArrayInputStream.)
{:parser tagsoup/parser})
(html-resource (-> "<html><head></head><body><h1>Hi, cgrand!</h1></body></html>"
(.getBytes "UTF-8")
java.io.ByteArrayInputStream.)
{:parser jsoup/parser}))))

(defn fixture-jsoup-parser [f]
(set-ns-parser! jsoup/parser)
(f))

(defn fixture-tsoup-parser [f]
(set-ns-parser! tagsoup/parser)
(f))

(use-fixtures :each fixture-tsoup-parser fixture-jsoup-parser)

0 comments on commit 5658fc2

Please sign in to comment.