pivotal-erector 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.txt +81 -0
- data/VERSION.yml +4 -0
- data/bin/erect +7 -0
- data/lib/erector/erect.rb +148 -0
- data/lib/erector/erected.rb +63 -0
- data/lib/erector/extensions/object.rb +18 -0
- data/lib/erector/indenting.rb +36 -0
- data/lib/erector/rails/extensions/action_controller/1.2.5/action_controller.rb +17 -0
- data/lib/erector/rails/extensions/action_controller/2.2.0/action_controller.rb +26 -0
- data/lib/erector/rails/extensions/action_controller.rb +8 -0
- data/lib/erector/rails/extensions/action_view.rb +21 -0
- data/lib/erector/rails/extensions/widget/1.2.5/widget.rb +18 -0
- data/lib/erector/rails/extensions/widget/2.2.0/widget.rb +36 -0
- data/lib/erector/rails/extensions/widget.rb +117 -0
- data/lib/erector/rails/supported_rails_versions.rb +14 -0
- data/lib/erector/rails/template_handlers/1.2.5/action_view_template_handler.rb +32 -0
- data/lib/erector/rails/template_handlers/2.0.0/action_view_template_handler.rb +36 -0
- data/lib/erector/rails/template_handlers/2.1.0/action_view_template_handler.rb +31 -0
- data/lib/erector/rails/template_handlers/2.2.0/action_view_template_handler.rb +46 -0
- data/lib/erector/rails/template_handlers/action_view_template_handler.rb +14 -0
- data/lib/erector/rails.rb +6 -0
- data/lib/erector/raw_string.rb +8 -0
- data/lib/erector/rhtml.treetop +156 -0
- data/lib/erector/unicode.rb +18185 -0
- data/lib/erector/unicode_builder.rb +67 -0
- data/lib/erector/version.rb +10 -0
- data/lib/erector/widget.rb +510 -0
- data/lib/erector/widgets/table.rb +96 -0
- data/lib/erector/widgets.rb +2 -0
- data/lib/erector.rb +16 -0
- data/spec/core_spec_suite.rb +3 -0
- data/spec/erect/erect_spec.rb +145 -0
- data/spec/erect/erected_spec.rb +80 -0
- data/spec/erect/rhtml_parser_spec.rb +318 -0
- data/spec/erector/indentation_spec.rb +136 -0
- data/spec/erector/unicode_builder_spec.rb +75 -0
- data/spec/erector/widget_spec.rb +657 -0
- data/spec/erector/widgets/table_spec.rb +99 -0
- data/spec/rails_spec_suite.rb +3 -0
- data/spec/spec_helper.rb +54 -0
- data/spec/spec_suite.rb +45 -0
- metadata +118 -0
@@ -0,0 +1,145 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../spec_helper")
|
2
|
+
|
3
|
+
module Erector
|
4
|
+
describe Erect do
|
5
|
+
it "parses an empty command line" do
|
6
|
+
erect = Erect.new([])
|
7
|
+
erect.files.should == []
|
8
|
+
end
|
9
|
+
|
10
|
+
it "parses a command line with one filename on it" do
|
11
|
+
erect = Erect.new(["foo.html"])
|
12
|
+
erect.files.should == ["foo.html"]
|
13
|
+
end
|
14
|
+
|
15
|
+
it "parses a command line with several filenames on it" do
|
16
|
+
erect = Erect.new(["foo.html", "bar/baz.html"])
|
17
|
+
erect.files.should == ["foo.html", "bar/baz.html"]
|
18
|
+
end
|
19
|
+
|
20
|
+
it "is verbose by default, but quiet when told" do
|
21
|
+
Erect.new([]).verbose.should be_true
|
22
|
+
Erect.new(["-q"]).verbose.should be_false
|
23
|
+
end
|
24
|
+
|
25
|
+
it "parses a command line with several filenames and an option on it" do
|
26
|
+
erect = Erect.new(["-q", "foo.html", "bar/baz.html"])
|
27
|
+
erect.files.should == ["foo.html", "bar/baz.html"]
|
28
|
+
end
|
29
|
+
|
30
|
+
def capture
|
31
|
+
output = StringIO.new
|
32
|
+
$stdout = output
|
33
|
+
yield
|
34
|
+
output.string
|
35
|
+
ensure
|
36
|
+
$stdout = STDOUT
|
37
|
+
end
|
38
|
+
|
39
|
+
it "exits immediately from help" do
|
40
|
+
output = capture do
|
41
|
+
lambda {
|
42
|
+
erect = Erect.new(["-h"])
|
43
|
+
}.should raise_error(SystemExit)
|
44
|
+
end
|
45
|
+
output.should =~ /^Usage/
|
46
|
+
end
|
47
|
+
|
48
|
+
it "exits immediately from --version" do
|
49
|
+
output = capture do
|
50
|
+
lambda {
|
51
|
+
erect = Erect.new(["--version"])
|
52
|
+
}.should raise_error(SystemExit)
|
53
|
+
end
|
54
|
+
output.should == Erector::VERSION + "\n"
|
55
|
+
end
|
56
|
+
|
57
|
+
it "changes to html output" do
|
58
|
+
erect = Erect.new(["--to-html"])
|
59
|
+
erect.mode.should == :to_html
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
describe "Erect functionally" do
|
65
|
+
|
66
|
+
attr_reader :dir, :fred_html, :wilma_rhtml, :barney_html_erb, :fred_rb
|
67
|
+
|
68
|
+
def create(file, body="hi")
|
69
|
+
File.open(file, "w") do |f|
|
70
|
+
f.puts(body)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
before :all do
|
75
|
+
@dir = Dir.tmpdir + "/#{Time.now.to_i}" + "/explode"
|
76
|
+
@fred_html = "#{dir}/fred.html"
|
77
|
+
@wilma_rhtml = "#{dir}/wilma.rhtml"
|
78
|
+
@barney_html_erb = "#{dir}/barney.html.erb"
|
79
|
+
@fred_rb = "#{dir}/fred.rb"
|
80
|
+
|
81
|
+
FileUtils.mkdir_p(dir)
|
82
|
+
create(fred_html)
|
83
|
+
create(wilma_rhtml)
|
84
|
+
create(barney_html_erb)
|
85
|
+
create(fred_rb, "class Fred < Erector::Widget\ndef render\ndiv 'dino'\nend\nend")
|
86
|
+
end
|
87
|
+
|
88
|
+
it "explodes dirs into .html etc. files when in to-rb mode" do
|
89
|
+
erect = Erect.new(["--to-erector", dir])
|
90
|
+
erect.files.sort.should == [barney_html_erb, fred_html, wilma_rhtml]
|
91
|
+
end
|
92
|
+
|
93
|
+
it "explodes dirs into .rb files when in to-html mode" do
|
94
|
+
erect = Erect.new(["--to-html", dir])
|
95
|
+
erect.files.should == [fred_rb]
|
96
|
+
end
|
97
|
+
|
98
|
+
it "outputs .rb files in the same directory as the input .html files" do
|
99
|
+
erect = Erect.new(["--to-erector", "-q", fred_html])
|
100
|
+
erect.run
|
101
|
+
File.exist?(fred_rb).should be_true
|
102
|
+
File.read(fred_rb).should include("text 'hi'")
|
103
|
+
end
|
104
|
+
|
105
|
+
it "outputs .html files in the same directory as the input .rb files" do
|
106
|
+
betty_rb = "#{dir}/betty.rb"
|
107
|
+
betty_html = "#{dir}/betty.html"
|
108
|
+
create(betty_rb, "class Betty < Erector::Widget\ndef render\ndiv 'bam bam'\nend\nend")
|
109
|
+
|
110
|
+
erect = Erect.new(["--to-html", "-q", betty_rb])
|
111
|
+
erect.run
|
112
|
+
File.exist?(betty_html).should be_true
|
113
|
+
File.read(betty_html).should == "<div>bam bam</div>\n"
|
114
|
+
end
|
115
|
+
|
116
|
+
it "outputs .html files in the given directory" do
|
117
|
+
create(fred_rb, "class Fred < Erector::Widget\ndef render\ndiv 'dino'\nend\nend")
|
118
|
+
|
119
|
+
|
120
|
+
out_dir = "#{dir}/out"
|
121
|
+
out_file = "#{out_dir}/fred.html"
|
122
|
+
Erect.new([]).output_dir.should be_nil
|
123
|
+
erect = Erect.new(["--to-html", "-o", "#{out_dir}", "-q", fred_rb])
|
124
|
+
erect.output_dir.should == out_dir
|
125
|
+
erect.run
|
126
|
+
File.exist?(out_file).should be_true
|
127
|
+
File.read(out_file).should == "<div>dino</div>\n"
|
128
|
+
end
|
129
|
+
|
130
|
+
it "supports the --add-to-svn option"
|
131
|
+
it "supports the --delete-original option"
|
132
|
+
|
133
|
+
it "skips rendering classes that aren't widgets" do
|
134
|
+
mr_slate_rb = "#{dir}/mr_slate.rb"
|
135
|
+
mr_slate_html = "#{dir}/mr_slate.html"
|
136
|
+
create(mr_slate_rb, "class MrSlate\nend")
|
137
|
+
erect = Erect.new(["-q", "--to-html", mr_slate_rb])
|
138
|
+
erect.run
|
139
|
+
File.exist?(mr_slate_html).should be_false
|
140
|
+
end
|
141
|
+
|
142
|
+
it "properly indents lines beginning with for, unless, etc."
|
143
|
+
it "escapes single quotes inside text strings"
|
144
|
+
end
|
145
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../spec_helper")
|
2
|
+
|
3
|
+
module Erector
|
4
|
+
describe Erected do
|
5
|
+
|
6
|
+
it "picks the right file name" do
|
7
|
+
Erected.new("foo.html.erb").filename.should == "foo.rb"
|
8
|
+
Erected.new("foo.html").filename.should == "foo.rb"
|
9
|
+
Erected.new("foo.bar.html").filename.should == "foo.rb"
|
10
|
+
Erected.new("foo_bar.html.erb").filename.should == "foo_bar.rb"
|
11
|
+
Erected.new("stuff/foo_bar.html.erb").filename.should == "stuff/foo_bar.rb"
|
12
|
+
end
|
13
|
+
|
14
|
+
it "picks a nice class name" do
|
15
|
+
Erected.new("foo.html.erb").classname.should == "Foo"
|
16
|
+
Erected.new("foo.html").classname.should == "Foo"
|
17
|
+
Erected.new("foo.bar.html").classname.should == "Foo"
|
18
|
+
Erected.new("foo_bar.html.erb").classname.should == "FooBar"
|
19
|
+
Erected.new("stuff/foo_bar.html.erb").classname.should == "FooBar"
|
20
|
+
end
|
21
|
+
|
22
|
+
it "picks an even nicer class name if it's in a views dir" do
|
23
|
+
Erected.new("app/views/stuff/foo_bar.html.erb").classname.should == "Views::Stuff::FooBar"
|
24
|
+
Erected.new("views/stuff/foo_bar.html.erb").classname.should == "Views::Stuff::FooBar"
|
25
|
+
end
|
26
|
+
|
27
|
+
def convert(dir, input, output)
|
28
|
+
dir = Dir.tmpdir + "/#{Time.now.to_i}" + "/#{dir}"
|
29
|
+
|
30
|
+
FileUtils.mkdir_p(dir)
|
31
|
+
html = "#{dir}/dummy.html"
|
32
|
+
rb = "#{dir}/dummy.rb"
|
33
|
+
|
34
|
+
File.open(html, "w") do |f|
|
35
|
+
f.puts(input)
|
36
|
+
end
|
37
|
+
|
38
|
+
@e = Erected.new(html)
|
39
|
+
@e.convert
|
40
|
+
|
41
|
+
File.read(rb).should == output
|
42
|
+
end
|
43
|
+
|
44
|
+
it "converts a normal file" do
|
45
|
+
convert(".",
|
46
|
+
"<div>hello</div>",
|
47
|
+
"class Dummy < Erector::Widget\n" +
|
48
|
+
" def render\n" +
|
49
|
+
" div do\n" +
|
50
|
+
" text 'hello'\n" +
|
51
|
+
" end\n" +
|
52
|
+
" end\n" +
|
53
|
+
"end\n"
|
54
|
+
)
|
55
|
+
end
|
56
|
+
|
57
|
+
it "converts a views file" do
|
58
|
+
convert("app/views/foos",
|
59
|
+
"<div>hello</div>",
|
60
|
+
"class Views::Foos::Dummy < Erector::Widget\n" +
|
61
|
+
" def render\n" +
|
62
|
+
" div do\n" +
|
63
|
+
" text 'hello'\n" +
|
64
|
+
" end\n" +
|
65
|
+
" end\n" +
|
66
|
+
"end\n"
|
67
|
+
)
|
68
|
+
end
|
69
|
+
|
70
|
+
# todo: figure out if there is any such thing as unparsable HTML anymore
|
71
|
+
# it "raises an exception if given unparsable HTML" do
|
72
|
+
# begin
|
73
|
+
# convert(".", "<", "")
|
74
|
+
# rescue => e
|
75
|
+
# e.to_s.should include("Could not parse")
|
76
|
+
# end
|
77
|
+
# end
|
78
|
+
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,318 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../spec_helper")
|
2
|
+
|
3
|
+
module ParserTestHelper
|
4
|
+
def assert_evals_to_self(input)
|
5
|
+
assert_evals_to(input, input)
|
6
|
+
end
|
7
|
+
|
8
|
+
def parse(input)
|
9
|
+
result = @parser.parse(input)
|
10
|
+
if result
|
11
|
+
result.set_indent(0) if result.respond_to? :set_indent
|
12
|
+
else
|
13
|
+
puts @parser.failure_reason
|
14
|
+
puts @parser.terminal_failures.join("\n")
|
15
|
+
result.should_not be_nil
|
16
|
+
end
|
17
|
+
result
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe RhtmlParser do
|
22
|
+
include ParserTestHelper
|
23
|
+
|
24
|
+
before :each do
|
25
|
+
@parser = RhtmlParser.new
|
26
|
+
end
|
27
|
+
|
28
|
+
it "converts text" do
|
29
|
+
parse("hello").convert.should == "text 'hello'\n"
|
30
|
+
parse("hello maude!").convert.should == "text 'hello maude!'\n"
|
31
|
+
parse(" hello ").convert.should == "text 'hello'\n"
|
32
|
+
end
|
33
|
+
|
34
|
+
it "unescapes HTML entities in text" do
|
35
|
+
parse("<").convert.should == "text '<'\n"
|
36
|
+
parse("5 > 2").convert.should == "text '5 > 2'\n"
|
37
|
+
end
|
38
|
+
|
39
|
+
it "converts self-closing tags" do
|
40
|
+
parse("<br/>").convert.should == "br\n"
|
41
|
+
parse("<br />").convert.should == "br\n"
|
42
|
+
end
|
43
|
+
|
44
|
+
it "converts open tag" do
|
45
|
+
parse("<div>").convert.should == "div do\n"
|
46
|
+
parse("<h1>").convert.should == "h1 do\n"
|
47
|
+
end
|
48
|
+
|
49
|
+
it "converts close tag" do
|
50
|
+
parse("</div>").convert.should == "end\n"
|
51
|
+
parse("</h1>").convert.should == "end\n"
|
52
|
+
end
|
53
|
+
|
54
|
+
it "converts two nested divs" do
|
55
|
+
parse("<div><div></div></div>").convert.should ==
|
56
|
+
"div do\n" +
|
57
|
+
" div do\n" +
|
58
|
+
" end\n" +
|
59
|
+
"end\n"
|
60
|
+
end
|
61
|
+
|
62
|
+
it "converts two nested divs with whitespace" do
|
63
|
+
parse("<div> <div> </div> </div>").convert.should ==
|
64
|
+
"div do\n" +
|
65
|
+
" div do\n" +
|
66
|
+
" end\n" +
|
67
|
+
"end\n"
|
68
|
+
end
|
69
|
+
|
70
|
+
it "converts no open, text, and no close tag" do
|
71
|
+
parse("hello</div>").convert.should == "text 'hello'\nend\n"
|
72
|
+
end
|
73
|
+
|
74
|
+
it "converts open, text, and no close tag" do
|
75
|
+
parse("<div>hello").convert.should == "div do\n text 'hello'\n"
|
76
|
+
end
|
77
|
+
|
78
|
+
it "converts open, text, close" do
|
79
|
+
parse("<div>hello</div>").convert.should == "div do\n text 'hello'\nend\n"
|
80
|
+
end
|
81
|
+
|
82
|
+
it "autocloses an img tag" do
|
83
|
+
parse("<img src='foo'>").convert.should == "img :src => 'foo'\n"
|
84
|
+
end
|
85
|
+
|
86
|
+
it "converts a scriptlet" do
|
87
|
+
parse("<% foo %>").convert.should == "foo\n"
|
88
|
+
end
|
89
|
+
|
90
|
+
it "converts open, text, scriptlet, text, close" do
|
91
|
+
parse("<div>hello <% 5.times do %> very <% end %> much</div>").convert.should ==
|
92
|
+
"div do\n" +
|
93
|
+
" text 'hello'\n" +
|
94
|
+
" 5.times do\n" +
|
95
|
+
" text 'very'\n" +
|
96
|
+
" end\n" +
|
97
|
+
" text 'much'\n" +
|
98
|
+
"end\n"
|
99
|
+
end
|
100
|
+
|
101
|
+
it "converts open, scriptlet, text, close" do
|
102
|
+
parse("<div><% 5.times do %> very <% end %> much</div>").convert.should ==
|
103
|
+
"div do\n" +
|
104
|
+
" 5.times do\n" +
|
105
|
+
" text 'very'\n" +
|
106
|
+
" end\n" +
|
107
|
+
" text 'much'\n" +
|
108
|
+
"end\n"
|
109
|
+
end
|
110
|
+
|
111
|
+
it "converts open, text, scriptlet, close" do
|
112
|
+
parse("<div>hello <% 5.times do %> very <% end %></div>").convert.should ==
|
113
|
+
"div do\n" +
|
114
|
+
" text 'hello'\n" +
|
115
|
+
" 5.times do\n" +
|
116
|
+
" text 'very'\n" +
|
117
|
+
" end\n" +
|
118
|
+
"end\n"
|
119
|
+
end
|
120
|
+
|
121
|
+
it "converts printlets into rawtext statements" do
|
122
|
+
parse("<%= 1+1 %>").convert.should == "rawtext 1+1\n"
|
123
|
+
parse("<%= link_to \"mom\" %>").convert.should == "rawtext link_to(\"mom\")\n"
|
124
|
+
end
|
125
|
+
|
126
|
+
it "converts h-printlets into text statements" do
|
127
|
+
parse("<%=h foo %>").convert.should == "text foo\n"
|
128
|
+
parse("<%= h \"mom\" %>").convert.should == "text \"mom\"\n"
|
129
|
+
end
|
130
|
+
|
131
|
+
it "allows naked percent signs inside scriptlets" do
|
132
|
+
parse("<% x = 10 % 5 %>").convert.should == "x = 10 % 5\n"
|
133
|
+
end
|
134
|
+
|
135
|
+
it "indents" do
|
136
|
+
i = Erector::Indenting.new(nil, nil)
|
137
|
+
i.line("foo").should == "foo\n"
|
138
|
+
i.line_in("bar").should == "bar\n"
|
139
|
+
i.line_in("baz").should == " baz\n"
|
140
|
+
i.line("baf").should == " baf\n"
|
141
|
+
i.line_out("end").should == " end\n"
|
142
|
+
i.line_out("end").should == "end\n"
|
143
|
+
end
|
144
|
+
|
145
|
+
it "indents extra when told to" do
|
146
|
+
parse("<div>hello</div>").set_indent(2).convert.should ==
|
147
|
+
" div do\n" +
|
148
|
+
" text 'hello'\n" +
|
149
|
+
" end\n"
|
150
|
+
end
|
151
|
+
|
152
|
+
it "indents scriptlets ending with do and end" do
|
153
|
+
parse("<% form_for :foo do |x,y| %><% 5.times do %>hello<% end %><% end %>bye").convert.should ==
|
154
|
+
"form_for :foo do |x,y|\n" +
|
155
|
+
" 5.times do\n" +
|
156
|
+
" text 'hello'\n" +
|
157
|
+
" end\n" +
|
158
|
+
"end\n" +
|
159
|
+
"text 'bye'\n"
|
160
|
+
end
|
161
|
+
|
162
|
+
it "converts HTML attributes" do
|
163
|
+
parse("<div id='foo'/>").convert.should == "div :id => 'foo'\n"
|
164
|
+
parse("<div id='foo' class='bar'/>").convert.should == "div :id => 'foo', :class => 'bar'\n"
|
165
|
+
parse("<div id='foo'>bar</div>").convert.should == "div :id => 'foo' do\n text 'bar'\nend\n"
|
166
|
+
end
|
167
|
+
|
168
|
+
it "escapes single quotes inside attribute values" do
|
169
|
+
@parser.root = :attribute
|
170
|
+
parse("a=\"don't worry\"").convert.should == ":a => 'don\\'t worry'"
|
171
|
+
end
|
172
|
+
|
173
|
+
it "allows newlines where whitespace is allowed" do
|
174
|
+
parse("<img src='foo' \nalt='bar' />").convert.should == "img :src => 'foo', :alt => 'bar'\n"
|
175
|
+
end
|
176
|
+
|
177
|
+
it "treats tab characters the same as spaces" do
|
178
|
+
parse("<div \t />").convert.should == "div\n"
|
179
|
+
end
|
180
|
+
|
181
|
+
it "deals with HTML entities in text" do
|
182
|
+
parse("<").convert.should == "text '<'\n"
|
183
|
+
end
|
184
|
+
|
185
|
+
it "deals with a naked less-than or greater-than sign inside text" do
|
186
|
+
parse("if x > 2 or x< 5 then").convert.should == "text 'if x > 2 or x< 5 then'\n"
|
187
|
+
end
|
188
|
+
|
189
|
+
it "wraps printlets in parens if necessary, to avoid warning: parenthesize argument(s) for future version" do
|
190
|
+
parse("<%= h \"mom\" %>").convert.should == "text \"mom\"\n"
|
191
|
+
parse("<%= h hi \"mom\" %>").convert.should == "text hi(\"mom\")\n"
|
192
|
+
|
193
|
+
parse("<%= \"mom\" %>").convert.should == "rawtext \"mom\"\n"
|
194
|
+
parse("<%= \"hi mom\" %>").convert.should == "rawtext \"hi mom\"\n"
|
195
|
+
parse("<%= hi \"mom\" %>").convert.should == "rawtext hi(\"mom\")\n"
|
196
|
+
|
197
|
+
parse("<%= link_to blah %>").convert.should == "rawtext link_to(blah)\n"
|
198
|
+
parse("<%= link_to blah blah %>").convert.should == "rawtext link_to(blah blah)\n"
|
199
|
+
parse("<%= link_to blah(blah) %>").convert.should == "rawtext link_to(blah(blah))\n"
|
200
|
+
|
201
|
+
parse("<%= link_to(blah) %>").convert.should == "rawtext link_to(blah)\n"
|
202
|
+
end
|
203
|
+
|
204
|
+
it "won't parenthesize expressions" do
|
205
|
+
parse("<%= h foo / bar %>").convert.should == "text foo / bar\n"
|
206
|
+
end
|
207
|
+
|
208
|
+
it "converts yield so layouts work" do
|
209
|
+
pending("easy enough to make this pass, but the result doesn't seem to work as a layout")
|
210
|
+
parse("<%= yield %>").convert.should == "rawtext @content\n"
|
211
|
+
parse("<%= \"yield\" %>").convert.should == "rawtext \"yield\"\n"
|
212
|
+
parse("<%= \"the yield is good\" %>").convert.should == "rawtext \"the yield is good\"\n"
|
213
|
+
end
|
214
|
+
|
215
|
+
it "parses quoted strings" do
|
216
|
+
@parser.root = :quoted
|
217
|
+
parse("'foo'").value.should == "foo"
|
218
|
+
parse("\"foo\"").value.should == "foo"
|
219
|
+
end
|
220
|
+
|
221
|
+
it "converts attributes in isolation" do
|
222
|
+
@parser.root = :attribute
|
223
|
+
parse("a='foo'").convert.should == ":a => 'foo'"
|
224
|
+
parse("a=\"foo\"").convert.should == ":a => 'foo'"
|
225
|
+
end
|
226
|
+
|
227
|
+
it "parses a set of attributes" do
|
228
|
+
@parser.root = :attributes
|
229
|
+
parse("a='foo' b='bar'").convert.should == " :a => 'foo', :b => 'bar'"
|
230
|
+
end
|
231
|
+
|
232
|
+
it "works with namespaced attributes" do
|
233
|
+
@parser.root = :attribute
|
234
|
+
parse('xml:lang="en"').convert.should == "'xml:lang' => 'en'"
|
235
|
+
end
|
236
|
+
|
237
|
+
it "deals with HTML entities in attribute values" do
|
238
|
+
@parser.root = :attribute
|
239
|
+
parse("foo='b<r'").convert.should == ":foo => 'b<r'"
|
240
|
+
parse("foo='b<r'").convert.should == ":foo => 'b<r'"
|
241
|
+
end
|
242
|
+
|
243
|
+
it "converts DOCTYPEs" do
|
244
|
+
html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
245
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
|
246
|
+
parse(html).convert.should == "rawtext '#{html}'\n"
|
247
|
+
end
|
248
|
+
|
249
|
+
## More functional-type specs below here
|
250
|
+
|
251
|
+
it "ignores spaces, tabs and newlines" do
|
252
|
+
parse(" <div>\t\n" + "\thello !" + "\n\t</div>").convert.should ==
|
253
|
+
"div do\n" +
|
254
|
+
" text 'hello !'\n" +
|
255
|
+
"end\n"
|
256
|
+
end
|
257
|
+
|
258
|
+
it "parses some scaffolding" do
|
259
|
+
parse("<p>
|
260
|
+
<b>Name:</b>
|
261
|
+
<%=h @foo.name %>
|
262
|
+
</p>").convert.should ==
|
263
|
+
"p do\n" +
|
264
|
+
" b do\n" +
|
265
|
+
" text 'Name:'\n" +
|
266
|
+
" end\n" +
|
267
|
+
" text @foo.name\n" +
|
268
|
+
"end\n"
|
269
|
+
end
|
270
|
+
|
271
|
+
it "parses edit.erb.html" do
|
272
|
+
parse("<h1>Editing foo</h1>
|
273
|
+
|
274
|
+
<%= error_messages_for :foo %>
|
275
|
+
|
276
|
+
<% form_for(@foo) do |f| %>
|
277
|
+
<p>
|
278
|
+
<b>Name</b><br />
|
279
|
+
<%= f.text_field :name %>
|
280
|
+
</p>
|
281
|
+
|
282
|
+
<p>
|
283
|
+
<b>Age</b><br />
|
284
|
+
<%= f.text_field :age %>
|
285
|
+
</p>
|
286
|
+
|
287
|
+
<p>
|
288
|
+
<%= f.submit \"Update\" %>
|
289
|
+
</p>
|
290
|
+
<% end %>
|
291
|
+
|
292
|
+
<%= link_to 'Show', @foo %> |
|
293
|
+
<%= link_to 'Back', foos_path %>
|
294
|
+
")
|
295
|
+
end
|
296
|
+
|
297
|
+
it "parses show.html.erb" do
|
298
|
+
parse("<p>
|
299
|
+
<b>Name:</b>
|
300
|
+
<%=h @foo.name %>
|
301
|
+
</p>
|
302
|
+
|
303
|
+
<p>
|
304
|
+
<b>Age:</b>
|
305
|
+
<%=h @foo.age %>
|
306
|
+
</p>
|
307
|
+
|
308
|
+
|
309
|
+
<%= link_to 'Edit', edit_foo_path(@foo) %> |
|
310
|
+
<%= link_to 'Back', foos_path %>
|
311
|
+
")
|
312
|
+
end
|
313
|
+
|
314
|
+
it "does meta" do
|
315
|
+
parse('<meta http-equiv="content-type" content="text/html;charset=UTF-8" />').convert.should ==
|
316
|
+
"meta 'http-equiv' => 'content-type', :content => 'text/html;charset=UTF-8'\n"
|
317
|
+
end
|
318
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/../spec_helper")
|
2
|
+
|
3
|
+
describe "indentation" do
|
4
|
+
|
5
|
+
it "can detect newliney tags" do
|
6
|
+
widget = ::Erector::Widget.new
|
7
|
+
string = widget.output
|
8
|
+
widget.enable_prettyprint = true
|
9
|
+
widget.newliney("i").should == false
|
10
|
+
widget.newliney("table").should == true
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should not add newline for non-newliney tags" do
|
14
|
+
Erector::Widget.new() do
|
15
|
+
text "Hello, "
|
16
|
+
b "World"
|
17
|
+
end.enable_prettyprint(true).to_s.should == "Hello, <b>World</b>"
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should add newlines before open newliney tags" do
|
21
|
+
Erector::Widget.new() do
|
22
|
+
p "foo"
|
23
|
+
p "bar"
|
24
|
+
end.enable_prettyprint(true).to_s.should == "<p>foo</p>\n<p>bar</p>\n"
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should add newlines between text and open newliney tag" do
|
28
|
+
Erector::Widget.new() do
|
29
|
+
text "One"
|
30
|
+
p "Two"
|
31
|
+
end.enable_prettyprint(true).to_s.should == "One\n<p>Two</p>\n"
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should add newlines after end newliney tags" do
|
35
|
+
Erector::Widget.new() do
|
36
|
+
tr do
|
37
|
+
td "cell"
|
38
|
+
end
|
39
|
+
end.enable_prettyprint(true).to_s.should == "<tr>\n <td>cell</td>\n</tr>\n"
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should treat empty elements as start and end" do
|
43
|
+
Erector::Widget.new() do
|
44
|
+
p "before"
|
45
|
+
br
|
46
|
+
p "after"
|
47
|
+
end.enable_prettyprint(true).to_s.should == "<p>before</p>\n<br />\n<p>after</p>\n"
|
48
|
+
end
|
49
|
+
|
50
|
+
it "empty elements sets at_start_of_line" do
|
51
|
+
Erector::Widget.new() do
|
52
|
+
text "before"
|
53
|
+
br
|
54
|
+
p "after"
|
55
|
+
end.enable_prettyprint(true).to_s.should == "before\n<br />\n<p>after</p>\n"
|
56
|
+
end
|
57
|
+
|
58
|
+
it "will not insert extra space before/after input element" do
|
59
|
+
# If dim memory serves, the reason for not adding spaces here is
|
60
|
+
# because it affects/affected the rendering in browsers.
|
61
|
+
Erector::Widget.new() do
|
62
|
+
text 'Name'
|
63
|
+
input :type => 'text'
|
64
|
+
text 'after'
|
65
|
+
end.enable_prettyprint(true).to_s.should == 'Name<input type="text" />after'
|
66
|
+
end
|
67
|
+
|
68
|
+
it "will indent" do
|
69
|
+
Erector::Widget.new() do
|
70
|
+
html do
|
71
|
+
head do
|
72
|
+
title "hi"
|
73
|
+
end
|
74
|
+
body do
|
75
|
+
div do
|
76
|
+
p "paragraph"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end.enable_prettyprint(true).to_s.should == <<END
|
81
|
+
<html>
|
82
|
+
<head>
|
83
|
+
<title>hi</title>
|
84
|
+
</head>
|
85
|
+
<body>
|
86
|
+
<div>
|
87
|
+
<p>paragraph</p>
|
88
|
+
</div>
|
89
|
+
</body>
|
90
|
+
</html>
|
91
|
+
END
|
92
|
+
end
|
93
|
+
|
94
|
+
it "can turn off newlines" do
|
95
|
+
Erector::Widget.new() do
|
96
|
+
text "One"
|
97
|
+
p "Two"
|
98
|
+
end.enable_prettyprint(false).to_s.should == "One<p>Two</p>"
|
99
|
+
end
|
100
|
+
|
101
|
+
it "cannot turn newlines on and off, because the output is cached" do
|
102
|
+
widget = Erector::Widget.new() do
|
103
|
+
text "One"
|
104
|
+
p "Two"
|
105
|
+
end.enable_prettyprint(false)
|
106
|
+
widget.to_s.should == "One<p>Two</p>"
|
107
|
+
widget.enable_prettyprint(true)
|
108
|
+
widget.to_s.should == "One<p>Two</p>"
|
109
|
+
widget.enable_prettyprint(false)
|
110
|
+
widget.to_s.should == "One<p>Two</p>"
|
111
|
+
end
|
112
|
+
|
113
|
+
it "can turn on newlines via to_pretty" do
|
114
|
+
widget = Erector::Widget.new() do
|
115
|
+
text "One"
|
116
|
+
p "Two"
|
117
|
+
end.enable_prettyprint(false).to_pretty.should == "One\n<p>Two</p>\n"
|
118
|
+
end
|
119
|
+
|
120
|
+
it "to_pretty will leave newlines on if they already were" do
|
121
|
+
widget = Erector::Widget.new() do
|
122
|
+
text "One"
|
123
|
+
p "Two"
|
124
|
+
end.enable_prettyprint(true).to_pretty.should == "One\n<p>Two</p>\n"
|
125
|
+
end
|
126
|
+
|
127
|
+
it "can turn newlines on/off via global variable" do
|
128
|
+
Erector::Widget.new { br }.to_s.should == "<br />"
|
129
|
+
Erector::Widget.prettyprint_default = true
|
130
|
+
Erector::Widget.new { br }.to_s.should == "<br />\n"
|
131
|
+
Erector::Widget.prettyprint_default = false
|
132
|
+
Erector::Widget.new { br }.to_s.should == "<br />"
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
136
|
+
|