-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds emulation for `java.text.Normalizer` using the native `normalize` method on JavaScript's String.
- Loading branch information
Showing
3 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2024 GWT Project Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package java.text; | ||
|
||
import javaemul.internal.JsUtils; | ||
import jsinterop.annotations.JsType; | ||
|
||
/** | ||
* Emulation of <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/text/Normalizer.html">java.text.Normalizer</a>. | ||
*/ | ||
public final class Normalizer { | ||
|
||
public enum Form { | ||
/** Canonical decomposition. */ | ||
NFD, | ||
/** Canonical decomposition followed by composition. */ | ||
NFC, | ||
/** Compatibility decomposition. */ | ||
NFKD, | ||
/** Compatibility decomposition followed by composition. */ | ||
NFKC | ||
} | ||
|
||
public static String normalize(CharSequence input, Form form) { | ||
return JsUtils.<NativeString>uncheckedCast(input.toString()).normalize(form.name()); | ||
} | ||
|
||
public static boolean isNormalized(CharSequence input, Form form) { | ||
String str = input.toString(); | ||
return str.equals(normalize(str, form)); | ||
} | ||
|
||
@JsType(isNative = true, name = "String", namespace = "<window>") | ||
private static class NativeString { | ||
public native String normalize(String form); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
user/test/com/google/gwt/emultest/java/text/NormalizerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2024 GWT Project Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package com.google.gwt.emultest.java.text; | ||
|
||
import com.google.gwt.junit.client.GWTTestCase; | ||
|
||
import java.text.Normalizer; | ||
|
||
public class NormalizerTest extends GWTTestCase { | ||
|
||
private String hangul = "\uD55C\uAE00"; | ||
private String hangulDecomposed = "\u1112\u1161\u11AB\u1100\u1173\u11AF"; | ||
private String ligatureFF = "\uFB00"; | ||
|
||
public void testNormalizeHangul() { | ||
assertEquals(hangul, Normalizer.normalize(hangul, Normalizer.Form.NFC)); | ||
assertEquals(hangulDecomposed, Normalizer.normalize(hangul, Normalizer.Form.NFD)); | ||
assertEquals(hangul, Normalizer.normalize(hangul, Normalizer.Form.NFKC)); | ||
assertEquals(hangulDecomposed, Normalizer.normalize(hangul, Normalizer.Form.NFKD)); | ||
assertEquals(hangul, Normalizer.normalize(hangulDecomposed, Normalizer.Form.NFC)); | ||
} | ||
|
||
public void testNormalizeLigature() { | ||
assertEquals(ligatureFF, Normalizer.normalize(ligatureFF, Normalizer.Form.NFC)); | ||
assertEquals(ligatureFF, Normalizer.normalize(ligatureFF, Normalizer.Form.NFD)); | ||
assertEquals("ff", Normalizer.normalize(ligatureFF, Normalizer.Form.NFKC)); | ||
assertEquals("ff", Normalizer.normalize(ligatureFF, Normalizer.Form.NFKD)); | ||
} | ||
|
||
public void testIsNormalizedHangul() { | ||
assertTrue(Normalizer.isNormalized(hangul, Normalizer.Form.NFC)); | ||
assertFalse(Normalizer.isNormalized(hangul, Normalizer.Form.NFD)); | ||
assertTrue(Normalizer.isNormalized(hangul, Normalizer.Form.NFKC)); | ||
assertFalse(Normalizer.isNormalized(hangul, Normalizer.Form.NFKD)); | ||
assertFalse(Normalizer.isNormalized(hangulDecomposed, Normalizer.Form.NFC)); | ||
} | ||
|
||
public void testIsNormalizedLigature() { | ||
assertTrue(Normalizer.isNormalized(ligatureFF, Normalizer.Form.NFC)); | ||
assertTrue(Normalizer.isNormalized(ligatureFF, Normalizer.Form.NFD)); | ||
assertFalse(Normalizer.isNormalized(ligatureFF, Normalizer.Form.NFKC)); | ||
assertFalse(Normalizer.isNormalized(ligatureFF, Normalizer.Form.NFKD)); | ||
} | ||
|
||
@Override | ||
public String getModuleName() { | ||
return "com.google.gwt.emultest.EmulSuite"; | ||
} | ||
} |