Change the type of the attr function so it returns Maybe instead of

breaking with a javascript error when there are no selected elements.
This commit is contained in:
Gareth Daniel Smith 2018-07-29 14:31:38 +01:00
parent 57983d156d
commit 4145c4634c
4 changed files with 38 additions and 14 deletions

View File

@ -1,8 +1,8 @@
const cheerio = require('cheerio') const cheerio = require('cheerio')
// Attributes // Attributes
exports.attrImpl = function(name, cheerioInst) { exports.attrImpl = function(nothing, just, name, cheerioInst) {
return cheerioInst.attr(name); return cheerioInst.length ? just(cheerioInst.attr(name)) : nothing;
} }
exports.hasClassImpl = function(className, cheerioInst) { exports.hasClassImpl = function(className, cheerioInst) {
@ -48,8 +48,7 @@ exports.eqImpl = function(index, cheerioInst) {
// Rendering // Rendering
exports.htmlImpl = function(nothing, just, cheerioInst) { exports.htmlImpl = function(nothing, just, cheerioInst) {
const ret = cheerioInst.html() return cheerioInst.length ? just(cheerioInst.html()) : nothing;
return ret != null ? just(ret) : nothing
} }
exports.text = function(cheerioInst) { exports.text = function(cheerioInst) {

View File

@ -1,15 +1,18 @@
module Cheerio where module Cheerio where
import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3) import Data.Function.Uncurried (Fn2, Fn3, Fn4, runFn2, runFn3, runFn4)
import Data.Maybe (Maybe(..)) import Data.Maybe (Maybe(..))
foreign import data Cheerio :: Type foreign import data Cheerio :: Type
-- Attributes -- Attributes
foreign import attrImpl :: Fn2 String Cheerio String foreign import attrImpl :: forall a.
Fn4 (Maybe a) (a -> Maybe a) String Cheerio (Maybe String)
attr :: String -> Cheerio -> String -- | Gets an attribute value from the first selected element, returning
attr = runFn2 attrImpl -- | Nothing when there are no selected elements.
attr :: String -> Cheerio -> Maybe String
attr = runFn4 attrImpl Nothing Just
foreign import hasClassImpl :: Fn2 String Cheerio Boolean foreign import hasClassImpl :: Fn2 String Cheerio Boolean
@ -39,6 +42,8 @@ eq = runFn2 eqImpl
foreign import htmlImpl :: forall a. foreign import htmlImpl :: forall a.
Fn3 (Maybe a) (a -> Maybe a) Cheerio (Maybe String) Fn3 (Maybe a) (a -> Maybe a) Cheerio (Maybe String)
-- | Gets an html content string from the first selected element, returning
-- | Nothing when there are no selected elements.
html :: Cheerio -> Maybe String html :: Cheerio -> Maybe String
html = runFn3 htmlImpl Nothing Just html = runFn3 htmlImpl Nothing Just

View File

@ -9,7 +9,8 @@ import Test.Unit.Assert as Assert
import Test.Unit.Main (runTest) import Test.Unit.Main (runTest)
import Cheerio import Cheerio
( attr ( Cheerio
, attr
, children , children
, eq , eq
, find , find
@ -32,14 +33,21 @@ import Test.HtmlEx (htmlEx)
main :: Effect Unit main :: Effect Unit
main = runTest suites main = runTest suites
emptyCheerio :: Cheerio
emptyCheerio = loadRoot htmlEx # find ".no-such-element"
suites :: TestSuite suites :: TestSuite
suites = do suites = do
suite "Attributes" do suite "Attributes" do
test "attr" do test "attr" do
Assert.equal Assert.equal
"fruits" (Just "fruits")
(loadRoot htmlEx # find "ul" # attr "id") (loadRoot htmlEx # find "ul" # attr "id")
Assert.equal
Nothing
(emptyCheerio # attr "id")
test "hasClass" do test "hasClass" do
Assert.equal Assert.equal
true true
@ -53,6 +61,10 @@ suites = do
true true
(loadRoot htmlEx # find "li" # hasClass "pear") (loadRoot htmlEx # find "li" # hasClass "pear")
Assert.equal
false
(emptyCheerio # hasClass "pear")
suite "Traversing" do suite "Traversing" do
test "find" do test "find" do
Assert.equal Assert.equal
@ -61,7 +73,7 @@ suites = do
test "parent" do test "parent" do
Assert.equal Assert.equal
"fruits" (Just "fruits")
(loadRoot htmlEx # find ".pear" # parent # attr "id") (loadRoot htmlEx # find ".pear" # parent # attr "id")
test "next" do test "next" do
@ -109,11 +121,19 @@ suites = do
(Just "Apple") (Just "Apple")
(loadRoot htmlEx # find ".apple" # html) (loadRoot htmlEx # find ".apple" # html)
Assert.equal
Nothing
(emptyCheerio # html)
test "text" do test "text" do
Assert.equal Assert.equal
"Apple" "Apple"
(loadRoot htmlEx # find ".apple" # text) (loadRoot htmlEx # find ".apple" # text)
Assert.equal
""
(emptyCheerio # text)
suite "More" do suite "More" do
test "Long chain" do test "Long chain" do
Assert.equal Assert.equal

View File

@ -35,7 +35,7 @@ suites = do
suite "Selectors" do suite "Selectors" do
test "select" do test "select" do
Assert.equal Assert.equal
"pear" (Just "pear")
(load htmlEx # select "ul .pear" # attr "class") (load htmlEx # select "ul .pear" # attr "class")
test "selectDeep" do test "selectDeep" do