Skip to content

img attrs: srcset, currentSrc, sizes, referrerPolicy, decoding, loading #40

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

Merged
merged 2 commits into from
Dec 11, 2020
Merged
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
79 changes: 75 additions & 4 deletions src/Web/HTML/HTMLImageElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,56 @@ exports.setSrc = function (src) {

// ----------------------------------------------------------------------------

exports.crossOrigin = function (image) {
exports.srcset = function (image) {
return function () {
return image.crossOrigin;
return image.srcset;
};
};

exports.setCrossOrigin = function (crossOrigin) {
exports.setSrcset = function (srcset) {
return function (image) {
return function () {
image.crossOrigin = crossOrigin;
image.srcset = srcset;
};
};
};

// ----------------------------------------------------------------------------

exports.sizes = function (image) {
return function () {
return image.sizes;
};
};

exports.setSizes = function (sizes) {
return function (image) {
return function () {
image.sizes = sizes;
};
};
};

// ----------------------------------------------------------------------------

exports.currentSrc = function (image) {
return function () {
return image.currentSrc;
};
};

// ----------------------------------------------------------------------------

exports._crossOrigin = function (image) {
return image.crossOrigin;
};

exports._setCrossOrigin = function (crossOrigin, image) {
image.crossOrigin = crossOrigin;
};

// ----------------------------------------------------------------------------

exports.useMap = function (image) {
return function () {
return image.useMap;
Expand Down Expand Up @@ -143,6 +177,43 @@ exports.naturalHeight = function (image) {

// ----------------------------------------------------------------------------

exports.referrerPolicy = function (image) {
return function () {
return image.referrerPolicy;
};
};

exports.setReferrerPolicy = function (referrerPolicy) {
return function (image) {
return function () {
image.referrerPolicy = referrerPolicy;
};
};
};


// ----------------------------------------------------------------------------

exports._decoding = function (image) {
return image.decoding;
};

exports._setDecoding = function (decoding, image) {
image.decoding = decoding;
};

// ----------------------------------------------------------------------------

exports._loading = function (image) {
return image.loading;
};

exports._setLoading = function (loading, image) {
image.loading = loading;
};

// ----------------------------------------------------------------------------

exports.complete = function (image) {
return function () {
return image.complete;
Expand Down
67 changes: 63 additions & 4 deletions src/Web/HTML/HTMLImageElement.purs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ module Web.HTML.HTMLImageElement
, setAlt
, src
, setSrc
, srcset
, setSrcset
, currentSrc
, sizes
, setSizes
, crossOrigin
, setCrossOrigin
, useMap
Expand All @@ -32,16 +37,31 @@ module Web.HTML.HTMLImageElement
, setHeight
, naturalWidth
, naturalHeight
, referrerPolicy
, setReferrerPolicy
, decoding
, setDecoding
, loading
, setLoading
, complete
) where

import Data.Maybe (Maybe)
import Data.Nullable (Nullable)
import Data.Nullable as Nullable
import Data.Maybe (Maybe, fromMaybe)
import Effect (Effect)
import Prelude (Unit)
import Effect.Uncurried (EffectFn1, EffectFn2, runEffectFn1, runEffectFn2)
import Prelude (Unit, map, (<<<), (<=<))
import Unsafe.Coerce (unsafeCoerce)
import Web.DOM (ChildNode, Element, Node, NonDocumentTypeChildNode, ParentNode)
import Web.Event.EventTarget (EventTarget)
import Web.HTML.HTMLElement (HTMLElement)
import Web.HTML.HTMLImageElement.CORSMode (CORSMode)
import Web.HTML.HTMLImageElement.CORSMode as CORSMode
import Web.HTML.HTMLImageElement.DecodingHint (DecodingHint)
import Web.HTML.HTMLImageElement.DecodingHint as DecodingHint
import Web.HTML.HTMLImageElement.Laziness (Laziness)
import Web.HTML.HTMLImageElement.Laziness as Laziness
import Web.Internal.FFI (unsafeReadProtoTagged)

foreign import data HTMLImageElement :: Type
Expand Down Expand Up @@ -101,8 +121,23 @@ foreign import setAlt :: String -> HTMLImageElement -> Effect Unit
foreign import src :: HTMLImageElement -> Effect String
foreign import setSrc :: String -> HTMLImageElement -> Effect Unit

foreign import crossOrigin :: HTMLImageElement -> Effect String
foreign import setCrossOrigin :: String -> HTMLImageElement -> Effect Unit
foreign import srcset :: HTMLImageElement -> Effect String
foreign import setSrcset :: String -> HTMLImageElement -> Effect Unit

foreign import currentSrc :: HTMLImageElement -> Effect String

foreign import sizes :: HTMLImageElement -> Effect String
foreign import setSizes :: String -> HTMLImageElement -> Effect Unit

foreign import _crossOrigin :: EffectFn1 HTMLImageElement (Nullable String)

crossOrigin :: HTMLImageElement -> Effect (Maybe CORSMode)
crossOrigin = map (CORSMode.parse <=< Nullable.toMaybe) <<< runEffectFn1 _crossOrigin

foreign import _setCrossOrigin :: EffectFn2 String HTMLImageElement Unit

setCrossOrigin :: CORSMode -> HTMLImageElement -> Effect Unit
setCrossOrigin mode = runEffectFn2 _setCrossOrigin (CORSMode.print mode)

foreign import useMap :: HTMLImageElement -> Effect String
foreign import setUseMap :: String -> HTMLImageElement -> Effect Unit
Expand All @@ -118,4 +153,28 @@ foreign import setHeight :: Int -> HTMLImageElement -> Effect Unit

foreign import naturalWidth :: HTMLImageElement -> Effect Int
foreign import naturalHeight :: HTMLImageElement -> Effect Int

foreign import referrerPolicy :: HTMLImageElement -> Effect String
foreign import setReferrerPolicy :: String -> HTMLImageElement -> Effect Unit

foreign import _decoding :: EffectFn1 HTMLImageElement String

decoding :: HTMLImageElement -> Effect DecodingHint
decoding = map (fromMaybe DecodingHint.Auto <<< DecodingHint.parse) <<< runEffectFn1 _decoding

foreign import _setDecoding :: EffectFn2 String HTMLImageElement Unit

setDecoding :: DecodingHint -> HTMLImageElement -> Effect Unit
setDecoding hint = runEffectFn2 _setDecoding (DecodingHint.print hint)

foreign import _loading :: EffectFn1 HTMLImageElement String

loading :: HTMLImageElement -> Effect Laziness
loading = map (fromMaybe Laziness.Eager <<< Laziness.parse) <<< runEffectFn1 _loading

foreign import _setLoading :: EffectFn2 String HTMLImageElement Unit

setLoading :: Laziness -> HTMLImageElement -> Effect Unit
setLoading laziness = runEffectFn2 _setLoading (Laziness.print laziness)

foreign import complete :: HTMLImageElement -> Effect Boolean
32 changes: 32 additions & 0 deletions src/Web/HTML/HTMLImageElement/CORSMode.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Web.HTML.HTMLImageElement.CORSMode
( CORSMode(..)
, parse
, print
) where

import Data.Maybe (Maybe(..))
import Prelude (class Eq, class Ord, class Show)

data CORSMode
= Anonymous
| UseCredentials

derive instance eqCORSMode :: Eq CORSMode
derive instance ordCORSMode :: Ord CORSMode

instance showDecodingHint :: Show CORSMode where
show = case _ of
Anonymous -> "Anonymous"
UseCredentials -> "UseCredentials"

parse :: String -> Maybe CORSMode
parse = case _ of
"" -> Just Anonymous
"anonymous" -> Just Anonymous
"use-credentials" -> Just UseCredentials
_ -> Nothing

print :: CORSMode -> String
print = case _ of
Anonymous -> "anonymous"
UseCredentials -> "use-credentials"
36 changes: 36 additions & 0 deletions src/Web/HTML/HTMLImageElement/DecodingHint.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Web.HTML.HTMLImageElement.DecodingHint
( DecodingHint(..)
, parse
, print
) where

import Data.Maybe (Maybe(..))
import Prelude (class Eq, class Ord, class Show)

data DecodingHint
= Sync
| Async
| Auto

derive instance eqDecodingHint :: Eq DecodingHint
derive instance ordDecodingHint :: Ord DecodingHint

instance showDecodingHint :: Show DecodingHint where
show = case _ of
Sync -> "Sync"
Async -> "Async"
Auto -> "Auto"

parse :: String -> Maybe DecodingHint
parse = case _ of
"" -> Just Auto
"sync" -> Just Sync
"async" -> Just Async
"auto" -> Just Auto
_ -> Nothing

print :: DecodingHint -> String
print = case _ of
Sync -> "sync"
Async -> "async"
Auto -> "auto"
33 changes: 33 additions & 0 deletions src/Web/HTML/HTMLImageElement/Laziness.purs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module Web.HTML.HTMLImageElement.Laziness
( Laziness(..)
, parse
, print
) where

import Data.Maybe (Maybe(..))
import Prelude (class Eq, class Ord, class Show)

data Laziness
= Eager
| Lazy

derive instance eqDecodingHint :: Eq Laziness
derive instance ordDecodingHint :: Ord Laziness

instance showDecodingHint :: Show Laziness where
show = case _ of
Eager -> "Eager"
Lazy -> "Lazy"

parse :: String -> Maybe Laziness
parse = case _ of
"" -> Just Eager
"eager" -> Just Eager
"lazy" -> Just Lazy
_ -> Nothing


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

print :: Laziness -> String
print = case _ of
Eager -> "eager"
Lazy -> "lazy"