I can't figure out how to do something that seems like it should be easy. Either I can't think straight due to sleep deprivation or it is harder than it seems. Please help.
Umbraco 4.0.3 (upgraded from 4.0.2.1)
I want to paste some raw html into the TinyMCE RichText Editor (RTE) but Tidy "cleans up" the raw html and thereby changes it to something unwanted. How can I put some raw html into the RTE?
For example, to paste in this code to show an icon and textbox with the code to link to that icon we'd like to get this code out:
3. Create a simple macro with a textstring parameter that outputs an <xsl:copy-of select="/macro/html" />. This kind of works but there is a bug that makes it unusable in production... When you first paste the raw html into the macro it works perfectly, but when you save the page again the markup in the macro parameter is corrupted. The resulting macro parameter (umb_html= in this case) is:
<div umb_macroalias="InsertRawHtml" umb_html="<a href=" ismacro=" mce_href=" onresizestart="return false;" umbversionid="842894ab-075e-4fbb-a4bd-ed29f4a9f086" umbpageid="1833" title="This is rendered content from macro" class="umbMacroHolder"><!-- startUmbMacro --><span>This macro does not provides rendering in WYSIWYG editor</span><!-- endUmbMacro --></div>
4. I could make a template with custom markup for this specific page. Not a good option if pasting in raw html is needed on more than one or two pages on the site. And it would always require intervention by the a site admin.
5. Add a document type property for 'raw html' that is a multiline
text field and output it after the bodyText in the template. This not only clutters up the docTypes with a rarely-used field but the raw html could only appear before or after the RTE content, not embedded in the midst of the RTE.
What about a variation on 5 where you have a RTE field, and a multiline text field. Create a macro which takes a fieldname as a parameter and outputs the content of the field. This could then be inserted into the RTE field whereever you want.
There is a section called <validElements> in the tinyMceConfig.config file that specify the HTML tags that are allowed (and hence not removed upon saving/pasting).
Getting closer to a solution but still need some more brain cells thinking about this!
@Gerben's package is definetly more convenient than using/allowing the HTML button in the RTE. In the end, though, It puts the markup into TinyMCE just the same as if you'd used the HTML button. And that means the output will be "cleaned" so no great advance there.
But Gerben's package got me thinking about the /config/tinymce.config file and after a few additions to the 'valid elements' section for a 'textarea' I'm much closer. I paste in this code:
Notice the mce_href= parameter that have been added (you also get mce_src= when it is an <img> tag).
How can I keep those from being included?
Interestingly, if I paste in html that is already encoded so that the
markup is not encoded by the RTE then the mce_href= is not included.
That is, if I paste in this line I get that exact line out again:
This gets more interesting (and frustrating) by the minute.
I finally thought I would disable Tidy entirely (<TidyEditorContent>False</TidyEditorContent> in the /config/umbracosettings.config file) but that made no difference because the markup is valid already.
This confirms that it is TinyMCE that is adding the mce_href= and mce_src= tags and handling the encoding of pasted HTML, rather than Tidy.
Does anyone know how to configure TinyMCE to NOT rewrite pasted html?
Allrighty. I just spent some time debugging my way through the tinymce source code, and it seems that it is not possible to get it to do what you want.
The thing is, that when you insert your code in the source view and click the update button, then the text is run through a serializer. As far as I can tell, that serializer relies on the DOM to iterate through all the elements of the html that is inserted. This means that any content inside a textarea (encoded or not) is considered to be a text node, and because of that it is written to the editor as a text node, thus being escaped.
So it is not something that tinyMCE does specifically, but rather related to the way that the DOM works.
Trying to enter the markup in the w3c validator told me that it is not allowed to enter child elements in a textarea element, so I think that tinyMCE is doing the right thing.
Will investigate a bit more on the _mce_href thing...
Ok, investigated a bit more, and it seems like it must be a bug in tinymce, so that it adds the attribute, but fails to remove it because the element has been escaped in the meantime.
My best bet would be to either fix it in an eventhandler using regex, or use one of the suggestions with pasting code to a regular textbox, and insert it using a macro placeholder.
Thanks everyone for excellent advice and support on the forum and on twitter!
@Morten, you're a star. Thanks for taking the time to look into this issue. Great to know what is going on and why!!
In case anyone is interested, I finally resolved the problem by looking at the problem from a fresh perspective. I had it in my mind that I wanted to insert some html into the RTE and that's where all the trouble is.
Thinking about it more I realized that I only wanted it to LOOK like I had entered html in the editor. In other words, I wanted to display a code snippet to the website visitor. I wanted to use a <textarea> to encapsulate it. Or so I thought.
My solution was to do the following:
Add the following to my CSS:
pre { font-family:consolas,"Andale Mono","Courier New",monospace; font-size: 12px; width: 472px; border:1px solid #CCC; padding: 5px; margin-top: 0; /* AUTO-WRAP IF NECESSARY */ overflow-x: auto; /* Use horizontal scroller if needed; for Firefox 2, not needed in Firefox 3 */ white-space: pre-wrap; /* css-3 */ white-space: -moz-pre-wrap !important; /* Mozilla, since 1999 */ white-space: -pre-wrap; /* Opera 4-6 */ white-space: -o-pre-wrap; /* Opera 7 */ /* width: 99%; */ word-wrap: break-word; /* Internet Explorer 5.5+ */ }
Right-click my CSS file and create a new property called "Code Block", setting the alias to "pre"
Paste in the HTML I wanted to display into the RTE (but without the <template> tags... just the anchor and image)
Highlight that HTML and select "Code Block" from the styles dropdown in the RTE
cheers, and thanks again for everyone's help, doug.
I have a stylesheet property that surrounds the "pre" tag around my code snippet. I the javascript code highlighter by Alex Gorbatchev to make the code look nice. For this to work properly, I have to give the "pre" block a class which defines the programming language. So I have some extra stylesheet properties to set those classes, one of them is aliased ".brush: c-sharp" for example. So with my current selection on the "pre" block, also add the C# class to it and voilá, it looks pretty.
How to insert raw html in RTE
I can't figure out how to do something that seems like it should be easy. Either I can't think straight due to sleep deprivation or it is harder than it seems. Please help.
Umbraco 4.0.3 (upgraded from 4.0.2.1)
I want to paste some raw html into the TinyMCE RichText Editor (RTE) but Tidy "cleans up" the raw html and thereby changes it to something unwanted. How can I put some raw html into the RTE?
For example, to paste in this code to show an icon and textbox with the code to link to that icon we'd like to get this code out:
1. Paste into RTE directly then all the codes are escaped and the page displays the markup rarther than running the markup. This is the result:
2. Click the HTML button on the RTE toolbar and paste the raw html into the popup dialog. This is the result (same as pasting into the RTE directly):
3. Create a simple macro with a textstring parameter that outputs an <xsl:copy-of select="/macro/html" />. This kind of works but there is a bug that makes it unusable in production... When you first paste the raw html into the macro it works perfectly, but when you save the page again the markup in the macro parameter is corrupted. The resulting macro parameter (umb_html= in this case) is:
4. I could make a template with custom markup for this specific page. Not a good option if pasting in raw html is needed on more than one or two pages on the site. And it would always require intervention by the a site admin.
5. Add a document type property for 'raw html' that is a multiline text field and output it after the bodyText in the template. This not only clutters up the docTypes with a rarely-used field but the raw html could only appear before or after the RTE content, not embedded in the midst of the RTE.
What is the solution?
cheers,
doug.
What about a variation on 5 where you have a RTE field, and a multiline text field. Create a macro which takes a fieldname as a parameter and outputs the content of the field. This could then be inserted into the RTE field whereever you want.
I usually do option 5...
Hi Douglas,
There is a section called <validElements> in the tinyMceConfig.config file that specify the HTML tags that are allowed (and hence not removed upon saving/pasting).
You can use my TinyMCE Insert Embed code package to insert HTML straight into the HTML editor. http://our.umbraco.org/projects/wvd-media---tinymce-insert-embed-code
Hope this is of any use!
Greets,
Gerben
Getting closer to a solution but still need some more brain cells thinking about this!
@Gerben's package is definetly more convenient than using/allowing the HTML button in the RTE. In the end, though, It puts the markup into TinyMCE just the same as if you'd used the HTML button. And that means the output will be "cleaned" so no great advance there.
But Gerben's package got me thinking about the /config/tinymce.config file and after a few additions to the 'valid elements' section for a 'textarea' I'm much closer. I paste in this code:
And get this code after saving the page:
Notice the mce_href= parameter that have been added (you also get mce_src= when it is an <img> tag).
How can I keep those from being included?
Interestingly, if I paste in html that is already encoded so that the markup is not encoded by the RTE then the mce_href= is not included. That is, if I paste in this line I get that exact line out again:
But I can't ask users to encode the appropriate characters.
All ideas welcome!
cheers,
doug.
This gets more interesting (and frustrating) by the minute.
I finally thought I would disable Tidy entirely (<TidyEditorContent>False</TidyEditorContent> in the /config/umbracosettings.config file) but that made no difference because the markup is valid already.
This confirms that it is TinyMCE that is adding the mce_href= and mce_src= tags and handling the encoding of pasted HTML, rather than Tidy.
Does anyone know how to configure TinyMCE to NOT rewrite pasted html?
cheers,
doug.
I even tried the old TinyMCE (which is deprecated and not supported in umbraco 4 anymore)... just in case it might be better but... no.
Open to all ideas; even crazy ones.
cheers,
doug.
Doug,
Here's a crazy idea: could you place the raw html in an XSLT file and insert it with a macro?
David Hill
When I need paste html, I temporary change datatype to textstring multiple,paste my html and save. Then change it back.
It's not elegant, but it works for me...
Allrighty. I just spent some time debugging my way through the tinymce source code, and it seems that it is not possible to get it to do what you want.
The thing is, that when you insert your code in the source view and click the update button, then the text is run through a serializer. As far as I can tell, that serializer relies on the DOM to iterate through all the elements of the html that is inserted. This means that any content inside a textarea (encoded or not) is considered to be a text node, and because of that it is written to the editor as a text node, thus being escaped.
So it is not something that tinyMCE does specifically, but rather related to the way that the DOM works.
Trying to enter the markup in the w3c validator told me that it is not allowed to enter child elements in a textarea element, so I think that tinyMCE is doing the right thing.
Will investigate a bit more on the _mce_href thing...
Ok, investigated a bit more, and it seems like it must be a bug in tinymce, so that it adds the attribute, but fails to remove it because the element has been escaped in the meantime.
My best bet would be to either fix it in an eventhandler using regex, or use one of the suggestions with pasting code to a regular textbox, and insert it using a macro placeholder.
Thanks everyone for excellent advice and support on the forum and on twitter!
@Morten, you're a star. Thanks for taking the time to look into this issue. Great to know what is going on and why!!
In case anyone is interested, I finally resolved the problem by looking at the problem from a fresh perspective. I had it in my mind that I wanted to insert some html into the RTE and that's where all the trouble is.
Thinking about it more I realized that I only wanted it to LOOK like I had entered html in the editor. In other words, I wanted to display a code snippet to the website visitor. I wanted to use a <textarea> to encapsulate it. Or so I thought.
My solution was to do the following:
cheers, and thanks again for everyone's help,
doug.
I have a similar set-up for my blog.
I have a stylesheet property that surrounds the "pre" tag around my code snippet. I the javascript code highlighter by Alex Gorbatchev to make the code look nice. For this to work properly, I have to give the "pre" block a class which defines the programming language. So I have some extra stylesheet properties to set those classes, one of them is aliased ".brush: c-sharp" for example. So with my current selection on the "pre" block, also add the C# class to it and voilá, it looks pretty.
Login to admin side -> plugin Manager -> MCE editor ->
code cleanup on save - "Always",
Now, afetr u save article, it will remove mce_herf arribute,
Thanks,
Vishal Kinkar
Krawler Networks,
is working on a reply...