Skip to content

Commit a71ab7c

Browse files
author
Igor Polevoy
committed
#225 Add ability to specify HTML5 data attributes in tags
1 parent 067e5d8 commit a71ab7c

File tree

10 files changed

+94
-13
lines changed

10 files changed

+94
-13
lines changed

activeweb/src/main/java/org/javalite/activeweb/freemarker/FormTag.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@
109109
* <input type="hidden" name="blah">
110110
* </form>
111111
</pre>
112+
113+
* <h4>Adding HTML5-style attributes</h4>
114+
* Use a special attribute "data", whose value will be added to the resulting tag verbatim.
115+
*
116+
*<pre>
117+
&lt;@form data="data-greeting='hola' data-bye='astalavista'" ... &gt;
118+
*</pre>
119+
*
112120
* @author Igor Polevoy
113121
*/
114122
public class FormTag extends FreeMarkerTag{
@@ -157,13 +165,16 @@ protected void render(Map params, String body, Writer writer) throws Exception {
157165
tf.attribute("method", "post");
158166
}else{
159167
if(params.get("method") != null)
160-
tf.attribute("method", params.get("method").toString());
168+
tf.attribute("method", params.get("method").toString());
161169
}
162170
if(params.get("html_id")!= null){
163171
tf.attribute("id", params.get("html_id").toString());
164172
}
165173

166-
tf.addAttributesExcept(params, "controller", "action", "method", "id", "html_id");
174+
tf.addAttributesExcept(params, "controller", "action", "method", "id", "html_id", "data");
175+
if(params.containsKey("data")){
176+
tf.textAttributes(params.get("data").toString());
177+
}
167178
tf.write(writer);
168179
}
169180
}

activeweb/src/main/java/org/javalite/activeweb/freemarker/LinkToTag.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,14 @@
164164
* </code>
165165
*
166166
* In this example, the link is trying to make an Ajax call to a controlled action which does not exists.
167+
*
168+
* <h4>Adding HTML5-style attributes</h4>
169+
* Use a special attribute "data", whose value will be added to the resulting tag verbatim.
170+
*
171+
*<pre>
172+
&lt;@link_to data="data-greeting='hola' data-bye='astalavista'" ... &gt;
173+
*</pre>
174+
167175
*
168176
* @author Igor Polevoy
169177
*/
@@ -253,7 +261,13 @@ protected void render(Map params, String body, Writer writer) throws Exception {
253261

254262
tf.addAttributesExcept(params, "controller", "action", "form", "id", "method",
255263
"query_string", "query_params", "context_path", "destination",
256-
"before", "before_arg", "after", "after_arg", "confirm", "error", "html_id");
264+
"before", "before_arg", "after", "after_arg", "confirm", "error", "html_id", "data");
265+
266+
267+
if(params.containsKey("data")){
268+
tf.textAttributes(params.get("data").toString());
269+
}
270+
257271
writer.write(tf.toString());
258272
}
259273

activeweb/src/main/java/org/javalite/activeweb/freemarker/SelectTag.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,27 @@
3636
*
3737
* then the output from the tag will be:
3838
*<pre>
39-
* &lt;select&gt;&lt;option value=&quot;3&quot;&gt;A Tale of Two Cities&lt;/option&gt;
40-
&lt;option value=&quot;1&quot;&gt;The Hitchhiker&apos;s Guide to the Galaxy&lt;/option&gt;&lt;option value=&quot;2&quot; selected=&quot;true&quot;&gt;All Quiet on Western Front&lt;/option&gt;&lt;/select&gt;
39+
&lt;select&gt;
40+
&lt;option value=&quot;3&quot;&gt;A Tale of Two Cities&lt;/option&gt;
41+
&lt;option value=&quot;1&quot;&gt;The Hitchhiker&apos;s Guide to the Galaxy&lt;/option&gt;
42+
&lt;option value=&quot;2&quot; selected=&quot;true&quot;&gt;All Quiet on Western Front&lt;/option&gt;
43+
&lt;/select&gt;
4144
*</pre>
4245
*
4346
* Which means that the generated code is appended to hand-written body.
4447
*
48+
* <br>
49+
*
50+
* <h4>Adding HTML5-style attributes</h4>
51+
* Use a special attribute "data", whose value will be added to the resulting tag verbatim.
52+
*
53+
*<pre>
54+
&lt;select data="data-greeting='hola' data-bye='astalavista'" &gt;
55+
...
56+
&lt;/select&gt;
57+
*</pre>
58+
*
59+
*
4560
* @author Igor Polevoy: 4/12/12 1:13 PM
4661
*/
4762
public class SelectTag extends FreeMarkerTag {
@@ -86,7 +101,11 @@ protected void render(Map params, String body, Writer writer) throws Exception {
86101
}
87102

88103
TagFactory selectTf = new TagFactory("select", body + optionsBuffer);
89-
selectTf.addAttributesExcept(params, "list");
90-
writer.write(selectTf.toString());
104+
selectTf.addAttributesExcept(params, "list", "data");
105+
if(params.containsKey("data")){
106+
selectTf.textAttributes(params.get("data").toString());
107+
}
108+
selectTf.write(writer);
109+
91110
}
92111
}

activeweb/src/main/java/org/javalite/activeweb/freemarker/TagFactory.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,19 @@
2929
* @author Igor Polevoy
3030
*/
3131
public class TagFactory {
32-
private String name, body;
33-
32+
private String name, body, textAttributes;
3433
private List<Attribute> attributes = new ArrayList<Attribute>();
3534

35+
3636
public TagFactory(String name, String body) {
3737
this.name = name;
3838
this.body = body;
3939
}
4040

41+
public void textAttributes(String textAttributes) {
42+
this.textAttributes = textAttributes;
43+
}
44+
4145
public void attribute(String name, String value) {
4246
attributes.add(new Attribute(name, value));
4347

@@ -63,6 +67,12 @@ public void write(Writer w) {
6367
w.write("\"");
6468
}
6569

70+
71+
if(textAttributes != null){
72+
w.write(" ");
73+
w.write(textAttributes);
74+
}
75+
6676
if(Util.blank(body)){
6777
w.write("/>");
6878
}else{

activeweb/src/test/java/org/javalite/activeweb/freemarker/FormTagSpec.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,15 @@ public void should_add_nbsp_to_emty_form(){
160160
StringWriter sw = new StringWriter();
161161
manager.merge(map("context_path", "/simple_context", "activeweb", map("controller", "simple", "restful", true)),
162162
"/form/empty_form", sw);
163+
a(sw.toString()).shouldBeEqual("<form action=\"/simple_context/simple\">&nbsp;</form>");
164+
}
163165

166+
@Test
167+
public void should_add_HTML5_attributes(){
168+
StringWriter sw = new StringWriter();
169+
manager.merge(map("context_path", "/simple_context", "activeweb", map("controller", "simple", "restful", true)),
170+
"/form/html5_form", sw);
164171

165-
System.out.println(sw.toString());
166-
167-
172+
a(sw.toString()).shouldBeEqual("<form action=\"/simple_context/simple\" data-input='hello'>&nbsp;</form>");
168173
}
169174
}

activeweb/src/test/java/org/javalite/activeweb/freemarker/LinkToTagSpec.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public void shouldFailRestfulIfIdProvidedToNewForm(){
125125

126126
sw = new StringWriter();
127127
manager.merge(map("context_path", "/bookstore", "activeweb", map("controller", "/rest/book", "restful", true)),
128-
"link_to/restful_id_for_new_form", sw);
128+
"link_to/restful_id_for_new_form", sw);
129129
}
130130

131131
/**
@@ -191,4 +191,13 @@ public void shouldFixIssue102() {
191191
"/link_to/defect_105", sw);
192192
a(sw.toString()).shouldBeEqual("<a href=\"/book/read?first_name=John\" data-link=\"aw\" class=\"red_button\">Click here to read book 2</a>");
193193
}
194+
195+
@Test
196+
public void should_add_HTML5_attributes() {
197+
sw = new StringWriter();
198+
manager.getTag("link_to").overrideContext("");
199+
manager.merge(map("context_path", "/bookstore", "activeweb", map("controller", "simple", "restful", false)),
200+
"/link_to/html5", sw);
201+
a(sw.toString()).shouldBeEqual("<a href=\"/book/read?first_name=John\" data-link=\"aw\" class=\"red_button\" data-attributes='hello'>Click here to read book 2</a>");
202+
}
194203
}

activeweb/src/test/java/org/javalite/activeweb/freemarker/SelectTagSpec.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ public void shouldRenderSelectTag() {
8080
a(sw.toString()).shouldBeEqual("<select><option value=\"1\">The Hitchhiker's Guide to the Galaxy</option><option value=\"2\" selected=\"true\">All Quiet on Western Front</option></select>");
8181
}
8282

83+
@Test
84+
public void shouldRenderHTML5Attributes() {
85+
sw = new StringWriter();
86+
manager.merge(map("context_path", "/bookstore", "activeweb", map("controller", "simple", "restful", false),
87+
"books", list(new SelectOption(1, "The Hitchhiker's Guide to the Galaxy"), new SelectOption(2, "All Quiet on Western Front", true))), //<<--- -this is data passed to tag
88+
"/select/html5", sw);
89+
90+
a(sw.toString()).shouldBeEqual("<select data-attributes='hello'><option value=\"1\">The Hitchhiker's Guide to the Galaxy</option><option value=\"2\" selected=\"true\">All Quiet on Western Front</option></select>");
91+
}
92+
8393
@Test
8494
public void shouldRenderSelectTagWithBody() {
8595
sw = new StringWriter();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<@form controller="simple" data="data-input='hello'"/>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<@link_to controller="book" action="read" id=null query_string="first_name=John" class="red_button" data="data-attributes='hello'">Click here to read book 2</@>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<@select list=books data="data-attributes='hello'"/>

0 commit comments

Comments
 (0)