Skip to content

Commit

Permalink
Merge pull request #2 from chasefleming/chasefleming/text-formatting-…
Browse files Browse the repository at this point in the history
…elements

Add Additional Text Formatting and Structure Elements
  • Loading branch information
chasefleming authored Oct 7, 2023
2 parents 381f2f1 + 35989dc commit 31b37bb
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 52 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,27 @@ In this example, if `isAdmin` is `true`, the `Admin Panel` link is rendered. Oth
`elem` provides utility functions for creating common HTML elements:

- `A`: Anchor `<a>`
- `Blockquote`: Blockquote `<blockquote>`
- `Body`: Body `<body>`
- `Br`: Break `<br>`
- `Button`: Button `<button>`
- `Code`: Code `<code>`
- `Div`: Division `<div>`
- `Em`: Emphasis `<em>`
- `H1`: Heading 1 `<h1>`
- `H2`: Heading 2 `<h2>`
- `H3`: Heading 3 `<h3>`
- `Head`: Head `<head>`
- `Html`: HTML `<html>`
- `Hr`: Horizontal Rule `<hr>`
- `Img`: Image `<img>`
- `Li`: List Item `<li>`
- `Meta`: Meta `<meta>`
- `P`: Paragraph `<p>`
- `Pre`: Preformatted Text `<pre>`
- `Script`: Script `<script>`
- `Span`: Span `<span>`
- `Strong`: Strong `<strong>`
- `Title`: Title `<title>`
- `Ul`: Unordered List `<ul>`

Expand Down
84 changes: 62 additions & 22 deletions elements.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,49 @@
package elem

// ========== Document Structure ==========

func Body(props Attrs, children ...interface{}) *Element {
return NewElement("body", props, children...)
}

func Button(props Attrs, children ...interface{}) *Element {
return NewElement("button", props, children...)
func Head(props Attrs, children ...interface{}) *Element {
return NewElement("head", props, children...)
}

func Html(props Attrs, children ...interface{}) *Element {
return NewElement("html", props, children...)
}

func Title(props Attrs, children ...interface{}) *Element {
return NewElement("title", props, children...)
}

// ========== Text Formatting and Structure ==========

func A(props Attrs, children ...interface{}) *Element {
return NewElement("a", props, children...)
}

func Br(props Attrs) *Element {
return NewElement("br", props)
}

func Blockquote(props Attrs, children ...interface{}) *Element {
return NewElement("blockquote", props, children...)
}

func Code(props Attrs, children ...interface{}) *Element {
return NewElement("code", props, children...)
}

func Div(props Attrs, children ...interface{}) *Element {
return NewElement("div", props, children...)
}

func Em(props Attrs, children ...interface{}) *Element {
return NewElement("em", props, children...)
}

func H1(props Attrs, children ...interface{}) *Element {
return NewElement("h1", props, children...)
}
Expand All @@ -24,50 +56,58 @@ func H3(props Attrs, children ...interface{}) *Element {
return NewElement("h3", props, children...)
}

func Head(props Attrs, children ...interface{}) *Element {
return NewElement("head", props, children...)
}

func Html(props Attrs, children ...interface{}) *Element {
return NewElement("html", props, children...)
func Hr(props Attrs) *Element {
return NewElement("hr", props)
}

func P(props Attrs, children ...interface{}) *Element {
return NewElement("p", props, children...)
}

func A(props Attrs, children ...interface{}) *Element {
return NewElement("a", props, children...)
func Pre(props Attrs, children ...interface{}) *Element {
return NewElement("pre", props, children...)
}

func Span(props Attrs, children ...interface{}) *Element {
return NewElement("span", props, children...)
}

func Ul(props Attrs, children ...interface{}) *Element {
return NewElement("ul", props, children...)
func Strong(props Attrs, children ...interface{}) *Element {
return NewElement("strong", props, children...)
}

func Text(content string) string {
return content
}

// ========== Lists ==========

func Li(props Attrs, children ...interface{}) *Element {
return NewElement("li", props, children...)
}

func Img(props Attrs) *Element {
return NewElement("img", props)
func Ul(props Attrs, children ...interface{}) *Element {
return NewElement("ul", props, children...)
}

func Meta(props Attrs) *Element {
return NewElement("meta", props)
// ========== Forms ==========

func Button(props Attrs, children ...interface{}) *Element {
return NewElement("button", props, children...)
}

func Script(props Attrs) *Element {
return NewElement("script", props)
// ========== Hyperlinks and Multimedia ==========

func Img(props Attrs) *Element {
return NewElement("img", props)
}

func Text(content string) string {
return content
// ========== Meta Elements ==========

func Meta(props Attrs) *Element {
return NewElement("meta", props)
}

func Title(props Attrs, children ...interface{}) *Element {
return NewElement("title", props, children...)
func Script(props Attrs, children ...interface{}) *Element {
return NewElement("script", props, children...)
}
118 changes: 88 additions & 30 deletions elements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,53 @@ import (
"testing"
)

// ========== Document Structure ==========

func TestBody(t *testing.T) {
expected := `<body class="page-body"><p>Welcome to Elem!</p></body>`
el := Body(Attrs{attrs.Class: "page-body"}, P(nil, Text("Welcome to Elem!")))
assert.Equal(t, expected, el.Render())
}

func TestHead(t *testing.T) {
// ... [Code for the Head test here]
}

func TestHtml(t *testing.T) {
expected := `<html lang="en"><head><meta charset="UTF-8"><title>Elem Page</title></head><body><p>Welcome to Elem!</p></body></html>`
el := Html(Attrs{attrs.Lang: "en"},
Head(nil,
Meta(Attrs{attrs.Charset: "UTF-8"}),
Title(nil, Text("Elem Page")),
),
Body(nil, P(nil, Text("Welcome to Elem!"))),
)
assert.Equal(t, expected, el.Render())
}

// ========== Text Formatting and Structure ==========

func TestA(t *testing.T) {
expected := `<a href="https://example.com">Visit Example</a>`
el := A(Attrs{attrs.Href: "https://example.com"}, Text("Visit Example"))
assert.Equal(t, expected, el.Render())
}

func TestBody(t *testing.T) {
expected := `<body class="page-body"><p>Welcome to Elem!</p></body>`
el := Body(Attrs{attrs.Class: "page-body"}, P(nil, Text("Welcome to Elem!")))
func TestBlockquote(t *testing.T) {
expected := `<blockquote>Quote text</blockquote>`
el := Blockquote(nil, Text("Quote text"))
assert.Equal(t, expected, el.Render())
}

func TestButton(t *testing.T) {
expected := `<button class="btn">Click Me</button>`
el := Button(Attrs{attrs.Class: "btn"}, Text("Click Me"))
func TestBr(t *testing.T) {
expected := `<br>`
el := Br(nil)
assert.Equal(t, expected, el.Render())
}

func TestCode(t *testing.T) {
expected := `<code>Code snippet</code>`
el := Code(nil, Text("Code snippet"))
assert.Equal(t, expected, el.Render())
}

Expand All @@ -30,6 +62,12 @@ func TestDiv(t *testing.T) {
assert.Equal(t, expected, el.Render())
}

func TestEm(t *testing.T) {
expected := `<em>Italic text</em>`
el := Em(nil, Text("Italic text"))
assert.Equal(t, expected, el.Render())
}

func TestH1(t *testing.T) {
expected := `<h1 class="title">Hello, Elem!</h1>`
el := H1(Attrs{attrs.Class: "title"}, Text("Hello, Elem!"))
Expand All @@ -48,27 +86,9 @@ func TestH3(t *testing.T) {
assert.Equal(t, expected, el.Render())
}

func TestHtml(t *testing.T) {
expected := `<html lang="en"><head><meta charset="UTF-8"><title>Elem Page</title></head><body><p>Welcome to Elem!</p></body></html>`
el := Html(Attrs{attrs.Lang: "en"},
Head(nil,
Meta(Attrs{attrs.Charset: "UTF-8"}),
Title(nil, Text("Elem Page")),
),
Body(nil, P(nil, Text("Welcome to Elem!"))),
)
assert.Equal(t, expected, el.Render())
}

func TestImg(t *testing.T) {
expected := `<img alt="An image" src="image.jpg">`
el := Img(Attrs{attrs.Src: "image.jpg", attrs.Alt: "An image"})
assert.Equal(t, expected, el.Render())
}

func TestLi(t *testing.T) {
expected := `<li>Item 1</li>`
el := Li(nil, Text("Item 1"))
func TestHr(t *testing.T) {
expected := `<hr>`
el := Hr(nil)
assert.Equal(t, expected, el.Render())
}

Expand All @@ -78,9 +98,9 @@ func TestP(t *testing.T) {
assert.Equal(t, expected, el.Render())
}

func TestScript(t *testing.T) {
expected := `<script src="https://example.com/script.js"></script>`
el := Script(Attrs{attrs.Src: "https://example.com/script.js"})
func TestPre(t *testing.T) {
expected := `<pre>Preformatted text</pre>`
el := Pre(nil, Text("Preformatted text"))
assert.Equal(t, expected, el.Render())
}

Expand All @@ -90,8 +110,46 @@ func TestSpan(t *testing.T) {
assert.Equal(t, expected, el.Render())
}

func TestStrong(t *testing.T) {
expected := `<strong>Bold text</strong>`
el := Strong(nil, Text("Bold text"))
assert.Equal(t, expected, el.Render())
}

// ========== Lists ==========

func TestLi(t *testing.T) {
expected := `<li>Item 1</li>`
el := Li(nil, Text("Item 1"))
assert.Equal(t, expected, el.Render())
}

func TestUl(t *testing.T) {
expected := `<ul><li>Item 1</li><li>Item 2</li></ul>`
el := Ul(nil, Li(nil, Text("Item 1")), Li(nil, Text("Item 2")))
assert.Equal(t, expected, el.Render())
}

// ========== Forms ==========

func TestButton(t *testing.T) {
expected := `<button class="btn">Click Me</button>`
el := Button(Attrs{attrs.Class: "btn"}, Text("Click Me"))
assert.Equal(t, expected, el.Render())
}

// ========== Hyperlinks and Multimedia ==========

func TestImg(t *testing.T) {
expected := `<img alt="An image" src="image.jpg">`
el := Img(Attrs{attrs.Src: "image.jpg", attrs.Alt: "An image"})
assert.Equal(t, expected, el.Render())
}

// ========== Meta Elements ==========

func TestScript(t *testing.T) {
expected := `<script src="https://example.com/script.js"></script>`
el := Script(Attrs{attrs.Src: "https://example.com/script.js"})
assert.Equal(t, expected, el.Render())
}

0 comments on commit 31b37bb

Please sign in to comment.