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

Line breaks break "array with three elements" structure #3

Open
dmr opened this issue Oct 13, 2023 · 2 comments
Open

Line breaks break "array with three elements" structure #3

dmr opened this issue Oct 13, 2023 · 2 comments

Comments

@dmr
Copy link

dmr commented Oct 13, 2023

Hi,

thank you for this simple library, I have one detail problem with newlines.

This code works correctly:

 jsxToJson(`<Test attr={null}><C /></Test>`)

and it generates

[ ['Test', { attr: null }, ['C', {}]] ]

But if I add a newline "in" the children"

 jsxToJson(`<Test attr={null}><C /></Test>`)

then it generates an array with 4 elements

[ [ 'Test', { attr: null }, [ 'C', {} ], '\n  ' ] ]

Am I the first one to have this problem?

Having an array of 4 is very bad in my opinion I think it should always have 3 and then I can iterate through the children. Maybe I understood jsx wrongly but this is my opinion do you agree?

@gglnx
Copy link
Owner

gglnx commented Oct 13, 2023

The library preserves whitespace and control characters. So a JSX string parsed through jsxToJson and then afterward recreated via a jsonToJsx function (still missing #1) would be the same.

This would be good as a new option to jsxToJson:

jsxToJson(
  `<Test attr={null}><C />  </Test>\n`,
  {
    preserveWhitespace: false,
    stripControlCharacters: true,
  },
) -> [ ['Test', { attr: null }, ['C', {}]] ]

I'm open for pull requests for that, but I can't do it myself currently.

@dmr
Copy link
Author

dmr commented Oct 13, 2023

Thank you for the quick response. For me this still feels like a thing that should "just work" by default. I stumbled upon this because my editor automatically formatted a jsx file and introduced some newlines that have no effect on the behavior of the jsx.
I feel that "preserviceWhitespace" is a complicated topic in jsx but I have to thing about it more for a qualified opinion.

Thank you for being open for pull requests, I haven't decided yet how to solve this.

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