Skip to content

Commit

Permalink
Merge pull request #56 from chasefleming/chasefleming/30
Browse files Browse the repository at this point in the history
Add Semantic Text Content Elements
  • Loading branch information
chasefleming authored Nov 2, 2023
2 parents 14b5e28 + abb2edc commit b88730b
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
6 changes: 6 additions & 0 deletions attrs/attrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ const (
Width = "width"
IsMap = "ismap"

// Semantic Text Attributes
DateTime = "datetime"

// Form/Input Attributes
Accept = "accept"
Action = "action"
Expand All @@ -57,6 +60,9 @@ const (
Type = "type"
Value = "value"

// Interactive Attributes
Open = "open"

// Miscellaneous Attributes
DataPrefix = "data-" // Used for custom data attributes e.g., "data-custom"
Download = "download"
Expand Down
1 change: 1 addition & 0 deletions elem.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var booleanAttrs = map[string]struct{}{
attrs.IsMap: {},
attrs.Multiple: {},
attrs.NoValidate: {},
attrs.Open: {},
attrs.Readonly: {},
attrs.Required: {},
attrs.Selected: {},
Expand Down
32 changes: 32 additions & 0 deletions elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ func Script(props Attrs, children ...Node) *Element {

// ========== Semantic Elements ==========

// --- Semantic Sectioning Elements ---

func Article(props Attrs, children ...Node) *Element {
return NewElement("article", props, children...)
}
Expand Down Expand Up @@ -190,6 +192,36 @@ func Section(props Attrs, children ...Node) *Element {
return NewElement("section", props, children...)
}

// --- Semantic Text Content Elements ---

func Address(props Attrs, children ...Node) *Element {
return NewElement("address", props, children...)
}

func Details(props Attrs, children ...Node) *Element {
return NewElement("details", props, children...)
}

func FigCaption(props Attrs, children ...Node) *Element {
return NewElement("figcaption", props, children...)
}

func Figure(props Attrs, children ...Node) *Element {
return NewElement("figure", props, children...)
}

func Mark(props Attrs, children ...Node) *Element {
return NewElement("mark", props, children...)
}

func Summary(props Attrs, children ...Node) *Element {
return NewElement("summary", props, children...)
}

func Time(props Attrs, children ...Node) *Element {
return NewElement("time", props, children...)
}

// ========== Tables ==========

func Table(props Attrs, children ...Node) *Element {
Expand Down
58 changes: 58 additions & 0 deletions elements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ func TestScript(t *testing.T) {

// ========== Semantic Elements ==========

// --- Semantic Sectioning Elements ---

func TestArticle(t *testing.T) {
expected := `<article><h2>Article Title</h2><p>Article content.</p></article>`
el := Article(nil, H2(nil, Text("Article Title")), P(nil, Text("Article content.")))
Expand Down Expand Up @@ -293,6 +295,62 @@ func TestSection(t *testing.T) {
assert.Equal(t, expected, el.Render())
}

// --- Semantic Text Content Elements ---

func TestAddress(t *testing.T) {
expected := `<address>123 Example St.</address>`
el := Address(nil, Text("123 Example St."))
assert.Equal(t, expected, el.Render())
}

func TestDetails(t *testing.T) {
expected := `<details><summary>More Info</summary><p>Details content here.</p></details>`
el := Details(nil, Summary(nil, Text("More Info")), P(nil, Text("Details content here.")))
assert.Equal(t, expected, el.Render())
}

func TestDetailsWithOpenFalse(t *testing.T) {
expected := `<details><summary>More Info</summary><p>Details content here.</p></details>`
el := Details(Attrs{attrs.Open: "false"}, Summary(nil, Text("More Info")), P(nil, Text("Details content here.")))
assert.Equal(t, expected, el.Render())
}

func TestDetailsWithOpenTrue(t *testing.T) {
expected := `<details open><summary>More Info</summary><p>Details content here.</p></details>`
el := Details(Attrs{attrs.Open: "true"}, Summary(nil, Text("More Info")), P(nil, Text("Details content here.")))
assert.Equal(t, expected, el.Render())
}

func TestFigCaption(t *testing.T) {
expected := `<figcaption>Description of the figure.</figcaption>`
el := FigCaption(nil, Text("Description of the figure."))
assert.Equal(t, expected, el.Render())
}

func TestFigure(t *testing.T) {
expected := `<figure><img alt="An image" src="image.jpg"><figcaption>An image</figcaption></figure>`
el := Figure(nil, Img(Attrs{attrs.Src: "image.jpg", attrs.Alt: "An image"}), FigCaption(nil, Text("An image")))
assert.Equal(t, expected, el.Render())
}

func TestMark(t *testing.T) {
expected := `<p>You must <mark>highlight</mark> this word.</p>`
el := P(nil, Text("You must "), Mark(nil, Text("highlight")), Text(" this word."))
assert.Equal(t, expected, el.Render())
}

func TestSummary(t *testing.T) {
expected := `<details><summary>Summary Title</summary></details>`
el := Details(nil, Summary(nil, Text("Summary Title")))
assert.Equal(t, expected, el.Render())
}

func TestTime(t *testing.T) {
expected := `<time datetime="2023-01-01T00:00:00Z">New Year's Day</time>`
el := Time(Attrs{attrs.DateTime: "2023-01-01T00:00:00Z"}, Text("New Year's Day"))
assert.Equal(t, expected, el.Render())
}

// ========== Tables ==========

func TestTr(t *testing.T) {
Expand Down

0 comments on commit b88730b

Please sign in to comment.