From: <ad...@us...> - 2010-11-15 14:17:12
|
Revision: 1196 http://jtidy.svn.sourceforge.net/jtidy/?rev=1196&view=rev Author: aditsu Date: 2010-11-15 14:17:00 +0000 (Mon, 15 Nov 2010) Log Message: ----------- fixed test 1027888 - replaced mergeDivs with mergeNestedElements (called for spans too), and added copyAttrs Modified Paths: -------------- branches/CodeUpdateAndJava5/src/main/java/org/w3c/tidy/AttVal.java branches/CodeUpdateAndJava5/src/main/java/org/w3c/tidy/Clean.java branches/CodeUpdateAndJava5/src/main/java/org/w3c/tidy/Configuration.java branches/CodeUpdateAndJava5/src/main/java/org/w3c/tidy/Node.java Modified: branches/CodeUpdateAndJava5/src/main/java/org/w3c/tidy/AttVal.java =================================================================== --- branches/CodeUpdateAndJava5/src/main/java/org/w3c/tidy/AttVal.java 2010-11-15 13:11:37 UTC (rev 1195) +++ branches/CodeUpdateAndJava5/src/main/java/org/w3c/tidy/AttVal.java 2010-11-15 14:17:00 UTC (rev 1196) @@ -458,6 +458,10 @@ return dict != null && dict.id == id; } + protected AttrId getId() { + return dict != null ? dict.id : AttrId.UNKNOWN; + } + protected static AttVal addAttrToList(final AttVal list, final AttVal av) { if (list == null) { return av; Modified: branches/CodeUpdateAndJava5/src/main/java/org/w3c/tidy/Clean.java =================================================================== --- branches/CodeUpdateAndJava5/src/main/java/org/w3c/tidy/Clean.java 2010-11-15 13:11:37 UTC (rev 1195) +++ branches/CodeUpdateAndJava5/src/main/java/org/w3c/tidy/Clean.java 2010-11-15 14:17:00 UTC (rev 1196) @@ -1166,24 +1166,69 @@ return false; } - /** - * Symptom: <code><div><div>...</div></div></code> Action: merge the two divs. This is useful after - * nested <dir>s used by Word for indenting have been converted to <div>s. - * @param lexer Lexer - * @param node first div - * @return true if the divs have been merged - */ - private boolean mergeDivs(final Lexer lexer, final Node node, final TriState state) { - if (state == TriState.No || !node.is(TagId.DIV)) { + /* Copy child attributes to node. Duplicate attributes are overwritten. + Unique attributes (such as ID) disable the action. + Attributes style and class are not dealt with. A call to MergeStyles + will do that. + */ + private boolean copyAttrs(final Node node, final Node child) { + /* Detect attributes that cannot be merged or overwritten. */ + if (child.getAttrById(AttrId.ID) != null && node.getAttrById(AttrId.ID) != null) { + return false; + } + + /* Move child attributes to node. Attributes in node + can be overwritten or merged. */ + for (AttVal av2 = child.attributes; av2 != null; ) { + /* Dealt by MergeStyles. */ + if (av2.is(AttrId.STYLE) || av2.is(AttrId.CLASS)) { + av2 = av2.next; + continue; + } + /* Avoid duplicates in node */ + final AttrId id = av2.getId(); + AttVal av1; + if (id != AttrId.UNKNOWN && (av1 = node.getAttrById(id)) != null) { + node.removeAttribute(av1); + } + + /* Move attribute from child to node */ + child.removeAttribute(av2); + av1 = av2; + av2 = av2.next; + av1.next = null; + node.insertAttributeAtEnd(av1); + } + return true; + } + + /* + Symptom <XX><XX>...</XX></XX> + Action: merge the two XXs + + For instance, this is useful after nested <dir>s used by Word + for indenting have been converted to <div>s + + If state is "no", no merging. + If state is "yes", inner element is discarded. Only Style and Class + attributes are merged using MergeStyles(). + If state is "auto", atttibutes are merged as described in CopyAttrs(). + Style and Class attributes are merged using MergeStyles(). + */ + private boolean mergeNestedElements(final TagId id, final TriState state, final Node node) { + if (state == TriState.No || !node.is(id)) { return false; } final Node child = node.content; - if (child == null || child.next != null || !child.is(TagId.DIV)) { + if (child == null || child.next != null || !child.is(id)) { return false; } + if (state == TriState.Auto && !copyAttrs(node, child)) { + return false; + } mergeStyles(node, child); stripOnlyChild(node); return true; @@ -1504,6 +1549,7 @@ Node[] o = new Node[1]; boolean b = false; final TriState mergeDivs = lexer.configuration.getMergeDivs(); + final TriState mergeSpans = lexer.configuration.getMergeSpans(); for (next = node; node != null && node.isElement(); node = next) { @@ -1532,13 +1578,14 @@ continue; } - b = mergeDivs(lexer, node, mergeDivs); - next = o[0]; - if (b) - { + if (mergeNestedElements(TagId.DIV, mergeDivs, node)) { continue; } + if (mergeNestedElements(TagId.SPAN, mergeSpans, node)) { + continue; + } + b = blockStyle(lexer, node); next = o[0]; if (b) Modified: branches/CodeUpdateAndJava5/src/main/java/org/w3c/tidy/Configuration.java =================================================================== --- branches/CodeUpdateAndJava5/src/main/java/org/w3c/tidy/Configuration.java 2010-11-15 13:11:37 UTC (rev 1195) +++ branches/CodeUpdateAndJava5/src/main/java/org/w3c/tidy/Configuration.java 2010-11-15 14:17:00 UTC (rev 1196) @@ -1253,6 +1253,14 @@ return (TriState) getOptionEnum(Option.MergeDivs); } + protected void setMergeSpans(final TriState mergeSpans) { + set(Option.MergeSpans, mergeSpans); + } + + protected TriState getMergeSpans() { + return (TriState) getOptionEnum(Option.MergeSpans); + } + protected void setTidyCompat(final boolean tidyCompat) { set(Option.TidyCompat, tidyCompat); } Modified: branches/CodeUpdateAndJava5/src/main/java/org/w3c/tidy/Node.java =================================================================== --- branches/CodeUpdateAndJava5/src/main/java/org/w3c/tidy/Node.java 2010-11-15 13:11:37 UTC (rev 1195) +++ branches/CodeUpdateAndJava5/src/main/java/org/w3c/tidy/Node.java 2010-11-15 14:17:00 UTC (rev 1196) @@ -386,33 +386,20 @@ } } + protected void insertAttributeAtEnd(final AttVal av) { + attributes = AttVal.addAttrToList(attributes, av); + } + /** * Adds an attribute to the node. * @param name attribute name * @param value attribute value * @return */ - public AttVal addAttribute(String name, String value) - { - AttVal av = new AttVal(null, null, null, null, '"', name, value); + public AttVal addAttribute(final String name, final String value) { + final AttVal av = new AttVal(null, null, null, null, '"', name, value); av.dict = AttributeTable.getDefaultAttributeTable().findAttribute(av); - - if (this.attributes == null) - { - this.attributes = av; - } - else - { - // append to end of attributes - AttVal here = this.attributes; - - while (here.next != null) - { - here = here.next; - } - - here.next = av; - } + insertAttributeAtEnd(av); return av; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |