diff --git a/html/parse.go b/html/parse.go
index 46a89eda6..d67705d9e 100644
--- a/html/parse.go
+++ b/html/parse.go
@@ -840,6 +840,10 @@ func afterHeadIM(p *parser) bool {
p.parseImpliedToken(StartTagToken, a.Body, a.Body.String())
p.framesetOK = true
+ if p.tok.Type == ErrorToken {
+ // Stop parsing.
+ return true
+ }
return false
}
diff --git a/html/parse_test.go b/html/parse_test.go
index aa91f4c2b..24b352e2a 100644
--- a/html/parse_test.go
+++ b/html/parse_test.go
@@ -468,6 +468,7 @@ func TestParseFragmentForeignContentTemplates(t *testing.T) {
srcs := []string{
"