Class: Rackful::Parser::XHTML

Inherits:
DOM show all
Defined in:
lib/rackful/parser.rb

Overview

Parses XHTML as generated by Serializer::XHTML.

Constant Summary

MEDIA_TYPES =

The media types parsed by this parser.

See Also:

Parser::DOM::MEDIA_TYPES + [
  'application/xhtml+xml',
  'text/html'
]

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

This class inherits a constructor from Rackful::Parser::DOM

Instance Attribute Details

- (Nokogiri::XML::Document) document (readonly) Originally defined in class DOM

Returns:

  • (Nokogiri::XML::Document)

Since:

  • 0.2.0

- (Request) request (readonly) Originally defined in class Rackful::Parser

Returns:

- (Resource) resource (readonly) Originally defined in class Rackful::Parser

Returns:

Instance Method Details

- (void) parse

See Also:

  • Parser#parse


115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rackful/parser.rb', line 115

def parse
  # Try to find the actual content:
  content = self.document.root.xpath(
    '//html:div[@id="rackful-content"]',
    'html' => 'http://www.w3.org/1999/xhtml'
  )
  # There must be exactly one element <div id="rackful_content"/> in the document:
  if content.empty?
    raise HTTP400BadRequest, 'Couldn’t find div#rackful-content in request body.'
  end
  if content.length > 1
    raise HTTP400BadRequest, 'Multiple instances of div#rackful-content found in request body.'
  end
  # Initialize @base_url:
  base_url = self.document.root.xpath(
    '/html:html/html:head/html:base',
    'html' => 'http://www.w3.org/1999/xhtml'
  )
  if base_url.empty?
    @base_url = self.request.canonical_uri.dup
  else
    @base_url = URI( base_url.first.attribute('href').text ).normalize
    if @base_url.relative?
      @base_url = self.request.canonical_uri + @base_url
    end
  end
  # Parse the f*cking thing:
  self.parse_recursive content.first
end