File: util.rb

package info (click to toggle)
samizdat 0.7.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,576 kB
  • sloc: ruby: 7,776; xml: 899; sql: 897; sh: 67; makefile: 11
file content (37 lines) | stat: -rw-r--r-- 871 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env ruby
#
# Samizdat test suite utility methods
#
#   Copyright (c) 2002-2011  Dmitry Borodaenko <angdraug@debian.org>
#
#   This program is free software.
#   You can distribute/modify this program under the terms of
#   the GNU General Public License version 3 or later.
#
# vim: et sw=2 sts=2 ts=8 tw=0

require 'rexml/document'
require 'rexml/xpath'

XHTML_NS = { 'h' => 'http://www.w3.org/1999/xhtml' }

def elements(element, path, attribute=nil)
  REXML::XPath.match(element, path, XHTML_NS).collect {|e|
    attribute ? e.attributes[attribute] : e.text.strip
  }
end

def text(element, path)
  REXML::XPath.match(element, path, XHTML_NS).collect {|e|
    e.texts.join.strip
  }.join
end

def parse(html)
  begin
    doc = REXML::Document.new(html)
  rescue REXML::ParseException
    assert false, "REXML raised #{$!.class}: #{$!}"
  end
  doc.root
end