Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong output on malformed HTML #268

Open
amartini opened this issue Feb 21, 2024 · 0 comments
Open

Wrong output on malformed HTML #268

amartini opened this issue Feb 21, 2024 · 0 comments

Comments

@amartini
Copy link

I know it's hard to predict every malformed HTML possibilities, but I came across this while scraping a website. The misplaced apostrophe before the > of the <a> makes the parser skip the rest of the row. This code displays correctly on browsers (the invalid token is discarded). If you remove the ' the code runs correctly.

import { parse } from 'node-html-parser';

const html = `
<table id="mytable">
<tr class="myrow">
  <td>1</td>
  <td><a href="#" 2'>x</a></td>
  <td>2</td>
</tr>
<tr class="myrow">
  <td>3</td>
  <td><a href="#" 2'>x</a></td>
  <td>4</td>
</tr>
</table>
`;

const root = parse(html);

for (let tr of root.querySelectorAll("#mytable tr.myrow")) {
  console.log(tr.querySelectorAll(":scope > td").map(e => e.innerText));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant