Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Mar 25, 2010 @ 13:01
    Douglas Robar
    0

    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:

    <a href="http://www.example.com/">
    <img src="http://static.example.com/cc/link1.gif" />
    </a>
    <br/>
    <textarea cols="50" rows="3">
    <a href="http://www.example.com/">
    <img src="http://static.example.com/cc/link1.gif">
    </a>
    </textarea>

     

    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:

    <p><a href="http://www.example.com/"> <img src="http://static.example.com/cc/link1.gif" alt="" /> </a> <br /> &lt;a href="http://www.example.com/" mce_href="http://www.example.com/"&gt;        &lt;img src="http://static.example.com/cc/link1.gif" mce_src="http://static.example.com/cc/link1.gif"&gt;   &lt;/a&gt;</p>

     

    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):

    <p><a href="http://www.example.com/"> <img src="http://static.example.com/cc/link1.gif" alt="" /> </a> <br /> &lt;a href="http://www.example.com/" mce_href="http://www.example.com/"&gt;        &lt;img src="http://static.example.com/cc/link1.gif" mce_src="http://static.example.com/cc/link1.gif"&gt;   &lt;/a&gt;</p>

     

    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="&lt;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 is the solution?

    cheers,
    doug.

  • Paul Marden 235 posts 338 karma points MVP c-trib
    Mar 25, 2010 @ 13:05
    Paul Marden
    0

    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.

  • Ian Smedley 97 posts 192 karma points
    Mar 25, 2010 @ 13:07
    Ian Smedley
    0

    I usually do option 5... 

  • Gerben 41 posts 136 karma points
    Mar 25, 2010 @ 13:28
    Gerben
    0

    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


     

     

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Mar 25, 2010 @ 15:02
    Douglas Robar
    0

    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:

    <textarea><a href="abc.aspx">abc</a></textarea>

    And get this code after saving the page:

    <p><textarea>&lt;a href="abc.aspx" mce_href="/abc.aspx"&gt;abc&lt;/a&gt;</textarea></p>

    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:

    <textarea>&lt;a href="abc.aspx"&gt;abc&lt;/a&gt;</textarea>

    But I can't ask users to encode the appropriate characters.

     

    All ideas welcome!

    cheers,
    doug.

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Mar 25, 2010 @ 19:08
    Douglas Robar
    0

    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.

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Mar 25, 2010 @ 19:16
    Douglas Robar
    0

    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.

  • David F. Hill 122 posts 242 karma points
    Mar 25, 2010 @ 19:33
    David F. Hill
    0

    Doug,

    Here's a crazy idea: could you place the raw html in an XSLT file and insert it with a macro?

    David Hill

  • Petr Snobelt 923 posts 1535 karma points
    Mar 26, 2010 @ 14:38
    Petr Snobelt
    0

    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...

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Mar 28, 2010 @ 03:13
    Morten Bock
    0

    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...

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Mar 28, 2010 @ 03:51
    Morten Bock
    0

    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.

     

     

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Mar 29, 2010 @ 17:17
    Douglas Robar
    0

    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:

    1. 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+ */
          }
       
    2. Right-click my CSS file and create a new property called "Code Block", setting the alias to "pre"
    3. Paste in the HTML I wanted to display into the RTE (but without the <template> tags... just the anchor and image)
    4. Highlight that HTML and select "Code Block" from the styles dropdown in the RTE

     

    cheers, and thanks again for everyone's help,
    doug.

  • Sebastiaan Janssen 5045 posts 15478 karma points MVP admin hq
    Mar 31, 2010 @ 15:02
    Sebastiaan Janssen
    0

    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.

  • vishal 1 post 21 karma points
    Apr 09, 2012 @ 08:36
    vishal
    0

    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,

Please Sign in or register to post replies

Write your reply to:

Draft