add encoding for HTML5 'data' attribute... mix with jQuery for massive fun.

Johnathon E. Wright committed Nov 10, 2011
commit dc87ad091e7f86ff6426267b58d91166f4219d08
Showing 4 changed files with 20 additions and 0 deletions
sketch.rb b/lib/sketch.rb +1 -0
@@ @@ -6,6 +6,7 @@ class Sketch < Valuable
has_value :width
has_value :baseProfile, :alias => :base_profile
has_value :doctype
+ has_value :data, :default => {}
def draw(canvas)
raise NotImplementedError
sketch/base.rb b/lib/sketch/base.rb +7 -0
@@ @@ -4,9 +4,16 @@ module Sketch::Base
#svg is heavy in dashes. Ruby symbols can't handle them.
attributes.each do |name, value|
+ next if name == :data
out[name.to_s.gsub('_', '-')] = value
end
+ if attributes.has_key?( :data )
+ attributes[:data].each do |name, value|
+ out["data-#{name.to_s.gsub('_', '-')}"] = value
+ end
+ end
+
out[:class] = out.delete(:klass) if out.has_key?(:klass)
out
sketch/element.rb b/lib/sketch/element.rb +1 -0
@@ @@ -4,6 +4,7 @@ class Sketch::Element < Valuable
has_value :id
has_value :klass
has_value :style
+ has_value :data, :default => {}
def draw(canvas)
canvas.send( self.svg_node, svg_attributes )
test/base_test.rb +11 -0
@@ @@ -12,5 +12,16 @@ class SketchTest < Test::Unit::TestCase
Squigly.new.draw(canvas)
end
+ def test_that_underscores_become_dashes
+ canvas = stub
+ canvas.expects(:squigly).with(has_entry('reflective-coating', 'dark'))
+ Squigly.new(:reflective_coating => 'dark').draw(canvas)
+ end
+
+ def test_that_data_is_added_in_html5_format
+ canvas = stub
+ canvas.expects(:squigly).with('data-visit-id' => 21)
+ Squigly.new(:data => {:visit_id => 21}).draw(canvas)
+ end
end