Skip to content

BUG: newlines are added before tables #26

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

Closed
4 tasks done
aarondill opened this issue Jul 22, 2024 · 4 comments
Closed
4 tasks done

BUG: newlines are added before tables #26

aarondill opened this issue Jul 22, 2024 · 4 comments
Labels
🙅 no/wontfix This is not (enough of) an issue for this project 👎 phase/no Post cannot or will not be acted on

Comments

@aarondill
Copy link

Initial checklist

Affected packages and versions

9.0.4

Link to runnable example

No response

Steps to reproduce

> mkdir repro && cd repro
> npm init
> npm i "hast-util-from-html" "hast-util-raw" "hast-util-to-html"
> editor index.mjs # Paste contents from below
> node index.mjs
// index.mjs
import { fromHtml } from "hast-util-from-html";
import { raw } from "hast-util-raw";
import { toHtml } from "hast-util-to-html";

const contents = `<table>
      <thead>
        <tr><th>Column</th></tr>
      </thead>
      <tbody>
        <tr><td>foo</td></tr>
        <tr><td>bar</td></tr>
      </tbody>
    </table>`;
const hast = fromHtml(contents, { fragment: true });
const reformatted = raw(hast);

console.log("Without util-raw:");
console.log(toHtml(hast));
console.log("With util-raw:");
console.log(toHtml(reformatted));

Expected behavior

hast-util-raw shouldn't add whitespace lines at the start.

This was discussed in rehypejs/rehype-raw#22 and they decided it wasn't a bug since ordinarily browsers parse it the same, however, when using non-default values of the CSS property white-space (such as pre or pre-wrap) this issue appears.

Actual behavior

A text node with whitespace is added:

{
   // ...,
   {
      type: 'text',
      value: '\n      \n        \n      \n      \n        \n        \n      \n    '
    },
    // ...
}

Affected runtime and version

[email protected]

Affected package manager and version

[email protected]

Affected OS and version

Arch Linux (Rolling)

Build and bundle tools

No response

@github-actions github-actions bot added 👋 phase/new Post is being triaged automatically 🤞 phase/open Post is being triaged manually and removed 👋 phase/new Post is being triaged automatically labels Jul 22, 2024
@wooorm
Copy link
Member

wooorm commented Jul 22, 2024

#22 stands; this project does what HTML does.

however, when using non-default values of the CSS property white-space (such as pre or pre-wrap) this issue appears.

Don‘t.

Or, explain what you are actually doing. https://xyproblem.info

@aarondill
Copy link
Author

I'd like newlines the user inputted to be maintained, while also allowing user inputted html to be rendered (yes, I know the security risks).
For example:

Input: Hello World
Output: Hello
World

Input:

Hello


<table>
      <thead>
        <tr><th>Column</th></tr>
      </thead>
      <tbody>
        <tr><td>foo</td></tr>
        <tr><td>bar</td></tr>
      </tbody>
    </table>
World

Output:

Hello

Column
foo
bar

World

@wooorm
Copy link
Member

wooorm commented Jul 22, 2024

It’s not possible. The information is lost.

If you want to remove unneeded whitespace, use https://github.com/rehypejs/rehype-minify/tree/main/packages/rehype-minify-whitespace. If you want pretty whitespace, use https://github.com/rehypejs/rehype-format.

@wooorm wooorm closed this as not planned Won't fix, can't repro, duplicate, stale Jul 22, 2024
@wooorm wooorm added the 🙅 no/wontfix This is not (enough of) an issue for this project label Jul 22, 2024

This comment has been minimized.

@github-actions github-actions bot added 👎 phase/no Post cannot or will not be acted on and removed 🤞 phase/open Post is being triaged manually labels Jul 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🙅 no/wontfix This is not (enough of) an issue for this project 👎 phase/no Post cannot or will not be acted on
Development

No branches or pull requests

2 participants