Skip to content

Commit

Permalink
[AGNT-366][C2-20897] add disabled or readonly attributes to some comp…
Browse files Browse the repository at this point in the history
…onents (#381)
  • Loading branch information
mohamed-rojbeni authored Oct 2, 2023
1 parent 1403766 commit bda0f94
Show file tree
Hide file tree
Showing 15 changed files with 456 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ public void validate() throws InvalidInputException {
assertAttributeValue(CHECKED_ATTR, Arrays.asList(Boolean.TRUE.toString(), Boolean.FALSE.toString()));
}

if (getAttribute(DISABLED_ATTR) != null) {
assertAttributeValue(DISABLED_ATTR,
Arrays.asList(Boolean.TRUE.toString(), Boolean.FALSE.toString()));
}

if (getAttribute(READONLY_ATTR) != null) {
assertAttributeValue(READONLY_ATTR,
Arrays.asList(Boolean.TRUE.toString(), Boolean.FALSE.toString()));
}

if (!getChildren().isEmpty()) {
assertContentModel(Arrays.asList(TextNode.class, Bold.class, Italic.class));
}
Expand All @@ -54,6 +64,8 @@ protected void buildAttribute(MessageMLParser parser,
case VALUE_ATTR:
case CHECKED_ATTR:
case LABEL:
case DISABLED_ATTR:
case READONLY_ATTR:
setAttribute(item.getNodeName(), getStringAttribute(item));
break;
case ID_ATTR:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public class DatePicker extends FormElement implements LabelableElement, Tooltip
private static final String PRESENTATIONML_DISABLED_DATE_ATTR = "data-disabled-date";
private static final String PRESENTATIONML_HIGHLIGHTED_DATE_ATTR = "data-highlighted-date";
private static final String PRESENTATIONML_FORMAT_ATTR = "data-format";
protected static final String DISABLED_ATTR = "disabled";
protected static final String READONLY_ATTR = "readonly";

private static final String DATE_FORMAT_ALLOWED = "^[0-9Mdy\\/. -:]+$";

Expand All @@ -63,6 +65,8 @@ protected void buildAttribute(MessageMLParser parser, Node item) throws InvalidI
case MAX_ATTR:
case LABEL:
case TITLE:
case DISABLED_ATTR:
case READONLY_ATTR:
setAttribute(item.getNodeName(), getStringAttribute(item));
break;
case DISABLED_DATE_ATTR:
Expand Down Expand Up @@ -108,6 +112,12 @@ public void validate() throws InvalidInputException {
if (getAttribute(MAX_ATTR) != null) {
assertDateFormat(MAX_ATTR, DateTimeFormatter.ISO_DATE);
}
if (getAttribute(DISABLED_ATTR) != null) {
assertAttributeValue(DISABLED_ATTR, Arrays.asList("true", "false"));
}
if (getAttribute(READONLY_ATTR) != null) {
assertAttributeValue(READONLY_ATTR, Arrays.asList("true", "false"));
}
assertJsonDatesRange(DISABLED_DATE_ATTR);
assertJsonDatesRange(HIGHLIGHTED_DATE_ATTR);

Expand Down Expand Up @@ -177,6 +187,12 @@ private Map<String, Object> buildDataPickerInputAttributes() {
if (getAttribute(FORMAT_ATTR) != null) {
presentationAttrs.put(PRESENTATIONML_FORMAT_ATTR, getAttribute(FORMAT_ATTR));
}
if (getAttribute(DISABLED_ATTR) != null) {
presentationAttrs.put(DISABLED_ATTR, getAttribute(DISABLED_ATTR));
}
if (getAttribute(READONLY_ATTR) != null) {
presentationAttrs.put(READONLY_ATTR, getAttribute(READONLY_ATTR));
}
// PresentationML compatibility
if (getAttribute(PRESENTATIONML_DISABLED_DATE_ATTR) != null) {
presentationAttrs.put(PRESENTATIONML_DISABLED_DATE_ATTR, getAttribute(PRESENTATIONML_DISABLED_DATE_ATTR));
Expand All @@ -187,7 +203,6 @@ private Map<String, Object> buildDataPickerInputAttributes() {
if (getAttribute(PRESENTATIONML_FORMAT_ATTR) != null) {
presentationAttrs.put(PRESENTATIONML_FORMAT_ATTR, getAttribute(PRESENTATIONML_FORMAT_ATTR));
}

return presentationAttrs;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public abstract class GroupedElement extends FormElement {
protected static final String VALUE_ATTR = "value";
protected static final String CHECKED_ATTR = "checked";
protected static final String FOR_ATTR = "for";
protected static final String DISABLED_ATTR = "disabled";
protected static final String READONLY_ATTR = "readonly";


public GroupedElement(Element parent, String messageMLTag,
FormatEnum format) {
Expand Down Expand Up @@ -188,6 +191,15 @@ protected Map<String, String> buildGroupedElementInputAttributes(String id) {
} else {
presentationAttrs.put(VALUE_ATTR, PRESENTATIONML_DEFAULT_VALUE);
}

if (getAttribute(DISABLED_ATTR) != null) {
presentationAttrs.put(DISABLED_ATTR, getAttribute(DISABLED_ATTR));
}

if (getAttribute(READONLY_ATTR) != null) {
presentationAttrs.put(READONLY_ATTR, getAttribute(READONLY_ATTR));
}

return presentationAttrs;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ public void validate() throws InvalidInputException {
assertAttributeValue(CHECKED_ATTR, Arrays.asList(Boolean.TRUE.toString(), Boolean.FALSE.toString()));
}

if (getAttribute(DISABLED_ATTR) != null) {
assertAttributeValue(DISABLED_ATTR,
Arrays.asList(Boolean.TRUE.toString(), Boolean.FALSE.toString()));
}

if (getAttribute(READONLY_ATTR) != null) {
assertAttributeValue(READONLY_ATTR,
Arrays.asList(Boolean.TRUE.toString(), Boolean.FALSE.toString()));
}

if (!getChildren().isEmpty()) {
assertContentModel(Arrays.asList(TextNode.class, Bold.class, Italic.class));
}
Expand All @@ -80,6 +90,8 @@ protected void buildAttribute(MessageMLParser parser,
case NAME_ATTR:
case VALUE_ATTR:
case LABEL:
case DISABLED_ATTR:
case READONLY_ATTR:
setAttribute(item.getNodeName(), getStringAttribute(item));
break;
case ID_ATTR:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@

package org.symphonyoss.symphony.messageml.elements;

import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;

import org.symphonyoss.symphony.messageml.MessageMLParser;
import org.symphonyoss.symphony.messageml.bi.BiContext;
import org.symphonyoss.symphony.messageml.bi.BiFields;
Expand Down Expand Up @@ -62,9 +61,10 @@ public class Select extends FormElement implements LabelableElement, Tooltipable
private static final String MIN_ATTR = "data-min";
private static final String MML_MAX_ATTR = "max";
private static final String MAX_ATTR = "data-max";
private static final String MML_AUTO_SUBMIT_ATTR = "auto-submit";
protected static final String DISABLED_ATTR = "disabled";
protected static final String READONLY_ATTR = "readonly";
private static final String AUTO_SUBMIT_ATTR = "data-auto-submit";

private static final String MML_AUTO_SUBMIT_ATTR = "auto-submit";

public Select(Element parent) {
super(parent, MESSAGEML_TAG);
Expand Down Expand Up @@ -95,6 +95,17 @@ public void validate() throws InvalidInputException {
assertAttributeValue(MULTIPLE_ATTR, Arrays.asList(Boolean.TRUE.toString(), Boolean.FALSE.toString()));
}

if (getAttribute(DISABLED_ATTR) != null) {
assertAttributeValue(DISABLED_ATTR,
Arrays.asList(Boolean.TRUE.toString(), Boolean.FALSE.toString()));
}

if (getAttribute(READONLY_ATTR) != null) {
assertAttributeValue(READONLY_ATTR,

Arrays.asList(Boolean.TRUE.toString(), Boolean.FALSE.toString()));
}

if (getAttribute(AUTO_SUBMIT_ATTR) != null) {
assertAttributeValue(AUTO_SUBMIT_ATTR,
Arrays.asList(Boolean.TRUE.toString(), Boolean.FALSE.toString()));
Expand Down Expand Up @@ -135,6 +146,8 @@ protected void buildAttribute(MessageMLParser parser, Node item) throws InvalidI
case LABEL:
case TITLE:
case MULTIPLE_ATTR:
case DISABLED_ATTR:
case READONLY_ATTR:
setAttribute(item.getNodeName(), getStringAttribute(item));
break;
case MML_MIN_ATTR:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class TextArea extends FormElement implements RegexElement, LabelableElem

private static final String PLACEHOLDER_ATTR = "placeholder";
private static final String REQUIRED_ATTR = "required";
private static final String DISABLED_ATTR = "disabled";
private static final String READONLY_ATTR = "readonly";
private static final String ROWS = "rows";
private static final String COLS = "cols";
private static final List<String> VALID_VALUES_FOR_REQUIRED_ATTR = Arrays.asList("true", "false");
Expand All @@ -48,6 +50,14 @@ public void validate() throws InvalidInputException {
assertAttributeValue(REQUIRED_ATTR, VALID_VALUES_FOR_REQUIRED_ATTR);
}


if (getAttribute(DISABLED_ATTR) != null) {
assertAttributeValue(DISABLED_ATTR, VALID_VALUES_FOR_REQUIRED_ATTR);
}

if (getAttribute(READONLY_ATTR) != null) {
assertAttributeValue(READONLY_ATTR, VALID_VALUES_FOR_REQUIRED_ATTR);
}
if (getAttribute(ROWS) != null) {
checkIntegerAttribute(ROWS, 0, "Attribute \"rows\" is not valid, it must be an integer >= 0");
}
Expand All @@ -74,6 +84,8 @@ protected void buildAttribute(MessageMLParser parser,
case PATTERN_ATTR:
case LABEL:
case TITLE:
case DISABLED_ATTR:
case READONLY_ATTR:
case ROWS:
case COLS:
setAttribute(item.getNodeName(), getStringAttribute(item));
Expand Down
21 changes: 19 additions & 2 deletions src/main/java/org/symphonyoss/symphony/messageml/elements/TextField.java
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public class TextField extends FormElement implements RegexElement, LabelableEle
private static final String MASKED_ATTR = "masked";
private static final String PLACEHOLDER_ATTR = "placeholder";
private static final String VALUE_ATTR = "value";

private static final String DISABLED_ATTR = "disabled";
private static final String READONLY_ATTR = "readonly";
private static final String PRESENTATIONML_MASKED_ATTR = "data-masked";

private static final String MML_AUTO_SUBMIT_ATTR = "auto-submit";
private static final String AUTO_SUBMIT_ATTR = "data-auto-submit";

Expand Down Expand Up @@ -67,6 +67,14 @@ public void validate() throws InvalidInputException {
assertAttributeValue(MASKED_ATTR, VALID_BOOLEAN_VALUES);
}

if (getAttribute(DISABLED_ATTR) != null) {
assertAttributeValue(DISABLED_ATTR, VALID_BOOLEAN_VALUES);
}

if (getAttribute(READONLY_ATTR) != null) {
assertAttributeValue(READONLY_ATTR, VALID_BOOLEAN_VALUES);
}

if (getAttribute(AUTO_SUBMIT_ATTR) != null) {
assertAttributeValue(AUTO_SUBMIT_ATTR, VALID_BOOLEAN_VALUES);
}
Expand Down Expand Up @@ -109,6 +117,8 @@ protected void buildAttribute(MessageMLParser parser,
case PATTERN_ERROR_MESSAGE_ATTR:
case LABEL:
case TITLE:
case DISABLED_ATTR:
case READONLY_ATTR:
setAttribute(item.getNodeName(), getStringAttribute(item));
break;
case ID_ATTR:
Expand Down Expand Up @@ -194,6 +204,13 @@ public Map<String, String> getOtherAttributes() {
presentationAttrs.put(MAXLENGTH_ATTR, getAttribute(MAXLENGTH_ATTR));
}

if (getAttribute(DISABLED_ATTR) != null) {
presentationAttrs.put(DISABLED_ATTR, getAttribute(DISABLED_ATTR));
}

if (getAttribute(READONLY_ATTR) != null) {
presentationAttrs.put(READONLY_ATTR, getAttribute(READONLY_ATTR));
}
if (getAttribute(AUTO_SUBMIT_ATTR) != null) {
presentationAttrs.put(AUTO_SUBMIT_ATTR, getAttribute(AUTO_SUBMIT_ATTR));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class TimePicker extends FormElement implements LabelableElement, Tooltip
private static final String STEP_ATTR = "step";
private static final String STRICT_ATTR = "strict";
private static final String FORMAT_ATTR = "format";
private static final String DISABLED_ATTR = "disabled";
private static final String READONLY_ATTR = "readonly";

// PresentationML specific attributes
private static final String PRESENTATIONML_FORMAT_ATTR = "data-format";
Expand Down Expand Up @@ -67,6 +69,8 @@ protected void buildAttribute(MessageMLParser parser,
case MIN_ATTR:
case MAX_ATTR:
case STEP_ATTR:
case DISABLED_ATTR:
case READONLY_ATTR:
setAttribute(item.getNodeName(), getStringAttribute(item));
break;
case DISABLED_TIME_ATTR:
Expand Down Expand Up @@ -115,6 +119,13 @@ public void validate() throws InvalidInputException {
if (getAttribute(STRICT_ATTR) != null) {
assertAttributeValue(STRICT_ATTR, Arrays.asList("true", "false"));
}

if (getAttribute(DISABLED_ATTR) != null) {
assertAttributeValue(DISABLED_ATTR, Arrays.asList("true", "false"));
}
if (getAttribute(READONLY_ATTR) != null) {
assertAttributeValue(READONLY_ATTR, Arrays.asList("true", "false"));
}
}

@Override
Expand Down Expand Up @@ -295,6 +306,12 @@ private Map<String, Object> buildTimePickerInputAttributes() {
if (getAttribute(PRESENTATIONML_DISABLED_TIME_ATTR) != null) {
presentationAttrs.put(PRESENTATIONML_DISABLED_TIME_ATTR, getAttribute(PRESENTATIONML_DISABLED_TIME_ATTR));
}
if (getAttribute(DISABLED_ATTR) != null) {
presentationAttrs.put(DISABLED_ATTR, getAttribute(DISABLED_ATTR));
}
if (getAttribute(READONLY_ATTR) != null) {
presentationAttrs.put(READONLY_ATTR, getAttribute(READONLY_ATTR));
}
return presentationAttrs;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,58 @@ public void testMoreThanFiftyCheckboxesAsTableSelectWithinForm() throws Exceptio
context.parseMessageML(input, null, MessageML.MESSAGEML_VERSION);
}

@Test
public void testCheckboxWithReadyOnlyAndDisabledAttributes() throws Exception {
String input =
"<messageML><form id=\"form_id\"><checkbox name=\"id1\" value=\"value01\" "
+ "checked=\"true\" readonly=\"false\" disabled=\"true\">Red</checkbox><button "
+ "name=\"submit\" type=\"action\">Submit</button></form></messageML>";
context.parseMessageML(input, null, MessageML.MESSAGEML_VERSION);
String presentationML = context.getPresentationML();
int startId = presentationML.indexOf("label for=\"");
int endId = presentationML.indexOf('"', startId + "label for=\"".length());
String id = presentationML.substring(startId + "label for=\"".length(), endId);

String expectedPresentationML =
String.format(
"<div data-format=\"PresentationML\" data-version=\"2.0\"><form id=\"form_id\"><div "
+ "class=\"checkbox-group\"><input type=\"checkbox\" name=\"id1\" checked=\"true\" "
+ "value=\"value01\" disabled=\"true\" readonly=\"false\" "
+ "id=\"%s\"/><label "
+ "for=\"%s\">Red</label></div><button type=\"action\" "
+ "name=\"submit\">Submit</button></form></div>", id, id);

assertEquals(expectedPresentationML, context.getPresentationML());
}

@Test
public void testCheckboxWithInvalidReadyOnlyAttribute() throws Exception {
String input =
"<messageML><form id=\"form_id\"><checkbox name=\"id1\" value=\"value01\" "
+ "checked=\"true\" readonly=\"invalid\">Red</checkbox><button "
+ "name=\"submit\" type=\"action\">Submit</button></form></messageML>";
expectedException.expect(InvalidInputException.class);
expectedException.expectMessage(
"Attribute \"readonly\" of element \"checkbox\" can only be one of the following values: "
+ "[true, false].");
context.parseMessageML(input, null, MessageML.MESSAGEML_VERSION);
}

@Test
public void testCheckboxWithInvalidDisabledAttribute() throws Exception {
String input =
"<messageML><form id=\"form_id\"><checkbox name=\"id1\" value=\"value01\" "
+ "checked=\"true\" disabled=\"invalid\">Red</checkbox><button "
+ "name=\"submit\" type=\"action\">Submit</button></form></messageML>";
expectedException.expect(InvalidInputException.class);
expectedException.expectMessage(
"Attribute \"disabled\" of element \"checkbox\" can only be one of the following values: "
+ "[true, false].");
context.parseMessageML(input, null, MessageML.MESSAGEML_VERSION);
}



private static Stream<Arguments> messageMlStream() {
return Stream.of(
Arguments.of(
Expand Down
Loading

0 comments on commit bda0f94

Please sign in to comment.