markaby 0.8.1 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,61 @@
1
+ require "markaby/xml_tagset"
2
+ require "markaby/xhtml_strict"
3
+ module Markaby
4
+ # Additional tags found in XHTML 1.0 Transitional
5
+ class XHTMLTransitional < XmlTagset
6
+ @doctype = ["-//W3C//DTD XHTML 1.0 Transitional//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"]
7
+ @tagset = XHTMLStrict.tagset.merge({
8
+ strike: Attrs,
9
+ center: Attrs,
10
+ dir: Attrs + [:compact],
11
+ noframes: Attrs,
12
+ basefont: [:id, :size, :color, :face],
13
+ u: Attrs,
14
+ menu: Attrs + [:compact],
15
+ iframe: AttrCore + [:longdesc, :name, :src, :frameborder, :marginwidth, :marginheight, :scrolling, :align, :height, :width],
16
+ font: AttrCore + AttrI18n + [:size, :color, :face],
17
+ s: Attrs,
18
+ applet: AttrCore + [:codebase, :archive, :code, :object, :alt, :name, :width, :height, :align, :hspace, :vspace],
19
+ isindex: AttrCore + AttrI18n + [:prompt]
20
+ })
21
+
22
+ # Additional attributes found in XHTML 1.0 Transitional
23
+ additional_tags = {
24
+ script: [:language],
25
+ a: [:target],
26
+ td: [:bgcolor, :nowrap, :width, :height],
27
+ p: [:align],
28
+ h5: [:align],
29
+ h3: [:align],
30
+ li: [:type, :value],
31
+ div: [:align],
32
+ pre: [:width],
33
+ body: [:background, :bgcolor, :text, :link, :vlink, :alink],
34
+ ol: [:type, :compact, :start],
35
+ h4: [:align],
36
+ h2: [:align],
37
+ object: [:align, :border, :hspace, :vspace],
38
+ img: [:name, :align, :border, :hspace, :vspace],
39
+ link: [:target],
40
+ legend: [:align],
41
+ dl: [:compact],
42
+ input: [:align],
43
+ h6: [:align],
44
+ hr: [:align, :noshade, :size, :width],
45
+ base: [:target],
46
+ ul: [:type, :compact],
47
+ br: [:clear],
48
+ form: [:name, :target],
49
+ area: [:target],
50
+ h1: [:align]
51
+ }
52
+
53
+ additional_tags.each do |k, v|
54
+ @tagset[k] += v
55
+ end
56
+
57
+ @tags = @tagset.keys
58
+ @forms = @tags & FORM_TAGS
59
+ @self_closing = @tags & SELF_CLOSING_TAGS
60
+ end
61
+ end
@@ -0,0 +1,19 @@
1
+ require "markaby/tagset"
2
+ module Markaby
3
+ # Additional tags found in XHTML 1.0 Frameset
4
+ class XmlTagset < Tagset
5
+ class << self
6
+ def default_options
7
+ super.merge({
8
+ output_xml_instruction: true,
9
+ output_meta_tag: "xhtml",
10
+ root_attributes: {
11
+ xmlns: "http://www.w3.org/1999/xhtml",
12
+ "xml:lang": "en",
13
+ lang: "en"
14
+ }
15
+ })
16
+ end
17
+ end
18
+ end
19
+ end
data/lib/markaby.rb CHANGED
@@ -4,7 +4,7 @@
4
4
  # as well as the full set of Markaby classes.
5
5
  #
6
6
  # For a full list of features and instructions, see the README.
7
- $:.unshift File.expand_path(File.dirname(__FILE__))
7
+ $:.unshift __dir__
8
8
 
9
9
  # Markaby is a module containing all of the great Markaby classes that
10
10
  # do such an excellent job.
@@ -21,6 +21,7 @@ module Markaby
21
21
  end
22
22
 
23
23
  require "markaby/version"
24
- require 'builder' unless defined?(Builder)
25
- require 'markaby/builder'
26
- require 'markaby/cssproxy'
24
+ require "builder" unless defined?(Builder)
25
+ require "markaby/tagset"
26
+ require "markaby/builder"
27
+ require "markaby/cssproxy"
@@ -39,7 +39,7 @@ module Markaby
39
39
  end
40
40
  end.new
41
41
 
42
- builder = Markaby::Builder.new({:helpers => nil}, helper)
42
+ builder = Markaby::Builder.new({helpers: nil}, helper)
43
43
  builder.some_method.should == "a value"
44
44
  end
45
45
  end
@@ -9,34 +9,35 @@ describe Markaby::CssProxy do
9
9
  end
