10

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

nanndoj
  • 6,580
  • 7
  • 30
  • 42
  • What version of JTidy are you using? Are you sure there is no other configuration? AFAIK `setXmlTags(true)` should solve this. – M A Jul 02 '15 at 20:05
  • `setXmlTags(true)` actually solves this, but it starts trying to close self enclosing tags example – nanndoj Jul 02 '15 at 20:09

2 Answers2

3

Try setting the following property after your current configuration:

Properties props = new Properties();
props.setProperty("new-blocklevel-tags", "datasource");
tidy.getConfiguration().addProps(props);

See http://tidy.sourceforge.net/docs/quickref.html#new-blocklevel-tags.

M A
  • 71,713
  • 13
  • 134
  • 174
  • Thanks for your answer @manouti, but i'm writing a pretty formatter for a text editor. I can't assure what directives our users will define and use. It must be a generic solution – nanndoj Jul 03 '15 at 00:17
0

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

nanndoj
  • 6,580
  • 7
  • 30
  • 42