class Jabber::XHTML::HTML

XHTML-IM (XEP-0071) container

The important methods are:

Public Class Methods

new(contents=[]) click to toggle source

Initialize element with HTML contents (see #contents=)

Calls superclass method
# File lib/xmpp4r/xhtml/html.rb, line 20
def initialize(contents=[])
  super()
  self.contents = contents
end

Public Instance Methods

body() click to toggle source

Get first XHTML::Body child

# File lib/xmpp4r/xhtml/html.rb, line 27
def body
  first_element('body') || add(Body.new)
end
body=(body) click to toggle source

Replace first XHTML::Body child

# File lib/xmpp4r/xhtml/html.rb, line 33
def body=(body)
  delete_elements('body')
  add(body)
end
contents=(contents) click to toggle source

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

# 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
set_body(body) click to toggle source

Replace first XHTML::Body child (chainable)

# File lib/xmpp4r/xhtml/html.rb, line 40
def set_body(body)
  self.body = body
  self
end
set_contents(contents) click to toggle source

#contents= chainable

# File lib/xmpp4r/xhtml/html.rb, line 75
def set_contents(contents)
  self.contents = contents
  self
end
to_text() click to toggle source

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!

# 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