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

94
Visualizações
How to serialize HTML without head and body tags using jsdom

I want to clear some tags from a html document like so:

const { JSDOM } = require('jsdom');

function clearAnchorTags(pDom: JSDOM) {
    if (typeof pDom === 'string') {
        pDom = new JSDOM(pDom);
    }

// Get all anchor tags from dom
for (const anchorTag of pDom.window.document.querySelectorAll('a')) {
    for (const child of anchorTag.children) {
        if (child.nodeName === 'EM') {
            const node = pDom.window.document.createTextNode(child.textContent);
            anchorTag.replaceChild(node, child);
        }
        if (child.nodeName === 'B') {
            const node = pDom.window.document.createTextNode(child.textContent);
            anchorTag.replaceChild(node, child);
        }
        if (child.nodeName === 'U') {
            const node = pDom.window.document.createTextNode(child.textContent);
            anchorTag.replaceChild(node, child);
        }
    }
}

// return as string just as we received it -- also replace   with space
return pDom.serialize().replace(/ /g, ' ');
}

The issue seems to be the serialize() method here at the end, as it returns a fully featured HTML document.

I need it without the <html>, <head> and <body> tags. I only need a HTML fragment.

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

0

I fixed my problem using

return pDom.window.document.body.innerHTML.replace(/&nbsp;/g, ' ');
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