Skip to content
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

Add nonce option to layoutWith #341

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
41 changes: 41 additions & 0 deletions examples/BasicWithNonce.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module BasicWithNonce exposing (..)

{-| -}

import Element exposing (..)
import Element.Background as Background
import Element.Font as Font
import Element.Input
import Element.Lazy


main =
Element.layoutWith
{ options = [ Element.nonce "12345" ]
}
[ Background.color (rgba 0 0 0 1)
, Font.color (rgba 1 1 1 1)
, Font.italic
, Font.size 32
, Font.family
[ Font.external
{ url = "https://fonts.googleapis.com/css?family=EB+Garamond"
, name = "EB Garamond"
}
, Font.sansSerif
]
]
<|
Element.column [ centerX, centerY ]
[ el
[]
(text "Hello stylish friend!")
, Element.Lazy.lazy viewSignature "Matt"
]


viewSignature : String -> Element msg
viewSignature name =
el
[]
(text ("— " ++ name))
12 changes: 10 additions & 2 deletions src/Element.elm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module Element exposing
, moveUp, moveDown, moveRight, moveLeft, rotate, scale
, clip, clipX, clipY
, scrollbars, scrollbarX, scrollbarY
, layout, layoutWith, Option, noStaticStyleSheet, forceHover, noHover, focusStyle, FocusStyle
, layout, layoutWith, Option, noStaticStyleSheet, forceHover, noHover, focusStyle, FocusStyle, nonce
, link, newTabLink, download, downloadAs
, image
, Color, rgba, rgb, rgb255, rgba255, fromRgb, fromRgb255, toRgb
Expand Down Expand Up @@ -137,7 +137,7 @@ Add a scrollbar if the content is larger than the element.

# Rendering

@docs layout, layoutWith, Option, noStaticStyleSheet, forceHover, noHover, focusStyle, FocusStyle
@docs layout, layoutWith, Option, noStaticStyleSheet, forceHover, noHover, focusStyle, FocusStyle, nonce


# Links
Expand Down Expand Up @@ -479,6 +479,14 @@ noStaticStyleSheet =
Internal.RenderModeOption Internal.NoStaticStyleSheet


{-| Adds a [nonce](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/nonce)
to the style tags so that they are compliant with the Content Security Policy of your website.
-}
nonce : String -> Option
nonce nonce_ =
Internal.NonceOption (Internal.Nonce nonce_)


{-| -}
defaultFocus :
{ borderColor : Maybe Color
Expand Down
1 change: 1 addition & 0 deletions src/Element/Lazy.elm
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ embed x =
, backgroundColor = Nothing
}
, mode = Layout
, nonce = Nothing
}
styled.styles
)
Expand Down
42 changes: 39 additions & 3 deletions src/Internal/Model.elm
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module Internal.Model exposing
, Location(..)
, NearbyChildren(..)
, NodeName(..)
, Nonce(..)
, Option(..)
, OptionRecord
, Padding(..)
Expand Down Expand Up @@ -534,6 +535,7 @@ finalizeNode has node attributes children embedMode parentContext =
html


embedWith : Bool -> OptionRecord -> List Style -> List (VirtualDom.Node msg) -> List (VirtualDom.Node msg)
embedWith static opts styles children =
let
dynamicStyleSheet =
Expand Down Expand Up @@ -1824,7 +1826,10 @@ staticRoot opts =
-- wrap the style node in a div to prevent `Dark Reader` from blowin up the dom.
VirtualDom.node "div"
[]
[ VirtualDom.node "style" [] [ VirtualDom.text Internal.Style.rules ] ]
[ VirtualDom.node "style"
(nonceAttributes opts.nonce)
[ VirtualDom.text Internal.Style.rules ]
]

NoStaticStyleSheet ->
VirtualDom.text ""
Expand Down Expand Up @@ -2089,6 +2094,7 @@ type Children x
| Keyed (List ( String, x ))


toHtml : (List Style -> EmbedStyle) -> Element msg -> VirtualDom.Node msg
toHtml mode el =
case el of
Unstyled html ->
Expand All @@ -2108,9 +2114,11 @@ toHtml mode el =
renderRoot : List Option -> List (Attribute aligned msg) -> Element msg -> VirtualDom.Node msg
renderRoot optionList attributes child =
let
options : OptionRecord
options =
optionsToRecord optionList

embedStyle : List Style -> EmbedStyle
embedStyle =
case options.mode of
NoStaticStyleSheet ->
Expand All @@ -2133,9 +2141,14 @@ type alias OptionRecord =
{ hover : HoverSetting
, focus : FocusStyle
, mode : RenderMode
, nonce : Maybe Nonce
}


type Nonce
= Nonce String


type HoverSetting
= NoHover
| AllowHover
Expand All @@ -2146,6 +2159,7 @@ type Option
= HoverOption HoverSetting
| FocusStyleOption FocusStyle
| RenderModeOption RenderMode
| NonceOption Nonce


type alias FocusStyle =
Expand Down Expand Up @@ -2292,6 +2306,7 @@ focusDefaultStyle =
optionsToRecord : List Option -> OptionRecord
optionsToRecord options =
let
combine : Option -> { hover : Maybe HoverSetting, focus : Maybe FocusStyle, mode : Maybe RenderMode, nonce : Maybe Nonce } -> { hover : Maybe HoverSetting, focus : Maybe FocusStyle, mode : Maybe RenderMode, nonce : Maybe Nonce }
combine opt record =
case opt of
HoverOption hoverable ->
Expand All @@ -2318,6 +2333,15 @@ optionsToRecord options =
_ ->
record

NonceOption nonce_ ->
case record.nonce of
Nothing ->
{ record | nonce = Just nonce_ }

_ ->
record

andFinally : { hover : Maybe HoverSetting, focus : Maybe FocusStyle, mode : Maybe RenderMode, nonce : Maybe Nonce } -> OptionRecord
andFinally record =
{ hover =
case record.hover of
Expand All @@ -2340,13 +2364,15 @@ optionsToRecord options =

Just actualMode ->
actualMode
, nonce = record.nonce
}
in
andFinally <|
List.foldr combine
{ hover = Nothing
, focus = Nothing
, mode = Nothing
, nonce = Nothing
}
options

Expand All @@ -2359,7 +2385,7 @@ toStyleSheet options styleSheet =
VirtualDom.node "div"
[]
[ VirtualDom.node "style"
[]
(nonceAttributes options.nonce)
[ VirtualDom.text (toStyleSheetString options styleSheet) ]
]

Expand All @@ -2368,7 +2394,7 @@ toStyleSheet options styleSheet =
VirtualDom.node "div"
[]
[ VirtualDom.node "style"
[]
(nonceAttributes options.nonce)
[ VirtualDom.text (toStyleSheetString options styleSheet) ]
]

Expand All @@ -2380,6 +2406,16 @@ toStyleSheet options styleSheet =
[]


nonceAttributes : Maybe Nonce -> List (VirtualDom.Attribute msg)
nonceAttributes maybeNonce =
case maybeNonce of
Just (Nonce nonce) ->
[ VirtualDom.attribute "nonce" nonce ]

Nothing ->
[]


renderTopLevelValues rules =
let
withImport font =
Expand Down