Skip to content

Commit

Permalink
Simplify style attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
johnspade committed Feb 9, 2024
1 parent 21989c9 commit 7a430ff
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 22 deletions.
2 changes: 1 addition & 1 deletion docs/dsl/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ are suffixed `attr` to easily distinguish those from html elements:
import zio.http.template._

val divHtml3: Html = div(
classAttr := "container1" :: "container2" :: Nil,
classAttr := "container1 container2",
a(hrefAttr := "http://zio.dev", "ZIO Homepage")
)
```
Expand Down
4 changes: 2 additions & 2 deletions zio-http-example/src/main/scala/example/HtmlTemplating.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ object HtmlTemplating extends ZIOAppDefault {
body(
div(
// Support for css class names
css := "container" :: "text-align-left" :: Nil,
css := "container text-align-left",
h1("Hello World"),
ul(
// Support for inline css
styles := Seq("list-style" -> "none"),
styles := "list-style: none",
li(
// Support for attributes
a(href := "/hello/world", "Hello World"),
Expand Down
4 changes: 2 additions & 2 deletions zio-http/jvm/src/test/scala/zio/http/FlashSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ object FlashSpec extends ZIOHttpSpec {
object ui {
def flashEmpty = Html.fromString("no-flash")
def flashBoth(notice: Html, alert: Html): Html = notice ++ alert
def flashNotice(html: Html): Html = div(styleAttr := Seq("background" -> "green"), html)
def flashAlert(html: Html): Html = div(styleAttr := Seq("background" -> "red"), html)
def flashNotice(html: Html): Html = div(styleAttr := "background: green", html)
def flashAlert(html: Html): Html = div(styleAttr := "background: red", html)
}

val routeUserSavePath = Method.POST / "users" / "save"
Expand Down
4 changes: 2 additions & 2 deletions zio-http/jvm/src/test/scala/zio/http/codec/DocSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ object DocSpec extends ZIOHttpSpec {
|
|[https://www.google.com](https://www.google.com)
|
|<span style="color:red">This is an error</span>
|<span style="color: red">This is an error</span>
|
|`ZIO.succeed(1)`
|
Expand Down Expand Up @@ -114,7 +114,7 @@ object DocSpec extends ZIOHttpSpec {
| <a href="https://www.google.com">https://www.google.com</a>
|</p>
|<p>
| <span style="color:red">This is an error</span>
| <span style="color: red">This is an error</span>
|</p>
|<p>
| <code>ZIO.succeed(1)</code>
Expand Down
12 changes: 6 additions & 6 deletions zio-http/jvm/src/test/scala/zio/http/template/HtmlSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,36 @@ case object HtmlSpec extends ZIOHttpSpec {
assert(view.encode)(equalTo(expected.stripMargin))
},
test("tags with attributes") {
val view = html(body(div(css := "container" :: Nil, "Hello!")))
val view = html(body(div(css := "container", "Hello!")))
val expected = """<html><body><div class="container">Hello!</div></body></html>"""
assert(view.encode)(equalTo(expected.stripMargin))
},
test("tags with children") {
val view = html(body(div(css := "container" :: Nil, "Hello!", span("World!"))))
val view = html(body(div(css := "container", "Hello!", span("World!"))))
val expected =
"""<html><body><div class="container">Hello!<span>World!</span></div></body></html>"""
assert(view.encode)(equalTo(expected.stripMargin))
},
test("tags with attributes and children") {
val view = html(body(div(css := "container" :: Nil, "Hello!", span("World!"))))
val view = html(body(div(css := "container", "Hello!", span("World!"))))
val expected =
"""<html><body><div class="container">Hello!<span>World!</span></div></body></html>"""
assert(view.encode)(equalTo(expected.stripMargin))
},
test("tags with attributes and children") {
val view = html(body(div(css := "container" :: Nil, "Hello!", span("World!"))))
val view = html(body(div(css := "container", "Hello!", span("World!"))))
val expected =
"""<html><body><div class="container">Hello!<span>World!</span></div></body></html>"""
assert(view.encode)(equalTo(expected.stripMargin))
},
test("tags with attributes and children") {
val view = html(body(div(css := "container" :: Nil, "Hello!", span("World!"))))
val view = html(body(div(css := "container", "Hello!", span("World!"))))
val expected =
"""<html><body><div class="container">Hello!<span>World!</span></div></body></html>"""
assert(view.encode)(equalTo(expected.stripMargin))
},
test("tags with attributes and children") {
val view = div("Hello!", css := "container" :: Nil)
val view = div("Hello!", css := "container")
val expected = """<div class="container">Hello!</div>"""
assert(view.encode)(equalTo(expected.stripMargin))
},
Expand Down
4 changes: 2 additions & 2 deletions zio-http/shared/src/main/scala/zio/http/HandlerAspect.scala
Original file line number Diff line number Diff line change
Expand Up @@ -840,8 +840,8 @@ private[http] trait HandlerAspects extends zio.http.internal.HeaderModifier[Hand
val data = Template.container(s"${response.status}") {
div(
div(
styles := Seq("text-align" -> "center"),
div(s"${response.status.code}", styles := Seq("font-size" -> "20em")),
styles := "text-align: center",
div(s"${response.status.code}", styles := "font-size: 20em"),
div(message),
),
)
Expand Down
4 changes: 2 additions & 2 deletions zio-http/shared/src/main/scala/zio/http/codec/Doc.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ sealed trait Doc { self =>
case Span.Italic(value) =>
s"${render("*")}${renderSpan(value, indent).trim}${render("*")}"
case Span.Error(value) =>
s"${render(s"""<span style="color:red">""")}${render(value)}${render("</span>")}"
s"${render(s"""<span style="color: red">""")}${render(value)}${render("</span>")}"
case Span.Sequence(left, right) =>
val l = renderSpan(left, indent)
val r = renderSpan(right, indent)
Expand Down Expand Up @@ -408,7 +408,7 @@ object Doc {
case Span.Text(value) => value
case Span.Code(value, CodeStyle.Block) => pre(code(value))
case Span.Code(value, CodeStyle.Inline) => code(value)
case Span.Error(value) => span(styleAttr := ("color", "red") :: Nil, value)
case Span.Error(value) => span(styleAttr := "color: red", value)
case Span.Bold(value) => b(value.toHtml)
case Span.Italic(value) => i(value.toHtml)
case Span.Link(value, text) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ trait Attributes {

final def citeAttr: PartialAttribute[String] = PartialAttribute("cite")

final def classAttr: PartialAttribute[List[String]] = PartialAttribute("class")
final def classAttr: PartialAttribute[String] = PartialAttribute("class")

final def colSpanAttr: PartialAttribute[String] = PartialAttribute("colspan")

Expand All @@ -65,7 +65,7 @@ trait Attributes {

final def coordsAttr: PartialAttribute[String] = PartialAttribute("coords")

final def css: PartialAttribute[List[String]] = classAttr
final def css: PartialAttribute[String] = classAttr

final def dataAttr(name: String): PartialAttribute[String] = PartialAttribute("data-" + name)

Expand Down Expand Up @@ -337,9 +337,9 @@ trait Attributes {

final def stepAttr: PartialAttribute[String] = PartialAttribute("step")

final def styleAttr: PartialAttribute[Seq[(String, String)]] = PartialAttribute("style")
final def styleAttr: PartialAttribute[String] = PartialAttribute("style")

final def styles: PartialAttribute[Seq[(String, String)]] = styleAttr
final def styles: PartialAttribute[String] = styleAttr

final def tabIndexAttr: PartialAttribute[String] = PartialAttribute("tabindex")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ object Template {
),
body(
div(
styles := Seq("margin" -> "auto", "padding" -> "2em 4em", "max-width" -> "80%"),
styles := "margin: auto; padding: 2em 4em; max-width: 80%",
h1(heading),
element,
),
Expand Down

0 comments on commit 7a430ff

Please sign in to comment.