You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@royale.apache.org by jo...@apache.org on 2021/10/21 21:33:22 UTC
[royale-compiler] 03/04: formatter: MXML formatting tweeaks and
tests
This is an automated email from the ASF dual-hosted git repository.
joshtynjala pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/royale-compiler.git
commit 0755d8fa485b810f05fbf4a9d66be567e9f70137
Author: Josh Tynjala <jo...@apache.org>
AuthorDate: Thu Oct 21 13:39:05 2021 -0700
formatter: MXML formatting tweeaks and tests
---
.../org/apache/royale/formatter/FORMATTER.java | 18 +++--
.../apache/royale/formatter/TestMXMLScript.java | 89 ++++++++++++++++++++++
2 files changed, 100 insertions(+), 7 deletions(-)
diff --git a/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java b/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java
index 2c33869..c4506cf 100644
--- a/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java
+++ b/formatter/src/main/java/org/apache/royale/formatter/FORMATTER.java
@@ -401,12 +401,14 @@ public class FORMATTER {
if (!ignoreProblems && hasErrors(problems)) {
return text;
}
- String[] formattedLines = formattedScriptText.split("\n");
- String lineIndent = indent + indent + indent;
- for (int i = 0; i < formattedLines.length; i++) {
- formattedLines[i] = lineIndent + formattedLines[i];
+ if (formattedScriptText.length() > 0) {
+ String[] formattedLines = formattedScriptText.split("\n");
+ String lineIndent = indent + indent + indent;
+ for (int i = 0; i < formattedLines.length; i++) {
+ formattedLines[i] = lineIndent + formattedLines[i];
+ }
+ formattedScriptText = String.join("\n", formattedLines);
}
- formattedScriptText = String.join("\n", formattedLines);
builder.append(text.substring(lastIndex, start));
builder.append(indent);
builder.append("<");
@@ -415,8 +417,10 @@ public class FORMATTER {
builder.append(indent);
builder.append(indent);
builder.append("<![CDATA[\n");
- builder.append(formattedScriptText);
- builder.append("\n");
+ if (formattedScriptText.length() > 0) {
+ builder.append(formattedScriptText);
+ builder.append("\n");
+ }
builder.append(indent);
builder.append(indent);
builder.append("]]>\n");
diff --git a/formatter/src/test/java/org/apache/royale/formatter/TestMXMLScript.java b/formatter/src/test/java/org/apache/royale/formatter/TestMXMLScript.java
new file mode 100644
index 0000000..c485e65
--- /dev/null
+++ b/formatter/src/test/java/org/apache/royale/formatter/TestMXMLScript.java
@@ -0,0 +1,89 @@
+package org.apache.royale.formatter;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+public class TestMXMLScript extends BaseFormatterTests {
+ @Test
+ public void testEmptyScriptNoCdata() {
+ FORMATTER formatter = new FORMATTER();
+ formatter.insertSpaces = false;
+ String result = formatter.formatMXMLText(
+ // @formatter:off
+ "<s:Application>\n" +
+ "<fx:Script>\n" +
+ "</fx:Script>\n" +
+ "</s:Application>",
+ // @formatter:on
+ problems
+ );
+ assertEquals(
+ // @formatter:off
+ "<s:Application>\n" +
+ "\t<fx:Script>\n" +
+ "\t\t<![CDATA[\n" +
+ "\t\t]]>\n" +
+ "\t</fx:Script>\n" +
+ "</s:Application>",
+ // @formatter:on
+ result);
+ }
+
+ @Test
+ public void testEmptyScriptWithCdata() {
+ FORMATTER formatter = new FORMATTER();
+ formatter.insertSpaces = false;
+ String result = formatter.formatMXMLText(
+ // @formatter:off
+ "<s:Application>\n" +
+ "<fx:Script>\n" +
+ "<![CDATA[\n" +
+ "]]>\n" +
+ "</fx:Script>\n" +
+ "</s:Application>",
+ // @formatter:on
+ problems
+ );
+ assertEquals(
+ // @formatter:off
+ "<s:Application>\n" +
+ "\t<fx:Script>\n" +
+ "\t\t<![CDATA[\n" +
+ "\t\t]]>\n" +
+ "\t</fx:Script>\n" +
+ "</s:Application>",
+ // @formatter:on
+ result);
+ }
+
+ @Test
+ public void testScriptWithActionScript() {
+ FORMATTER formatter = new FORMATTER();
+ formatter.insertSpaces = false;
+ String result = formatter.formatMXMLText(
+ // @formatter:off
+ "<s:Application>\n" +
+ "<fx:Script>\n" +
+ "<![CDATA[\n" +
+ "public var a: Number=123.4;\n" +
+ "]]>\n" +
+ "</fx:Script>\n" +
+ "</s:Application>",
+ // @formatter:on
+ problems
+ );
+ assertEquals(
+ // @formatter:off
+ "<s:Application>\n" +
+ "\t<fx:Script>\n" +
+ "\t\t<![CDATA[\n" +
+ "\t\t\tpublic var a:Number = 123.4;\n" +
+ "\t\t]]>\n" +
+ "\t</fx:Script>\n" +
+ "</s:Application>",
+ // @formatter:on
+ result);
+ }
+
+}