// 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)
        }
 }
 
+// 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) {
        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
 
 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>"