📄 clean.java
字号:
/* adjust parent and set margin on contents of <li> */
//for (child = content; child != null; child = child.next)
//{
// child.parent = node.parent;
// addStyleProperty(child, "margin-left: 1em");
//}
/* hook first/last into sequence */
//if (content != null)
//{
// content.prev = node.prev;
// last.next = node.next;
// fixNodeLinks(content);
// fixNodeLinks(last);
//}
//node.next = null;
/* ensure that new node is cleaned */
//pnode.setObject(cleanNode(lexer, content));
//return true;
//#endif
}
return false;
}
/*
Symptom: <center>
Action: replace <center> by <div style="text-align: center">
*/
private static boolean center2Div(Lexer lexer, Node node, MutableObject pnode)
{
if (node.tag == TagTable.tagCenter)
{
if (lexer.configuration.DropFontTags)
{
if (node.content != null)
{
Node last = node.last;
Node parent = node.parent;
discardContainer(node, pnode);
node = lexer.inferredTag("br");
if (last.next != null)
last.next.prev = node;
node.next = last.next;
last.next = node;
node.prev = last;
if (parent.last == last)
parent.last = node;
node.parent = parent;
}
else
{
Node prev = node.prev;
Node next = node.next;
Node parent = node.parent;
discardContainer(node, pnode);
node = lexer.inferredTag("br");
node.next = next;
node.prev = prev;
node.parent = parent;
if (next != null)
next.prev = node;
else
parent.last = node;
if (prev != null)
prev.next = node;
else
parent.content = node;
}
return true;
}
node.tag = TagTable.tagDiv;
node.element = new String("div");
addStyleProperty(node, "text-align: center");
return true;
}
return false;
}
/*
Symptom <div><div>...</div></div>
Action: merge the two divs
This is useful after nested <dir>s used by Word
for indenting have been converted to <div>s
*/
private static boolean mergeDivs(Lexer lexer, Node node, MutableObject pnode)
{
Node child;
if (node.tag != TagTable.tagDiv)
return false;
child = node.content;
if (child == null)
return false;
if (child.tag != TagTable.tagDiv)
return false;
if (child.next != null)
return false;
mergeStyles(node, child);
stripOnlyChild(node);
return true;
}
/*
Symptom: <ul><li><ul>...</ul></li></ul>
Action: discard outer list
*/
private static boolean nestedList(Lexer lexer, Node node, MutableObject pnode)
{
Node child, list;
if (node.tag == TagTable.tagUl || node.tag == TagTable.tagOl)
{
child = node.content;
if (child == null)
return false;
/* check child has no peers */
if (child.next != null)
return false;
list = child.content;
if (list == null)
return false;
if (list.tag != node.tag)
return false;
pnode.setObject(node.next);
/* move inner list node into position of outer node */
list.prev = node.prev;
list.next = node.next;
list.parent = node.parent;
fixNodeLinks(list);
/* get rid of outer ul and its li */
child.content = null;
node.content = null;
node.next = null;
/*
If prev node was a list the chances are this node
should be appended to that list. Word has no way of
recognizing nested lists and just uses indents
*/
if (list.prev != null)
{
node = list;
list = node.prev;
if (list.tag == TagTable.tagUl || list.tag == TagTable.tagOl)
{
list.next = node.next;
if (list.next != null)
list.next.prev = list;
child = list.last; /* <li> */
node.parent = child;
node.next = null;
node.prev = child.last;
fixNodeLinks(node);
}
}
cleanNode(lexer, node);
return true;
}
return false;
}
/*
Symptom: the only child of a block-level element is a
presentation element such as B, I or FONT
Action: add style "font-weight: bold" to the block and
strip the <b> element, leaving its children.
example:
<p>
<b><font face="Arial" size="6">Draft Recommended Practice</font></b>
</p>
becomes:
<p style="font-weight: bold; font-family: Arial; font-size: 6">
Draft Recommended Practice
</p>
This code also replaces the align attribute by a style attribute.
However, to avoid CSS problems with Navigator 4, this isn't done
for the elements: caption, tr and table
*/
private static boolean blockStyle(Lexer lexer, Node node, MutableObject pnode)
{
Node child;
if ((node.tag.model & (Dict.CM_BLOCK | Dict.CM_LIST | Dict.CM_DEFLIST | Dict.CM_TABLE)) != 0)
{
if (node.tag != TagTable.tagTable
&& node.tag != TagTable.tagTr
&& node.tag != TagTable.tagLi)
{
/* check for align attribute */
if (node.tag != TagTable.tagCaption)
textAlign(lexer, node);
child = node.content;
if (child == null)
return false;
/* check child has no peers */
if (child.next != null)
return false;
if (child.tag == TagTable.tagB)
{
mergeStyles(node, child);
addStyleProperty(node, "font-weight: bold");
stripOnlyChild(node);
return true;
}
if (child.tag == TagTable.tagI)
{
mergeStyles(node, child);
addStyleProperty(node, "font-style: italic");
stripOnlyChild(node);
return true;
}
if (child.tag == TagTable.tagFont)
{
mergeStyles(node, child);
addFontStyles(node, child.attributes);
stripOnlyChild(node);
return true;
}
}
}
return false;
}
/* the only child of table cell or an inline element such as em */
private static boolean inlineStyle(Lexer lexer, Node node, MutableObject pnode)
{
Node child;
if (node.tag != TagTable.tagFont && (node.tag.model & (Dict.CM_INLINE|Dict.CM_ROW)) != 0)
{
child = node.content;
if (child == null)
return false;
/* check child has no peers */
if (child.next != null)
return false;
if (child.tag == TagTable.tagB && lexer.configuration.LogicalEmphasis)
{
mergeStyles(node, child);
addStyleProperty(node, "font-weight: bold");
stripOnlyChild(node);
return true;
}
if (child.tag == TagTable.tagI && lexer.configuration.LogicalEmphasis)
{
mergeStyles(node, child);
addStyleProperty(node, "font-style: italic");
stripOnlyChild(node);
return true;
}
if (child.tag == TagTable.tagFont)
{
mergeStyles(node, child);
addFontStyles(node, child.attributes);
stripOnlyChild(node);
return true;
}
}
return false;
}
/*
Replace font elements by span elements, deleting
the font element's attributes and replacing them
by a single style attribute.
*/
private static boolean font2Span(Lexer lexer, Node node, MutableObject pnode)
{
AttVal av, style, next;
if (node.tag == TagTable.tagFont)
{
if (lexer.configuration.DropFontTags)
{
discardContainer(node, pnode);
return false;
}
/* if FONT is only child of parent element then leave alone */
if (node.parent.content == node
&& node.next == null)
return false;
addFontStyles(node, node.attributes);
/* extract style attribute and free the rest */
av = node.attributes;
style = null;
while (av != null)
{
next = av.next;
if (av.attribute.equals("style"))
{
av.next = null;
style = av;
}
av = next;
}
node.attributes = style;
node.tag = TagTable.tagSpan;
node.element = new String("span");
return true;
}
return false;
}
/*
Applies all matching rules to a node.
*/
private static Node cleanNode(Lexer lexer, Node node)
{
Node next = null;
MutableObject o = new MutableObject();
boolean b = false;
for (next = node; node.isElement(); node = next)
{
o.setObject(next);
b = dir2Div(lexer, node, o);
next = (Node)o.getObject();
if (b)
continue;
b = nestedList(lexer, node, o);
next = (Node)o.getObject();
if (b)
continue;
b = center2Div(lexer, node, o);
next = (Node)o.getObject();
if (b)
continue;
b = mergeDivs(lexer, node, o);
next = (Node)o.getObject();
if (b)
continue;
b = blockStyle(lexer, node, o);
next = (Node)o.getObject();
if (b)
continue;
b = inlineStyle(lexer, node, o);
next = (Node)o.getObject();
if (b)
continue;
b = font2Span(lexer, node, o);
next = (Node)o.getObject();
if (b)
continue;
break;
}
return next;
}
private static Node createStyleProperties(Lexer lexer, Node node)
{
Node child;
if (node.content != null)
{
for (child = node.content; child != null; child = child.next)
{
child = createStyleProperties(lexer, child);
}
}
return cleanNode(lexer, node);
}
private void defineStyleRules(Lexer lexer, Node node)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -