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

screen.debug() deletes elements from output #42

Closed
CreativeTechGuy opened this issue Nov 18, 2022 · 1 comment · Fixed by #43
Closed

screen.debug() deletes elements from output #42

CreativeTechGuy opened this issue Nov 18, 2022 · 1 comment · Fixed by #43

Comments

@CreativeTechGuy
Copy link

Take the following example which is very similar to how toJSDOM is implemented in this library:

const htmlString = "<tr><td>Cell 1 <a>Link in Cell 1</a></td><td>Cell 2</td>";
const dom = new DOMParser().parseFromString(htmlString, "text/html");
console.log(dom.body.firstElementChild.outerHTML);

This will return just "<a>Link in Cell 1</a>".

The reason being, <tr> and <td> aren't valid tags in HTML if it isn't inside of a <table>. So when parsing, it seems to throw those away since they aren't correct. (As the very forgiving HTML parser likes to do.) As a result, the dom.body looks like: <body>Cell 1 <a>Link in Cell 1</a>Cell 2</body> as it has stripped all of those tags. Now since the next line is expecting just one root node, when it picks the first element, it ends up picking the a since that is the only element which remains.

In my testing, it seems like using the XML parser doesn't have this issue, but I'm not sure what other issues/differences the XML parser might introduce.

@KonnorRogers
Copy link
Owner

@CreativeTechGuy Hmm...perhaps its time I just bite the bullet and introduce a proper pretty-printer.

testing-library/dom-testing-library#1188 (comment)

^ this offers a possible way to do it. It's signficantly more code, but is the "proper" way to introduce printing.

Its pretty much almost exactly like whats implemented by PrettyDOM.

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

Successfully merging a pull request may close this issue.

2 participants