From f1212161a8d12da90d5207a0c6fec466a974ee4a Mon Sep 17 00:00:00 2001 From: Steve Moyer Date: Mon, 3 Jun 2019 12:09:54 -0400 Subject: [PATCH 1/2] Adds document title element Resolves #46 --- global.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/global.go b/global.go index dab32d3..2b1d082 100644 --- a/global.go +++ b/global.go @@ -7,9 +7,10 @@ import ( ) var ( - Doc = GetDocument() - Body = getFirstWithTag("body") - Head = getFirstWithTag("head") + Doc = GetDocument() + Body = getFirstWithTag("body") + Head = getFirstWithTag("head") + Title = getOrCreateFirstWithTag(Head, "title") ) func getFirstWithTag(tag string) *HTMLElement { @@ -20,6 +21,16 @@ func getFirstWithTag(tag string) *HTMLElement { return list[0].AsHTMLElement() } +func getOrCreateFirstWithTag(parent *HTMLElement, tag string) *HTMLElement { + e := getFirstWithTag(tag) + if e != nil { + return e + } + e = Doc.CreateElement(tag).AsHTMLElement() + parent.AppendChild(e) + return e +} + // Value is an alias for js.Wrapper. // // Derprecated: use js.Wrapper From cabb34885969ec669733bb54da8ec77bddbaa9a7 Mon Sep 17 00:00:00 2001 From: Steve Moyer Date: Mon, 3 Jun 2019 14:24:33 -0400 Subject: [PATCH 2/2] Adds nil check to fix test failures --- global.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/global.go b/global.go index 2b1d082..5270e44 100644 --- a/global.go +++ b/global.go @@ -10,7 +10,7 @@ var ( Doc = GetDocument() Body = getFirstWithTag("body") Head = getFirstWithTag("head") - Title = getOrCreateFirstWithTag(Head, "title") + Title = getOrCreateFirstWithTag(getFirstWithTag("head"), "title") ) func getFirstWithTag(tag string) *HTMLElement { @@ -26,6 +26,9 @@ func getOrCreateFirstWithTag(parent *HTMLElement, tag string) *HTMLElement { if e != nil { return e } + if parent == nil { + return nil + } e = Doc.CreateElement(tag).AsHTMLElement() parent.AppendChild(e) return e