Skip to content

Commit

Permalink
Add support for namespaced elements
Browse files Browse the repository at this point in the history
This allows the creation of elements like inline SVG.

Refs #54
  • Loading branch information
pdf committed Mar 30, 2017
1 parent a32ca06 commit ad06d25
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions dom.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ type Restorer interface {
type HTML struct {
Node *js.Object

tag, text, innerHTML string
styles, dataset map[string]string
properties, attributes map[string]interface{}
eventListeners []*EventListener
children []ComponentOrHTML
namespace, tag, text, innerHTML string
styles, dataset map[string]string
properties, attributes map[string]interface{}
eventListeners []*EventListener
children []ComponentOrHTML
}

func (h *HTML) restoreText(prev *HTML) {
Expand Down Expand Up @@ -224,9 +224,12 @@ func (h *HTML) Restore(old ComponentOrHTML) {
if h.text != "" && h.innerHTML != "" {
panic("vecty: only HTML may have UnsafeHTML attribute")
}
if h.tag != "" {
switch {
case h.tag != "" && h.namespace == "":
h.Node = js.Global.Get("document").Call("createElement", h.tag)
} else {
case h.tag != "" && h.namespace != "":
h.Node = js.Global.Get("document").Call("createElementNS", h.namespace, h.tag)
default:
h.Node = js.Global.Get("document").Call("createTextNode", h.text)
}
if h.innerHTML != "" {
Expand Down Expand Up @@ -268,8 +271,16 @@ func (h *HTML) Restore(old ComponentOrHTML) {
// function is not used directly but rather the elem subpackage (which is type
// safe) is used instead.
func Tag(tag string, m ...MarkupOrComponentOrHTML) *HTML {
return NamespacedTag(``, tag, m...)
}

// NamespacedTag returns an HTML element with the given tag name, and namespace.
// Generally, this function is not used directly but rather the elem subpackage
// (which is type safe) is used instead.
func NamespacedTag(namespace, tag string, m ...MarkupOrComponentOrHTML) *HTML {
h := &HTML{
tag: tag,
namespace: namespace,
tag: tag,
}
for _, m := range m {
apply(m, h)
Expand Down

0 comments on commit ad06d25

Please sign in to comment.