10

我正在尝试使用 JTidy 漂亮地打印用户生成的格式良好的 HTML:

<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>

这是我的配置:

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);

但是 jTidy 正在删除我的 AngularJSdatasource指令。有没有办法解决这个问题?

我从日志中得到这个:

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

删除tidy.setXHTML(true)或将其设置为 false 并添加tidy.setXmlTags(true)实际上解决了这个问题,它开始考虑用户定义的标签,但这不是一个好的解决方案,因为 JTidy 开始尝试关闭自封闭标签。

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

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

我需要一个文本编辑器的格式化程序。我无法保证我们的用户将定义和使用哪些指令。它必须是适用于任何用户定义指令的通用解决方案

4

2 回答 2

3

尝试在当前配置之后设置以下属性:

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

请参阅http://tidy.sourceforge.net/docs/quickref.html#new-blocklevel-tags

于 2015-07-02T21:01:33.783 回答
0

我通过在 JTidy 源代码中进行一些更改解决了这个问题

https://github.com/nanndoj/jtidy

我添加了一个名为的新配置dropProprietaryTags

tidy.setDropProprietaryTags(false);

现在它对我来说很好。它true默认设置为,因此如果新属性未设置为,JTidy 可以以旧方式工作false

于 2015-07-09T18:29:58.363 回答