0
votes

I enabled codesample plugin https://www.tinymce.com/docs/plugins/codesample/ and to test it I entered just head tag like this

first

and it displays like this in the editor

second

when I save this to database it gets saved like this:

<pre class="language-markup"><code>&lt;head&gt;</code></pre>

When I reload the page all i get is this in the editor

<pre>&nbsp;</pre>

Database still looks like

<pre class="language-markup"><code>&lt;head&gt;</code></pre>

but there is no head tag visible in the editor. Any ideas why?

1
How are you loading the content back in to the editor? Perhaps you could make a simple JS Fiddle or CodePen that shows what you are doing?Michael Fromin
i am using php to write out the content so I do this <textarea id="postBody" name="postBody" class="postBody">{{$post->body}}</textarea> Textarea gets loaded with this i.imgur.com/LMNpZ21.png But TinyMCE does not initialise this correctly and shows up empty. Any ideas what is wrong?niko craft

1 Answers

0
votes

When placing raw HTML into a <textarea> you need to properly escape the HTML code that you are placing in the <textarea>. As you are not doing that the browser (and by proxy TinyMCE) is "cleaning up" the invalid markup as best as it can.

You can either:

  1. Properly escape the HTML
  2. Use the setContent() API to load your HTML into TinyMCE (as opposed to injecting the HTML into the <textarea>)

I would personally suggest the second option above as it completely eliminates the vagaries of injecting HTML into a <textarea>.

If you want to try the first solution I listed above you might do something like this if you were using PHP

<?php
  $content = "This string contains the TM symbol: &trade;";
  print "<textarea>". htmlentities($content) ."</textarea>";
?>

Every language I have used has some equivalent to htmlentities().