Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure TinyMCE to allow block-level elements inside anchor (a) tag?

Tags:

html

tinymce

Here is my scenario:

I want to be able to create content like

<div> <a id="supportTile" class="contentModule" href="/Support"> <h2>Support</h2> </a> </div>

However tinyMCE strips it to

<div> <h2>Support</h2> </div>

My configuration is currently like this (using TinyMCE jQuery):

script_url: _applicationRoot + "Scripts/tiny_mce/tiny_mce.js",
theme: "advanced",                    
plugins: "paste,filemanager,imagemanager,advimage,inlinepopups",
...
extended_valid_elements: "img[!src|border:0|alt|title|width|height|style|name|id|class],a[href|target|title|onclick|name|id|class],article[name|id|class],div[name|id|class],section[name|id|class]",
schema: "html5",               
...
convert_urls: true,
document_base_url: _applicationRoot

I tried setting verify_html: false but no luck.

I tried removing extended_valid_elements and replacing it with:

valid_elements: "*[*]",
verify_html: false

No luck either.

Can you see something wrong with my configuration? Is this achievable at all?

Thanks!

like image 482
ayls Avatar asked Jan 30 '13 05:01

ayls


People also ask

How to add extra elements and attributes to TinyMCE?

You can manually add extra elements and attributes using the extended_valid_elements option. The valid_children enables you to control what child elements can exists within specified parent elements. TinyMCE will remove/split any non HTML5 or HTML transitional contents by default. So for example a p can’t be a child of another p element.

What is the HTML option in TinyMCE?

This option controls whether elements are output in the HTML or XHTML mode. xhtml is the default state for this option. This means that for example <br /> will be <br> if you set this option to html. This option allows you to get XML escaped content out of TinyMCE.

What is the forced_root_block option in TinyMCE?

Important: Setting forced_root_block to false or an empty string has been deprecated in TinyMCE 5.10. For TinyMCE 6.0, this option will only accept valid block elements. Warning: Not using p elements as the root block will impair the functionality of the editor. This option enables you specify attributes for the forced_root_block.

What is the use of TinyMCE’s span option?

If you set this option to true, TinyMCE will convert all font elements to span elements and generate span elements instead of font elements. This option should be used in order to get more W3C compatible code, since font elements are deprecated. This option enables you to specify non-HTML elements for the editor.


2 Answers

Yes, this should be achievable. In order to be able to produce output like

<div> <a id="supportTile" class="contentModule" href="/Support"> <h2>Support</h2> </a> </div>

you will have to modify the tinymce setting valid_children and valid_elements. You need to be aware that there is a default setting which you might need to enlarge.

valid_elements: "*[*]" won't work because of a bug that hopefully will be removed in the newest or next version.

I use this settings (i don't use divs nor h2s)

valid_elements: "@[id|class|title|style],"
+ "a[name|href|target|title|alt],"
+ "#p,-ol,-ul,-li,br,img[src|unselectable],-sub,-sup,-b,-i,-u,"
+ "-span[data-mce-type],hr",

valid_child_elements : "body[p,ol,ul]"
+ ",p[a|span|b|i|u|sup|sub|img|hr|#text]"
+ ",span[a|b|i|u|sup|sub|img|#text]"
+ ",a[span|b|i|u|sup|sub|img|#text]"
+ ",b[span|a|i|u|sup|sub|img|#text]"
+ ",i[span|a|b|u|sup|sub|img|#text]"
+ ",sup[span|a|i|b|u|sub|img|#text]"
+ ",sub[span|a|i|b|u|sup|img|#text]"
+ ",li[span|a|b|i|u|sup|sub|img|ol|ul|#text]"
+ ",ol[li]"
+ ",ul[li]",
like image 194
Thariama Avatar answered Oct 24 '22 17:10

Thariama


I found an alternative way of doing this and thought of sharing! There's a flag called allow_html_in_named_anchor which keeps any HTML inside the anchor.

like image 45
ra00l Avatar answered Oct 24 '22 19:10

ra00l