Skip to content

Commit

Permalink
Merge pull request #126 from chasefleming/chasefleming/125
Browse files Browse the repository at this point in the history
Move `styles.CSS` to `elem.CSS`
  • Loading branch information
chasefleming authored Mar 3, 2024
2 parents 2f5a0cb + 728826e commit 403a932
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 30 deletions.
5 changes: 5 additions & 0 deletions elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,3 +525,8 @@ func None() NoneNode {
func Raw(html string) RawNode {
return RawNode(html)
}

// CSS takes css content and returns a TextNode.
func CSS(content string) TextNode {
return TextNode(content)
}
9 changes: 8 additions & 1 deletion elements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func TestScript(t *testing.T) {
func TestStyle(t *testing.T) {
expected := `<style type="text/css">.test-class {color: #333;}</style>`
cssContent := `.test-class {color: #333;}`
el := Style(attrs.Props{attrs.Type: "text/css"}, TextNode(cssContent))
el := Style(attrs.Props{attrs.Type: "text/css"}, CSS(cssContent))
assert.Equal(t, expected, el.Render())
}

Expand Down Expand Up @@ -680,3 +680,10 @@ func TestRaw(t *testing.T) {

assert.Equal(t, expected, el.Render())
}

func TestCSS(t *testing.T) {
cssContent := `.test-class {color: #333;}`
expected := `.test-class {color: #333;}`
el := CSS(cssContent)
assert.Equal(t, expected, el.Render())
}
2 changes: 1 addition & 1 deletion styles/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ These functions facilitate the embedding of CSS into HTML documents, particularl
cssContent := `/* ... */`

// Creating a <style> tag
styleTag := elem.Style(nil, styles.CSS(cssContent))
styleTag := elem.Style(nil, elem.CSS(cssContent))

// Incorporating the <style> tag in an HTML document
document := elem.Html(nil, elem.Head(nil, styleTag), /* ... */)
Expand Down
20 changes: 0 additions & 20 deletions styles/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package styles
import (
"sort"
"strings"

"github.com/chasefleming/elem-go"
)

// Props is a map of CSS properties
Expand Down Expand Up @@ -35,21 +33,3 @@ func (p Props) ToInline() string {

return styleStr
}

// CSSNode is a Node that renders to a CSS string
type CSSNode string

// RenderTo satisfies part of the Node interface by allowing CSSNode to be written to a strings.Builder
func (cn CSSNode) RenderTo(builder *strings.Builder, opts elem.RenderOptions) {
builder.WriteString(string(cn))
}

// Render satisfies part of the Node interface by returning the CSS as a string
func (cn CSSNode) Render() string {
return string(cn)
}

// CSS creates a new CSSNode from the given CSS content string
func CSS(content string) CSSNode {
return CSSNode(content)
}
8 changes: 0 additions & 8 deletions styles/styles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ import (
"github.com/stretchr/testify/assert"
)

func TestCSS(t *testing.T) {
cssContent := `.test-class {color: #333;}`
expected := `.test-class {color: #333;}`
el := CSS(cssContent)
assert.Equal(t, expected, el.Render())
}

func TestStyleToInline(t *testing.T) {
style := Props{
BackgroundColor: "blue",
Expand Down Expand Up @@ -44,4 +37,3 @@ func TestStyleString_UnorderedKeys(t *testing.T) {
}
assert.Equal(t, "background-color: blue; color: white; font-size: 16px; outline-style: solid;", style.ToInline())
}

0 comments on commit 403a932

Please sign in to comment.