Add helper functions to DirectionalTextHelper.
Boolean parameters decrease readability of the code. In
setTextOrHtml(..., false), does the boolean parameter mean isText or
isHtml? You need to check or add a comment. It's also more cumbersome
than simple setText(...) or setHtml(). Another reason to introduce these
methods is that simple error checkers warn about setTextOrHtml() calls
and don't recognize the boolean parameter.
Change-Id: Iabdef1e0f7ed46a96558ad3a47eb84e314da9507
Review-Link: https://gwt-review.googlesource.com/#/c/13885/
diff --git a/user/src/com/google/gwt/user/client/ui/Anchor.java b/user/src/com/google/gwt/user/client/ui/Anchor.java
index 95c91fa..1d39293 100644
--- a/user/src/com/google/gwt/user/client/ui/Anchor.java
+++ b/user/src/com/google/gwt/user/client/ui/Anchor.java
@@ -484,7 +484,7 @@
@Override
public String getText() {
- return directionalTextHelper.getTextOrHtml(false);
+ return directionalTextHelper.getText();
}
@Override
@@ -571,17 +571,17 @@
@Override
public void setHTML(SafeHtml html) {
- setHTML(html.asString());
+ directionalTextHelper.setHtml(html);
}
@Override
public void setHTML(String html) {
- directionalTextHelper.setTextOrHtml(html, true);
+ directionalTextHelper.setHtml(html);
}
@Override
public void setHTML(SafeHtml html, Direction dir) {
- directionalTextHelper.setTextOrHtml(html.asString(), dir, true);
+ directionalTextHelper.setHtml(html, dir);
}
@Override
@@ -606,12 +606,12 @@
@Override
public void setText(String text) {
- directionalTextHelper.setTextOrHtml(text, false);
+ directionalTextHelper.setText(text);
}
@Override
public void setText(String text, Direction dir) {
- directionalTextHelper.setTextOrHtml(text, dir, false);
+ directionalTextHelper.setText(text, dir);
}
@Override
diff --git a/user/src/com/google/gwt/user/client/ui/CheckBox.java b/user/src/com/google/gwt/user/client/ui/CheckBox.java
index 2feafe1..fb6b4bd 100644
--- a/user/src/com/google/gwt/user/client/ui/CheckBox.java
+++ b/user/src/com/google/gwt/user/client/ui/CheckBox.java
@@ -235,7 +235,7 @@
@Override
public String getHTML() {
- return directionalTextHelper.getTextOrHtml(true);
+ return directionalTextHelper.getHtml();
}
@Override
@@ -250,7 +250,7 @@
@Override
public String getText() {
- return directionalTextHelper.getTextOrHtml(false);
+ return directionalTextHelper.getText();
}
@Override
@@ -376,12 +376,12 @@
@Override
public void setHTML(SafeHtml html, Direction dir) {
- directionalTextHelper.setTextOrHtml(html.asString(), dir, true);
+ directionalTextHelper.setHtml(html, dir);
}
@Override
public void setHTML(String html) {
- directionalTextHelper.setTextOrHtml(html, true);
+ directionalTextHelper.setHtml(html);
}
@Override
@@ -402,12 +402,12 @@
@Override
public void setText(String text) {
- directionalTextHelper.setTextOrHtml(text, false);
+ directionalTextHelper.setText(text);
}
@Override
public void setText(String text, Direction dir) {
- directionalTextHelper.setTextOrHtml(text, dir, false);
+ directionalTextHelper.setText(text, dir);
}
/**
diff --git a/user/src/com/google/gwt/user/client/ui/DirectionalTextHelper.java b/user/src/com/google/gwt/user/client/ui/DirectionalTextHelper.java
index 68d3fbd..519d37d 100644
--- a/user/src/com/google/gwt/user/client/ui/DirectionalTextHelper.java
+++ b/user/src/com/google/gwt/user/client/ui/DirectionalTextHelper.java
@@ -22,6 +22,7 @@
import com.google.gwt.i18n.shared.DirectionEstimator;
import com.google.gwt.i18n.shared.HasDirectionEstimator;
import com.google.gwt.i18n.shared.WordCountDirectionEstimator;
+import com.google.gwt.safehtml.shared.SafeHtml;
/**
* A helper class for displaying bidi (i.e. potentially opposite-direction) text
@@ -84,7 +85,8 @@
* The direction of the element's content.
* Note: this may not match the direction attribute of the element itself.
* See
- * {@link #setTextOrHtml(String, com.google.gwt.i18n.client.HasDirection.Direction, boolean) setTextOrHtml(String, Direction, boolean)}
+ * {@link #setText(String, com.google.gwt.i18n.client.HasDirection.Direction, boolean)
+ * setText(String, Direction, boolean)}
* for details.
*/
private Direction textDir;
@@ -112,8 +114,28 @@
}
/**
- * Get the inner text or html of the element, taking the inner span wrap into
+ * Get the inner text of the element, taking the inner span wrap into
* consideration, if needed.
+ *
+ * @return the text
+ */
+ public String getText() {
+ return getTextOrHtml(false /* isHtml */);
+ }
+
+ /**
+ * Get the inner html of the element, taking the inner span wrap into
+ * consideration, if needed.
+ *
+ * @return the html
+ */
+ public String getHtml() {
+ return getTextOrHtml(true /* isHtml */);
+ }
+
+ /**
+ * Get the inner text or html of the element, taking the inner span wrap into consideration, if
+ * needed. Prefer using {@link #getText} or {@link #getHtml} instead of this method.
*
* @param isHtml true to get the inner html, false to get the inner text
* @return the text or html
@@ -137,7 +159,7 @@
* For backwards compatibility, assure there's no span wrap, and update the
* content direction.
*/
- setInnerTextOrHtml(getTextOrHtml(true), true);
+ setInnerTextOrHtml(getHtml(), true);
isSpanWrapped = false;
textDir = initialElementDir;
isDirectionExplicitlySet = true;
@@ -164,16 +186,56 @@
* setTextOrHtml call.
*/
if (!isDirectionExplicitlySet) {
- setTextOrHtml(getTextOrHtml(true), true);
+ setHtml(getHtml());
}
}
/**
- * Sets the element's content to the given value (either plain text or HTML).
+ * Sets the element's content to the given value (plain text).
* If direction estimation is off, the direction is verified to match the
* element's initial direction. Otherwise, the direction is affected as
* described at
- * {@link #setTextOrHtml(String, com.google.gwt.i18n.client.HasDirection.Direction, boolean) setTextOrHtml(String, Direction, boolean)}.
+ * {@link #setText(String, com.google.gwt.i18n.client.HasDirection.Direction)
+ * setText(String, Direction)}.
+ *
+ * @param content the element's new content
+ */
+ public void setText(String content) {
+ setTextOrHtml(content, false /* isHtml */);
+ }
+
+ /**
+ * Sets the element's content to the given value (html).
+ * If direction estimation is off, the direction is verified to match the
+ * element's initial direction. Otherwise, the direction is affected as
+ * described at
+ * {@link #setHtml(String, com.google.gwt.i18n.client.HasDirection.Direction)
+ * setHtml(String, Direction)}.
+ *
+ * @param content the element's new content
+ */
+ public void setHtml(SafeHtml content) {
+ setHtml(content.asString());
+ }
+
+ /**
+ * Sets the element's content to the given value (html).
+ * If direction estimation is off, the direction is verified to match the
+ * element's initial direction. Otherwise, the direction is affected as
+ * described at
+ * {@link #setHtml(String, com.google.gwt.i18n.client.HasDirection.Direction)
+ * setHtml(String, Direction)}.
+ *
+ * @param content the element's new content
+ */
+ public void setHtml(String content) {
+ setTextOrHtml(content, true /* isHtml */);
+ }
+
+ /**
+ * Sets the element's content to the given value (either plain text or HTML).
+ * Prefer using {@link #setText(String) setText} or {@link #setHtml(String) setHtml} instead of
+ * this method.
*
* @param content the element's new content
* @param isHtml whether the content is HTML
@@ -200,8 +262,8 @@
}
/**
- * Sets the element's content to the given value (either plain text or HTML),
- * applying the given direction.
+ * Sets the element's content to the given value (plain text), applying the
+ * given direction.
* <p>
* Implementation details:
* <ul>
@@ -218,6 +280,68 @@
*
* @param content the element's new content
* @param dir the content's direction
+ */
+ public void setText(String content, Direction dir) {
+ setTextOrHtml(content, dir, false /* isHtml */);
+ }
+
+ /**
+ * Sets the element's content to the given value (html), applying the given
+ * direction.
+ * <p>
+ * Implementation details:
+ * <ul>
+ * <li> If the element is a block element, sets its dir attribute according
+ * to the given direction.
+ * <li> Otherwise (i.e. the element is inline), the direction is set using a
+ * nested <span dir=...> element which holds the content of the element.
+ * This nested span may be followed by a zero-width Unicode direction
+ * character (LRM or RLM). This manipulation is necessary to prevent garbling
+ * in case the direction of the element is opposite to the direction of its
+ * context. See {@link com.google.gwt.i18n.shared.BidiFormatter} for more
+ * details.
+ * </ul>
+ *
+ * @param content the element's new content
+ * @param dir the content's direction
+ */
+ public void setHtml(SafeHtml content, Direction dir) {
+ setHtml(content.asString(), dir);
+ }
+
+ /**
+ * Sets the element's content to the given value (html), applying the given
+ * direction.
+ * <p>
+ * Implementation details:
+ * <ul>
+ * <li> If the element is a block element, sets its dir attribute according
+ * to the given direction.
+ * <li> Otherwise (i.e. the element is inline), the direction is set using a
+ * nested <span dir=...> element which holds the content of the element.
+ * This nested span may be followed by a zero-width Unicode direction
+ * character (LRM or RLM). This manipulation is necessary to prevent garbling
+ * in case the direction of the element is opposite to the direction of its
+ * context. See {@link com.google.gwt.i18n.shared.BidiFormatter} for more
+ * details.
+ * </ul>
+ *
+ * @param content the element's new content
+ * @param dir the content's direction
+ */
+ public void setHtml(String content, Direction dir) {
+ setTextOrHtml(content, dir, true /* isHtml */);
+ }
+
+ /**
+ * Sets the element's content to the given value (either plain text or HTML),
+ * applying the given direction. Prefer using
+ * {@link #setText(String, com.google.gwt.i18n.client.HasDirection.Direction) setText} or
+ * {@link #setHtml(String, com.google.gwt.i18n.client.HasDirection.Direction) setHtml}
+ * instead of this method.
+ *
+ * @param content the element's new content
+ * @param dir the content's direction
* @param isHtml whether the content is HTML
*/
public void setTextOrHtml(String content, Direction dir, boolean isHtml) {
diff --git a/user/src/com/google/gwt/user/client/ui/HTML.java b/user/src/com/google/gwt/user/client/ui/HTML.java
index fe4e6ac..cfb9bf1 100644
--- a/user/src/com/google/gwt/user/client/ui/HTML.java
+++ b/user/src/com/google/gwt/user/client/ui/HTML.java
@@ -169,7 +169,7 @@
}
public String getHTML() {
- return directionalTextHelper.getTextOrHtml(true);
+ return directionalTextHelper.getHtml();
}
/**
@@ -180,7 +180,7 @@
* @param html the new widget's HTML content
*/
public void setHTML(String html) {
- directionalTextHelper.setTextOrHtml(html, true);
+ directionalTextHelper.setHtml(html);
updateHorizontalAlignment();
}
@@ -195,7 +195,7 @@
* direction should be inherited from the widget's parent element.
*/
public void setHTML(String html, Direction dir) {
- directionalTextHelper.setTextOrHtml(html, dir, true);
+ directionalTextHelper.setHtml(html, dir);
updateHorizontalAlignment();
}
diff --git a/user/src/com/google/gwt/user/client/ui/Hyperlink.java b/user/src/com/google/gwt/user/client/ui/Hyperlink.java
index ec2d1da..7f6fdd6 100644
--- a/user/src/com/google/gwt/user/client/ui/Hyperlink.java
+++ b/user/src/com/google/gwt/user/client/ui/Hyperlink.java
@@ -257,7 +257,7 @@
}
public String getHTML() {
- return directionalTextHelper.getTextOrHtml(true);
+ return directionalTextHelper.getHtml();
}
/**
@@ -271,7 +271,7 @@
}
public String getText() {
- return directionalTextHelper.getTextOrHtml(false);
+ return directionalTextHelper.getText();
}
public Direction getTextDirection() {
@@ -323,11 +323,11 @@
}
public void setHTML(String html) {
- directionalTextHelper.setTextOrHtml(html, true);
+ directionalTextHelper.setHtml(html);
}
public void setHTML(SafeHtml html, Direction dir) {
- directionalTextHelper.setTextOrHtml(html.asString(), dir, true);
+ directionalTextHelper.setHtml(html, dir);
}
/**
@@ -347,11 +347,11 @@
}
public void setText(String text) {
- directionalTextHelper.setTextOrHtml(text, false);
+ directionalTextHelper.setText(text);
}
public void setText(String text, Direction dir) {
- directionalTextHelper.setTextOrHtml(text, dir, false);
+ directionalTextHelper.setText(text, dir);
}
/**
diff --git a/user/src/com/google/gwt/user/client/ui/Label.java b/user/src/com/google/gwt/user/client/ui/Label.java
index 56f29b3..a5910a6 100644
--- a/user/src/com/google/gwt/user/client/ui/Label.java
+++ b/user/src/com/google/gwt/user/client/ui/Label.java
@@ -330,7 +330,7 @@
}
public String getText() {
- return directionalTextHelper.getTextOrHtml(false);
+ return directionalTextHelper.getText();
}
public Direction getTextDirection() {
@@ -386,7 +386,7 @@
* @param text the widget's new text
*/
public void setText(String text) {
- directionalTextHelper.setTextOrHtml(text, false);
+ directionalTextHelper.setText(text);
updateHorizontalAlignment();
}
@@ -411,7 +411,7 @@
* direction should be inherited from the widget's parent element.
*/
public void setText(String text, Direction dir) {
- directionalTextHelper.setTextOrHtml(text, dir, false);
+ directionalTextHelper.setText(text, dir);
updateHorizontalAlignment();
}
}
diff --git a/user/src/com/google/gwt/user/client/ui/ValueLabel.java b/user/src/com/google/gwt/user/client/ui/ValueLabel.java
index f1fd11c..9607da3 100644
--- a/user/src/com/google/gwt/user/client/ui/ValueLabel.java
+++ b/user/src/com/google/gwt/user/client/ui/ValueLabel.java
@@ -137,7 +137,7 @@
public void setValue(T value) {
this.value = value;
- directionalTextHelper.setTextOrHtml(renderer.render(value), false);
+ directionalTextHelper.setText(renderer.render(value));
updateHorizontalAlignment();
}
-}
\ No newline at end of file
+}
diff --git a/user/test/com/google/gwt/user/client/ui/DirectionalTextHelperTest.java b/user/test/com/google/gwt/user/client/ui/DirectionalTextHelperTest.java
index 2f181ca..d8d2a70 100644
--- a/user/test/com/google/gwt/user/client/ui/DirectionalTextHelperTest.java
+++ b/user/test/com/google/gwt/user/client/ui/DirectionalTextHelperTest.java
@@ -111,6 +111,22 @@
testSetTextOrHtml(false);
}
+ public void testSetText() {
+ element = Document.get().createDivElement();
+ directionalTextHelper = new DirectionalTextHelper(element,
+ /* is inline? */ false);
+ directionalTextHelper.setText(EN_TEXT);
+ assertEquals(EN_TEXT, directionalTextHelper.getText());
+ }
+
+ public void testSetHtml() {
+ element = Document.get().createDivElement();
+ directionalTextHelper = new DirectionalTextHelper(element,
+ /* is inline? */ false);
+ directionalTextHelper.setHtml(EN_HTML);
+ assertEquals(EN_HTML, directionalTextHelper.getHtml());
+ }
+
/**
* Asserts that both the {@link HasDirectionalText#getTextDirection} and the
* physical dir attribute match the expected direction.