10
10
 
11
11
  def mock_builder
12
- mock_builder = Class.new do
13
- def tag!(*args); end
12
+ Class.new do
13
+ def tag!(*args)
14
+ end
14
15
  end.new
15
16
  end
16
17
 
17
18
  it "responds_to_everything" do
18
- proxy = Markaby::CssProxy.new(mock_builder, 'stream', :sym)
19
- proxy.respond_to?(:any_method).should be_true
20
- proxy.respond_to?(:foobarbazasdfasdfadfs).should be_true
19
+ proxy = Markaby::CssProxy.new(mock_builder, "stream", :sym)
20
+ proxy.respond_to?(:any_method).should be true
21
+ proxy.respond_to?(:foobarbazasdfasdfadfs).should be true
21
22
  end
22
23
 
23
24
  it "does_not_respond_to_method_missing" do
24
- proxy = Markaby::CssProxy.new(mock_builder, 'stream', :sym)
25
+ proxy = Markaby::CssProxy.new(mock_builder, "stream", :sym)
25
26
  proxy.should_not respond_to(:method_missing)
26
27
  end
27
28
 
28
29
  it "does_respond_to_private_instance_methods_with_private_flag_set_to_true" do
29
- proxy = Markaby::CssProxy.new(mock_builder, 'stream', :sym)
30
- proxy.respond_to?(:method_missing, true).should be_true
30
+ proxy = Markaby::CssProxy.new(mock_builder, "stream", :sym)
31
+ proxy.respond_to?(:method_missing, true).should be true
31
32
  end
32
33
 
33
34
  it "does_not_respond_to_private_instance_methods_with_private_flag_set_to_false" do
34
- proxy = Markaby::CssProxy.new(mock_builder, 'stream', :sym)
35
- proxy.respond_to?(:method_missing, false).should be_false
35
+ proxy = Markaby::CssProxy.new(mock_builder, "stream", :sym)
36
+ proxy.respond_to?(:method_missing, false).should be false
36
37
  end
37
38
 
38
39
  it "respond_to_should_always_return_boolean" do
39
- proxy = Markaby::CssProxy.new(mock_builder, 'stream', :sym)
40
- proxy.respond_to?(:method_missing, :a_value).should be_true
40
+ proxy = Markaby::CssProxy.new(mock_builder, "stream", :sym)
41
+ proxy.respond_to?(:method_missing, :a_value).should be true
41
42
  end
42
43
  end
@@ -1,6 +1,6 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
2
 
3
- class FragmentTest < Test::Unit::TestCase
3
+ describe "Fragments" do
4
4
  it "should have method_missing as a private instance method" do
5
5
  private_methods = Markaby::Fragment.private_instance_methods.dup
6
6
  private_methods.map! { |m| m.to_sym }
@@ -2,12 +2,12 @@ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
2
 
3
3
  describe Markaby do
4
4
  it "should insert an html5 doctype" do
5
- document = mab { html5 { head { title 'OKay' } } }
5
+ document = mab { html5 { head { title "OKay" } } }
6
6
  document.should include("<!DOCTYPE html>")
7
7
  end
8
8
 
9
9
  it "should not have xmlns in html5 html tag" do
10
- document = mab { html5 { head { title 'OKay' } } }
10
+ document = mab { html5 { head { title "OKay" } } }
11
11
  document.should_not include("xmlns")
12
12
  end
13
13
 
@@ -27,12 +27,14 @@ describe Markaby do
27
27
  end
28
28
 
29
29
  it "should put correct xhtml charset meta" do
30
- document = mab { xhtml_strict { head { title 'OKay' } } }
31
- document.should include('<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>')
30
+ document = mab { xhtml_strict { head { title "OKay" } } }
31
+ document.should include("<meta")
32
+ document.should include('http-equiv="Content-Type"')
33
+ document.should include('content="text/html; charset=utf-8"')
32
34
  end
33
35
 
34
36
  it "should put correct html5 charset meta" do
35
- document = mab { html5 { head { title 'OKay' } } }
37
+ document = mab { html5 { head { title "OKay" } } }
36
38
  document.should include('<meta charset="utf-8"/>')
37
39
  end
38
40
 
@@ -48,38 +50,42 @@ describe Markaby do
48
50
 
49
51
  it "should add a closing slash to self-closing tags in xhtml" do
50
52
  document = mab { br }
51
- document.should include('<br/>')
53
+ document.should include("<br/>")
52
54
  end
53
55
 
54
- it "should not add a closing slash to self-closing tags in html5" do
55
- pending do
56
- document = mab { html5 { br } }
57
- document.should include('<br>')
58
- end
59
- end
56
+ # it "should not add a closing slash to self-closing tags in html5" do
57
+ # pending
60
58
 
61
- it "should close empty non-self-closing tags in html5" do
62
- pending do
63
- document = mab { html5 { header } }
64
- document.should include("<header></header>")
65
- end
59
+ # document = mab { html5 { br } }
60
+ # document.should include("<br>")
61
+ # end
62
+
63
+ # it "should close empty non-self-closing tags in html5" do
64
+ # pending
65
+ # document = mab { html5 { header } }
66
+ # document.should include("<header></header>")
67
+ # end
68
+
69
+ it "should allow custom elements" do
70
+ document = mab { html5 { my_custom_element(id: "mce-123") { "Hello" } } }
71
+ document.should include("<my-custom-element id=\"mce-123\">Hello</my-custom-element>")
66
72
  end
67
73
 
68
74
  it "should not allow fake attributes" do
69
75
  expect {
70
76
  mab do
71
77
  html5 do
72
- input "something", :fake => "fake", :foo => "bar"
78
+ input "something", fake: "fake", foo: "bar"
73
79
  end
74
80
  end
75
- }.to raise_error
81
+ }.to raise_error(Markaby::InvalidXhtmlError)
76
82
  end
77
83
 
78
84
  it "should allow new attributes" do
79
85
  expect {
80
86
  mab do
81
87
  html5 do
82
- input "something", :placeholder => "placeholder"
88
+ input "something", placeholder: "placeholder"
83
89
  end
84
90
  end
85
91
  }.not_to raise_error
@@ -88,13 +94,56 @@ describe Markaby do
88
94
  it "should allow a placeholder on an input" do
89
95
  doc = mab do
90
96
  html5 do
91
- input :type => "text",
92
- :name => "foo",
93
- :value => "bar",
94
- :placeholder => "something"
97
+ input type: "text",
98
+ name: "foo",
99
+ value: "bar",
100
+ placeholder: "something"
101
+ end
102
+ end
103
+
104
+ doc.should include('<input type="text" ')
105
+ doc.should include('name="foo"')
106
+ doc.should include('value="bar"')
107
+ doc.should include('placeholder="something"')
108
+ end
109
+
110
+ it "should allow data attributes anywhere" do
111
+ doc = mab do
112
+ html5 do
113
+ div("data-foo" => "bar")
114
+ end
115
+ end
116
+
117
+ doc.should include('<div data-foo="bar"/>')
118
+ end
119
+
120
+ it "expands data attributes provided as a hash" do
121
+ doc = mab do
122
+ html5 do
123
+ div(data: {foo: "bar"})
124
+ end
125
+ end
126
+
127
+ doc.should include('<div data-foo="bar"/>')
128
+ end
129
+
130
+ it "dasherises data attributes provided as a hash" do
131
+ doc = mab do
132
+ html5 do
133
+ div(data: {some_attribute: "value"})
134
+ end
135
+ end
136
+
137
+ doc.should include('<div data-some-attribute="value"/>')
138
+ end
139
+
140
+ it "should allow aria attributes everywhere" do
141
+ doc = mab do
142
+ html5 do
143
+ div("aria-foo" => "bar")
95
144
  end
96
145
  end
97
146
 
98
- doc.should == "<!DOCTYPE html><html><input type=\"text\" name=\"foo\" value=\"bar\" placeholder=\"something\"/></html>"
147
+ doc.should include('<div aria-foo="bar"/>')
99
148
  end
100
149
  end
@@ -1,8 +1,19 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
2
 
3
3
  describe Markaby do
4
+ it "should work with classes and ids" do
5
+ mab { div.one "" }.should == %(<div class="one"></div>)
6
+ mab { div.one.two "" }.should == %(<div class="one two"></div>)
7
+ mab { div.three! "" }.should == %(<div id="three"></div>)
8
+ mab { hr.hidden }.should == %(<hr class="hidden"/>)
9
+
10
+ out = mab { input.foo id: "bar" }
11
+ out.should match("<input.*class=\"foo\".*/>")
12
+ out.should match("<input.*name=\"bar\".*/>")
13
+ end
14
+
4
15
  it "can assign helpers after instantiation" do
5
- helper = double 'helper', :foo => :bar
16
+ helper = double "helper", foo: :bar
6
17
 
7
18
  builder = Markaby::Builder.new
8
19
  builder.helper = helper
@@ -11,35 +22,31 @@ describe Markaby do
11
22
 
12
23
  it "should be able to set a local" do
13
24
  builder = Markaby::Builder.new
14
- builder.locals = { :foo => "bar" }
25
+ builder.locals = {foo: "bar"}
15
26
  builder.foo.should == "bar"
16
27
  end
17
28
 
18
29
  it "should be able to set a different local value" do
19
30
  builder = Markaby::Builder.new
20
- builder.locals = { :foo => "baz" }
31
+ builder.locals = {foo: "baz"}
21
32
  builder.foo.should == "baz"
22
33
  end
23
34
 
24
35
  it "should assign the correct key" do
25
36
  builder = Markaby::Builder.new
26
- builder.locals = { :key => :value }
37
+ builder.locals = {key: :value}
27
38
  builder.key.should == :value
28
39
  end
29
40
 
30
41
  it "should be able to assign multiple locals" do
31
42
  builder = Markaby::Builder.new
32
43
 
33
- builder.locals = { :one => "two", :three => "four" }
44
+ builder.locals = {one: "two", three: "four"}
34
45
 
35
46
  builder.one.should == "two"
36
47
  builder.three.should == "four"
37
48
  end
38
49
 
39
- def test_builder_bang_methods
40
- assert_equal "<?xml version=\"1.0\" encoding=\"UTF-8\"?>", mab { instruct! }
41
- end
42
-
43
50
  it "should be able to produce the correct html from a fragment" do
44
51
  str = ""
45
52
  str += "<div>"
@@ -53,115 +60,89 @@ describe Markaby do
53
60
  div {
54
61
  h1 "Monkeys"
55
62
  h2 {
56
- "Giraffes #{small('Miniature')} and #{strong 'Large'}"
63
+ "Giraffes #{small("Miniature")} and #{strong "Large"}"
57
64
  }
58
65
  h3 "Donkeys"
59
- h4 { "Parakeet #{b { i 'Innocent IV' }} in Classic Chartreuse" }
66
+ h4 { "Parakeet #{b { i "Innocent IV" }} in Classic Chartreuse" }
60
67
  }
61
68
  }
62
69
 
63
70
  generated.should == str
64
71
  end
65
72
 
66
- def test_fragments
67
- assert_equal %{<div><h1>Monkeys</h1><h2>Giraffes <strong>Miniature</strong></h2><h3>Donkeys</h3></div>},
68
- mab { div { h1 "Monkeys"; h2 { "Giraffes #{strong 'Miniature' }" }; h3 "Donkeys" } }
69
-
70
- assert_equal %{<div><h1>Monkeys</h1><h2>Giraffes <small>Miniature</small> and <strong>Large</strong></h2><h3>Donkeys</h3><h4>Parakeet <strong>Large</strong> as well...</h4></div>},
71
- mab { div { @a = small 'Miniature'; @b = strong 'Large'; h1 "Monkeys"; h2 { "Giraffes #{@a} and #{@b}" }; h3 "Donkeys"; h4 { "Parakeet #{@b} as well..." } } }
72
- end
73
-
74
- def test_invalid_xhtml
75
- assert_exception(NoMethodError, "undefined method `dav'") { dav {} }
76
- assert_exception(Markaby::InvalidXhtmlError, "no attribute `styl' on div elements") { div(:styl => 'ok') {} }
77
- assert_exception(Markaby::InvalidXhtmlError, "no attribute `class' on tbody elements") { tbody.okay {} }
78
- end
79
-
80
- def test_full_doc_transitional
81
- doc = mab { xhtml_transitional { head { title 'OKay' } } }
82
- assert doc =~ /^<\?xml version="1.0" encoding="UTF-8"\?>/
83
- assert doc.include?(%{"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">})
84
- assert doc.include?(%{<title>OKay</title>})
85
- end
86
-
87
- def test_full_doc_strict
88
- doc = mab { xhtml_strict { head { title 'OKay' } } }
89
- assert doc =~ /^<\?xml version="1.0" encoding="UTF-8"\?>/
90
- assert doc.include?(%{"-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">})
91
- assert doc.include?(%{<title>OKay</title>})
92
- end
73
+ it "should copy instance variables from a helper object" do
74
+ klass = Class.new do
75
+ def initialize
76
+ @hello = "hello there"
77
+ end
78
+ end
93
79
 
94
- def test_full_doc_frameset
95
- doc = mab { xhtml_frameset { head { title 'OKay' } } }
96
- assert doc =~ /^<\?xml version="1.0" encoding="UTF-8"\?>/
97
- assert doc.include?(%{"-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">})
98
- assert doc.include?(%{<title>OKay</title>})
80
+ builder = Markaby::Builder.new({}, klass.new)
81
+ builder.capture { @hello }.should == "hello there"
99
82
  end
100
83
 
101
- def test_root_attributes_can_be_changed
102
- doc = mab { xhtml_strict(:lang => 'fr') { head { title { 'Salut!' } } } }
103
- assert doc.include?(%{"-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">})
104
- assert doc.include?(%{<title>Salut!</title>})
105
- assert doc.include?(%{ lang="fr"})
84
+ describe Markaby::InvalidXhtmlError do
85
+ it "should inherit from StandardError" do
86
+ Markaby::InvalidXhtmlError.superclass.should == StandardError
87
+ end
106
88
  end
107
89
 
108
- def version_file
109
- File.expand_path(File.dirname(__FILE__) + "/../../VERSION")
110
- end
90
+ it "can assign helpers after instantiation" do
91
+ helper = double "helper", foo: :bar
111
92
 
112
- def test_markaby_should_have_correct_version
113
- assert_equal Markaby::VERSION, File.read(version_file).strip
93
+ builder = Markaby::Builder.new
94
+ builder.helper = helper
95
+ builder.foo.should == :bar
114
96
  end
115
97
 
116
- def test_duplicate_usage_of_same_id
117
- assert_raises Markaby::InvalidXhtmlError do
118
- mab do
119
- p.one!
120
- p.one!
121
- end
122
- end
98
+ it "should be able to set a local" do
99
+ builder = Markaby::Builder.new
100
+ builder.locals = {foo: "bar"}
101
+ builder.foo.should == "bar"
123
102
  end
124
103
 
125
- # auto validation
126
-
127
- def test_tagging_with_invalid_tag_should_raise_error
128
- assert_raises Markaby::InvalidXhtmlError do
129
- mab do
130
- tag! :an_invalid_tag
131
- end
132
- end
104
+ it "should be able to set a different local value" do
105
+ builder = Markaby::Builder.new
106
+ builder.locals = {foo: "baz"}
107
+ builder.foo.should == "baz"
133
108
  end
134
109
 
135
- def test_self_closing_html_tag_with_block_throws_errors
136
- assert_raises Markaby::InvalidXhtmlError do
137
- mab do
138
- html_tag :img do
139
- end
140
- end
141
- end
110
+ it "should assign the correct key" do
111
+ builder = Markaby::Builder.new
112
+ builder.locals = {key: :value}
113
+ builder.key.should == :value
142
114
  end
143
115
 
144
- def test_local_assigning
145
- builder = Markaby::Builder.new(:variable => :a_value)
116
+ it "should be able to assign multiple locals" do
117
+ builder = Markaby::Builder.new
146
118
 
147
- assert_equal :a_value, builder.variable
148
- end
119
+ builder.locals = {one: "two", three: "four"}
149
120
 
150
- def test_local_assignment_with_strings
151
- builder = Markaby::Builder.new("variable" => :a_value)
152
- assert_equal :a_value, builder.variable
121
+ builder.one.should == "two"
122
+ builder.three.should == "four"
153
123
  end
154
124
 
155
- def test_local_assignment_prefers_symbols_to_strings
156
- builder = Markaby::Builder.new("variable" => "string_value", :variable => :symbol_value)
157
- assert_equal :symbol_value, builder.variable
158
- end
125
+ it "should be able to produce the correct html from a fragment" do
126
+ str = ""
127
+ str += "<div>"
128
+ str += "<h1>Monkeys</h1>"
129
+ str += "<h2>Giraffes <small>Miniature</small> and <strong>Large</strong></h2>"
130
+ str += "<h3>Donkeys</h3>"
131
+ str += "<h4>Parakeet <b><i>Innocent IV</i></b> in Classic Chartreuse</h4>"
132
+ str += "</div>"
159
133
 
160
- def test_method_missing_should_call_tag_if_no_tagset_present
161
- Markaby::Builder.set(:tagset, nil)
134
+ generated = mab {
135
+ div {
136
+ h1 "Monkeys"
137
+ h2 {
138
+ "Giraffes #{small("Miniature")} and #{strong "Large"}"
139
+ }
140
+ h3 "Donkeys"
141
+ h4 { "Parakeet #{b { i "Innocent IV" }} in Classic Chartreuse" }
142
+ }
143
+ }
162
144
 
163
- builder = Markaby::Builder.new
164
- builder.something.should == "<something/>"
145
+ generated.should == str
165
146
  end
166
147
 
167
148
  it "should copy instance variables from a helper object" do
@@ -181,4 +162,3 @@ describe Markaby do
181
162
  end
182
163
  end
183
164
  end
184
-