forked from vti/text-haml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
html-attributes.t
112 lines (99 loc) · 2.36 KB
/
html-attributes.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 7;
use Text::Haml;
my $haml = Text::Haml->new;
# Attributes: {} or ()
my $output = $haml->render(<<'EOF');
%html{xmlns => "http://www.w3.org/1999/xhtml", "xml:lang" => "en", lang => "en"}
EOF
is($output, <<'EOF');
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'></html>
EOF
$output = $haml->render(<<'EOF');
- my $link = 'http://foo.bar';
%a{href => $link} FooBar
EOF
is($output, <<'EOF');
<a href='http://foo.bar'>FooBar</a>
EOF
$output = $haml->render(<<'EOF');
%script{:src => "javascripts/script.js",
:type => "text/javascript"}
EOF
is($output, <<'EOF');
<script src='javascripts/script.js' type='text/javascript'></script>
EOF
# HTML-style Attributes: ()
$output = $haml->render(<<'EOF', var => 'bar');
%html(xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en")
.hello(class=$var)
EOF
is($output, <<'EOF');
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<div class='bar hello'></div>
</html>
EOF
#
## Attribute Methods
#
##def html_attrs(lang = 'en-US')
## {:xmlns => "http://www.w3.org/1999/xhtml", 'xml:lang' => lang, :lang => lang}
##end
##
##This can then be used in Text::Haml, like so:
##
##$output = $haml->render(<<'EOF');
##%html{html_attrs('fr-fr')}
##
##This is compiled to:
##
##<html lang='fr-fr' xml:lang='fr-fr' xmlns='http://www.w3.org/1999/xhtml'>
##</html>
#
##You can use as many such attribute methods as you want by separating them with commas, like a Ruby argument list. All the hashes will me merged together, from left to right. For example, if you defined
##
##def hash1
## {:bread => 'white', :filling => 'peanut butter and jelly'}
##end
##
##def hash2
## {:bread => 'whole wheat'}
##end
##
##then
##
##$output = $haml->render(<<'EOF');
##%sandwich{hash1, hash2, :delicious => true}/
##
##would compile to:
##
##<sandwich bread='whole wheat' delicious='true' filling='peanut butter and jelly' />
##
#
# Boolean Attributes
# XHTML
$haml->format('xhtml');
$output = $haml->render(<<'EOF');
%input{:selected => true}
EOF
is($output, <<'EOF');
<input selected='selected' />
EOF
# HTML
$haml->format('html');
$output = $haml->render(<<'EOF');
%input{:selected => false}
EOF
is($output, <<'EOF');
<input>
EOF
# HTML
$haml->format('html');
$output = $haml->render(<<'EOF');
%input(selected=true)
EOF
is($output, <<'EOF');
<input selected>
EOF