aboutsummaryrefslogtreecommitdiffstats
path: root/eclipse
diff options
context:
space:
mode:
authorTor Norbye <tnorbye@google.com>2012-06-04 17:51:26 -0700
committerTor Norbye <tnorbye@google.com>2012-06-05 07:46:28 -0700
commit0153e5ee62afd5c0c63cc3811b1f2e36fdd26f2b (patch)
tree3d9b557dc757391fb312a3f4f92d87089922660a /eclipse
parentd1878a571fb5c452250a4408cd3c221e64e7f662 (diff)
downloadsdk-0153e5ee62afd5c0c63cc3811b1f2e36fdd26f2b.zip
sdk-0153e5ee62afd5c0c63cc3811b1f2e36fdd26f2b.tar.gz
sdk-0153e5ee62afd5c0c63cc3811b1f2e36fdd26f2b.tar.bz2
Fix XML formatter to handle complex HTML strings better
The XML formatter did not handle <string> definitions where the content contained nested HTML (where the tags included other tags than <u>, <i> and <s>). An exmaple of a string which formats poorly is in the testcase for issue 32619 (which is an issue unrelated to formatting). Change-Id: Ie70a0f0097aca99c40953a6e5dd4408f903d19e2
Diffstat (limited to 'eclipse')
-rw-r--r--eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinter.java32
-rw-r--r--eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinterTest.java28
2 files changed, 54 insertions, 6 deletions
diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinter.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinter.java
index 22a86af..a5e26bf 100644
--- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinter.java
+++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinter.java
@@ -752,6 +752,10 @@ public class XmlPrettyPrinter {
return false;
}
+ if (isMarkupElement(element)) {
+ return false;
+ }
+
// See if this element should be separated from the previous element.
// This is the case if we are not compressing whitespace (checked above),
// or if we are not immediately following a comment (in which case the
@@ -876,14 +880,36 @@ public class XmlPrettyPrinter {
return false;
}
+ if (isMarkupElement(element)) {
+ return false;
+ }
+
return element.getParentNode().getNodeType() == Node.ELEMENT_NODE
&& !keepElementAsSingleLine(depth - 1, (Element) element.getParentNode());
}
private boolean isMarkupElement(Element element) {
- // <u>, <b>, <i>, ...
- // http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
- return mStyle == XmlFormatStyle.RESOURCE && element.getTagName().length() == 1;
+ // The documentation suggests that the allowed tags are <u>, <b> and <i>:
+ // developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
+ // However, the full set of tags accepted by Html.fromHtml is much larger. Therefore,
+ // instead consider *any* element nested inside a <string> definition to be a markup
+ // element. See frameworks/base/core/java/android/text/Html.java and look for
+ // HtmlToSpannedConverter#handleStartTag.
+
+ if (mStyle != XmlFormatStyle.RESOURCE) {
+ return false;
+ }
+
+ Node curr = element.getParentNode();
+ while (curr != null) {
+ if (STRING_ELEMENT.equals(curr.getNodeName())) {
+ return true;
+ }
+
+ curr = curr.getParentNode();
+ }
+
+ return false;
}
/**
diff --git a/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinterTest.java b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinterTest.java
index 736931b..731621c 100644
--- a/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinterTest.java
+++ b/eclipse/plugins/com.android.ide.eclipse.tests/src/com/android/ide/eclipse/adt/internal/editors/formatting/XmlPrettyPrinterTest.java
@@ -236,7 +236,7 @@ public class XmlPrettyPrinterTest extends TestCase {
"]>\n" +
"<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" +
" android:orientation=\"vertical\" >\n" +
- " <![CDATA[\n" +
+ "<![CDATA[\n" +
"This is character data!\n" +
"<!-- This is not a comment! -->\n" +
"and <this is not an element>\n" +
@@ -850,8 +850,9 @@ public class XmlPrettyPrinterTest extends TestCase {
"<resources>\n" +
"\n" +
" <string name=\"welcome\">Welcome to <b>Android</b>!</string>\n" +
- " <string name=\"glob_settings_top_text\"><b>To install a 24 Clock Widget, please <i>long press</i>\n" +
- " in Home Screen.</b> Configure the Global Settings here.</string>\n" +
+ " <string name=\"glob_settings_top_text\"><b>To install a 24 Clock Widget, " +
+ "please <i>long press</i> in Home Screen.</b> Configure the Global Settings " +
+ "here.</string>\n" +
"\n" +
"</resources>");
}
@@ -915,4 +916,25 @@ public class XmlPrettyPrinterTest extends TestCase {
"\n" +
"</resources>");
}
+
+ public void testComplexString() throws Exception {
+ checkFormat(
+ "res/values/strings.xml",
+ "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
+ "<resources>\n" +
+ "<string name=\"progress_completed_export_all\">The database has " +
+ "<b>successfully</b> been exported into: <br /><br /><font size=\"14\">" +
+ "\\\"<i>%s</i>\\\"</font></string>" +
+ "</resources>",
+
+ "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
+ "<resources>\n" +
+ "\n" +
+ " <string name=\"progress_completed_export_all\">The database has " +
+ "<b>successfully</b> been exported into: <br /><br /><font size=\"14\">" +
+ "\\\"<i>%s</i>\\\"</font></string>\n" +
+ "\n" +
+ "</resources>");
+ }
+
}