Class: Wunderbar::CDATANode

Inherits:
PreformattedNode show all
Defined in:
lib/wunderbar/node.rb

Direct Known Subclasses

ScriptNode, StyleNode

Constant Summary

Constants inherited from Node

Node::ESCAPE, Node::VOID

Instance Attribute Summary

Attributes inherited from Node

#attrs, #children, #name, #node, #parent, #text

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PreformattedNode

#preserve_spaces?

Methods inherited from Node

#add_child, #at, #initialize, #method_missing, parse_css_selector, #preserve_spaces?, #root, #search, #walk

Constructor Details

This class inherits a constructor from Wunderbar::Node

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Wunderbar::Node

Class Method Details

.normalize(data, indent = '') ⇒ Object



275
276
277
278
279
280
281
282
283
284
# File 'lib/wunderbar/node.rb', line 275

def self.normalize(data, indent='')
  data = data.sub(/\n\s*\Z/, '').sub(/\A\s*\n/, '')

  unindent = data.sub(/s+\Z/,'').scan(/^ *\S/).map(&:length).min || 0

  before  = ::Regexp.new('^'.ljust(unindent))
  data.gsub! before, indent
  data.gsub!(/^#{indent}$/, '') if unindent == 0
  data
end

Instance Method Details

#serialize(options = {}, result = [], indent = '') ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/wunderbar/node.rb', line 286

def serialize(options = {}, result = [], indent='')
  if @text and @text.include? "\n"
    tindent = (indent ? "#{indent}#{options[:indent]}" : indent)
    children.unshift CDATANode.normalize(@text, tindent).rstrip
    @text = nil
  end

  if @text and @text =~ /[<^>]/
    indent += options[:indent] if indent
    children.unshift @text.gsub(/^/, indent).gsub(/^ +$/,'').rstrip
    @text = nil
    super(options.merge(pre: pre, post: post), result, indent)
  elsif children && children.any? {|node| String===node && node =~ /[<^>]/}
    super(options.merge(pre: pre, post: post), result, indent)
  else
    super
  end
end