📄 clean.java
字号:
new String("text/css"));
av.dict = AttributeTable.getDefaultAttributeTable().findAttribute(av);
node.attributes = av;
body = Node.findBody(doc);
lexer.txtstart = lexer.lexsize;
if (body != null)
cleanBodyAttrs(lexer, body);
for (style = lexer.styles; style != null; style = style.next)
{
lexer.addCharToLexer(' ');
lexer.addStringLiteral(style.tag);
lexer.addCharToLexer('.');
lexer.addStringLiteral(style.tagClass);
lexer.addCharToLexer(' ');
lexer.addCharToLexer('{');
lexer.addStringLiteral(style.properties);
lexer.addCharToLexer('}');
lexer.addCharToLexer('\n');
}
lexer.txtend = lexer.lexsize;
Node.insertNodeAtEnd(node,
lexer.newNode(Node.TextNode,
lexer.lexbuf,
lexer.txtstart,
lexer.txtend));
/*
now insert style element into document head
doc is root node. search its children for html node
the head node should be first child of html node
*/
head = Node.findHead(doc);
if (head != null)
Node.insertNodeAtEnd(head, node);
}
/* ensure bidirectional links are consistent */
private static void fixNodeLinks(Node node)
{
Node child;
if (node.prev != null)
node.prev.next = node;
else
node.parent.content = node;
if (node.next != null)
node.next.prev = node;
else
node.parent.last = node;
for (child = node.content; child != null; child = child.next)
child.parent = node;
}
/*
used to strip child of node when
the node has one and only one child
*/
private static void stripOnlyChild(Node node)
{
Node child;
child = node.content;
node.content = child.content;
node.last = child.last;
child.content = null;
for (child = node.content; child != null; child = child.next)
child.parent = node;
}
/* used to strip font start and end tags */
private static void discardContainer(Node element, MutableObject pnode)
{
Node node;
Node parent = element.parent;
if (element.content != null)
{
element.last.next = element.next;
if (element.next != null)
{
element.next.prev = element.last;
element.last.next = element.next;
}
else
parent.last = element.last;
if (element.prev != null)
{
element.content.prev = element.prev;
element.prev.next = element.content;
}
else
parent.content = element.content;
for (node = element.content; node != null; node = node.next)
node.parent = parent;
pnode.setObject(element.content);
}
else
{
if (element.next != null)
element.next.prev = element.prev;
else
parent.last = element.prev;
if (element.prev != null)
element.prev.next = element.next;
else
parent.content = element.next;
pnode.setObject(element.next);
}
element.next = null;
element.content = null;
}
/*
Add style property to element, creating style
attribute as needed and adding ; delimiter
*/
private static void addStyleProperty(Node node, String property)
{
AttVal av;
for (av = node.attributes; av != null; av = av.next)
{
if (av.attribute.equals("style"))
break;
}
/* if style attribute already exists then insert property */
if (av != null)
{
String s;
s = addProperty(av.value, property);
av.value = s;
}
else /* else create new style attribute */
{
av = new AttVal(node.attributes, null, '"',
new String("style"),
new String(property));
av.dict = AttributeTable.getDefaultAttributeTable().findAttribute(av);
node.attributes = av;
}
}
/*
Create new string that consists of the
combined style properties in s1 and s2
To merge property lists, we build a linked
list of property/values and insert properties
into the list in order, merging values for
the same property name.
*/
private static String mergeProperties(String s1, String s2)
{
String s;
StyleProp prop;
prop = createProps(null, s1);
prop = createProps(prop, s2);
s = createPropString(prop);
return s;
}
private static void mergeStyles(Node node, Node child)
{
AttVal av;
String s1, s2, style;
for (s2 = null, av = child.attributes; av != null; av = av.next)
{
if (av.attribute.equals("style"))
{
s2 = av.value;
break;
}
}
for (s1 = null, av = node.attributes; av != null; av = av.next)
{
if (av.attribute.equals("style"))
{
s1 = av.value;
break;
}
}
if (s1 != null)
{
if (s2 != null) /* merge styles from both */
{
style = mergeProperties(s1, s2);
av.value = style;
}
}
else if (s2 != null) /* copy style of child */
{
av = new AttVal(node.attributes, null, '"',
new String("style"),
new String(s2));
av.dict = AttributeTable.getDefaultAttributeTable().findAttribute(av);
node.attributes = av;
}
}
private static String fontSize2Name(String size)
{
/*
String[] sizes =
{
new String("50%"),
new String("60%"),
new String("80%"),
null,
new String("120%"),
new String("150%"),
new String("200%")
};
*/
String[] sizes =
{
"60%",
"70%",
"80%",
null,
"120%",
"150%",
"200%"
};
String buf;
if (size.length() > 0 &&
'0' <= size.charAt(0) && size.charAt(0) <= '6')
{
int n = size.charAt(0) - '0';
return sizes[n];
}
if (size.length() > 0 && size.charAt(0) == '-')
{
if (size.length() > 1 &&
'0' <= size.charAt(1) && size.charAt(1) <= '6')
{
int n = size.charAt(1) - '0';
double x;
for (x = 1.0; n > 0; --n)
x *= 0.8;
x *= 100.0;
buf = "" + (int)x + "%";
return buf;
}
return "smaller"; /*"70%"; */
}
if (size.length() > 1 &&
'0' <= size.charAt(1) && size.charAt(1) <= '6')
{
int n = size.charAt(1) - '0';
double x;
for (x = 1.0; n > 0; --n)
x *= 1.2;
x *= 100.0;
buf = "" + (int)x + "%";
return buf;
}
return "larger"; /* "140%" */
}
private static void addFontFace(Node node, String face)
{
addStyleProperty(node, "font-family: " + face);
}
private static void addFontSize(Node node, String size)
{
String value;
if (size.equals("6") && node.tag == TagTable.tagP)
{
node.element = new String("h1");
TagTable.getDefaultTagTable().findTag(node);
return;
}
if (size.equals("5") && node.tag == TagTable.tagP)
{
node.element = new String("h2");
TagTable.getDefaultTagTable().findTag(node);
return;
}
if (size.equals("4") && node.tag == TagTable.tagP)
{
node.element = new String("h3");
TagTable.getDefaultTagTable().findTag(node);
return;
}
value = fontSize2Name(size);
if (value != null)
{
addStyleProperty(node, "font-size: " + value);
}
}
private static void addFontColor(Node node, String color)
{
addStyleProperty(node, "color: " + color);
}
private static void addAlign(Node node, String align)
{
/* force alignment value to lower case */
addStyleProperty(node, "text-align: " + align.toLowerCase());
}
/*
add style properties to node corresponding to
the font face, size and color attributes
*/
private static void addFontStyles(Node node, AttVal av)
{
while (av != null)
{
if (av.attribute.equals("face"))
addFontFace(node, av.value);
else if (av.attribute.equals("size"))
addFontSize(node, av.value);
else if (av.attribute.equals("color"))
addFontColor(node, av.value);
av = av.next;
}
}
/*
Symptom: <p align=center>
Action: <p style="text-align: center">
*/
private static void textAlign(Lexer lexer, Node node)
{
AttVal av, prev;
prev = null;
for (av = node.attributes; av != null; av = av.next)
{
if (av.attribute.equals("align"))
{
if (prev != null)
prev.next = av.next;
else
node.attributes = av.next;
if (av.value != null)
{
addAlign(node, av.value);
}
break;
}
prev = av;
}
}
/*
The clean up rules use the pnode argument to return the
next node when the orignal node has been deleted
*/
/*
Symptom: <dir> <li> where <li> is only child
Action: coerce <dir> <li> to <div> with indent.
*/
private static boolean dir2Div(Lexer lexer, Node node, MutableObject pnode)
{
Node child;
if (node.tag == TagTable.tagDir ||
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;
if (child.tag != TagTable.tagLi)
return false;
if (!child.implicit)
return false;
/* coerce dir to div */
node.tag = TagTable.tagDiv;
node.element = new String("div");
addStyleProperty(node, "margin-left: 2em");
stripOnlyChild(node);
return true;
//#if 0
//Node content;
//Node last;
//content = child.content;
//last = child.last;
//child.content = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -