Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: find-in-text returning a string instead of a zip-loc #89

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/cljc/hickory/select.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,11 @@
selected."
[re]
(fn [hzip-loc]
(some #(re-find re %) (->> (zip/node hzip-loc)
:content
(filter string?)))))
(when (some #(re-find re %)
(->> (zip/node hzip-loc)
:content
(filter string?)))
hzip-loc)))

(defn n-moves-until
"This selector returns a selector function that selects its argument if
Expand Down
70 changes: 38 additions & 32 deletions test/cljc/hickory/test/select.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,38 @@

(def html1
"<!DOCTYPE html>
<!-- Comment 1 -->
<html>
<head></head>
<body>
<h1>Heading</h1>
<p>Paragraph</p>
<a href=\"http://example.com\">Link</a>
<div class=\"aclass bclass cool\">
<span disabled anotherattr=\"\" thirdthing=\"44\" id=\"attrspan\"
Capitalized=\"UPPERCASED\">
<div class=\"subdiv cool\" id=\"deepestdiv\">Div</div>
</span>
<!-- Comment 2 -->
<span id=\"anid\" class=\"line-feed-ahead
cool\">Span</span>
</div>
</body>
</html>")
<!-- Comment 1 -->
<html>
<head></head>
<body>
<h1>Heading</h1>
<p>Paragraph</p>
<a href=\"http://example.com\">Link</a>
<div class=\"aclass bclass cool\">
<span disabled anotherattr=\"\" thirdthing=\"44\" id=\"attrspan\" Capitalized=\"UPPERCASED\">
<div class=\"subdiv cool\" id=\"deepestdiv\">Div</div>
</span>
<!-- Comment 2 -->
<span id=\"anid\" class=\"line-feed-ahead cool\">Span</span>
</div>
</body>
</html>")

(def html2
"<!DOCTYPE html>
<html>
<head></head>
<body>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<p>Paragraph 3</p>
<p>Paragraph 4</p>
<p>Paragraph 5</p>
<p>Paragraph 6</p>
<p>Paragraph 7</p>
<p>Paragraph 8</p>
</body>
</html>")
<html>
<head></head>
<body>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<p>Paragraph 3</p>
<p>Paragraph 4</p>
<p>Paragraph 5</p>
<p>Paragraph 6</p>
<p>Paragraph 7</p>
<p>Paragraph 8</p>
</body>
</html>")

(deftest select-next-loc-test
(testing "The select-next-loc function."
Expand Down Expand Up @@ -232,7 +230,15 @@ cool\">Span</span>
(= :h1 (-> selection first :tag)))))
(let [selection (select/select (select/find-in-text #"Div") htree)]
(is (and (= 1 (count selection))
(= :div (-> selection first :tag))))))
(= :div (-> selection first :tag)))))
(let [selection-locs (select/select-locs
(select/child (select/tag :body)
(select/find-in-text #"Paragraph"))
htree)
selection (mapv zip/node selection-locs)]
(is (and (= 1 (count selection))
(= :p (-> selection first :tag))
(= :body (-> selection-locs first zip/up zip/node :tag))))))
(let [htree (hickory/as-hickory (hickory/parse html2))]
(let [selection (select/select (select/find-in-text #"Paragraph") htree)]
(is (and (= 8 (count selection))
Expand Down
Loading