-
-
Save baroquebobcat/741000 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
with selected true as a param. | |
Element.select. | |
option("1", "one"). | |
option("2", "two", true). | |
option("3", "three").to_s | |
produces: | |
<select><option value="1">one</option><option selected="selected" value="2">two</option><option value="3">three</option></select> | |
pretty printed: | |
<select> | |
<option value="1">one</option> | |
<option selected="selected" value="2">two</option> | |
<option value="3">three</option> | |
</select> | |
*/ | |
import java.util.HashMap | |
import java.util.ArrayList | |
import java.util.regex.Pattern | |
class Element | |
def self.select:Select | |
Select.new | |
end | |
def initialize(tag_name:String) | |
@tag_name = tag_name | |
@attributes = HashMap(nil) | |
@inner_text = String(nil) | |
@children = ArrayList(nil) | |
end | |
def to_s | |
html = begin_tag | |
if inner = self.html | |
html += inner | |
end | |
html += end_tag | |
html | |
end | |
def begin_tag | |
"<#{@tag_name}#{html_attributes}>" | |
end | |
def html_attributes | |
a = '' | |
if @attributes != null | |
i = @attributes.keySet().iterator() | |
while i.hasNext | |
key = i.next | |
a+=" #{key}=\"#{Element.html_escape(String(@attributes.get(key)))}\"" | |
end | |
end | |
a | |
end | |
def get(attribute:String) | |
String(@attributes.get(attribute)) | |
end | |
def set(attribute:String, value:String) | |
unless @attributes | |
@attributes = HashMap.new | |
end | |
if value | |
@attributes.put(attribute, value) | |
self | |
else | |
remove(attribute) | |
end | |
end | |
def remove(attribute:String) | |
@attributes.remove(attribute) | |
self | |
end | |
def html() | |
if @children | |
html = '' | |
@children.each { |child| | |
html += child.toString | |
} | |
html | |
elsif @inner_html | |
@inner_html | |
end | |
end | |
def children | |
@children | |
end | |
def html(html:string) | |
@inner_html = html | |
self | |
end | |
def append_child(e:Element) | |
unless @children | |
@children = ArrayList.new() | |
end | |
@children.add(e) | |
end | |
# escape special characters | |
def self.initialize; returns :void | |
@escape_pattern = Pattern.compile("[<>&'\"]") | |
@escaped = HashMap.new | |
@escaped.put("<", "<") | |
@escaped.put(">", ">") | |
@escaped.put("&", "&") | |
@escaped.put("\"", """) | |
@escaped.put("'", "'") | |
end | |
def self.html_escape(text:String) | |
return "" unless text | |
matcher = @escape_pattern.matcher(text) | |
buffer = StringBuffer.new | |
while matcher.find | |
replacement = String(@escaped.get(matcher.group)) | |
matcher.appendReplacement(buffer, replacement) | |
end | |
matcher.appendTail(buffer) | |
return buffer.toString | |
end | |
def end_tag | |
if pair_tag? | |
"</#{@tag_name}>" | |
else | |
"" | |
end | |
end | |
def pair_tag? | |
true | |
end | |
def toString | |
to_s | |
end | |
class Option < Element | |
def initialize | |
super(:option) | |
end | |
def value(value:string) | |
self.set(:value, value) | |
end | |
end | |
class Select < Element | |
def initialize | |
super(:select) | |
@selected = Element(nil) | |
end | |
def option(value:string, caption:string, selected=false) | |
new_option = Option.new. | |
set(:value, value). | |
html(caption) | |
if selected | |
new_option.set(:selected, :selected) | |
end | |
append_child new_option | |
self | |
end | |
end | |
end | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
railsy -- don't know if this works, but it's neat idea/sketch. | |
select_tag "number", options_for_select({one: '1', two: '2', three: '3'}, '2') | |
produces: | |
<select name="number"><option value="1">one</option><option selected="selected" value="2">two</option><option value="3">three</option></select> | |
pretty printed: | |
<select> | |
<option value="1">one</option> | |
<option selected="selected" value="2">two</option> | |
<option value="3">three</option> | |
</select> | |
*/ | |
def options_for_select options:List, selected:String;returning List | |
options.map do |k,v| | |
opt = Option.new | |
opt.set :value, v | |
opt.html k | |
if selected && v == selected | |
opt.set :selected, :selected | |
end | |
end | |
end | |
import java.util.HashMap | |
import java.util.ArrayList | |
import java.util.regex.Pattern | |
class Element | |
def self.select:Select | |
Select.new | |
end | |
def initialize(tag_name:String) | |
@tag_name = tag_name | |
@attributes = HashMap(nil) | |
@inner_text = String(nil) | |
@children = ArrayList(nil) | |
end | |
def to_s | |
html = begin_tag | |
if inner = self.html | |
html += inner | |
end | |
html += end_tag | |
html | |
end | |
def begin_tag | |
"<#{@tag_name}#{html_attributes}>" | |
end | |
def html_attributes | |
a = '' | |
if @attributes != null | |
i = @attributes.keySet().iterator() | |
while i.hasNext | |
key = i.next | |
a+=" #{key}=\"#{Element.html_escape(String(@attributes.get(key)))}\"" | |
end | |
end | |
a | |
end | |
def get(attribute:String) | |
String(@attributes.get(attribute)) | |
end | |
def set(attribute:String, value:String) | |
unless @attributes | |
@attributes = HashMap.new | |
end | |
if value | |
@attributes.put(attribute, value) | |
self | |
else | |
remove(attribute) | |
end | |
end | |
def remove(attribute:String) | |
@attributes.remove(attribute) | |
self | |
end | |
def html() | |
if @children | |
html = '' | |
@children.each { |child| | |
html += child.toString | |
} | |
html | |
elsif @inner_html | |
@inner_html | |
end | |
end | |
def children | |
@children | |
end | |
def html(html:string) | |
@inner_html = html | |
self | |
end | |
def append_child(e:Element) | |
unless @children | |
@children = ArrayList.new() | |
end | |
@children.add(e) | |
end | |
# escape special characters | |
def self.initialize; returns :void | |
@escape_pattern = Pattern.compile("[<>&'\"]") | |
@escaped = HashMap.new | |
@escaped.put("<", "<") | |
@escaped.put(">", ">") | |
@escaped.put("&", "&") | |
@escaped.put("\"", """) | |
@escaped.put("'", "'") | |
end | |
def self.html_escape(text:String) | |
return "" unless text | |
matcher = @escape_pattern.matcher(text) | |
buffer = StringBuffer.new | |
while matcher.find | |
replacement = String(@escaped.get(matcher.group)) | |
matcher.appendReplacement(buffer, replacement) | |
end | |
matcher.appendTail(buffer) | |
return buffer.toString | |
end | |
def end_tag | |
if pair_tag? | |
"</#{@tag_name}>" | |
else | |
"" | |
end | |
end | |
def pair_tag? | |
true | |
end | |
def toString | |
to_s | |
end | |
class Option < Element | |
def initialize | |
super(:option) | |
end | |
def value(value:string) | |
self.set(:value, value) | |
end | |
end | |
class Select < Element | |
def initialize | |
super(:select) | |
@selected = Element(nil) | |
end | |
def option(value:string, caption:string, selected=false) | |
new_option = Option.new. | |
set(:value, value). | |
html(caption) | |
if selected | |
new_option.set(:selected, :selected) | |
end | |
append_child new_option | |
self | |
end | |
end | |
end | |
def select_tag name:String, options:List | |
tag = Select.new | |
tag.set "name", name | |
options.each do |Option(option)| | |
tag.append_child option | |
end | |
tag | |
end | |
def options_for_select options:Map, selected:String;returning List | |
options.map do |k,v| | |
opt = Option.new | |
opt.set :value, v | |
opt.html k | |
if selected && v == selected | |
opt.set :selected, :selected | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment