Commit fca45719 authored by Andrew Balholm's avatar Andrew Balholm Committed by Nigel Tao

exp/html: foster-parent text correctly

If a table contained whitespace, text nodes would not get foster parented
correctly.

Pass 1 additional test.

R=nigeltao
CC=golang-dev
https://golang.org/cl/6459054
parent 5f7bec69
...@@ -208,15 +208,7 @@ loop: ...@@ -208,15 +208,7 @@ loop:
// addChild adds a child node n to the top element, and pushes n onto the stack // addChild adds a child node n to the top element, and pushes n onto the stack
// of open elements if it is an element node. // of open elements if it is an element node.
func (p *parser) addChild(n *Node) { func (p *parser) addChild(n *Node) {
fp := false if p.shouldFosterParent() {
if p.fosterParenting {
switch p.top().DataAtom {
case a.Table, a.Tbody, a.Tfoot, a.Thead, a.Tr:
fp = true
}
}
if fp {
p.fosterParent(n) p.fosterParent(n)
} else { } else {
p.top().Add(n) p.top().Add(n)
...@@ -227,6 +219,18 @@ func (p *parser) addChild(n *Node) { ...@@ -227,6 +219,18 @@ func (p *parser) addChild(n *Node) {
} }
} }
// shouldFosterParent returns whether the next node to be added should be
// foster parented.
func (p *parser) shouldFosterParent() bool {
if p.fosterParenting {
switch p.top().DataAtom {
case a.Table, a.Tbody, a.Tfoot, a.Thead, a.Tr:
return true
}
}
return false
}
// fosterParent adds a child node according to the foster parenting rules. // fosterParent adds a child node according to the foster parenting rules.
// Section 12.2.5.3, "foster parenting". // Section 12.2.5.3, "foster parenting".
func (p *parser) fosterParent(n *Node) { func (p *parser) fosterParent(n *Node) {
...@@ -277,7 +281,15 @@ func (p *parser) addText(text string) { ...@@ -277,7 +281,15 @@ func (p *parser) addText(text string) {
if text == "" { if text == "" {
return return
} }
// TODO: distinguish whitespace text from others.
if p.shouldFosterParent() {
p.fosterParent(&Node{
Type: TextNode,
Data: text,
})
return
}
t := p.top() t := p.top()
if i := len(t.Child); i > 0 && t.Child[i-1].Type == TextNode { if i := len(t.Child); i > 0 && t.Child[i-1].Type == TextNode {
t.Child[i-1].Data += text t.Child[i-1].Data += text
......
...@@ -26,5 +26,5 @@ PASS "<div><p>a</x> b" ...@@ -26,5 +26,5 @@ PASS "<div><p>a</x> b"
PASS "<table><tr><td><code></code> </table>" PASS "<table><tr><td><code></code> </table>"
PASS "<table><b><tr><td>aaa</td></tr>bbb</table>ccc" PASS "<table><b><tr><td>aaa</td></tr>bbb</table>ccc"
PASS "A<table><tr> B</tr> B</table>" PASS "A<table><tr> B</tr> B</table>"
FAIL "A<table><tr> B</tr> </em>C</table>" PASS "A<table><tr> B</tr> </em>C</table>"
PASS "<select><keygen>" PASS "<select><keygen>"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment