Class: Jabber::XHTML::HTML

Class | Jabber::XHTML::HTML
In: | lib/xmpp4r/xhtml/html.rb
Parent: | XMPPElement

XHTML-IM (XEP-0071) container

The important methods are:

- HTML#contents=
- HTML#to_text

### Methods

body body= contents= new set_body set_contents to_text

### Public Class methods

new(contents=[])

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

[Source]

        # File lib/xmpp4r/xhtml/html.rb, line 20
    20:       def initialize(contents=[])
    21:         super()
    22:         self.contents = contents
    23:       end

### Public Instance methods

body()

Get first XHTML::Body child

[Source]

        # File lib/xmpp4r/xhtml/html.rb, line 27
    27:       def body
    28:         first_element('body') || add(Body.new)
    29:       end

body=(body)

Replace first XHTML::Body child

[Source]

        # File lib/xmpp4r/xhtml/html.rb, line 33
    33:       def body=(body)
    34:         delete_elements('body')
    35:         add(body)
    36:       end

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

[Source]

        # File lib/xmpp4r/xhtml/html.rb, line 53
    53:       def contents=(contents)
    54:         if contents.kind_of? String
    55:           self.body = REXML::Document.new("<body xmlns='#{NS_XHTML}'>#{contents}</body>").root
    56:         elsif contents.kind_of? Body
    57:           self.body = contents
    58:         elsif contents.kind_of? Array
    59:           self.body = Body.new
    60:           contents.each do |element|
    61:             if element.kind_of? String
    62:               body.add_text(element)
    63:             else
    64:               body.add(element)
    65:             end
    66:           end
    67:         else
    68:           self.body = Body.new
    69:           body.add(contents)
    70:         end
    71:       end

set_body(body)

Replace first XHTML::Body child (chainable)

[Source]

        # File lib/xmpp4r/xhtml/html.rb, line 40
    40:       def set_body(body)
    41:         self.body = body
    42:         self
    43:       end

set_contents(contents)

HTML#contents= chainable

[Source]

        # File lib/xmpp4r/xhtml/html.rb, line 75
    75:       def set_contents(contents)
    76:         self.contents = contents
    77:         self
    78:       end

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!

[Source]

         # File lib/xmpp4r/xhtml/html.rb, line 91
     91:       def to_text
     92:         text_getter = nil # Create binding so that the following lambda can work recursively
     93:
     94:         text_getter = lambda do |element|
     95:           if element.kind_of? REXML::Text
     96:             element.value
     97:           elsif element.kind_of? REXML::Element
     98:             element.children.collect { |child|
     99:               text_getter.call(child)
    100:             }.join
    101:           end
    102:         end
    103:
    104:         text_getter.call(self) # Finally, execute and return results
    105:       end

[Validate]