From a1ead38c39ceabdfc0e0e276645c93c8d485b37f Mon Sep 17 00:00:00 2001 From: Orion Kindel Date: Thu, 5 Oct 2023 09:24:21 -0500 Subject: [PATCH] fix: screenshot marshaling --- spago.yaml | 1 + src/Puppeteer.Handle.HTML.purs | 9 +-------- src/Puppeteer.Screenshot.purs | 2 +- test/Puppeteer.Handle.Spec.purs | 25 +++++++++++++++++++++++-- 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/spago.yaml b/spago.yaml index ee3ed61..a7ebc15 100644 --- a/spago.yaml +++ b/spago.yaml @@ -23,6 +23,7 @@ package: - node-path - node-process - node-streams + - node-url - nullable - ordered-collections - parallel diff --git a/src/Puppeteer.Handle.HTML.purs b/src/Puppeteer.Handle.HTML.purs index ce5ecdd..2391763 100644 --- a/src/Puppeteer.Handle.HTML.purs +++ b/src/Puppeteer.Handle.HTML.purs @@ -83,14 +83,7 @@ offsetHeight = Eval.unsafeRunJs0 "e => e.offsetHeight" attrs :: forall a. IsElement a => Handle a -> Aff (Map String String) attrs = let - js = String.joinWith "\n" - [ "e => Array.from(e.attributes)" - , " .reduce(" - , " (m, a) => [...m, {k: a.name, v: a.value}]," - , " []," - , " )" - , " .filter(({k}) => k)" - ] + js = "e => Array.from(e.attributes).map(a => ({k: a.name, v: a.value}))" in map FFI.makeMap <<< Eval.unsafeRunJs0 @(Array { k :: String, v :: String }) js diff --git a/src/Puppeteer.Screenshot.purs b/src/Puppeteer.Screenshot.purs index 03d68cb..ba4b08f 100644 --- a/src/Puppeteer.Screenshot.purs +++ b/src/Puppeteer.Screenshot.purs @@ -59,5 +59,5 @@ prepareScreenshotOptions , omitBackground: FFI.maybeToUndefined omitBackground , optimizeForSpeed: FFI.maybeToUndefined optimizeForSpeed , quality: FFI.maybeToUndefined quality - , format: FFI.maybeToUndefined $ map prepareScreenshotFormat format + , type: FFI.maybeToUndefined $ map prepareScreenshotFormat format } diff --git a/test/Puppeteer.Handle.Spec.purs b/test/Puppeteer.Handle.Spec.purs index d383f6f..f8568b9 100644 --- a/test/Puppeteer.Handle.Spec.purs +++ b/test/Puppeteer.Handle.Spec.purs @@ -2,19 +2,23 @@ module Puppeteer.Handle.Spec where import Prelude -import Control.Monad.Error.Class (liftMaybe) +import Control.Monad.Error.Class (liftMaybe, try) import Control.Monad.Rec.Class (Step(..), tailRecM) import Data.Array as Array +import Data.Either (hush) +import Data.Filterable (filterMap) import Data.Map as Map -import Data.Set as Set import Data.Maybe (isJust) import Data.Newtype (wrap) +import Data.Set as Set +import Data.Traversable (for) import Data.Tuple.Nested ((/\)) import Effect (Effect) import Effect.Aff (Aff, forkAff, joinFiber) import Effect.Class (liftEffect) import Effect.Exception (error) import Node.Buffer as Buffer +import Node.URL as Node.URL import Puppeteer as Pup import Puppeteer.Base (timeoutThrow) import Puppeteer.Browser as Pup.Browser @@ -91,6 +95,12 @@ html =
+ + + + + + @@ -214,3 +224,14 @@ spec = withPage $ describe "Handle" do describe "HTML" do test "equals" findFirstOrHtmlEquals + test "attrs" \p -> do + anchors <- Pup.Page.findAll (S.anchor `S.hasAttr` "href") p + hrefs <- filterMap (Map.lookup "href") <$> for anchors Pup.Handle.HTML.attrs + urls <- liftEffect $ filterMap hush <$> for hrefs (try <<< Node.URL.new) + Array.length urls `shouldEqual` 3 + + anchors' <- Pup.Page.findAll (S.anchor `S.hasAttr` "href" `S.hasAttr` "disabled") p + hrefs' <- filterMap (Map.lookup "href") <$> for anchors' Pup.Handle.HTML.attrs + urls' <- liftEffect $ filterMap hush <$> for hrefs' (try <<< Node.URL.new) + Array.length urls' `shouldEqual` 0 + pure unit