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

Trix adds newline after list #1085

Open
n4nos opened this issue Aug 31, 2023 · 2 comments
Open

Trix adds newline after list #1085

n4nos opened this issue Aug 31, 2023 · 2 comments

Comments

@n4nos
Copy link

n4nos commented Aug 31, 2023

If there is a list (ordered or unordered) at the end of the data, trix adds another <div><br></div> to the end. So if you submit the data and reopen the editor three times you have 3 extra lines in the data.

Steps to Reproduce
  1. Click in the editor and insert a list with text
  2. Submit the data and reopen it
  3. Now you have an extra newline at the end
  4. Repeat 2. for more extra lines
Details
  • Trix version: 2.0.5
  • Browser name and version: Chrome latest 3 versions
  • Operating system: Windows, Linux
@n4nos
Copy link
Author

n4nos commented Oct 16, 2023

This problem is not limited to lists, it's the same with <code>, quoted Text and Links.
If the content begins with normal Text, everything works as expected, but if it begins with a list, code, quote or link, it adds newlines to the content.

@chadrschroeder
Copy link

We ran into this too, where an extra newline was being added here.

shouldAddExtraNewlineElement looks for \n characters in the content to decide if it needs to add a <br>. This is necessary while someone is typing in the editor to get the appropriate behavior. But it can add unwanted extra <br> elements during the initial load from the hidden input element.

We were using Nokogiri to process the HTML before storage. Nokogiri can add newline characters by default because of the FORMAT flag in the default HTML save options:

For example, if the HTML that Trix passes down to the server is something like this:

html = "<div>X:<br><br></div><ul><li>Y</li><li>Z</li></ul>"

Running it through Nokogiri will add newlines:

doc = Nokogiri::HTML::DocumentFragment.parse(html)
# (Do other stuff to the doc here)
doc.to_html
 => "<div>X:<br><br>\n</div><ul>\n<li>Y</li>\n<li>Z</li>\n</ul>"

Then passing that to Trix will cause an extra <br> to be added:

"<div>X:<br><br><br></div><ul><li>Y</li><li>Z</li></ul>"

In our case, we were able to work around this by stripping out the \n characters before storage. One way to do this:

doc.to_html(save_with: Nokogiri::XML::Node::SaveOptions::AS_HTML)
 => "<div>X:<br><br></div><ul><li>Y</li><li>Z</li></ul>"

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