From 4d4c63ced5d7b48ac58701d5a4d0ab115d1cc44b Mon Sep 17 00:00:00 2001 From: Nikolas Sepos Date: Wed, 6 Feb 2019 15:34:16 +0100 Subject: [PATCH] Introduce HTMLElement & Update Element * HTMLElement with all properties implemented * Update Element to implement all properties * Elements' Style method moved to HTMLElement * Imput inherits from HTMLElement instead of element Signed-off-by: Nikolas Sepos --- element.go | 202 +++++++++++++++++++++++++++++++++++--- htmlElement.go | 260 +++++++++++++++++++++++++++++++++++++++++++++++++ input.go | 4 +- style.go | 7 ++ 4 files changed, 457 insertions(+), 16 deletions(-) create mode 100644 htmlElement.go diff --git a/element.go b/element.go index f6808e2..5056e9b 100644 --- a/element.go +++ b/element.go @@ -33,10 +33,190 @@ type Element struct { NodeBase } -func (e *Element) SetInnerHTML(s string) { - e.v.Set("innerHTML", s) +// Properties + +// Attributes returns a NamedNodeMap object containing the assigned attributes of the corresponding HTML element. +// func (e *Element) Attributes() NamedNodeMap { +// return e.v.Get("attributes") +// } + +// ClassList returns a DOMTokenList containing the list of class attributes. +func (e *Element) ClassList() *TokenList { + return AsTokenList(e.v.Get("classList")) +} + +// ClassName is a DOMString representing the class of the element. +func (e *Element) ClassName() string { + return e.v.Get("className").String() +} + +// SetClassName is a DOMString representing the class of the element. +func (e *Element) SetClassName(v string) { + e.v.Set("className", v) +} + +// ClientHeight returns a Number representing the inner height of the element. +func (e *Element) ClientHeight() int { + return e.v.Get("clientHeight").Int() +} + +// ClientLeft returns a Number representing the width of the left border of the element. +func (e *Element) ClientLeft() int { + return e.v.Get("clientLeft").Int() +} + +// ClientTop returns a Number representing the width of the top border of the element. +func (e *Element) ClientTop() int { + return e.v.Get("clientTop").Int() +} + +// ClientWidth returns a Number representing the inner width of the element. +func (e *Element) ClientWidth() int { + return e.v.Get("clientWidth").Int() +} + +// ComputedName returns a DOMString containing the label exposed to accessibility. +func (e *Element) ComputedName() string { + return e.v.Get("computedName").String() +} + +// ComputedRole returns a DOMString containing the ARIA role that has been applied to a particular element. +func (e *Element) ComputedRole() string { + return e.v.Get("computedRole").String() +} + +// Id is a DOMString representing the id of the element. +func (e *Element) Id() string { + return e.v.Get("id").String() +} + +// SetId is a DOMString representing the id of the element. +func (e *Element) SetId(v string) { + e.v.Set("id", v) +} + +// InnerHTML is a DOMString representing the markup of the element's content. +func (e *Element) InnerHTML() string { + return e.v.Get("innerHTML").String() +} + +// SetInnerHTML is a DOMString representing the markup of the element's content. +func (e *Element) SetInnerHTML(v string) { + e.v.Set("innerHTML", v) +} + +// LocalName a DOMString representing the local part of the qualified name of the element. +func (e *Element) LocalName() string { + return e.v.Get("localName").String() +} + +// NamespaceURI the namespace URI of the element, or null if it is no namespace. +func (e *Element) NamespaceURI() string { + return e.v.Get("namespaceURI").String() +} + +// OuterHTML is a DOMString representing the markup of the element including its content. When used as a setter, replaces the element with nodes parsed from the given string. +func (e *Element) OuterHTML() string { + return e.v.Get("outerHTML").String() +} + +// SetOuterHTML is a DOMString representing the markup of the element including its content. When used as a setter, replaces the element with nodes parsed from the given string. +func (e *Element) SetOuterHTML(v string) { + e.v.Set("outerHTML", v) +} + +// Prefix a DOMString representing the namespace prefix of the element, or null if no prefix is specified. +func (e *Element) Prefix() string { + return e.v.Get("prefix").String() +} + +// ScrollHeight returns a Number representing the scroll view height of an element. +func (e *Element) ScrollHeight() int { + return e.v.Get("scrollHeight").Int() +} + +// ScrollLeft is a Number representing the left scroll offset of the element. +func (e *Element) ScrollLeft() int { + return e.v.Get("scrollLeft").Int() } +// SetScrollLeft is a Number representing the left scroll offset of the element. +func (e *Element) SetScrollLeft(v int) { + e.v.Set("scrollLeft", v) +} + +// ScrollLeftMax returns a Number representing the maximum left scroll offset possible for the element. +func (e *Element) ScrollLeftMax() int { + return e.v.Get("scrollLeftMax").Int() +} + +// ScrollTop a Number representing number of pixels the top of the document is scrolled vertically. +func (e *Element) ScrollTop() int { + return e.v.Get("scrollTop").Int() +} + +// SetScrollTop a Number representing number of pixels the top of the document is scrolled vertically. +func (e *Element) SetScrollTop(v int) { + e.v.Set("scrollTop", v) +} + +// ScrollTopMax returns a Number representing the maximum top scroll offset possible for the element. +func (e *Element) ScrollTopMax() int { + return e.v.Get("scrollTopMax").Int() +} + +// ScrollWidth returns a Number representing the scroll view width of the element. +func (e *Element) ScrollWidth() int { + return e.v.Get("scrollWidth").Int() +} + +// Shadow returns the open shadow root that is hosted by the element, or null if no open shadow root is present. +func (e *Element) ShadowRoot() *ShadowRoot { + return AsShadowRoot(e.v.Get("shadowRoot")) +} + +// Slot returns the name of the shadow DOM slot the element is inserted in. +func (e *Element) Slot() string { + return e.v.Get("slot").String() +} + +// SetSlot returns the name of the shadow DOM slot the element is inserted in. +func (e *Element) SetSlot(v string) { + e.v.Set("slot", v) +} + +// TabStop is a Boolean indicating if the element can receive input focus via the tab key. +func (e *Element) TabStop() bool { + return e.v.Get("tabStop").Bool() +} + +// SetTabStop is a Boolean indicating if the element can receive input focus via the tab key. +func (e *Element) SetTabStop(v bool) { + e.v.Set("tabStop", v) +} + +// TagName returns a String with the name of the tag for the given element. +func (e *Element) TagName() string { + return e.v.Get("tagName").String() +} + +// UndoManager returns the UndoManager associated with the element. +func (e *Element) UndoManager() js.Value { + return e.v.Get("undoManager") +} + +// UndoScope is a Boolean indicating if the element is an undo scope host, or not. +func (e *Element) UndoScope() bool { + return e.v.Get("undoScope").Bool() +} + +// SetUndoScope is a Boolean indicating if the element is an undo scope host, or not. +func (e *Element) SetUndoScope(v bool) { + e.v.Set("undoScope", v) +} + +// Methods + func (e *Element) SetAttribute(k string, v interface{}) { e.v.Call("setAttribute", k, fmt.Sprint(v)) } @@ -49,10 +229,6 @@ func (e *Element) RemoveAttribute(k string) { e.v.Call("removeAttribute", k) } -func (e *Element) Style() *Style { - return &Style{v: e.v.Get("style")} -} - func (e *Element) GetBoundingClientRect() Rect { rv := e.v.Call("getBoundingClientRect") x, y := rv.Get("x").Int(), rv.Get("y").Int() @@ -82,20 +258,18 @@ func (e *Element) OnMouseUp(h MouseEventHandler) { e.onMouseEvent("mouseup", int(sjs.StopPropagation), h) } -func (e *Element) ClassList() *TokenList { - return AsTokenList(e.v.Get("classList")) +type AttachShadowOpts struct { + Open bool + DeligatesFocus bool } -func (e *Element) AttachShadow(open bool) *ShadowRoot { +func (e *Element) AttachShadow(opts AttachShadowOpts) *ShadowRoot { m := map[string]interface{}{} - if open { + if opts.Open { m["mode"] = "open" } else { m["mode"] = "closed" } + m["delegatesFocus"] = opts.DeligatesFocus return AsShadowRoot(e.v.Call("attachShadow", js.ValueOf(m))) } - -func (e *Element) ShadowRoot() *ShadowRoot { - return AsShadowRoot(e.v.Get("shadowRoot")) -} diff --git a/htmlElement.go b/htmlElement.go new file mode 100644 index 0000000..e31aa4e --- /dev/null +++ b/htmlElement.go @@ -0,0 +1,260 @@ +package dom + +import "github.com/dennwc/dom/js" + +// https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement + +type HTMLElement struct { + Element +} + +// Properties + +// AccessKey is a DOMString representing the access key assigned to the element. +func (e *HTMLElement) AccessKey() string { + return e.v.Get("accessKey").String() +} + +// SetAccessKey is a DOMString representing the access key assigned to the element. +func (e *HTMLElement) SetAccessKey(v string) { + e.v.Set("accessKey", v) +} + +// AccessKeyLabel returns a DOMString containing the element's assigned access key. +func (e *HTMLElement) AccessKeyLabel() string { + return e.v.Get("accessKeyLabel").String() +} + +// ContentEditable is a DOMString, where a value of "true" means the element is editable and a value of "false" means it isn't. +func (e *HTMLElement) ContentEditable() string { + return e.v.Get("contentEditable").String() +} + +// SetContentEditable is a DOMString, where a value of "true" means the element is editable and a value of "false" means it isn't. +func (e *HTMLElement) SetContentEditable(v string) { + e.v.Set("contentEditable", v) +} + +// IsContentEditable returns a Boolean that indicates whether or not the content of the element can be edited. +func (e *HTMLElement) IsContentEditable() bool { + return e.v.Get("isContentEditable").Bool() +} + +// Dataset returns a DOMStringMap with which script can read and write the element's custom data attributes (data-*) . +func (e *HTMLElement) Dataset() js.Value { + return e.v.Get("dataset") +} + +// Dir is a DOMString, reflecting the dir global attribute, representing the directionality of the element. Possible values are "ltr", "rtl", and "auto". +func (e *HTMLElement) Dir() string { + return e.v.Get("dir").String() +} + +// SetDir is a DOMString, reflecting the dir global attribute, representing the directionality of the element. Possible values are "ltr", "rtl", and "auto". +func (e *HTMLElement) SetDir(v string) { + e.v.Set("dir", v) +} + +// Draggable is a Boolean indicating if the element can be dragged. +func (e *HTMLElement) Draggable() bool { + return e.v.Get("draggable").Bool() +} + +// SetDraggable is a Boolean indicating if the element can be dragged. +func (e *HTMLElement) SetDraggable(v bool) { + e.v.Set("draggable", v) +} + +// Dropzone returns a DOMSettableTokenList reflecting the dropzone global attribute and describing the behavior of the element regarding a drop operation. +func (e *HTMLElement) Dropzone() *TokenList { + return AsTokenList(e.v.Get("dropzone")) +} + +// Hidden is a Boolean indicating if the element is hidden or not. +func (e *HTMLElement) Hidden() bool { + return e.v.Get("hidden").Bool() +} + +// SetHidden is a Boolean indicating if the element is hidden or not. +func (e *HTMLElement) SetHidden(v bool) { + e.v.Set("hidden", v) +} + +// Inert is a Boolean indicating whether the user agent must act as though the given node is absent for the purposes of user interaction events, in-page text searches ("find in page"), and text selection. +func (e *HTMLElement) Inert() bool { + return e.v.Get("inert").Bool() +} + +// SetInert is a Boolean indicating whether the user agent must act as though the given node is absent for the purposes of user interaction events, in-page text searches ("find in page"), and text selection. +func (e *HTMLElement) SetInert(v bool) { + e.v.Set("inert", v) +} + +// InnerText represents the "rendered" text content of a node and its descendants. As a getter, it approximates the text the user would get if they highlighted the contents of the element with the cursor and then copied it to the clipboard. +func (e *HTMLElement) InnerText() string { + return e.v.Get("innerText").String() +} + +// SetInnerText represents the "rendered" text content of a node and its descendants. As a getter, it approximates the text the user would get if they highlighted the contents of the element with the cursor and then copied it to the clipboard. +func (e *HTMLElement) SetInnerText(v string) { + e.v.Set("innerText", v) +} + +// ItemScope is a Boolean representing the item scope. +func (e *HTMLElement) ItemScope() bool { + return e.v.Get("itemScope").Bool() +} + +// SetItemScope is a Boolean representing the item scope. +func (e *HTMLElement) SetItemScope(v bool) { + e.v.Set("itemScope", v) +} + +// ItemType returns a DOMSettableTokenList… +func (e *HTMLElement) ItemType() *TokenList { + return AsTokenList(e.v.Get("itemType")) +} + +// ItemId is a DOMString representing the item ID. +func (e *HTMLElement) ItemId() string { + return e.v.Get("itemId").String() +} + +// SetItemId is a DOMString representing the item ID. +func (e *HTMLElement) SetItemId(v string) { + e.v.Set("itemId", v) +} + +// ItemRef returns a DOMSettableTokenList… +func (e *HTMLElement) ItemRef() *TokenList { + return AsTokenList(e.v.Get("itemRef")) +} + +// ItemProp returns a DOMSettableTokenList… +func (e *HTMLElement) ItemProp() *TokenList { + return AsTokenList(e.v.Get("itemProp")) +} + +// ItemValue returns a Object representing the item value. +func (e *HTMLElement) ItemValue() js.Value { + return e.v.Get("itemValue") +} + +// SetItemValue returns a Object representing the item value. +func (e *HTMLElement) SetItemValue(v js.Value) { + e.v.Set("itemValue", v) +} + +// Lang is a DOMString representing the language of an element's attributes, text, and element contents. +func (e *HTMLElement) Lang() string { + return e.v.Get("lang").String() +} + +// SetLang is a DOMString representing the language of an element's attributes, text, and element contents. +func (e *HTMLElement) SetLang(v string) { + e.v.Set("lang", v) +} + +// NoModule is a Boolean indicating whether an import script can be executed in user agents that support module scripts. +func (e *HTMLElement) NoModule() bool { + return e.v.Get("noModule").Bool() +} + +// SetNoModule is a Boolean indicating whether an import script can be executed in user agents that support module scripts. +func (e *HTMLElement) SetNoModule(v bool) { + e.v.Set("noModule", v) +} + +// Nonce returns the cryptographic number used once that is used by Content Security Policy to determine whether a given fetch will be allowed to proceed. +func (e *HTMLElement) Nonce() js.Value { + return e.v.Get("nonce") +} + +// SetNonce returns the cryptographic number used once that is used by Content Security Policy to determine whether a given fetch will be allowed to proceed. +func (e *HTMLElement) SetNonce(v js.Value) { + e.v.Set("nonce", v) +} + +// OffsetHeight returns a double containing the height of an element, relative to the layout. +func (e *HTMLElement) OffsetHeight() float64 { + return e.v.Get("offsetHeight").Float() +} + +// OffsetLeft returns a double, the distance from this element's left border to its offsetParent's left border. +func (e *HTMLElement) OffsetLeft() float64 { + return e.v.Get("offsetLeft").Float() +} + +// OffsetParent returns a Element that is the element from which all offset calculations are currently computed. +func (e *HTMLElement) OffsetParent() *Element { + return AsElement(e.v.Get("offsetParent")) +} + +// OffsetTop returns a double, the distance from this element's top border to its offsetParent's top border. +func (e *HTMLElement) OffsetTop() float64 { + return e.v.Get("offsetTop").Float() +} + +// OffsetWidth returns a double containing the width of an element, relative to the layout. +func (e *HTMLElement) OffsetWidth() float64 { + return e.v.Get("offsetWidth").Float() +} + +// Properties returns a HTMLPropertiesCollection… +// func (e *HTMLElement) Properties() HTMLPropertiesCollection { +// return e.v.Get("properties") +// } + +// Spellcheck is a Boolean that controls spell-checking. It is present on all HTML elements, though it doesn't have an effect on all of them. +func (e *HTMLElement) Spellcheck() bool { + return e.v.Get("spellcheck").Bool() +} + +// SetSpellcheck is a Boolean that controls spell-checking. It is present on all HTML elements, though it doesn't have an effect on all of them. +func (e *HTMLElement) SetSpellcheck(v bool) { + e.v.Set("spellcheck", v) +} + +// Style is a CSSStyleDeclaration, an object representing the declarations of an element's style attributes. +func (e *HTMLElement) Style() *Style { + return AsStyle(e.v.Get("style")) +} + +// SetStyle is a CSSStyleDeclaration, an object representing the declarations of an element's style attributes. +func (e *HTMLElement) SetStyle(v *Style) { + e.v.Set("style", v.v) +} + +// TabIndex is a long representing the position of the element in the tabbing order. +func (e *HTMLElement) TabIndex() int { + return e.v.Get("tabIndex").Int() +} + +// SetTabIndex is a long representing the position of the element in the tabbing order. +func (e *HTMLElement) SetTabIndex(v int) { + e.v.Set("tabIndex", v) +} + +// Title is a DOMString containing the text that appears in a popup box when mouse is over the element. +func (e *HTMLElement) Title() string { + return e.v.Get("title").String() +} + +// SetTitle is a DOMString containing the text that appears in a popup box when mouse is over the element. +func (e *HTMLElement) SetTitle(v string) { + e.v.Set("title", v) +} + +// Translate is a Boolean representing the translation. +func (e *HTMLElement) Translate() bool { + return e.v.Get("translate").Bool() +} + +// SetTranslate is a Boolean representing the translation. +func (e *HTMLElement) SetTranslate(v bool) { + e.v.Set("translate", v) +} + +// Methods + +// TODO diff --git a/input.go b/input.go index 99428f1..3bcdd4b 100644 --- a/input.go +++ b/input.go @@ -2,7 +2,7 @@ package dom func (d *Document) NewInput(typ string) *Input { e := d.CreateElement("input") - inp := &Input{*e} + inp := &Input{HTMLElement{*e}} inp.SetType(typ) return inp } @@ -12,7 +12,7 @@ func NewInput(typ string) *Input { } type Input struct { - Element + HTMLElement } func (inp *Input) Value() string { diff --git a/style.go b/style.go index 3e6414c..c8651f2 100644 --- a/style.go +++ b/style.go @@ -6,6 +6,13 @@ type Style struct { v js.Value } +func AsStyle(v js.Value) *Style { + if !v.Valid() { + return nil + } + return &Style{v: v} +} + func (s *Style) SetWidth(v Unit) { s.v.Set("width", v.String()) }