From: Andrew Balholm Date: Wed, 8 Aug 2012 00:00:57 +0000 (+1000) Subject: exp/html: foster-parent text correctly X-Git-Tag: go1.1rc2~2679 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=fca45719a4365a0dc709202aa3efc2c58bbe473b;p=gostls13.git 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 --- diff --git a/src/pkg/exp/html/parse.go b/src/pkg/exp/html/parse.go index 0bde2fe0e7..be33245600 100644 --- a/src/pkg/exp/html/parse.go +++ b/src/pkg/exp/html/parse.go @@ -208,15 +208,7 @@ loop: // 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. func (p *parser) addChild(n *Node) { - fp := false - if p.fosterParenting { - switch p.top().DataAtom { - case a.Table, a.Tbody, a.Tfoot, a.Thead, a.Tr: - fp = true - } - } - - if fp { + if p.shouldFosterParent() { p.fosterParent(n) } else { p.top().Add(n) @@ -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. // Section 12.2.5.3, "foster parenting". func (p *parser) fosterParent(n *Node) { @@ -277,7 +281,15 @@ func (p *parser) addText(text string) { if text == "" { return } - // TODO: distinguish whitespace text from others. + + if p.shouldFosterParent() { + p.fosterParent(&Node{ + Type: TextNode, + Data: text, + }) + return + } + t := p.top() if i := len(t.Child); i > 0 && t.Child[i-1].Type == TextNode { t.Child[i-1].Data += text diff --git a/src/pkg/exp/html/testlogs/tests7.dat.log b/src/pkg/exp/html/testlogs/tests7.dat.log index dfb956b01f..c19af1c822 100644 --- a/src/pkg/exp/html/testlogs/tests7.dat.log +++ b/src/pkg/exp/html/testlogs/tests7.dat.log @@ -26,5 +26,5 @@ PASS "

a b" PASS "
" PASS "bbb
aaa
ccc" PASS "A B B
" -FAIL "A BC
" +PASS "A BC
" PASS ""