]> Cypherpunks repositories - gostls13.git/commitdiff
exp/html: foster-parent text correctly
authorAndrew Balholm <andybalholm@gmail.com>
Wed, 8 Aug 2012 00:00:57 +0000 (10:00 +1000)
committerNigel Tao <nigeltao@golang.org>
Wed, 8 Aug 2012 00:00:57 +0000 (10:00 +1000)
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

src/pkg/exp/html/parse.go
src/pkg/exp/html/testlogs/tests7.dat.log

index 0bde2fe0e72cb70f47236400ddbc63bb042e335e..be33245600b2cad188a81339f44f999b48187565 100644 (file)
@@ -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
index dfb956b01f995e8c562d29e10b1658ab89b01f37..c19af1c822d28d68106144ef8ba7c1d91de2800b 100644 (file)
@@ -26,5 +26,5 @@ PASS "<div><p>a</x> b"
 PASS "<table><tr><td><code></code> </table>"
 PASS "<table><b><tr><td>aaa</td></tr>bbb</table>ccc"
 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>"