From 4145c4634c4ffa3d530412e78228dead52514dc7 Mon Sep 17 00:00:00 2001 From: Gareth Daniel Smith Date: Sun, 29 Jul 2018 14:31:38 +0100 Subject: [PATCH] Change the type of the attr function so it returns Maybe instead of breaking with a javascript error when there are no selected elements. --- src/Cheerio.js | 9 ++++----- src/Cheerio.purs | 15 ++++++++++----- test/Test/Cheerio.purs | 26 +++++++++++++++++++++++--- test/Test/Cheerio/Static.purs | 2 +- 4 files changed, 38 insertions(+), 14 deletions(-) diff --git a/src/Cheerio.js b/src/Cheerio.js index eaec570..73ec97e 100644 --- a/src/Cheerio.js +++ b/src/Cheerio.js @@ -1,8 +1,8 @@ const cheerio = require('cheerio') // Attributes -exports.attrImpl = function(name, cheerioInst) { - return cheerioInst.attr(name); +exports.attrImpl = function(nothing, just, name, cheerioInst) { + return cheerioInst.length ? just(cheerioInst.attr(name)) : nothing; } exports.hasClassImpl = function(className, cheerioInst) { @@ -48,8 +48,7 @@ exports.eqImpl = function(index, cheerioInst) { // Rendering exports.htmlImpl = function(nothing, just, cheerioInst) { - const ret = cheerioInst.html() - return ret != null ? just(ret) : nothing + return cheerioInst.length ? just(cheerioInst.html()) : nothing; } exports.text = function(cheerioInst) { @@ -59,4 +58,4 @@ exports.text = function(cheerioInst) { // Miscellaneous exports.length = function(cheerioInst) { return cheerioInst.length; -} \ No newline at end of file +} diff --git a/src/Cheerio.purs b/src/Cheerio.purs index 95728f3..888ab8c 100644 --- a/src/Cheerio.purs +++ b/src/Cheerio.purs @@ -1,15 +1,18 @@ 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(..)) foreign import data Cheerio :: Type -- 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 -attr = runFn2 attrImpl +-- | Gets an attribute value from the first selected element, returning +-- | Nothing when there are no selected elements. +attr :: String -> Cheerio -> Maybe String +attr = runFn4 attrImpl Nothing Just foreign import hasClassImpl :: Fn2 String Cheerio Boolean @@ -39,10 +42,12 @@ eq = runFn2 eqImpl foreign import htmlImpl :: forall a. 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 = runFn3 htmlImpl Nothing Just foreign import text :: Cheerio -> String -- Miscellaneous -foreign import length :: Cheerio -> Int \ No newline at end of file +foreign import length :: Cheerio -> Int diff --git a/test/Test/Cheerio.purs b/test/Test/Cheerio.purs index 26f8fca..c4d0e30 100644 --- a/test/Test/Cheerio.purs +++ b/test/Test/Cheerio.purs @@ -9,7 +9,8 @@ import Test.Unit.Assert as Assert import Test.Unit.Main (runTest) import Cheerio - ( attr + ( Cheerio + , attr , children , eq , find @@ -32,14 +33,21 @@ import Test.HtmlEx (htmlEx) main :: Effect Unit main = runTest suites +emptyCheerio :: Cheerio +emptyCheerio = loadRoot htmlEx # find ".no-such-element" + suites :: TestSuite suites = do suite "Attributes" do test "attr" do Assert.equal - "fruits" + (Just "fruits") (loadRoot htmlEx # find "ul" # attr "id") + Assert.equal + Nothing + (emptyCheerio # attr "id") + test "hasClass" do Assert.equal true @@ -53,6 +61,10 @@ suites = do true (loadRoot htmlEx # find "li" # hasClass "pear") + Assert.equal + false + (emptyCheerio # hasClass "pear") + suite "Traversing" do test "find" do Assert.equal @@ -61,7 +73,7 @@ suites = do test "parent" do Assert.equal - "fruits" + (Just "fruits") (loadRoot htmlEx # find ".pear" # parent # attr "id") test "next" do @@ -109,11 +121,19 @@ suites = do (Just "Apple") (loadRoot htmlEx # find ".apple" # html) + Assert.equal + Nothing + (emptyCheerio # html) + test "text" do Assert.equal "Apple" (loadRoot htmlEx # find ".apple" # text) + Assert.equal + "" + (emptyCheerio # text) + suite "More" do test "Long chain" do Assert.equal diff --git a/test/Test/Cheerio/Static.purs b/test/Test/Cheerio/Static.purs index 0534030..3060ae1 100644 --- a/test/Test/Cheerio/Static.purs +++ b/test/Test/Cheerio/Static.purs @@ -35,7 +35,7 @@ suites = do suite "Selectors" do test "select" do Assert.equal - "pear" + (Just "pear") (load htmlEx # select "ul .pear" # attr "class") test "selectDeep" do