Class: Mullet::HTML::ElementRenderer

Inherits:
Object
  • Object
show all
Includes:
Container
Defined in:
lib/mullet/html/element_renderer.rb

Overview

Renders an HTML element.

Direct Known Subclasses

CommandElementRenderer

Constant Summary collapse

ATTRIBUTE_SEPARATOR =
';'
ATTRIBUTE_NAME_SEPARATOR =
'='
ATTRIBUTE_SYNTAX_ERROR =
"expected '%s' in '%s'"
ATTRIBUTE_NAME_MISSING_ERROR =
"attribute name missing in '%s'"
CONVENIENT_ATTRIBUTE_COMMANDS =
[
Command::ACTION,
Command::ALT,
Command::HREF,
Command::SRC,
Command::TITLE,
Command::VALUE ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Container

#add_child, #children, #clear_children, #delete_child, #render_children

Constructor Details

#initialize(element) ⇒ ElementRenderer

Constructor

Parameters:

  • element (Element)

    element to render



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mullet/html/element_renderer.rb', line 32

def initialize(element)
  super()

  @element = element
  @attribute_commands = []
  @remove_mode = nil
  @text_variable_name = nil
  @text_message = nil
  @template = nil
  @escape_xml = nil
end

Instance Attribute Details

#remove_modeObject (readonly)

Returns the value of attribute remove_mode.



26
27
28
# File 'lib/mullet/html/element_renderer.rb', line 26

def remove_mode
  @remove_mode
end

Instance Method Details

#add_attribute_command(attribute_name, variable_name) ⇒ Object



44
45
46
47
# File 'lib/mullet/html/element_renderer.rb', line 44

def add_attribute_command(attribute_name, variable_name)
  @attribute_commands <<
      ScopeAttributeCommand.new(attribute_name, variable_name)
end

#add_attribute_commands(attribute_variable_pairs) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mullet/html/element_renderer.rb', line 49

def add_attribute_commands(attribute_variable_pairs)
  attribute_variable_pairs.split(ATTRIBUTE_SEPARATOR).each do |command_text|
    command_parts = command_text.split(ATTRIBUTE_NAME_SEPARATOR, 2)
    if command_parts.length() < 2
      raise TemplateError.new(
          ATTRIBUTE_SYNTAX_ERROR % [ATTRIBUTE_NAME_SEPARATOR, command_text])
    end

    attribute_name = command_parts[0].strip()
    if attribute_name.empty?()
      raise TemplateError.new(ATTRIBUTE_NAME_MISSING_ERROR % command_text)
    end

    variable_name = command_parts[1].strip()
    if variable_name.empty?()
      raise TemplateError.new("variable name missing in '#{command_text}'")
    end

    add_attribute_command(attribute_name, variable_name)
  end
end

#add_attribute_message_command(attribute_name, message_arguments) ⇒ Object



95
96
97
98
# File 'lib/mullet/html/element_renderer.rb', line 95

def add_attribute_message_command(attribute_name, message_arguments)
  @attribute_commands << MessageAttributeCommand.new(
      attribute_name, Message.new(message_arguments))
end

#add_attribute_message_commands(attribute_message_pairs) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/mullet/html/element_renderer.rb', line 100

def add_attribute_message_commands(attribute_message_pairs)
  attribute_message_pairs.split(ATTRIBUTE_SEPARATOR).each do |command_text|
    command_parts = command_text.split(ATTRIBUTE_NAME_SEPARATOR, 2)
    if command_parts.length() < 2
      raise TemplateError.new(
          ATTRIBUTE_SYNTAX_ERROR % [ATTRIBUTE_NAME_SEPARATOR, command_text])
    end

    attribute_name = command_parts[0].strip()
    if attribute_name.empty?()
      raise TemplateError.new(ATTRIBUTE_NAME_MISSING_ERROR % command_text)
    end

    message_arguments = command_parts[1].strip()
    if message_arguments.empty?()
      raise TemplateError.new(
          "message arguments missing in '#{command_text}'")
    end

    add_attribute_message_command(attribute_name, message_arguments)
  end
end

#configure_attribute_commands(command_attributes) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/mullet/html/element_renderer.rb', line 71

def configure_attribute_commands(command_attributes)
  value = command_attributes.fetch(Command::ATTR, nil)
  if value != nil
    add_attribute_commands(value)
  end

  CONVENIENT_ATTRIBUTE_COMMANDS.each do |attribute_name|
    value = command_attributes.fetch(attribute_name, nil)
    if value != nil
      add_attribute_command(attribute_name, value)
    end
  end

  value = command_attributes.fetch(Command::REMOVE, nil)
  if value != nil
    value.strip!();
    value.downcase!()
    @remove_mode = RemoveMode.value_of(value)
    if @remove_mode == nil
      raise TemplateError.new("invalid remove argument '#{value}'")
    end
  end
end

#configure_attribute_message_commands(command_attributes) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/mullet/html/element_renderer.rb', line 123

def configure_attribute_message_commands(command_attributes)
  value = command_attributes.fetch(Command::ALT_MESSAGE, nil)
  if value != nil
    add_attribute_message_command(Command::ALT, value)
  end

  value = command_attributes.fetch(Command::ATTR_MESSAGE, nil)
  if value != nil
    add_attribute_message_commands(value)
  end

  value = command_attributes.fetch(Command::TITLE_MESSAGE, nil)
  if value != nil
    add_attribute_message_command(Command::TITLE, value)
  end

  value = command_attributes.fetch(Command::VALUE_MESSAGE, nil)
  if value != nil
    add_attribute_message_command(Command::VALUE, value)
  end
end

#configure_commands(command_attributes, template_loader) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
# File 'lib/mullet/html/element_renderer.rb', line 165

def configure_commands(command_attributes, template_loader)
  configure_attribute_commands(command_attributes)
  configure_attribute_message_commands(command_attributes)

  configure_content(command_attributes, template_loader)

  escape_xml_value = command_attributes.fetch(Command::ESCAPE_XML, nil)
  if escape_xml_value != nil
    @escape_xml = escape_xml_value != 'false' 
  end
end

#configure_content(command_attributes, template_loader) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/mullet/html/element_renderer.rb', line 145

def configure_content(command_attributes, template_loader)
  @text_variable_name = command_attributes.fetch(Command::TEXT, nil)
  if @text_variable_name != nil
    @text_variable_name = @text_variable_name.to_sym()
    return
  end

  text_message_arguments =
      command_attributes.fetch(Command::TEXT_MESSAGE, nil)
  if text_message_arguments != nil
    @text_message = Message.new(text_message_arguments)
    return
  end

  uri = command_attributes.fetch(Command::INCLUDE, nil)
  if uri != nil
    @template = template_loader.load(uri)
  end
end

#execute_attribute_commands(render_context) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/mullet/html/element_renderer.rb', line 190

def execute_attribute_commands(render_context)
  if @attribute_commands.empty?()
    return @element.attributes()
  end

  # Copy the original attributes.  The commands modify the copy to
  # produce the attributes to render.
  render_attributes = @element.attributes().dup()
  @attribute_commands.each do |command|
    command.execute(render_context, render_attributes)
  end
  return render_attributes
end

#has_commandObject

Checks if this renderer has a command. If it has a command, then the template builder should not discard it.



179
180
181
# File 'lib/mullet/html/element_renderer.rb', line 179

def has_command()
  return !@attribute_commands.empty?()
end

#has_dynamic_contentObject

Checks if the element content will be rendered by a command.



184
185
186
187
188
# File 'lib/mullet/html/element_renderer.rb', line 184

def has_dynamic_content()
  return @text_variable_name != nil ||
         @text_message != nil ||
         @template != nil
end

#render(render_context) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/mullet/html/element_renderer.rb', line 243

def render(render_context)
  # Process the command to change the escaping mode.
  original_escape_xml_enabled = render_context.escape_xml_enabled()
  if @escape_xml != nil
    render_context.escape_xml_enabled = @escape_xml
  end

  render_start_tag(render_context)
  render_content(render_context)
  render_end_tag(render_context)

  # Restore the original escaping mode.
  if @escape_xml != nil
    render_context.escape_xml_enabled = original_escape_xml_enabled
  end
end

#render_content(render_context) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/mullet/html/element_renderer.rb', line 225

def render_content(render_context)
  if should_render_content()
    if @text_variable_name != nil
      value = render_context.get_display_value(@text_variable_name)
      text = render_context.escape_xml(value.to_s())
      render_context << text
    elsif @text_message != nil
      text = @text_message.translate(render_context)
      text = render_context.escape_xml(text)
      render_context << text
    elsif @template != nil
      @template.render(render_context)
    else
      render_children(render_context)
    end
  end
end

#render_end_tag(render_context) ⇒ Object



215
216
217
218
219
# File 'lib/mullet/html/element_renderer.rb', line 215

def render_end_tag(render_context)
  if should_render_tag()
    render_context << @element.render_end_tag()
  end
end

#render_start_tag(render_context) ⇒ Object



208
209
210
211
212
213
# File 'lib/mullet/html/element_renderer.rb', line 208

def render_start_tag(render_context)
  if should_render_tag()
    attributes = execute_attribute_commands(render_context)
    render_context << @element.render_start_tag(attributes)
  end
end

#should_render_contentObject



221
222
223
# File 'lib/mullet/html/element_renderer.rb', line 221

def should_render_content()
  return @remove_mode != RemoveMode::CONTENT
end

#should_render_tagObject



204
205
206
# File 'lib/mullet/html/element_renderer.rb', line 204

def should_render_tag()
  return @remove_mode == nil || @remove_mode == RemoveMode::CONTENT
end