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

Retain original hierarchy option #1354

Open
TheJaredWilcurt opened this issue Dec 13, 2024 · 1 comment
Open

Retain original hierarchy option #1354

TheJaredWilcurt opened this issue Dec 13, 2024 · 1 comment

Comments

@TheJaredWilcurt
Copy link

TheJaredWilcurt commented Dec 13, 2024

When working with markup generated via components, or component testing, sometimes you receive stubbed markup that represents HTML5 compliant code, but is abstracted. For example:

parseFragment Input Output
<tbody>
  <tr>
    <td>
      Text
    </td>
  </tr>
  <component-that-gets-rendered-as-tr />
</tbody>
<tbody>
  <tr>
    <td>
      Text
    </td>
  </tr>
</tbody>
<component-that-gets-rendered-as-tr>
</component-that-gets-rendered-as-tr>
<table>
  <tbody>
    <tr>
      <td>
        Text
      </td>
    </tr>
    <component-that-gets-rendered-as-tr />
  </tbody>
</table>
<component-that-gets-rendered-as-tr>
</component-that-gets-rendered-as-tr>
<table>
  <tbody>
    <tr>
      <td>
        Text
      </td>
    </tr>
  </tbody>
</table>

Parse5 sees that there is a non <tr> as a direct descendant of a <tbody>, and as a result, it tries to correct the situation by relocating the child to a different place in the DOM.

Would it be possible to retain the nested DOM hierarchy of the original input in the produced AST, given a specific boolean option being set? Something like this:

import { parseFragment } from 'parse5';

const markup = `
<tbody>
  <tr>
    <td>
      Text
    </td>
  </tr>
  <component-that-gets-rendered-as-tr />
</tbody>
`.trim();

const astOptions = {
  sourceCodeLocationInfo: true,
  retainOriginalHierarchy: true
};

const ast = parseFragment(markup, astOptions);
@fb55
Copy link
Collaborator

fb55 commented Dec 16, 2024

You can switch to the htmlparser2 backend, as that has simpler semantics.

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

2 participants