question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TinyMCE nested lists produce invalid HTML (ul child of ul)

See original GitHub issue

Steps to reproduce:

  • Start with the fiddle https://jsfiddle.net/cayy8tvk/
  • Click “Bullet list” toolbar icon
  • Enter “foo”, hit enter
  • Click “Increase indent” toolbar icon twice
  • Using the browser’s Inspector, inspect the last bullet point

The HTML will look something like:

<ul><li>foo<br data-mce-bogus="1"></li><ul><ul><li><br data-mce-bogus="1"></li></ul></ul></ul>

Notice the <ul><ul>. This is invalid HTML. You can verify this by using the W3C HTML5 validator which reports:

Error: Element ul not allowed as child of element ul in this context.

Additionally, this can be verified by the HTML5 spec which lists ul’s possible children as li only.


I think I understand why TinyMCE wants to do this, to avoid double bullet points with an additional li tag.

The issue I’m hitting, all content created by TinyMCE client side is validated and cleansed server side. One aspect of this cleansing is to transform any invalid HTML to valid HTML. The tag sequence <ul><ul><li> is converted to the valid <ul><li><ul><li> which, when rendered as plain HTML, has an extra bullet point.

I think an alternative solution to injecting invalid HTML would be to add style="list-style-type:none to empty <li> tag. What do you think?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
MichaelFromincommented, Aug 1, 2016

I believe that if you load the lists plugin this will cease to happen.

1reaction
anatidaephobiacommented, Jan 28, 2019

Try this in your list of init params:

setup: function (editor) {

    editor.on('PostProcess', function(editor) {  // remove duplicated ul and ol elements 

        editor.content = editor.content.replace(/<ul(.*?)>[\s]*<ul(.*?)>/gi,'<ul'+'$1'+'>');

        editor.content = editor.content.replace(/<\/ul>[\s]*<\/ul>/gi,'</ul>');

        editor.content = editor.content.replace(/<ol(.*?)>[\s]*<ol(.*?)>/gi,'<ol'+'$1'+'>');

        editor.content = editor.content.replace(/<\/ol>[\s]*<\/ol>/gi,'</ol>');

    });

}

Read more comments on GitHub >

github_iconTop Results From Across the Web

tinymce makes invalid nested lists - Stack Overflow
Element ul not allowed as child of element ul in this context. I'd prefer lists like this: <ul> <li>text</ ...
Read more >
#19632 (TinyMCE producing invalid nested list markup ...
I have learned that (jn XHTML) all list elements should be li elements, including nested lists itself. TinyMCE makes the nested list a...
Read more >
Content filtering options | Docs - TinyMCE
This option enables you to specify that list elements ( ul , ol ) should be converted to valid XHTML. This option is...
Read more >
tinyMCE and nested lists - concrete5
Because I wanted to do some jQuery stuff (clicking on the LI should show the nested UL) I ran into a problem with...
Read more >
HTML : tinymce makes invalid nested lists - YouTube
HTML : tinymce makes invalid nested lists [ Beautify Your Computer : https://www.hows.tech/p/recommended. html ] HTML : tinymce makes invalid ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found