Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

202
Visualizações
Make DOMParser use Standard mode instead of Quirks mode

Just realized that by default, DOMParser.parseFromString() creates documents in Quirks mode. Is there a way to make it Standard mode?

console.log(document.compatMode) // 'CSS1Compat'

const doc = new DOMParser().parseFromString('<div>Hello World</div>', 'text/html');
console.log(doc.compatMode) // 'BackCompat'

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

It looks like it will if you provide the full HTML markup including the standard doctype of <!DOCTYPE html>. If you only provide it an HTML fragment - like <div>Hello World</div> - rather than a full page, the engine will create a surrounding doctype and <html> and <body> for you, and will assume quirks mode.

console.log(document.compatMode);

const doc = new DOMParser().parseFromString(`
<!DOCTYPE html>
<html lang="en">
  <body>
    <div>Hello World</div>
  </body>
</html>
`, 'text/html');
console.log(doc.compatMode);

about 4 years ago · Juan Pablo Isaza Relatório

0

All you need to do is prepend <!DOCTYPE html> to the HTML your parsing

DOMParser will result in the following "document"

<html>
  <head>
  </head>
  <body>
    <div>Hello World</div>
  </body>
</html>

console.log(document.compatMode) // 'CSS1Compat'

const doc = new DOMParser().parseFromString('<!DOCTYPE html><div>Hello World</div>', 'text/html');
console.log(doc.compatMode) // 'BackCompat'
console.log(doc.documentElement.outerHTML)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda