Class: Jabber::XHTML::HTML

Inherits:
Jabber::XMPPElement show all
Defined in:
lib/xmpp4r/xhtml/html.rb

Overview

XHTML-IM (XEP-0071) container

The important methods are:

  • HTML#contents=

  • HTML#to_text

Instance Method Summary (collapse)

Methods inherited from Jabber::XMPPElement

class_for_name_xmlns, #clone, force_xmlns, force_xmlns?, import, name_xmlns, name_xmlns_for_class, #parent=, #set_xml_lang, #typed_add, #xml_lang, #xml_lang=

Methods inherited from REXML::Element

#==, #delete_elements, #each_elements, #first_element, #first_element_content, #first_element_text, #import, import, #replace_element_content, #replace_element_text, #typed_add

Constructor Details

- (HTML) initialize(contents = [])

Initialize element with HTML contents (see HTML#contents=)



20
21
22
23
# File 'lib/xmpp4r/xhtml/html.rb', line 20

def initialize(contents=[])
  super()
  self.contents = contents
end

Instance Method Details

- (Object) body

Get first XHTML::Body child



27
28
29
# File 'lib/xmpp4r/xhtml/html.rb', line 27

def body
  first_element('body') || add(Body.new)
end

- (Object) body=(body)

Replace first XHTML::Body child



33
34
35
36
# File 'lib/xmpp4r/xhtml/html.rb', line 33

def body=(body)
  delete_elements('body')
  add(body)
end

- (Object) contents=(contents)

Set contents of this HTML document. The “contents” parameter can be:

  • An Array of REXML::Element and Strings which will replace the current children of the body

  • A single REXML::Element which will replace all other children of the body

  • An instance of XHTML::Body which will replace the current body

  • A String comprising an HTML fragment. This will be parsed, which could raise an Exception. We must never send invalid XML over an XMPP stream. If you intend to put variable data in your HTML, use something like Rails' Builder::XmlMarkup or Ramaze::Gestalt



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/xmpp4r/xhtml/html.rb', line 53

def contents=(contents)
  if contents.kind_of? String
    self.body = REXML::Document.new("<body xmlns='#{NS_XHTML}'>#{contents}</body>").root
  elsif contents.kind_of? Body
    self.body = contents
  elsif contents.kind_of? Array
    self.body = Body.new
    contents.each do |element|
      if element.kind_of? String
        body.add_text(element)
      else
        body.add(element)
      end
    end
  else
    self.body = Body.new
    body.add(contents)
  end
end

- (Object) set_body(body)

Replace first XHTML::Body child (chainable)



40
41
42
43
# File 'lib/xmpp4r/xhtml/html.rb', line 40

def set_body(body)
  self.body = body
  self
end

- (Object) set_contents(contents)

HTML#contents= chainable



75
76
77
78
# File 'lib/xmpp4r/xhtml/html.rb', line 75

def set_contents(contents)
  self.contents = contents
  self
end

- (Object) to_text

Convert contents of this XHTML container to plain text for easy usage with an additional fall-back <body/> in message stanzas

The resulting string is recursively composed of the text nodes of all children. This works because of the design criteria of HTML/XHTML: readable content is not being put into attributes but as text children.

If you require clickable links and proper information representation then compose the text yourself!



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/xmpp4r/xhtml/html.rb', line 91

def to_text
  text_getter = nil # Create binding so that the following lambda can work recursively

  text_getter = lambda do |element|
    if element.kind_of? REXML::Text
      element.value
    elsif element.kind_of? REXML::Element
      element.children.collect { |child|
        text_getter.call(child)
      }.join
    end
  end

  text_getter.call(self) # Finally, execute and return results
end