aboutsummaryrefslogtreecommitdiffstats
path: root/eclipse
diff options
context:
space:
mode:
authorTor Norbye <tnorbye@google.com>2012-06-05 07:47:56 -0700
committerandroid code review <noreply-gerritcodereview@google.com>2012-06-05 07:47:57 -0700
commit10b2b237290594bc389a4c952866087b77f0f6ef (patch)
treec514ea2eec41bf595d5cd5fe0b73381378cbcc44 /eclipse
parent61304c090d68a134d0d8794e641212f6758a97d3 (diff)
parent0153e5ee62afd5c0c63cc3811b1f2e36fdd26f2b (diff)
downloadsdk-10b2b237290594bc389a4c952866087b77f0f6ef.zip
sdk-10b2b237290594bc389a4c952866087b77f0f6ef.tar.gz
sdk-10b2b237290594bc389a4c952866087b77f0f6ef.tar.bz2
Merge "Fix XML formatter to handle complex HTML strings better"
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>");
+ }
+
}