Skip to content
Go to file
Cannot retrieve contributors at this time
135 lines (130 sloc) 4.83 KB
(ns hickory-css-selectors-test
(:require [clojure.test :refer :all]
[hickory.core :as h]
[ :as s]
[hickory-css-selectors :refer :all]
[instaparse.core :as p]))
(deftest css-selector-parser-test
(are [css tree] (= tree (p/parse css-selector-parser css))
[[:token [:class "some-class"]]]
[[:token [:class "xyz"] [:attr "x"]]]
[[:token [:class "xyz"] [:attr "x" [:pred [:attr-op "="] [:value "y"]]]]]
[[:token [:class "xyz"] [:attr "x_z" [:pred [:attr-op "="] [:value "y"]]]]]
[[:token [:class "xyz"] [:attr "4"]]]
[[:token [:elem "div"] [:id "id"]]]
[[:token [:id "xyz"] [:class "bold"]]]
[[:token [:elem "h1"] [:class "bold"] [:attr "title"]]]
[[:token [:class "foo"] [:class "bar"] [:class "baz"]]]
" > #foo"
[:token [:class "foo"] [:class "bar"]]
[:token [:id "foo"]]]]
" + #foo"
[:token [:class "foo"] [:class "bar"]]
[:token [:id "foo"]]]]
" ~ #foo"
[:token [:class "foo"] [:class "bar"]]
[:token [:id "foo"]]]]
" #foo"
[:token [:class "foo"] [:class "bar"]]
[:token [:id "foo"]]]]
[[:token [:elem "body"] [:nth-child [:nth "1"]]]]
[[:token [:elem "div"] [:has-child [:token [:elem "p"]]]]]
[[:token [:elem "div"] [:has-text "bla"]]]
[:token [:class "foo"] [:class "bar"]]
[:token [:id "foo"] [:nth-child [:nth "1"]]]]]
"x > .y > #z a b > c"
[:token [:elem "x"]]
[:token [:class "y"]]]
[:token [:id "z"]]]
[:token [:elem "a"]]]
[:token [:elem "b"]]]
[:token [:elem "c"]]]]
"x y z"
[:token [:elem "x"]]
[:token [:elem "y"]]]
[:token [:elem "z"]]]]
"#readme > div.Box-body.p-6 > article > p:nth-child(19)"
[:token [:id "readme"]]
[:token [:elem "div"] [:class "Box-body"] [:class "p-6"]]]
[:token [:elem "article"]]]
[:token [:elem "p"] [:nth-child [:nth "19"]]]]]
"html > body > div > li:nth-child(2)"
[:token [:elem "html"]]
[:token [:elem "body"]]]
[:token [:elem "div"]]]
[:token [:elem "li"] [:nth-child [:nth "2"]]]]]))
(def tree
(-> "<html><body>
<p class='some-class other-class'>
some text</p>
<div id='bar' aria-name='ya'><i>italia</i>
<span id='f'>g</span>
<div id='another-div'><div id='child-div'>find this text</div></div>
(defn select-css [css] (s/select (parse-css-selector css) tree))
(deftest select-test
(is (= "bar" (-> (select-css "div#bar") first :attrs :id)))
(is (= ["italia"] (-> (select-css "div:nth-child(2) > i") first :content)))
(is (= ["foo"] (-> (select-css "body ul > li:nth-child(2)") first :content)))
(is (= ["2"] (-> (select-css "div#bar + p") first :content)))
(is (= ["bar" "another-div"] (map (comp :id :attrs) (select-css "p.some-class ~ div"))))
(is (= "another-div" (-> (select-css "div:has(div)") first :attrs :id)))
(is (= "child-div" (-> (select-css "div:contains('find this')") first :attrs :id)))
(is (= "child-div" (-> (select-css "div:contains(\"find this\")") first :attrs :id)))
(is (= "bar" (-> (select-css "*:has(*:has(*:contains('foo')))") first :attrs :id)))
(is (= ["find this text"] (-> (select-css "span + div > div") first :content))))
(s/select (parse-css-selector "div:nth-child(2) > i") tree)
(s/select (parse-css-selector "div#bar") tree)
(s/select (parse-css-selector "div[aria-name]") tree)
(s/select (parse-css-selector "div[aria-namez]") tree)
(s/select (parse-css-selector "div[aria-name^=y]") tree)
(s/select (parse-css-selector "div[aria-name=yaz]") tree)
(s/select (parse-css-selector "body > span[id$=f]") tree)
(s/select (parse-css-selector "html>body>div li:nth-child(2)") tree)
(s/select (parse-css-selector "body ul > li:nth-child(2)") tree)
(s/select (parse-css-selector "html > body > div > li:nth-child(2)") tree)
(s/select (parse-css-selector "div:nth-child(2) > i") tree)
(s/select (parse-css-selector "div + p") tree)
(s/select (parse-css-selector "div ~ p") tree)
(s/select (parse-css-selector "div:has(div > p)") tree)
(s/select (parse-css-selector "div:contains('bla! bla*')") tree))
You can’t perform that action at this time.