diff --git a/src/pkg/html/parse.go b/src/pkg/html/parse.go
index b0348790c19..49c7a65babf 100644
--- a/src/pkg/html/parse.go
+++ b/src/pkg/html/parse.go
@@ -756,6 +756,10 @@ func (p *parser) inBodyEndTagFormatting(tag string) {
furthestBlock.Add(clone)
// Step 14. Fix up the list of active formatting elements.
+ if oldLoc := p.afe.index(formattingElement); oldLoc != -1 && oldLoc < bookmark {
+ // Move the bookmark with the rest of the list.
+ bookmark--
+ }
p.afe.remove(formattingElement)
p.afe.insert(bookmark, clone)
diff --git a/src/pkg/html/parse_test.go b/src/pkg/html/parse_test.go
index e86a36f18a8..861070d103d 100644
--- a/src/pkg/html/parse_test.go
+++ b/src/pkg/html/parse_test.go
@@ -132,7 +132,7 @@ func TestParser(t *testing.T) {
rc := make(chan io.Reader)
go readDat(filename, rc)
// TODO(nigeltao): Process all test cases, not just a subset.
- for i := 0; i < 70; i++ {
+ for i := 0; i < 77; i++ {
// Parse the #data section.
b, err := ioutil.ReadAll(<-rc)
if err != nil {