HtmlCleaner API

HtmlCleaner API

Create cleaner instance:

Constructor or methodPurpose
HtmlCleaner() Create cleaner with default tag information provider.
HtmlCleaner(ITagInfoProvider) Create cleaner with custom tag information provider.

Set cleaner properties in order to tune its behavior:

Set cleaner transformations:new!

Constructor or methodPurpose
CleanerTransformations() Create collection of transformations.
TagTransformation(String, String, boolean) Create single tag transformation.
CleanerTransformations.
addTransformation(TagTransformation)
Add tag transormation to transformations collection.
TagTransformation.
addAttributeTransformation(String, String)
Specify attribute transformation for the tag transformation.
HtmlCleaner.
setTransformations(CleanerTransformations)
Set cleaner transformations.

Clean HTML with instance of HtmlCleaner:

Search cleaned DOM and modify its structure:

Serialize DOM nodes:

Providing custom tag info set

HtmlCleaner implements default HTML tag set and rules for their balancing, that is similar to the browsers' behavior. However, user is free to implement interface ITagInfoProvider or extend some of its imlementations in order to provide custom tag info set. The easiest way to do that is to write XML configuration file which describes all tags and their dependacies and use ConfigFileTagProvider like:

HtmlCleaner cleaner = 
    new
 HtmlCleaner(
 new
 ConfigFileTagProvider(
myConfigFile)
 )
;

 

Perhaps the best starting point is default tag ruleset description file . It is the basis for DefaultTagProvider .

For example, someone may not like the rule that implicit TBODY is inserted before TR in the HTML table. To remove it, find <tag name="tr"... element in the XML and remove tbody from req-enclosing-tags section.

Setting cleaner transformations

Following code snippet demonstrates how to set tranformations from the example :

...
HtmlCleaner
 cleaner = new
 HtmlCleaner(
...)
;

...
CleanerTransformations
 transformations = 
    new
 CleanerTransformations(
)
;

 
TagTransformation tt = new
 TagTransformation(
"cfoutput"
)
;

transformations.addTransformation
(
tt)
;

 
tt = new
 TagTransformation(
"c:block"
, "div"
, false
)
;

transformations.addTransformation
(
tt)
;

 
tt = new
 TagTransformation(
"font"
, "span"
, true
)
;

tt.addAttributeTransformation
(
"size"
)
;

tt.addAttributeTransformation
(
"face"
)
;

tt.addAttributeTransformation
(

    "style"
, 
    "${style};font-family=${face};font-size=${size};"

)
;

transformations.addTransformation
(
tt)
;

...
cleaner
.setTransformations
(
transformations)
;

...
TagNode
 node = cleaner.clean
(
...)
;
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值