preactjs / wmr

👩‍🚀 The tiny all-in-one development tool for modern web apps.
https://wmr.dev/
MIT License
4.93k stars 109 forks source link

Line breaks between tags and text nodes becomes whitespace if within a text node #899

Closed rschristian closed 2 years ago

rschristian commented 2 years ago

Describe the bug New lines are replaced with a space character if they occur within a text node, but this is incorrect. New lines between tags and text nodes, regardless of whether they're in a text node, should be ignored.

To Reproduce

<p>
    Example text{" "}
    <a
        href="https://example-link-location/"
        target="_blank"
        rel="noreferrer"
    >
        link
    </a>
    , and this is a problematic line break.
</p>

This is a format that tools like Prettier will generate, putting the punctuation on a new line after the end of the anchor tag. After building with WMR, however, this gets transformed into a space:

<p>Example text <a href="https://example-link-location/" target="_blank" rel="noreferrer">link</a> , and this is a problematic line break.</p>

Expected behavior New lines between tags and text nodes should be ignored, no extra whitespace.

Bug occurs with:

Additional context Just seems to be missing logic here: https://github.com/preactjs/wmr/blob/3401a9bfa6491d25108ad68688c067a7e17d0de5/packages/wmr/src/lib/acorn-traverse.js#L880-L894

Working on a fix myself and trying to see if there are any other inconsistencies at the moment.

developit commented 2 years ago

Good catch. It looks like leading whitespace should be removed from JSXText nodes if it contains a newline, and likewise for trailing whitespace (babel repl examples):

value = value.replace(/(^\s*\n\s*|\s*\n\s*$)/g, '');
rschristian commented 2 years ago

Ah, cheers. Might've forgotten to strip the trailing whitespace too.