Skip to content

Commit a5276cf

Browse files
committed
fix: InsertAdjacentHTML only handled elements
Non-elements, e.g., text nodes were only handles when child of an element in InsertAdjacentHTML.
1 parent 416470e commit a5276cf

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

dom/element_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,11 @@ func TestElementInsertAdjacentHTMLBeforeBegin(t *testing.T) {
175175
el := doc.GetElementById("2")
176176
gomega.Expect(el.InsertAdjacentHTML(
177177
"beforebegin",
178-
"<div>1st new child</div><div>2nd new child</div>",
178+
"<div>1st new child</div>Text node<div>2nd new child</div>",
179179
)).To(Succeed())
180180
gomega.Expect(doc.Body()).To(HaveOuterHTML(`<body>
181181
<div id="1">El 1</div>
182-
<div>1st new child</div><div>2nd new child</div><div id="2">El 2
182+
<div>1st new child</div>Text node<div>2nd new child</div><div id="2">El 2
183183
<div>El 2-a</div>
184184
<div>El 2-b</div>
185185
</div>

html/dom_parser.go

+20-17
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ func ParseFragment(
4343
result := ownerDocument.CreateDocumentFragment()
4444
if err == nil {
4545
for _, child := range nodes {
46-
element := createElementFromNode(ownerDocument, result, child)
47-
result.AppendChild(element)
46+
iterate(ownerDocument, result, child)
4847
}
4948
}
5049
return result, err
@@ -87,7 +86,7 @@ func parseIntoDocument(doc dom.Document, r io.Reader) error {
8786
if err != nil {
8887
return err
8988
}
90-
iterate(doc, doc, node)
89+
iterateChildren(doc, doc, node)
9190
return nil
9291
}
9392

@@ -128,23 +127,27 @@ func createElementFromNode(
128127
}
129128
newNode = newElm
130129
newNode = rules.AppendChild(parent, newElm)
131-
iterate(d, newNode, source)
130+
iterateChildren(d, newNode, source)
132131
return newElm
133132
}
134133

135-
func iterate(d dom.Document, dest dom.Node, source *html.Node) {
134+
func iterateChildren(d dom.Document, dest dom.Node, source *html.Node) {
136135
for child := range source.ChildNodes() {
137-
switch child.Type {
138-
case html.ElementNode:
139-
createElementFromNode(d, dest, child)
140-
case html.TextNode:
141-
dest.AppendChild(d.CreateText(child.Data))
142-
case html.DoctypeNode:
143-
dest.AppendChild(d.CreateDocumentType(child.Data))
144-
case html.CommentNode:
145-
dest.AppendChild(d.CreateComment(child.Data))
146-
default:
147-
panic(fmt.Sprintf("Node not yet supported: %v", child.Type))
148-
}
136+
iterate(d, dest, child)
137+
}
138+
}
139+
140+
func iterate(d dom.Document, dest dom.Node, child *html.Node) {
141+
switch child.Type {
142+
case html.ElementNode:
143+
createElementFromNode(d, dest, child)
144+
case html.TextNode:
145+
dest.AppendChild(d.CreateText(child.Data))
146+
case html.DoctypeNode:
147+
dest.AppendChild(d.CreateDocumentType(child.Data))
148+
case html.CommentNode:
149+
dest.AppendChild(d.CreateComment(child.Data))
150+
default:
151+
panic(fmt.Sprintf("Node not yet supported: %v", child.Type))
149152
}
150153
}

0 commit comments

Comments
 (0)