Skip to content

Commit

Permalink
add validation for no-runtime-error
Browse files Browse the repository at this point in the history
  • Loading branch information
jinjor committed Jun 25, 2016
1 parent 2c390d6 commit b52c0aa
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 deletions.
10 changes: 9 additions & 1 deletion README.md
@@ -1,5 +1,8 @@
# elm-inline-hover


[![Build Status](https://travis-ci.org/jinjor/elm-inline-hover.svg)](https://travis-ci.org/jinjor/elm-inline-hover)

An utility for using :hover by inline style.


Expand All @@ -18,4 +21,9 @@ styles =
[("background", "#abd")]
```

That's it :)
That's it!


## LICENSE

BSD3
4 changes: 2 additions & 2 deletions elm-package.json
@@ -1,5 +1,5 @@
{
"version": "1.0.0",
"version": "1.0.1",
"summary": "An utility for using :hover by inline style",
"repository": "https://github.com/jinjor/elm-inline-hover.git",
"license": "BSD3",
Expand All @@ -14,4 +14,4 @@
"elm-lang/html": "1.0.0 <= v < 2.0.0"
},
"elm-version": "0.17.0 <= v < 0.18.0"
}
}
31 changes: 27 additions & 4 deletions src/InlineHover.elm
@@ -1,4 +1,4 @@
module InlineHover exposing (hover) -- where
module InlineHover exposing (hover)

{-| Wrap any elements defined in [Html](http://package.elm-lang.org/packages/elm-lang/html/1.0.0/Html)
# Make Special Elements
Expand Down Expand Up @@ -31,11 +31,14 @@ hover
-> Html msg
hover styles tag attrs children =
let
validStyles =
List.filter (\(k, v) -> isValidKey k) styles

enter =
attribute "onmouseenter" <| String.join ";" <| List.map enterEach styles
attribute "onmouseenter" <| String.join ";" <| List.map enterEach validStyles

leave =
attribute "onmouseleave" <| String.join ";" <| List.map leaveEach styles
attribute "onmouseleave" <| String.join ";" <| List.map leaveEach validStyles

in
tag
Expand All @@ -58,13 +61,33 @@ toCamelCase s =
) (False, []) (String.toList s)


isValidKey : String -> Bool
isValidKey s =
s /= "" &&
List.any Char.isLower (String.toList s) &&
List.all (\c -> Char.isLower c || c == '-') (String.toList s)


isValidChars : List Char -> Bool
isValidChars list =
case list of
head :: tail ->
if Char.isLower head || head == '-' then
isValidChars tail
else
False
_ ->
True


enterEach : (String, String) -> String
enterEach (key, value) =
let
keyCamel = toCamelCase key
escapedValue = (String.join "\"" << String.split "'") value
in
"this.setAttribute('data-hover-" ++ key ++ "', this.style." ++ keyCamel ++ "||'');" ++
"this.style." ++ keyCamel ++ "='" ++ value ++ "'"
"this.style." ++ keyCamel ++ "='" ++ escapedValue ++ "'"


leaveEach : (String, String) -> String
Expand Down
1 change: 1 addition & 0 deletions test/Main.elm
Expand Up @@ -28,6 +28,7 @@ hoverStyle =
, ("---", "")
, ("=", "")
, ("font-family", "\"游ゴシック\", \"Yu Gothic\", sans-serif;")
, ("color", "'")
, ("color", "\'")
]

Expand Down

0 comments on commit b52c0aa

Please sign in to comment.