better doctype detection
(cherry picked from commit e16690a)
r=zolotov
fix test
(cherry picked from commit 403d4f1)
r=zolotov