Skip to content

Commit 4b4da3f

Browse files
authored
Merge pull request #62 from chasefleming/chasefleming/58
Add Style tag support and CSS
2 parents f7e3fc9 + 171a30c commit 4b4da3f

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ In this example, if `isAdmin` is `true`, the `Admin Panel` link is rendered. Oth
118118

119119
`elem` provides utility functions for creating HTML elements:
120120

121-
- **Document Structure**: `Html`, `Head`, `Body`, `Title`, `Link`, `Meta`
121+
- **Document Structure**: `Html`, `Head`, `Body`, `Title`, `Link`, `Meta`, `Style`
122122
- **Text Content**: `H1`, `H2`, `H3`, `P`, `Blockquote`, `Pre`, `Code`, `I`, `Br`, `Hr`
123123
- **Sectioning & Semantic Layout**: `Article`, `Aside`, `Footer`, `Header`, `Main`, `Nav`, `Section`
124124
- **Form Elements**: `Form`, `Input`, `Textarea`, `Button`, `Select`, `Option`, `Label`, `Fieldset`, `Legend`, `Datalist`, `Meter`, `Output`, `Progress`

elements.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,10 @@ func Script(attrs attrs.Props, children ...Node) *Element {
164164
return NewElement("script", attrs, children...)
165165
}
166166

167+
func Style(attrs attrs.Props, children ...Node) *Element {
168+
return NewElement("style", attrs, children...)
169+
}
170+
167171
// ========== Semantic Elements ==========
168172

169173
// --- Semantic Sectioning Elements ---

elements_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package elem
22

33
import (
4+
"github.com/chasefleming/elem-go/styles"
45
"testing"
56

67
"github.com/chasefleming/elem-go/attrs"
@@ -245,6 +246,13 @@ func TestScript(t *testing.T) {
245246
assert.Equal(t, expected, el.Render())
246247
}
247248

249+
func TestStyle(t *testing.T) {
250+
expected := `<style type="text/css">.test-class {color: #333;}</style>`
251+
cssContent := `.test-class {color: #333;}`
252+
el := Style(attrs.Props{attrs.Type: "text/css"}, styles.CSS(cssContent))
253+
assert.Equal(t, expected, el.Render())
254+
}
255+
248256
// ========== Semantic Elements ==========
249257

250258
// --- Semantic Sectioning Elements ---

styles/styles.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"strings"
66
)
77

8+
// Props is a map of CSS properties
89
type Props map[string]string
910

11+
// ToInline converts the Props to an inline style string
1012
func (p Props) ToInline() string {
1113
// Extract the keys and sort them for deterministic order
1214
keys := make([]string, 0, len(p))
@@ -31,3 +33,21 @@ func (p Props) ToInline() string {
3133

3234
return styleStr
3335
}
36+
37+
// CSSNode is a Node that renders to a CSS string
38+
type CSSNode string
39+
40+
// RenderTo satisfies part of the Node interface by allowing CSSNode to be written to a strings.Builder
41+
func (cn CSSNode) RenderTo(builder *strings.Builder) {
42+
builder.WriteString(string(cn))
43+
}
44+
45+
// Render satisfies part of the Node interface by returning the CSS as a string
46+
func (cn CSSNode) Render() string {
47+
return string(cn)
48+
}
49+
50+
// CSS creates a new CSSNode from the given CSS content string
51+
func CSS(content string) CSSNode {
52+
return CSSNode(content)
53+
}

0 commit comments

Comments
 (0)