Search code examples
wysihtml5

How to disabled wysihtml5 HTML Clean Up in Editor?


How to disable HTML Clean Up while in the editor mode? I'm in a need of allowing css format & inline html in code. The idea is to disable parser and html clean up action when pasting the code and entering the Editor for editing. Thanks.


Solution

  • Actually, this is what the parser rules are for.

    You can attach your custom rules to the included var wysihtml5ParserRules before instantiate the editor object or just create your own rules object and give to the editor's constructor.

    For example, to allow the h1 and h3 tag in addition to the tags allowed in the distributed simple example rules, you'd need to set up as follows:

      <form>
        <div id="toolbar" style="display: none;">
          <a data-wysihtml5-command="bold" title="CTRL+B">bold</a> |
          <a data-wysihtml5-command="italic" title="CTRL+I">italic</a>
          <a data-wysihtml5-action="change_view">switch to html view</a>
        </div>
        <textarea id="textarea" placeholder="Enter text ..."></textarea>
        <br><input type="reset" value="Reset form!">
      </form>
    
      <!-- The distributed parser rules -->
      <script src="../parser_rules/simple.js"></script>
      <script src="../dist/wysihtml5-0.4.0pre.min.js"></script>
      <script>
        // attach some custom rules
        wysihtml5ParserRules.tags.h1 = {remove: 0};
        wysihtml5ParserRules.tags.h3 = {remove: 0};
    
        var editor = new wysihtml5.Editor("textarea", {
          toolbar:        "toolbar",
          parserRules:    wysihtml5ParserRules,
          useLineBreaks:  false
        });
      </script>
    

    Now, when you enter/paste <title>test</title> into the editor, while you're in the editor mode, and then switch to html view, you'll get &lt;title&gt;test&lt;/title&gt;. And when you switch back to editor view, you'll get <title>test</title> again.


    That was the general part.

    Now, in your case, I'm not sure if it's the best idea to work with 121 custom parser rules (the count of HTML tags to handle) or if it wouldn't be better to take the time and dig into the source code to find a more performant solution (doesn't make much sense to tell a parser to actualy just return the input string anyway, right?). Furthermore, you said you want to allow CSS as well. So your custom parser rules will even extend.

    Anyway, as a starting point, feel free to use my custom parser rule set from here: https://github.com/eyecatchup/wysihtml5/blob/master/parser_rules/allow-all-html5.js.