Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen Cook committed Aug 8, 2018
1 parent 2c23616 commit c580a50
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 6 deletions.
20 changes: 20 additions & 0 deletions RNTester/js/TextExample.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,26 @@ class TextExample extends React.Component<{}> {
a<Text style={{textTransform: 'none'}}>b</Text>c
</Text>
</Text>
<Text style={{textTransform: 'none'}}>
{
'.aa\tbb\t\tcc dd EE \r\nZZ I like to eat apples. \n中文éé 我喜欢吃苹果。awdawd '
}
</Text>
<Text style={{textTransform: 'uppercase'}}>
{
'.aa\tbb\t\tcc dd EE \r\nZZ I like to eat apples. \n中文éé 我喜欢吃苹果。awdawd '
}
</Text>
<Text style={{textTransform: 'lowercase'}}>
{
'.aa\tbb\t\tcc dd EE \r\nZZ I like to eat apples. \n中文éé 我喜欢吃苹果。awdawd '
}
</Text>
<Text style={{textTransform: 'capitalize'}}>
{
'.aa\tbb\t\tcc dd EE \r\nZZ I like to eat apples. \n中文éé 我喜欢吃苹果。awdawd '
}
</Text>
</RNTesterBlock>
</RNTesterPage>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.style.ReplacementSpan;
import java.text.BreakIterator;

public class CustomTextTransformSpan extends ReplacementSpan {

Expand Down Expand Up @@ -59,14 +60,24 @@ private CharSequence transformText(CharSequence text) {
return transformed;
}

private String capitalize(String rawString) {
String[] stringParts = rawString.split(" ");
for(int i = 0; i < stringParts.length; i++) {
if(stringParts[i].length() == 0) continue;
stringParts[i] = stringParts[i].substring(0, 1).toUpperCase() + stringParts[i].substring(1).toLowerCase();
private String capitalize(String text) {
BreakIterator wordIterator = BreakIterator.getWordInstance();
wordIterator.setText(text);

StringBuilder res = new StringBuilder(text.length());
int start = wordIterator.first();
for (int end = wordIterator.next(); end != BreakIterator.DONE; end = wordIterator.next()) {
String word = text.substring(start, end);
if (Character.isLetterOrDigit(word.charAt(0))) {
res.append(Character.toUpperCase(word.charAt(0)));
res.append(word.substring(1).toLowerCase());
} else {
res.append(word);
}
start = end;
}

return String.join(" ", stringParts);
return res.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.facebook.react.uimanager.ViewProps;
import com.facebook.react.views.text.ReactRawTextShadowNode;
import com.facebook.react.views.view.ReactViewBackgroundDrawable;
import com.facebook.react.views.text.CustomTextTransformSpan;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -341,6 +342,70 @@ public void testBackgroundColorStyleApplied() {
assertThat(((ReactViewBackgroundDrawable) backgroundDrawable).getColor()).isEqualTo(Color.BLUE);
}

@Test
public void testTextTransformNoneApplied() {
UIManagerModule uiManager = getUIManagerModule();

String testTextEntered = ".aa\tbb\t\tcc dd EE \r\nZZ I like to eat apples. \n中文éé 我喜欢吃苹果。awdawd ";
String testTextTransformed = testTextEntered;

ReactRootView rootView = createText(
uiManager,
JavaOnlyMap.of("textTransform", "none"),
JavaOnlyMap.of(ReactRawTextShadowNode.PROP_TEXT, testTextEntered));

TextView textView = (TextView) rootView.getChildAt(0);
assertThat(textView.getText().toString()).isEqualTo(testTextTransformed);
}

@Test
public void testTextTransformUppercaseApplied() {
UIManagerModule uiManager = getUIManagerModule();

String testTextEntered = ".aa\tbb\t\tcc dd EE \r\nZZ I like to eat apples. \n中文éé 我喜欢吃苹果。awdawd ";
String testTextTransformed = ".AA\tBB\t\tCC DD EE \r\nZZ I LIKE TO EAT APPLES. \n中文ÉÉ 我喜欢吃苹果。AWDAWD ";

ReactRootView rootView = createText(
uiManager,
JavaOnlyMap.of("textTransform", "uppercase"),
JavaOnlyMap.of(ReactRawTextShadowNode.PROP_TEXT, testTextEntered));

TextView textView = (TextView) rootView.getChildAt(0);
assertThat(textView.getText().toString()).isEqualTo(testTextTransformed);
}

@Test
public void testTextTransformLowercaseApplied() {
UIManagerModule uiManager = getUIManagerModule();

String testTextEntered = ".aa\tbb\t\tcc dd EE \r\nZZ I like to eat apples. \n中文éé 我喜欢吃苹果。awdawd ";
String testTextTransformed = ".aa\tbb\t\tcc dd ee \r\nzz i like to eat apples. \n中文éé 我喜欢吃苹果。awdawd ";

ReactRootView rootView = createText(
uiManager,
JavaOnlyMap.of("textTransform", "lowercase"),
JavaOnlyMap.of(ReactRawTextShadowNode.PROP_TEXT, testTextEntered));

TextView textView = (TextView) rootView.getChildAt(0);
assertThat(textView.getText().toString()).isEqualTo(testTextTransformed);
}

@Test
public void testTextTransformCapitalizeApplied() {
UIManagerModule uiManager = getUIManagerModule();

String testTextEntered = ".aa\tbb\t\tcc dd EE \r\nZZ I like to eat apples. \n中文éé 我喜欢吃苹果。awdawd ";
String testTextTransformed = ".Aa\tBb\t\tCc Dd Ee \r\nZz I Like To Eat Apples. \n中文Éé 我喜欢吃苹果。Awdawd ";

ReactRootView rootView = createText(
uiManager,
JavaOnlyMap.of("textTransform", "capitalize"),
JavaOnlyMap.of(ReactRawTextShadowNode.PROP_TEXT, testTextEntered));

TextView textView = (TextView) rootView.getChildAt(0);
assertThat(textView.getText().toString()).isEqualTo(testTextTransformed);
}

// JELLY_BEAN is needed for TextView#getMaxLines(), which is OK, because in the actual code we
// only use TextView#setMaxLines() which exists since API Level 1.
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
Expand Down

0 comments on commit c580a50

Please sign in to comment.