Skip to content

Commit

Permalink
fix: CSP support for lazy functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pete-murphy committed Oct 28, 2024
1 parent 551376d commit 8353dad
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 3 deletions.
59 changes: 56 additions & 3 deletions src/Html/Styled/Lazy.elm
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
module Html.Styled.Lazy exposing (lazy, lazy2, lazy3, lazy4, lazy5, lazy6, lazy7)
module Html.Styled.Lazy exposing
( lazy, lazy2, lazy3, lazy4, lazy5, lazy6, lazy7
, lazyWithNonce, lazy2WithNonce, lazy3WithNonce, lazy4WithNonce, lazy5WithNonce, lazy6WithNonce
)

{-| **NOTE:** `Html.Lazy` goes up to `lazy8`, but `Html.Styled.Lazy` can only go
up to `lazy7` because it uses one of the arguments to track styling info.
{-| **NOTE:** `Html.Lazy` goes up to `lazy8` and `lazy7WithNonce`, but
`Html.Styled.Lazy` can only go up to `lazy7` and `lazy6WithNonce` because it
uses one of the arguments to track styling info.
Since all Elm functions are pure we have a guarantee that the same input
Expand All @@ -17,6 +21,7 @@ This is a really cheap test and often makes things a lot faster, but definitely
benchmark to be sure!
@docs lazy, lazy2, lazy3, lazy4, lazy5, lazy6, lazy7
@docs lazyWithNonce, lazy2WithNonce, lazy3WithNonce, lazy4WithNonce, lazy5WithNonce, lazy6WithNonce
-}

Expand Down Expand Up @@ -78,3 +83,51 @@ lazy6 =
lazy7 : (a -> b -> c -> d -> e -> f -> g -> Html msg) -> a -> b -> c -> d -> e -> f -> g -> Html msg
lazy7 =
VirtualDom.Styled.lazy7


{-| Same as [`lazy`](#lazy) but adds a
[nonce](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/nonce)
to the style tag so that it is compliant with the Content Security Policy of
your website.
If you don't need a nonce, you should use [`lazy`](#lazy).
-}
lazyWithNonce : String -> (a -> Html msg) -> a -> Html msg
lazyWithNonce =
VirtualDom.Styled.lazyWithNonce


{-| Same as `lazy2` but adds a nonce to the style tag.
-}
lazy2WithNonce : String -> (a -> b -> Html msg) -> a -> b -> Html msg
lazy2WithNonce =
VirtualDom.Styled.lazy2WithNonce


{-| Same as `lazy3` but adds a nonce to the style tag.
-}
lazy3WithNonce : String -> (a -> b -> c -> Html msg) -> a -> b -> c -> Html msg
lazy3WithNonce =
VirtualDom.Styled.lazy3WithNonce


{-| Same as `lazy4` but adds a nonce to the style tag.
-}
lazy4WithNonce : String -> (a -> b -> c -> d -> Html msg) -> a -> b -> c -> d -> Html msg
lazy4WithNonce =
VirtualDom.Styled.lazy4WithNonce


{-| Same as `lazy5` but adds a nonce to the style tag.
-}
lazy5WithNonce : String -> (a -> b -> c -> d -> e -> Html msg) -> a -> b -> c -> d -> e -> Html msg
lazy5WithNonce =
VirtualDom.Styled.lazy5WithNonce


{-| Same as `lazy6` but adds a nonce to the style tag.
-}
lazy6WithNonce : String -> (a -> b -> c -> d -> e -> f -> Html msg) -> a -> b -> c -> d -> e -> f -> Html msg
lazy6WithNonce =
VirtualDom.Styled.lazy6WithNonce
78 changes: 78 additions & 0 deletions src/VirtualDom/Styled.elm
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ module VirtualDom.Styled exposing
, keyedNodeNS
, lazy
, lazy2
, lazy2WithNonce
, lazy3
, lazy3WithNonce
, lazy4
, lazy4WithNonce
, lazy5
, lazy5WithNonce
, lazy6
, lazy6WithNonce
, lazy7
, lazyWithNonce
, makeSnippet
, map
, mapAttribute
Expand Down Expand Up @@ -256,6 +262,78 @@ lazyHelp7 fn arg1 arg2 arg3 arg4 arg5 arg6 arg7 =
|> toUnstyled


lazyWithNonce : String -> (a -> Node msg) -> a -> Node msg
lazyWithNonce nonce fn arg =
VirtualDom.lazy3 lazyHelpWithNonce nonce fn arg
|> Unstyled


lazyHelpWithNonce : String -> (a -> Node msg) -> a -> VirtualDom.Node msg
lazyHelpWithNonce nonce fn arg =
fn arg
|> toNonceUnstyled nonce


lazy2WithNonce : String -> (a -> b -> Node msg) -> a -> b -> Node msg
lazy2WithNonce nonce fn arg1 arg2 =
VirtualDom.lazy4 lazyHelp2WithNonce nonce fn arg1 arg2
|> Unstyled


lazyHelp2WithNonce : String -> (a -> b -> Node msg) -> a -> b -> VirtualDom.Node msg
lazyHelp2WithNonce nonce fn arg1 arg2 =
fn arg1 arg2
|> toNonceUnstyled nonce


lazy3WithNonce : String -> (a -> b -> c -> Node msg) -> a -> b -> c -> Node msg
lazy3WithNonce nonce fn arg1 arg2 arg3 =
VirtualDom.lazy5 lazyHelp3WithNonce nonce fn arg1 arg2 arg3
|> Unstyled


lazyHelp3WithNonce : String -> (a -> b -> c -> Node msg) -> a -> b -> c -> VirtualDom.Node msg
lazyHelp3WithNonce nonce fn arg1 arg2 arg3 =
fn arg1 arg2 arg3
|> toNonceUnstyled nonce


lazy4WithNonce : String -> (a -> b -> c -> d -> Node msg) -> a -> b -> c -> d -> Node msg
lazy4WithNonce nonce fn arg1 arg2 arg3 arg4 =
VirtualDom.lazy6 lazyHelp4WithNonce nonce fn arg1 arg2 arg3 arg4
|> Unstyled


lazyHelp4WithNonce : String -> (a -> b -> c -> d -> Node msg) -> a -> b -> c -> d -> VirtualDom.Node msg
lazyHelp4WithNonce nonce fn arg1 arg2 arg3 arg4 =
fn arg1 arg2 arg3 arg4
|> toNonceUnstyled nonce


lazy5WithNonce : String -> (a -> b -> c -> d -> e -> Node msg) -> a -> b -> c -> d -> e -> Node msg
lazy5WithNonce nonce fn arg1 arg2 arg3 arg4 arg5 =
VirtualDom.lazy7 lazyHelp5WithNonce nonce fn arg1 arg2 arg3 arg4 arg5
|> Unstyled


lazyHelp5WithNonce : String -> (a -> b -> c -> d -> e -> Node msg) -> a -> b -> c -> d -> e -> VirtualDom.Node msg
lazyHelp5WithNonce nonce fn arg1 arg2 arg3 arg4 arg5 =
fn arg1 arg2 arg3 arg4 arg5
|> toNonceUnstyled nonce


lazy6WithNonce : String -> (a -> b -> c -> d -> e -> f -> Node msg) -> a -> b -> c -> d -> e -> f -> Node msg
lazy6WithNonce nonce fn arg1 arg2 arg3 arg4 arg5 arg6 =
VirtualDom.lazy8 lazyHelp6WithNonce nonce fn arg1 arg2 arg3 arg4 arg5 arg6
|> Unstyled


lazyHelp6WithNonce : String -> (a -> b -> c -> d -> e -> f -> Node msg) -> a -> b -> c -> d -> e -> f -> VirtualDom.Node msg
lazyHelp6WithNonce nonce fn arg1 arg2 arg3 arg4 arg5 arg6 =
fn arg1 arg2 arg3 arg4 arg5 arg6
|> toNonceUnstyled nonce


type Scope
= Scope String

Expand Down

0 comments on commit 8353dad

Please sign in to comment.