javaangularjsjtidy

jTidy pretty print custom HTML tag


I'm trying to use JTidy to pretty print a well formed HTML generated by the user:

<div class="component-holder ng-binding ng-scope ui-draggable ui-draggable-handle" data-component="cronos-datasource" id="cronos-datasource-817277">
    <datasource name="" entity="" key="" endpoint="" rows-per-page="">
        <i class="cpn cpn-datasource"></i>
    </datasource>
</div>

This is my config:

Tidy tidy = new Tidy();
tidy.setXHTML(true);
tidy.setIndentContent(true);
tidy.setPrintBodyOnly(true);
tidy.setTidyMark(false);
tidy.setWraplen(2000);
tidy.setDropProprietaryAttributes(false);
tidy.setDropEmptyParas(false);
tidy.setTrimEmptyElements(false);

But jTidy is removing my AngularJS datasource directive. Is there a way to workarround this issue?

I'm getting this from the log:

line 1 column 191 - Error: <datasource> is not recognized!
line 1 column 191 - Warning: discarding unexpected <datasource>

Removing tidy.setXHTML(true) or setting it to false and adding tidy.setXmlTags(true) actually solve this issue and it start to consider user defined tags, but this is not a good solution because JTidy starts trying to close self enclosing tags.

 <!-- this code -->
 <img src="anythig.jpg"/>
 <div id="anyid"></div> 

 <!-- will become -->
 <img src="anythig.jpg">
     <div id="anyid"></div>
 </img>

I need a formatter for a text editor. I can't assure what directives our users will define and use. It must be a generic solution which works for any user defined directive


Solution

  • I have solved this problem by make some changes in JTidy source

    https://github.com/nanndoj/jtidy

    I have added a new configuration called dropProprietaryTags

    tidy.setDropProprietaryTags(false);
    

    Now it's working fine for me. It's set to true by default so JTidy can work in the old way if the new property is not set to false