Skip to content

Commit

Permalink
Merge pull request #76 from chasefleming/chasefleming/72
Browse files Browse the repository at this point in the history
Add audio, video, source elements
  • Loading branch information
chasefleming authored Nov 11, 2023
2 parents 99eba4d + cc06e29 commit e2593f3
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
15 changes: 15 additions & 0 deletions attrs/attrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ const (
ReferrerPolicy = "referrerpolicy"
Sandbox = "sandbox"
SrcDoc = "srcdoc"

// Audio/Video Attributes
Controls = "controls"
Loop = "loop"
Muted = "muted"
Preload = "preload"
Autoplay = "autoplay"

// Video-Specific Attributes
Poster = "poster"
Playsinline = "playsinline"

// Source Element-Specific Attributes
Media = "media"
Sizes = "sizes"
)

type Props map[string]string
5 changes: 5 additions & 0 deletions elem.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,18 @@ var booleanAttrs = map[string]struct{}{
attrs.AllowFullScreen: {},
attrs.Async: {},
attrs.Autofocus: {},
attrs.Autoplay: {},
attrs.Checked: {},
attrs.Controls: {},
attrs.Defer: {},
attrs.Disabled: {},
attrs.IsMap: {},
attrs.Loop: {},
attrs.Multiple: {},
attrs.Muted: {},
attrs.NoValidate: {},
attrs.Open: {},
attrs.Playsinline: {},
attrs.Readonly: {},
attrs.Required: {},
attrs.Selected: {},
Expand Down
15 changes: 15 additions & 0 deletions elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,18 @@ func Td(attrs attrs.Props, children ...Node) *Element {
func IFrame(attrs attrs.Props, children ...Node) *Element {
return NewElement("iframe", attrs, children...)
}

// Audio creates an <audio> element.
func Audio(attrs attrs.Props, children ...Node) *Element {
return NewElement("audio", attrs, children...)
}

// Video creates a <video> element.
func Video(attrs attrs.Props, children ...Node) *Element {
return NewElement("video", attrs, children...)
}

// Source creates a <source> element.
func Source(attrs attrs.Props, children ...Node) *Element {
return NewElement("source", attrs, children...)
}
20 changes: 20 additions & 0 deletions elements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,3 +485,23 @@ func TestAllowFullScreen(t *testing.T) {
el := IFrame(attrs.Props{attrs.Src: "https://www.youtube.com/embed/446E-r0rXHI", attrs.AllowFullScreen: "true"})
assert.Equal(t, expected, el.Render())
}

func TestAudioWithSourceElementsAndFallbackText(t *testing.T) {
expected := `<audio controls><source src="horse.ogg" type="audio/ogg"><source src="horse.mp3" type="audio/mpeg">Your browser does not support the audio tag.</audio>`
el := Audio(attrs.Props{attrs.Controls: "true"},
Source(attrs.Props{attrs.Src: "horse.ogg", attrs.Type: "audio/ogg"}),
Source(attrs.Props{attrs.Src: "horse.mp3", attrs.Type: "audio/mpeg"}),
Text("Your browser does not support the audio tag."),
)
assert.Equal(t, expected, el.Render())
}

func TestVideoWithSourceElementsAndFallbackText(t *testing.T) {
expected := `<video controls height="240" width="320"><source src="movie.mp4" type="video/mp4"><source src="movie.ogg" type="video/ogg">Your browser does not support the video tag.</video>`
el := Video(attrs.Props{attrs.Width: "320", attrs.Height: "240", attrs.Controls: "true"},
Source(attrs.Props{attrs.Src: "movie.mp4", attrs.Type: "video/mp4"}),
Source(attrs.Props{attrs.Src: "movie.ogg", attrs.Type: "video/ogg"}),
Text("Your browser does not support the video tag."),
)
assert.Equal(t, expected, el.Render())
}

0 comments on commit e2593f3

Please sign in to comment.