-
Notifications
You must be signed in to change notification settings - Fork 30
/
element.cr
65 lines (59 loc) · 2.2 KB
/
element.cr
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
module Slang
module Nodes
class Element < Node
SELF_CLOSING_TAGS = {"area", "base", "br", "col", "embed", "hr", "img", "input", "keygen", "link", "menuitem", "meta", "param", "source", "track", "wbr"}
RAW_TEXT_TAGS = %w(script style)
delegate :name, :id, :attributes, to: @token
def generate_class_names
names = attributes.delete("class").as Set
names.join(" ")
end
def allow_children_to_escape?
!RAW_TEXT_TAGS.includes?(name)
end
def to_s(str, buffer_name)
str << "#{buffer_name} << \"\n\"\n" unless str.empty?
str << "#{buffer_name} << \"#{indentation}\"\n" if indent?
str << "#{buffer_name} << \"<#{name}\"\n"
str << "#{buffer_name} << \" id=\\\"#{id}\\\"\"\n" if id
c_names = generate_class_names
if c_names && c_names != ""
str << "#{buffer_name} << \" class\"\n"
str << "#{buffer_name} << \"=\\\"\"\n"
str << "(\"#{c_names}\").to_s #{buffer_name}\n"
str << "#{buffer_name} << \"\\\"\"\n"
end
attributes.each do |name, value|
str << "unless #{value} == false\n" # remove the attribute if value evaluates to false
str << "#{buffer_name} << \" #{name}\"\n"
str << "unless #{value} == true\n" # remove the value if value evaluates to true
# any other attribute value.
str << "#{buffer_name} << \"=\\\"\"\n"
str << "#{buffer_name} << (#{value}).to_s.gsub(/\"/,\""\")\n"
str << "#{buffer_name} << \"\\\"\"\n"
str << "end\n"
str << "end\n"
end
str << "#{buffer_name} << \">\"\n"
if children?
nodes.each do |node|
node.to_s(str, buffer_name)
end
end
if !self_closing?
if children? && !only_inline_children?
str << "#{buffer_name} << \"\n\"\n"
str << "#{buffer_name} << \"#{indentation}\"\n" if indent?
end
str << "#{buffer_name} << \"</#{name}>\"\n"
end
end
def only_inline_children?
nodes.all? { |n| n.inline }
end
def self_closing?
SELF_CLOSING_TAGS.includes?(name)
end
end
end
end