Repository: tapestry-5 Updated Branches: refs/heads/master 37c2f877b -> 28b4dc805
TAP5-773: Select component should have parameter to allow option labels to be rendered w/o HTML entity escaping Project: http://git-wip-us.apache.org/repos/asf/tapestry-5/repo Commit: http://git-wip-us.apache.org/repos/asf/tapestry-5/commit/28b4dc80 Tree: http://git-wip-us.apache.org/repos/asf/tapestry-5/tree/28b4dc80 Diff: http://git-wip-us.apache.org/repos/asf/tapestry-5/diff/28b4dc80 Branch: refs/heads/master Commit: 28b4dc805dfc64891b89f8e5d83d6c5d42ff6b42 Parents: 37c2f87 Author: Howard M. Lewis Ship <[email protected]> Authored: Fri Aug 1 11:26:02 2014 -0700 Committer: Howard M. Lewis Ship <[email protected]> Committed: Fri Aug 1 11:26:02 2014 -0700 ---------------------------------------------------------------------- .../tapestry5/corelib/components/Palette.java | 4 +-- .../tapestry5/corelib/components/Select.java | 35 ++++++++++++++------ .../internal/util/SelectModelRenderer.java | 16 ++++++--- .../corelib/components/SelectTest.java | 35 ++++++++++++++++++-- .../components/output_with_raw_enabled.txt | 2 ++ 5 files changed, 72 insertions(+), 20 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/28b4dc80/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Palette.java ---------------------------------------------------------------------- diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Palette.java b/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Palette.java index 78339e1..764aa6d 100644 --- a/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Palette.java +++ b/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Palette.java @@ -1,5 +1,3 @@ -// Copyright 2007-2013 The Apache Software Foundation -// // 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 @@ -181,7 +179,7 @@ public class Palette extends AbstractField { public void render(MarkupWriter writer) { - SelectModelRenderer visitor = new SelectModelRenderer(writer, encoder); + SelectModelRenderer visitor = new SelectModelRenderer(writer, encoder, false); model.visit(visitor); } http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/28b4dc80/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Select.java ---------------------------------------------------------------------- diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Select.java b/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Select.java index 7c13a46..44f4e79 100644 --- a/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Select.java +++ b/tapestry-core/src/main/java/org/apache/tapestry5/corelib/components/Select.java @@ -1,5 +1,3 @@ -// Copyright 2007-2013 The Apache Software Foundation -// // 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 @@ -56,7 +54,7 @@ public class Select extends AbstractField public Renderer(MarkupWriter writer) { - super(writer, encoder); + super(writer, encoder, raw); } @Override @@ -94,6 +92,15 @@ public class Select extends AbstractField private SecureOption secure; /** + * If true, then the provided {@link org.apache.tapestry5.SelectModel} labels will be written raw (no escaping of + * embedded HTML entities); it becomes the callers responsibility to escape any such entities. + * + * @since 5.4 + */ + @Parameter(value = "false") + private boolean raw; + + /** * The model used to identify the option groups and options to be presented to the user. This can be generated * automatically for Enum types. */ @@ -143,15 +150,15 @@ public class Select extends AbstractField */ @Parameter(defaultPrefix = BindingConstants.LITERAL) private String zone; - + /** - * The context for the "valueChanged" event triggered by this component (optional parameter). + * The context for the "valueChanged" event triggered by this component (optional parameter). * This list of values will be converted into strings and included in * the URI. The strings will be coerced back to whatever their values are and made available to event handler * methods. The first parameter of the context passed to "valueChanged" event handlers will * still be the selected value chosen by the user, so the context passed through this parameter * will be added from the second position on. - * + * * @since 5.4 */ @Parameter @@ -251,17 +258,18 @@ public class Select extends AbstractField } } - Object onChange(final List<Context> context, - @RequestParameter(value = "t:selectvalue", allowBlank = true) final String selectValue) - throws ValidationException + Object onChange(final List<Context> context, + @RequestParameter(value = "t:selectvalue", allowBlank = true) final String selectValue) + throws ValidationException { final Object newValue = toValue(selectValue); CaptureResultCallback<Object> callback = new CaptureResultCallback<Object>(); - + Object[] newContext = new Object[context.size() + 1]; newContext[0] = newValue; - for (int i = 1; i < newContext.length; i++) { + for (int i = 1; i < newContext.length; i++) + { newContext[i] = context.get(i - 1); } @@ -484,4 +492,9 @@ public class Select extends AbstractField blankOption = option; blankLabel = label; } + + void setRaw(boolean b) + { + raw = b; + } } http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/28b4dc80/tapestry-core/src/main/java/org/apache/tapestry5/internal/util/SelectModelRenderer.java ---------------------------------------------------------------------- diff --git a/tapestry-core/src/main/java/org/apache/tapestry5/internal/util/SelectModelRenderer.java b/tapestry-core/src/main/java/org/apache/tapestry5/internal/util/SelectModelRenderer.java index 4f7c5dd..140ac3a 100644 --- a/tapestry-core/src/main/java/org/apache/tapestry5/internal/util/SelectModelRenderer.java +++ b/tapestry-core/src/main/java/org/apache/tapestry5/internal/util/SelectModelRenderer.java @@ -1,5 +1,3 @@ -// Copyright 2007, 2008 The Apache Software Foundation -// // 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 @@ -24,10 +22,13 @@ public class SelectModelRenderer implements SelectModelVisitor private final ValueEncoder encoder; - public SelectModelRenderer(final MarkupWriter writer, ValueEncoder encoder) + private final boolean raw; + + public SelectModelRenderer(final MarkupWriter writer, ValueEncoder encoder, boolean raw) { this.writer = writer; this.encoder = encoder; + this.raw = raw; } public void beginOptionGroup(OptionGroupModel groupModel) @@ -57,7 +58,14 @@ public class SelectModelRenderer implements SelectModelVisitor writeDisabled(optionModel.isDisabled()); writeAttributes(optionModel.getAttributes()); - writer.write(optionModel.getLabel()); + + if (raw) + { + writer.writeRaw(optionModel.getLabel()); + } else + { + writer.write(optionModel.getLabel()); + } writer.end(); } http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/28b4dc80/tapestry-core/src/test/java/org/apache/tapestry5/corelib/components/SelectTest.java ---------------------------------------------------------------------- diff --git a/tapestry-core/src/test/java/org/apache/tapestry5/corelib/components/SelectTest.java b/tapestry-core/src/test/java/org/apache/tapestry5/corelib/components/SelectTest.java index 9818ae4..62e04f2 100644 --- a/tapestry-core/src/test/java/org/apache/tapestry5/corelib/components/SelectTest.java +++ b/tapestry-core/src/test/java/org/apache/tapestry5/corelib/components/SelectTest.java @@ -1,5 +1,3 @@ -// Copyright 2007-2013 The Apache Software Foundation -// // 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 @@ -195,6 +193,39 @@ public class SelectTest extends InternalBaseTestCase } @Test + public void output_with_raw_enabled() throws Exception + { + ValidationTracker tracker = mockValidationTracker(); + + List<OptionModel> options = TapestryInternalUtils.toOptionModels("bold=<b>Bold</b>,italic=<i>Italic</i>"); + + Select select = new Select(); + + train_getInput(tracker, select, null); + + replay(); + + select.setModel(new SelectModelImpl(null, options)); + select.setValueEncoder(new StringValueEncoder()); + select.setValue("barney"); + select.setValidationTracker(tracker); + select.setRaw(true); + + MarkupWriter writer = new MarkupWriterImpl(new XMLMarkupModel()); + + writer.element("select"); + + select.options(writer); + + writer.end(); + + assertEquals(writer.toString(), read("output_with_raw_enabled.txt")); + + verify(); + + } + + @Test public void option_attributes() throws Exception { ValidationTracker tracker = mockValidationTracker(); http://git-wip-us.apache.org/repos/asf/tapestry-5/blob/28b4dc80/tapestry-core/src/test/resources/org/apache/tapestry5/corelib/components/output_with_raw_enabled.txt ---------------------------------------------------------------------- diff --git a/tapestry-core/src/test/resources/org/apache/tapestry5/corelib/components/output_with_raw_enabled.txt b/tapestry-core/src/test/resources/org/apache/tapestry5/corelib/components/output_with_raw_enabled.txt new file mode 100644 index 0000000..901c47c --- /dev/null +++ b/tapestry-core/src/test/resources/org/apache/tapestry5/corelib/components/output_with_raw_enabled.txt @@ -0,0 +1,2 @@ +<?xml version="1.0"?> +<select><option value="bold"><b>Bold</b></option><option value="italic"><i>Italic</i></option></select> \ No newline at end of file
