Class: HTree::GenCode::CDATABuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/htree/gencode.rb

Constant Summary collapse

ChRef =
{
  '&' => '&',
  '<' => '&lt;',
  '>' => '&gt;',
  '"' => '&quot;',
}

Instance Method Summary collapse

Constructor Details

#initializeCDATABuffer

Returns a new instance of CDATABuffer.



39
40
41
# File 'lib/htree/gencode.rb', line 39

def initialize
  @buf = ''
end

Instance Method Details

#html_output?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/htree/gencode.rb', line 43

def html_output?
  true
end

#not_valid_for_html_cdata(*args) ⇒ Object Also known as: output_slash_if_xml, output_cdata_content, output_dynamic_attvalue

Raises:

  • (ArgumentError)


47
48
49
# File 'lib/htree/gencode.rb', line 47

def not_valid_for_html_cdata(*args)
  raise ArgumentError, "CDATA content only accept texts."
end

#output_dynamic_text(string) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/htree/gencode.rb', line 69

def output_dynamic_text(string)
  if string.respond_to? :rcdata
    @buf << string.rcdata.gsub(/[<>]/) { ChRef[$&] }
  else
    @buf << string.to_s.gsub(/[&<>]/) { ChRef[$&] }
  end
end

#output_string(string) ⇒ Object



54
55
56
# File 'lib/htree/gencode.rb', line 54

def output_string(string)
  @buf << string
end

#output_text(string) ⇒ Object



58
59
60
# File 'lib/htree/gencode.rb', line 58

def output_text(string)
  @buf << string
end

#resultObject



77
78
79
80
81
82
83
84
85
86
# File 'lib/htree/gencode.rb', line 77

def result
  if %r{[<>]} =~ @buf
    raise ArgumentError, "cdata contains non-text : #{@buf.inspect}"
  end
  str = HTree::Text.parse_pcdata(@buf).to_s
  if %r{</} =~ str
    raise ArgumentError, "cdata contains '</' : #{str.inspect}"
  end
  str
end