Skip to content

Commit 9f1ecf4

Browse files
committed
feat: implement node.isEqualNode
2 parents 261a348 + a119013 commit 9f1ecf4

File tree

5 files changed

+65
-1
lines changed

5 files changed

+65
-1
lines changed

dom/character_data.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ func (n *characterData) Length() int {
5050

5151
func (d *characterData) cloneChildren() []Node { return nil }
5252

53+
func (d *characterData) IsEqualNode(other Node) bool {
54+
if od, ok := other.(CharacterData); ok {
55+
return od.Data() == d.data && d.node.isEqualNode(other)
56+
}
57+
return false
58+
}
59+
5360
/* -------- Comment -------- */
5461

5562
type Comment interface {

dom/element.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type Element interface {
4141
InnerHTML() string
4242
SetInnerHTML(string) error
4343
TagName() string
44+
Namespace() string
4445
Matches(string) (bool, error)
4546
ID() string
4647
SetID(string)
@@ -76,6 +77,28 @@ func NewElement(tagName string, ownerDocument Document) Element {
7677
return result
7778
}
7879

80+
func (e *element) IsEqualNode(n Node) bool {
81+
if !e.isEqualNode(n) {
82+
return false
83+
}
84+
other, ok := n.(Element)
85+
if !ok {
86+
return false
87+
}
88+
if len(e.attributes) != other.Attributes().Length() {
89+
return false
90+
}
91+
if e.tagName != other.TagName() || e.namespace != other.Namespace() {
92+
return false
93+
}
94+
for _, a := range e.attributes {
95+
if v, ok := other.GetAttribute(a.Name()); !ok || v != a.Value() {
96+
return false
97+
}
98+
}
99+
return true
100+
}
101+
79102
func (e *element) SetSelf(n Node) {
80103
if self, ok := n.(Element); ok {
81104
e.selfElement = self
@@ -103,6 +126,8 @@ func (e *element) TagName() string {
103126
return strings.ToLower(e.tagName)
104127
}
105128

129+
func (e *element) Namespace() string { return e.namespace }
130+
106131
func (e *element) ID() string {
107132
id, _ := e.GetAttribute("id")
108133
return id

dom/node.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ type Node interface {
165165
TextContent() string
166166
SetTextContent(value string)
167167
Connected()
168+
IsEqualNode(Node) bool
168169
// SetSelf must be called when creating instances of structs embedding a Node.
169170
//
170171
// If this is not called, the specialised type, which is itself a Node, will
@@ -260,6 +261,23 @@ func (n *node) Observe(observer observer) Closer {
260261
return observerCloser{n, observer}
261262
}
262263

264+
func (n *node) IsEqualNode(other Node) bool { return n.isEqualNode(other) }
265+
266+
func (n *node) isEqualNode(other Node) bool {
267+
if n.self.NodeType() != other.NodeType() {
268+
return false
269+
}
270+
if n.childNodes.Length() != other.ChildNodes().Length() {
271+
return false
272+
}
273+
for i := 0; i < n.childNodes.Length(); i++ {
274+
if !n.childNodes.Item(i).IsEqualNode(other.ChildNodes().Item(i)) {
275+
return false
276+
}
277+
}
278+
return true
279+
}
280+
263281
func (n *node) removeObserver(o observer) {
264282
n.observers = slices.DeleteFunc(n.observers, func(x observer) bool { return o == x })
265283
}

internal/code-gen/scripting/configuration/dom_configuration.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ func configureDOMNode(specs *WebAPIConfig) {
143143

144144
domNode.Method("hasChildNodes").Ignore()
145145
domNode.Method("normalize").Ignore()
146-
domNode.Method("isEqualNode").Ignore()
147146
domNode.Method("compareDocumentPosition").Ignore()
148147
domNode.Method("lookupPrefix").Ignore()
149148
domNode.Method("lookupNamespaceURI").Ignore()

scripting/internal/dom/node_generated.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func (wrapper Node[T]) Initialize(jsClass js.Class[T]) {
2222
func (w Node[T]) installPrototype(jsClass js.Class[T]) {
2323
jsClass.CreatePrototypeMethod("getRootNode", w.getRootNode)
2424
jsClass.CreatePrototypeMethod("cloneNode", w.cloneNode)
25+
jsClass.CreatePrototypeMethod("isEqualNode", w.isEqualNode)
2526
jsClass.CreatePrototypeMethod("isSameNode", w.isSameNode)
2627
jsClass.CreatePrototypeMethod("contains", w.contains)
2728
jsClass.CreatePrototypeMethod("insertBefore", w.insertBefore)
@@ -73,6 +74,20 @@ func (w Node[T]) cloneNode(cbCtx js.CallbackContext[T]) (js.Value[T], error) {
7374
return codec.EncodeEntity(cbCtx, result)
7475
}
7576

77+
func (w Node[T]) isEqualNode(cbCtx js.CallbackContext[T]) (js.Value[T], error) {
78+
cbCtx.Logger().Debug("JS Function call: Node.isEqualNode")
79+
instance, errInst := js.As[dom.Node](cbCtx.Instance())
80+
if errInst != nil {
81+
return nil, errInst
82+
}
83+
otherNode, errArg1 := js.ConsumeArgument(cbCtx, "otherNode", codec.ZeroValue, codec.DecodeNode)
84+
if errArg1 != nil {
85+
return nil, errArg1
86+
}
87+
result := instance.IsEqualNode(otherNode)
88+
return codec.EncodeBoolean(cbCtx, result)
89+
}
90+
7691
func (w Node[T]) isSameNode(cbCtx js.CallbackContext[T]) (js.Value[T], error) {
7792
cbCtx.Logger().Debug("JS Function call: Node.isSameNode")
7893
instance, errInst := js.As[dom.Node](cbCtx.Instance())

0 commit comments

Comments
 (0)