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

Wrong HTML generated: <p> tag placed around closing </div> tag #703

Open
MaxLap opened this issue Jan 24, 2021 · 1 comment
Open

Wrong HTML generated: <p> tag placed around closing </div> tag #703

MaxLap opened this issue Jan 24, 2021 · 1 comment

Comments

@MaxLap
Copy link

MaxLap commented Jan 24, 2021

Hello, for some reason, it seems that RedCarpet 3.5.1 can kind of get tripped up by html in the string, and the final string will be invalid HTML.

Basically:

puts Redcarpet::Markdown.new(Redcarpet::Render::HTML).render("<div><div></div>\n</div>")

Will print:

<div><div></div>

<p></div></p>

That last line is problematic. The <p> tag contains only a closing tag.

The actual string returned is "<div><div></div></div>\n"


Oddly, adding a newline between the first two <div> makes the problem go away. This isn't a workaround for generated code, but may help investigation:

Redcarpet::Markdown.new(Redcarpet::Render::XHTML).render("<div>\n<div></div>\n</div>")

Prints:

<div>
<div></div>
</div>

The actual string returned is "<div>\n<div></div>\n</div>\n"


Without the newline, between the two </div>, the problem also doesn't happen.

puts Redcarpet::Markdown.new(Redcarpet::Render::HTML).render("<div><div></div></div>")

Prints:

<div><div></div></div>

Actual string returned: "<div><div></div></div>\n"

Regards

MaxLap added a commit to MaxLap/MaxLap.github.io that referenced this issue Jan 24, 2021
@movermeyer
Copy link

I also ran into this problem. In my case, the end result was a Markdown table not getting rendered.

Here is a different (failing) test case, in case it helps:

def test_my_table_problem
  input = "<div><pre class=\"highlight ruby\"><code><span class=\"n\">foo</span>\n</code></pre></div>\n\n| Foo |\n| --- |\n| Bar |\n\n<div class=\"alert alert-info full-width\">\n  <h5 class=\"card-title\">Foo</h5>\n  <p>Blamco!</p>\n</div>\n"
  result = render(input, with: [:tables])
  assert result.include?("table"), result.inspect.to_s
end

Note that there are three elements in this example input: 2 HTML blocks and 1 table:

HTML block 1: <div><pre class=\"highlight ruby\"><code><span class=\"n\">foo</span>\n</code></pre></div>
Table: | Foo |\n| --- |\n| Bar |
HTML block 2: <div class=\"alert alert-info full-width\">\n <h5 class=\"card-title\">Foo</h5>\n <p>Blamco!</p>\n</div>

Within each HTML block, there is a \n character. Removing the \n from either of the HTML blocks causes the problem to go away.


I can confirm that this problem has been present since at least v2.3.0 (running the tests on older versions is difficult, since they rely on the deprecated rubygems source, and I'm not quite sure how to fix that)

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