jTidy pretty print custom HTML tag

北慕城南 提交于 2019-12-23 07:28:13

问题


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


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/30374232/jtidy-pretty-print-custom-html-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!