-
Notifications
You must be signed in to change notification settings - Fork 14
/
Extra.elm
116 lines (91 loc) · 3.4 KB
/
Extra.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
module Html.Attributes.Extra exposing (..)
{-| Additional attributes for html
# Inputs
@docs valueAsFloat, valueAsInt
# Semantic web
@docs role
# Meter element
@docs low, high, optimum
# Media element
@docs volume
# Unescaped HTML
@docs innerHtml
# Custom Attributes
@docs stringProperty
@docs boolProperty
@docs floatProperty
@docs intProperty
-}
import Html exposing (Attribute)
import Html.Attributes exposing (attribute, property)
import Json.Encode as Json
{-| Create arbitrary string *properties*.
-}
stringProperty : String -> String -> Attribute msg
stringProperty name string =
property name (Json.string string)
{-| Create arbitrary bool *properties*.
-}
boolProperty : String -> Bool -> Attribute msg
boolProperty name bool =
property name (Json.bool bool)
{-| Create arbitrary floating-point *properties*.
-}
floatProperty : String -> Float -> Attribute msg
floatProperty name float =
property name (Json.float float)
{-| Create arbitrary integer *properties*.
-}
intProperty : String -> Int -> Attribute msg
intProperty name int =
property name (Json.int int)
{-| Uses `valueAsNumber` to update an input with a floating-point value.
This should only be used on <input> of type `number`, `range`, or `date`.
It differs from `value` in that a floating point value will not necessarily overwrite the contents on an input element.
valueAsFloat 2.5 -- e.g. will not change the displayed value for input showing "2.5000"
valueAsFloat 0.4 -- e.g. will not change the displayed value for input showing ".4"
-}
valueAsFloat : Float -> Attribute msg
valueAsFloat value =
floatProperty "valueAsNumber" value
{-| Uses `valueAsNumber` to update an input with an integer value.
This should only be used on <input> of type `number`, `range`, or `date`.
It differs from `value` in that an integer value will not necessarily overwrite the contents on an input element.
valueAsInt 18 -- e.g. will not change the displayed value for input showing "00018"
-}
valueAsInt : Int -> Attribute msg
valueAsInt value =
intProperty "valueAsNumber" value
{-| Used to annotate markup languages with machine-extractable semantic information about the purpose of an element.
See the [official specs](http://www.w3.org/TR/role-attribute/).
-}
role : String -> Attribute msg
role r =
attribute "role" r
{-| The upper numeric bound of the low end of the measured range, used with the meter element.
-}
low : String -> Attribute msg
low =
stringProperty "low"
{-| The lower numeric bound of the high end of the measured range, used with the meter element.
-}
high : String -> Attribute msg
high =
stringProperty "high"
{-| This attribute indicates the optimal numeric value, used with the meter element.
-}
optimum : String -> Attribute msg
optimum =
stringProperty "optimum"
{-| Audio volume, starting from 0.0 (silent) up to 1.0 (loudest).
-}
volume : Float -> Attribute msg
volume =
floatProperty "volume"
{-| Useful for inserting arbitrary unescaped HTML into an element. This function comes with some caveats.
* **Security:** You should never pass untrusted strings (e.g. from user input) to this function. Doing so will lead to [XSS](https://www.owasp.org/index.php/Cross-site_Scripting_(XSS)) vulnerabilities.
* **Performance:** The virtual DOM subsystem is not aware of HTML inserted in this manner, so these HTML fragments will be slower.
-}
innerHtml : String -> Attribute msg
innerHtml =
stringProperty "innerHTML"