Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNitin Dahyabhai2020-05-18 00:44:34 +0000
committerNitin Dahyabhai2020-05-18 01:22:03 +0000
commit410ea7dbca0c40ce6fd8f2727cfc7042ffe80aa1 (patch)
tree889a0ebda71223140aff94ca470a4398e6711e0c
parent4aee0c5654c3e76e89b5ba1f4383445f97787aaa (diff)
downloadwebtools.sourceediting-410ea7dbca0c40ce6fd8f2727cfc7042ffe80aa1.tar.gz
webtools.sourceediting-410ea7dbca0c40ce6fd8f2727cfc7042ffe80aa1.tar.xz
webtools.sourceediting-410ea7dbca0c40ce6fd8f2727cfc7042ffe80aa1.zip
[527258] Preserve 'en space' and 'em space' in XML formatter
-rw-r--r--xml/bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/internal/formatter/DefaultXMLPartitionFormatter.java24
-rw-r--r--xml/tests/org.eclipse.wst.xml.core.tests/src/org/eclipse/wst/xml/core/tests/format/TestPartitionFormatterXML.java9
-rw-r--r--xml/tests/org.eclipse.wst.xml.core.tests/src/org/eclipse/wst/xml/core/tests/format/testfiles/xml/nbsp-after.xml2
-rw-r--r--xml/tests/org.eclipse.wst.xml.core.tests/src/org/eclipse/wst/xml/core/tests/format/testfiles/xml/nbsp-before.xml2
4 files changed, 30 insertions, 7 deletions
diff --git a/xml/bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/internal/formatter/DefaultXMLPartitionFormatter.java b/xml/bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/internal/formatter/DefaultXMLPartitionFormatter.java
index 61cbf0e652..d42b011198 100644
--- a/xml/bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/internal/formatter/DefaultXMLPartitionFormatter.java
+++ b/xml/bundles/org.eclipse.wst.xml.core/src/org/eclipse/wst/xml/core/internal/formatter/DefaultXMLPartitionFormatter.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2007, 2017 IBM Corporation and others.
+ * Copyright (c) 2007, 2020 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
@@ -68,6 +68,18 @@ public class DefaultXMLPartitionFormatter {
static private final String EMPTY = ""; //$NON-NLS-1$
static private final String PROPERTY_WHITESPACE_FACET = "org.eclipse.wst.xsd.cm.properties/whitespace"; //$NON-NLS-1$
+ /**
+ * @param c
+ * @return whether the character falls within the XML Recommendation
+ * syntactic construct for whitespace. The standard Java Character
+ * class considers "em space" and "en space" to be whitespace,
+ * even though ICU4J does not.
+ */
+ static boolean isWhitespace(char c) {
+ // https://bugs.eclipse.org/527258 : to preserve en space and em space
+ return c == 0x20 || c == '\t' || c == '\n' || c == '\r';
+ }
+
private XMLFormattingPreferences fPreferences = null;
private IProgressMonitor fProgressMonitor;
@@ -1061,7 +1073,7 @@ public class DefaultXMLPartitionFormatter {
for (int i = 0; i < length; i++) {
c = text.charAt(i);
// Compress whitespace unless its a line delimiter and formatting does not permit joining lines
- if (Character.isWhitespace(c)) {
+ if (isWhitespace(c)) {
if ((c != '\r' && c!= '\n') || joinLines) {
// Just came off of a word
if (start == end) {
@@ -1163,7 +1175,7 @@ public class DefaultXMLPartitionFormatter {
boolean nonCharacterFound = false;
while (textOffset < fullTextArray.length && !nonCharacterFound) {
char c = fullTextArray[textOffset];
- boolean isWhitespace = Character.isWhitespace(c);
+ boolean isWhitespace = isWhitespace(c);
if ((forWhitespace && isWhitespace) || (!forWhitespace && !isWhitespace))
characterRun.append(c);
else
@@ -1178,7 +1190,7 @@ public class DefaultXMLPartitionFormatter {
int index = text.length() - 1;
while(index >= 0) {
char c = text.charAt(index--);
- if (Character.isWhitespace(c))
+ if (isWhitespace(c))
whitespaceRun.insert(0, c);
else
break;
@@ -1311,7 +1323,7 @@ public class DefaultXMLPartitionFormatter {
previousRegionFullText = previousDocumentRegion.getFullText();
int length = previousRegionFullText.length();
if (length > 1)
- canIndent = Character.isWhitespace(previousRegionFullText.charAt(length - 1));
+ canIndent = isWhitespace(previousRegionFullText.charAt(length - 1));
}
}
if (canIndent) {
@@ -1712,4 +1724,4 @@ public class DefaultXMLPartitionFormatter {
void setProgressMonitor(IProgressMonitor monitor) {
fProgressMonitor = monitor;
}
-} \ No newline at end of file
+}
diff --git a/xml/tests/org.eclipse.wst.xml.core.tests/src/org/eclipse/wst/xml/core/tests/format/TestPartitionFormatterXML.java b/xml/tests/org.eclipse.wst.xml.core.tests/src/org/eclipse/wst/xml/core/tests/format/TestPartitionFormatterXML.java
index 2295e9ff9b..0b55bc0c59 100644
--- a/xml/tests/org.eclipse.wst.xml.core.tests/src/org/eclipse/wst/xml/core/tests/format/TestPartitionFormatterXML.java
+++ b/xml/tests/org.eclipse.wst.xml.core.tests/src/org/eclipse/wst/xml/core/tests/format/TestPartitionFormatterXML.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2007, 2017 IBM Corporation and others.
+ * Copyright (c) 2007, 2020 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
@@ -199,6 +199,13 @@ public class TestPartitionFormatterXML extends TestCase {
formatAndAssertEquals("testfiles/xml/xml-space-preserve-standalone.xml", "testfiles/xml/xml-space-preserve-standalone-newfmt.xml", prefs);
}
+ public void testPreserveESpace() throws UnsupportedEncodingException, IOException, CoreException {
+ // [527258] - XML formatter replaces 'en space' with space
+ XMLFormattingPreferences prefs = new XMLFormattingPreferences();
+ prefs.setClearAllBlankLines(true);
+ formatAndAssertEquals("testfiles/xml/nbsp-before.xml", "testfiles/xml/nbsp-after.xml", prefs);
+ }
+
public void testPreserveFormatDTD() throws UnsupportedEncodingException, IOException, CoreException {
// results are different than old formatter
// Bug [228495] - Result should have blank lines cleared
diff --git a/xml/tests/org.eclipse.wst.xml.core.tests/src/org/eclipse/wst/xml/core/tests/format/testfiles/xml/nbsp-after.xml b/xml/tests/org.eclipse.wst.xml.core.tests/src/org/eclipse/wst/xml/core/tests/format/testfiles/xml/nbsp-after.xml
new file mode 100644
index 0000000000..099fc2cab7
--- /dev/null
+++ b/xml/tests/org.eclipse.wst.xml.core.tests/src/org/eclipse/wst/xml/core/tests/format/testfiles/xml/nbsp-after.xml
@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<cell> 40° C.</cell> \ No newline at end of file
diff --git a/xml/tests/org.eclipse.wst.xml.core.tests/src/org/eclipse/wst/xml/core/tests/format/testfiles/xml/nbsp-before.xml b/xml/tests/org.eclipse.wst.xml.core.tests/src/org/eclipse/wst/xml/core/tests/format/testfiles/xml/nbsp-before.xml
new file mode 100644
index 0000000000..099fc2cab7
--- /dev/null
+++ b/xml/tests/org.eclipse.wst.xml.core.tests/src/org/eclipse/wst/xml/core/tests/format/testfiles/xml/nbsp-before.xml
@@ -0,0 +1,2 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<cell> 40° C.</cell> \ No newline at end of file

Back to the top