From 8ec8ebf9bff157caa1f228e714f6acd31244e079 Mon Sep 17 00:00:00 2001 From: Olivia Coumans Date: Fri, 9 Feb 2024 09:07:30 +0100 Subject: [PATCH] feat: add missing semantic text elements --- attrs/attrs.go | 1 + elements.go | 40 ++++++++++++++++++++++++++++++++++++++ elements_test.go | 50 +++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 90 insertions(+), 1 deletion(-) diff --git a/attrs/attrs.go b/attrs/attrs.go index 6458ce8..b83c414 100644 --- a/attrs/attrs.go +++ b/attrs/attrs.go @@ -44,6 +44,7 @@ const ( // Semantic Text Attributes + Cite = "cite" // Deprecated: Use Datetime instead DateTime = "datetime" Datetime = "datetime" diff --git a/elements.go b/elements.go index 215e8b1..a5baec6 100644 --- a/elements.go +++ b/elements.go @@ -348,11 +348,26 @@ func NoScript(attrs attrs.Props, children ...Node) *Element { // --- Semantic Text Content Elements --- +// Abbr creates an element. +func Abbr(attrs attrs.Props, children ...Node) *Element { + return newElement("abbr", attrs, children...) +} + // Address creates an
element. func Address(attrs attrs.Props, children ...Node) *Element { return newElement("address", attrs, children...) } +// Cite creates a element. +func Cite(attrs attrs.Props, children ...Node) *Element { + return newElement("cite", attrs, children...) +} + +// Data creates a element. +func Data(attrs attrs.Props, children ...Node) *Element { + return newElement("data", attrs, children...) +} + // Details creates a
element. func Details(attrs attrs.Props, children ...Node) *Element { return newElement("details", attrs, children...) @@ -368,11 +383,31 @@ func Figure(attrs attrs.Props, children ...Node) *Element { return newElement("figure", attrs, children...) } +// Kbd creates a element. +func Kbd(attrs attrs.Props, children ...Node) *Element { + return newElement("kbd", attrs, children...) +} + // Mark creates a element. func Mark(attrs attrs.Props, children ...Node) *Element { return newElement("mark", attrs, children...) } +// Q creates a element. +func Q(attrs attrs.Props, children ...Node) *Element { + return newElement("q", attrs, children...) +} + +// Samp creates a element. +func Samp(attrs attrs.Props, children ...Node) *Element { + return newElement("samp", attrs, children...) +} + +// Small creates a element. +func Small(attrs attrs.Props, children ...Node) *Element { + return newElement("small", attrs, children...) +} + // Summary creates a element. func Summary(attrs attrs.Props, children ...Node) *Element { return newElement("summary", attrs, children...) @@ -383,6 +418,11 @@ func Time(attrs attrs.Props, children ...Node) *Element { return newElement("time", attrs, children...) } +// Var creates a element. +func Var(attrs attrs.Props, children ...Node) *Element { + return newElement("var", attrs, children...) +} + // ========== Tables ========== // Table creates a element. diff --git a/elements_test.go b/elements_test.go index 78dd439..1b7af51 100644 --- a/elements_test.go +++ b/elements_test.go @@ -439,12 +439,24 @@ func TestNoScript(t *testing.T) { // --- Semantic Text Content Elements --- +func TestAbbr(t *testing.T) { + expected := `WHATWG` + el := Abbr(attrs.Props{attrs.Title: "Web Hypertext Application Technology Working Group"}, Text("WHATWG")) + assert.Equal(t, expected, el.Render()) +} + func TestAddress(t *testing.T) { expected := `
123 Example St.
` el := Address(nil, Text("123 Example St.")) assert.Equal(t, expected, el.Render()) } +func TestCite(t *testing.T) { + expected := `

My favorite book is The Reality Dysfunction by Peter F. Hamilton.

` + el := P(nil, Text("My favorite book is "), Cite(nil, Text("The Reality Dysfunction")), Text(" by Peter F. Hamilton.")) + assert.Equal(t, expected, el.Render()) +} + func TestDetails(t *testing.T) { expected := `
More Info

Details content here.

` el := Details(nil, Summary(nil, Text("More Info")), P(nil, Text("Details content here."))) @@ -463,6 +475,12 @@ func TestDetailsWithOpenTrue(t *testing.T) { assert.Equal(t, expected, el.Render()) } +func TestData(t *testing.T) { + expected := `Eight` + el := Data(attrs.Props{attrs.Value: "8"}, Text("Eight")) + assert.Equal(t, expected, el.Render()) +} + func TestFigCaption(t *testing.T) { expected := `
Description of the figure.
` el := FigCaption(nil, Text("Description of the figure.")) @@ -475,12 +493,36 @@ func TestFigure(t *testing.T) { assert.Equal(t, expected, el.Render()) } +func TestKbd(t *testing.T) { + expected := `

To make George eat an apple, select File | Eat Apple...

` + el := P(nil, Text("To make George eat an apple, select "), Kbd(nil, Text("File | Eat Apple..."))) + assert.Equal(t, expected, el.Render()) +} + func TestMark(t *testing.T) { expected := `

You must highlight this word.

` el := P(nil, Text("You must "), Mark(nil, Text("highlight")), Text(" this word.")) assert.Equal(t, expected, el.Render()) } +func TestQ(t *testing.T) { + expected := `

The W3C's mission is To lead the World Wide Web to its full potential.

` + el := P(nil, Text("The W3C's mission is "), Q(attrs.Props{attrs.Cite: "https://www.w3.org/Consortium/"}, Text("To lead the World Wide Web to its full potential")), Text(".")) + assert.Equal(t, expected, el.Render()) +} + +func TestSamp(t *testing.T) { + expected := `

The computer said Too much cheese in tray two but I didn't know what that meant.

` + el := P(nil, Text("The computer said "), Samp(nil, Text("Too much cheese in tray two")), Text(" but I didn't know what that meant.")) + assert.Equal(t, expected, el.Render()) +} + +func TestSmall(t *testing.T) { + expected := `

Single room breakfast included, VAT not included

` + el := P(nil, Text("Single room "), Small(nil, Text("breakfast included, VAT not included"))) + assert.Equal(t, expected, el.Render()) +} + func TestSummary(t *testing.T) { expected := `
Summary Title
` el := Details(nil, Summary(nil, Text("Summary Title"))) @@ -489,7 +531,13 @@ func TestSummary(t *testing.T) { func TestTime(t *testing.T) { expected := `` - el := Time(attrs.Props{attrs.DateTime: "2023-01-01T00:00:00Z"}, Text("New Year's Day")) + el := Time(attrs.Props{attrs.Datetime: "2023-01-01T00:00:00Z"}, Text("New Year's Day")) + assert.Equal(t, expected, el.Render()) +} + +func TestVar(t *testing.T) { + expected := `

After a few moment's thought, she wrote E.

` + el := P(nil, Text("After a few moment's thought, she wrote "), Var(nil, Text("E")), Text(".")) assert.Equal(t, expected, el.Render()) }