Skip to content

Commit bb37b29

Browse files
authored
Add image attributes: srcset, currentSrc, sizes, referrerPolicy, decoding, loading (purescript-web#40)
* img attrs: srcset, currentSrc, sizes, referrerPolicy, decoding, loading * adts for some attrs + sizes exports
1 parent aaa38f3 commit bb37b29

File tree

5 files changed

+239
-8
lines changed

5 files changed

+239
-8
lines changed

src/Web/HTML/HTMLImageElement.js

+75-4
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,56 @@ exports.setSrc = function (src) {
4848

4949
// ----------------------------------------------------------------------------
5050

51-
exports.crossOrigin = function (image) {
51+
exports.srcset = function (image) {
5252
return function () {
53-
return image.crossOrigin;
53+
return image.srcset;
5454
};
5555
};
5656

57-
exports.setCrossOrigin = function (crossOrigin) {
57+
exports.setSrcset = function (srcset) {
5858
return function (image) {
5959
return function () {
60-
image.crossOrigin = crossOrigin;
60+
image.srcset = srcset;
6161
};
6262
};
6363
};
6464

6565
// ----------------------------------------------------------------------------
6666

67+
exports.sizes = function (image) {
68+
return function () {
69+
return image.sizes;
70+
};
71+
};
72+
73+
exports.setSizes = function (sizes) {
74+
return function (image) {
75+
return function () {
76+
image.sizes = sizes;
77+
};
78+
};
79+
};
80+
81+
// ----------------------------------------------------------------------------
82+
83+
exports.currentSrc = function (image) {
84+
return function () {
85+
return image.currentSrc;
86+
};
87+
};
88+
89+
// ----------------------------------------------------------------------------
90+
91+
exports._crossOrigin = function (image) {
92+
return image.crossOrigin;
93+
};
94+
95+
exports._setCrossOrigin = function (crossOrigin, image) {
96+
image.crossOrigin = crossOrigin;
97+
};
98+
99+
// ----------------------------------------------------------------------------
100+
67101
exports.useMap = function (image) {
68102
return function () {
69103
return image.useMap;
@@ -142,6 +176,43 @@ exports.naturalHeight = function (image) {
142176

143177
// ----------------------------------------------------------------------------
144178

179+
exports.referrerPolicy = function (image) {
180+
return function () {
181+
return image.referrerPolicy;
182+
};
183+
};
184+
185+
exports.setReferrerPolicy = function (referrerPolicy) {
186+
return function (image) {
187+
return function () {
188+
image.referrerPolicy = referrerPolicy;
189+
};
190+
};
191+
};
192+
193+
194+
// ----------------------------------------------------------------------------
195+
196+
exports._decoding = function (image) {
197+
return image.decoding;
198+
};
199+
200+
exports._setDecoding = function (decoding, image) {
201+
image.decoding = decoding;
202+
};
203+
204+
// ----------------------------------------------------------------------------
205+
206+
exports._loading = function (image) {
207+
return image.loading;
208+
};
209+
210+
exports._setLoading = function (loading, image) {
211+
image.loading = loading;
212+
};
213+
214+
// ----------------------------------------------------------------------------
215+
145216
exports.complete = function (image) {
146217
return function () {
147218
return image.complete;

src/Web/HTML/HTMLImageElement.purs

+63-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ module Web.HTML.HTMLImageElement
2020
, setAlt
2121
, src
2222
, setSrc
23+
, srcset
24+
, setSrcset
25+
, currentSrc
26+
, sizes
27+
, setSizes
2328
, crossOrigin
2429
, setCrossOrigin
2530
, useMap
@@ -32,16 +37,31 @@ module Web.HTML.HTMLImageElement
3237
, setHeight
3338
, naturalWidth
3439
, naturalHeight
40+
, referrerPolicy
41+
, setReferrerPolicy
42+
, decoding
43+
, setDecoding
44+
, loading
45+
, setLoading
3546
, complete
3647
) where
3748

38-
import Data.Maybe (Maybe)
49+
import Data.Nullable (Nullable)
50+
import Data.Nullable as Nullable
51+
import Data.Maybe (Maybe, fromMaybe)
3952
import Effect (Effect)
40-
import Prelude (Unit)
53+
import Effect.Uncurried (EffectFn1, EffectFn2, runEffectFn1, runEffectFn2)
54+
import Prelude (Unit, map, (<<<), (<=<))
4155
import Unsafe.Coerce (unsafeCoerce)
4256
import Web.DOM (ChildNode, Element, Node, NonDocumentTypeChildNode, ParentNode)
4357
import Web.Event.EventTarget (EventTarget)
4458
import Web.HTML.HTMLElement (HTMLElement)
59+
import Web.HTML.HTMLImageElement.CORSMode (CORSMode)
60+
import Web.HTML.HTMLImageElement.CORSMode as CORSMode
61+
import Web.HTML.HTMLImageElement.DecodingHint (DecodingHint)
62+
import Web.HTML.HTMLImageElement.DecodingHint as DecodingHint
63+
import Web.HTML.HTMLImageElement.Laziness (Laziness)
64+
import Web.HTML.HTMLImageElement.Laziness as Laziness
4565
import Web.Internal.FFI (unsafeReadProtoTagged)
4666

4767
foreign import data HTMLImageElement :: Type
@@ -101,8 +121,23 @@ foreign import setAlt :: String -> HTMLImageElement -> Effect Unit
101121
foreign import src :: HTMLImageElement -> Effect String
102122
foreign import setSrc :: String -> HTMLImageElement -> Effect Unit
103123

104-
foreign import crossOrigin :: HTMLImageElement -> Effect String
105-
foreign import setCrossOrigin :: String -> HTMLImageElement -> Effect Unit
124+
foreign import srcset :: HTMLImageElement -> Effect String
125+
foreign import setSrcset :: String -> HTMLImageElement -> Effect Unit
126+
127+
foreign import currentSrc :: HTMLImageElement -> Effect String
128+
129+
foreign import sizes :: HTMLImageElement -> Effect String
130+
foreign import setSizes :: String -> HTMLImageElement -> Effect Unit
131+
132+
foreign import _crossOrigin :: EffectFn1 HTMLImageElement (Nullable String)
133+
134+
crossOrigin :: HTMLImageElement -> Effect (Maybe CORSMode)
135+
crossOrigin = map (CORSMode.parse <=< Nullable.toMaybe) <<< runEffectFn1 _crossOrigin
136+
137+
foreign import _setCrossOrigin :: EffectFn2 String HTMLImageElement Unit
138+
139+
setCrossOrigin :: CORSMode -> HTMLImageElement -> Effect Unit
140+
setCrossOrigin mode = runEffectFn2 _setCrossOrigin (CORSMode.print mode)
106141

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

119154
foreign import naturalWidth :: HTMLImageElement -> Effect Int
120155
foreign import naturalHeight :: HTMLImageElement -> Effect Int
156+
157+
foreign import referrerPolicy :: HTMLImageElement -> Effect String
158+
foreign import setReferrerPolicy :: String -> HTMLImageElement -> Effect Unit
159+
160+
foreign import _decoding :: EffectFn1 HTMLImageElement String
161+
162+
decoding :: HTMLImageElement -> Effect DecodingHint
163+
decoding = map (fromMaybe DecodingHint.Auto <<< DecodingHint.parse) <<< runEffectFn1 _decoding
164+
165+
foreign import _setDecoding :: EffectFn2 String HTMLImageElement Unit
166+
167+
setDecoding :: DecodingHint -> HTMLImageElement -> Effect Unit
168+
setDecoding hint = runEffectFn2 _setDecoding (DecodingHint.print hint)
169+
170+
foreign import _loading :: EffectFn1 HTMLImageElement String
171+
172+
loading :: HTMLImageElement -> Effect Laziness
173+
loading = map (fromMaybe Laziness.Eager <<< Laziness.parse) <<< runEffectFn1 _loading
174+
175+
foreign import _setLoading :: EffectFn2 String HTMLImageElement Unit
176+
177+
setLoading :: Laziness -> HTMLImageElement -> Effect Unit
178+
setLoading laziness = runEffectFn2 _setLoading (Laziness.print laziness)
179+
121180
foreign import complete :: HTMLImageElement -> Effect Boolean
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module Web.HTML.HTMLImageElement.CORSMode
2+
( CORSMode(..)
3+
, parse
4+
, print
5+
) where
6+
7+
import Data.Maybe (Maybe(..))
8+
import Prelude (class Eq, class Ord, class Show)
9+
10+
data CORSMode
11+
= Anonymous
12+
| UseCredentials
13+
14+
derive instance eqCORSMode :: Eq CORSMode
15+
derive instance ordCORSMode :: Ord CORSMode
16+
17+
instance showDecodingHint :: Show CORSMode where
18+
show = case _ of
19+
Anonymous -> "Anonymous"
20+
UseCredentials -> "UseCredentials"
21+
22+
parse :: String -> Maybe CORSMode
23+
parse = case _ of
24+
"" -> Just Anonymous
25+
"anonymous" -> Just Anonymous
26+
"use-credentials" -> Just UseCredentials
27+
_ -> Nothing
28+
29+
print :: CORSMode -> String
30+
print = case _ of
31+
Anonymous -> "anonymous"
32+
UseCredentials -> "use-credentials"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module Web.HTML.HTMLImageElement.DecodingHint
2+
( DecodingHint(..)
3+
, parse
4+
, print
5+
) where
6+
7+
import Data.Maybe (Maybe(..))
8+
import Prelude (class Eq, class Ord, class Show)
9+
10+
data DecodingHint
11+
= Sync
12+
| Async
13+
| Auto
14+
15+
derive instance eqDecodingHint :: Eq DecodingHint
16+
derive instance ordDecodingHint :: Ord DecodingHint
17+
18+
instance showDecodingHint :: Show DecodingHint where
19+
show = case _ of
20+
Sync -> "Sync"
21+
Async -> "Async"
22+
Auto -> "Auto"
23+
24+
parse :: String -> Maybe DecodingHint
25+
parse = case _ of
26+
"" -> Just Auto
27+
"sync" -> Just Sync
28+
"async" -> Just Async
29+
"auto" -> Just Auto
30+
_ -> Nothing
31+
32+
print :: DecodingHint -> String
33+
print = case _ of
34+
Sync -> "sync"
35+
Async -> "async"
36+
Auto -> "auto"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module Web.HTML.HTMLImageElement.Laziness
2+
( Laziness(..)
3+
, parse
4+
, print
5+
) where
6+
7+
import Data.Maybe (Maybe(..))
8+
import Prelude (class Eq, class Ord, class Show)
9+
10+
data Laziness
11+
= Eager
12+
| Lazy
13+
14+
derive instance eqDecodingHint :: Eq Laziness
15+
derive instance ordDecodingHint :: Ord Laziness
16+
17+
instance showDecodingHint :: Show Laziness where
18+
show = case _ of
19+
Eager -> "Eager"
20+
Lazy -> "Lazy"
21+
22+
parse :: String -> Maybe Laziness
23+
parse = case _ of
24+
"" -> Just Eager
25+
"eager" -> Just Eager
26+
"lazy" -> Just Lazy
27+
_ -> Nothing
28+
29+
30+
print :: Laziness -> String
31+
print = case _ of
32+
Eager -> "eager"
33+
Lazy -> "lazy"

0 commit comments

Comments
 (0)