File: parsehtml.rb

package info (click to toggle)
ruby-openid 2.5.0debian-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,980 kB
  • ctags: 2,219
  • sloc: ruby: 16,737; xml: 219; sh: 24; makefile: 2
file content (45 lines) | stat: -rw-r--r-- 1,358 bytes parent folder | download | duplicates (7)
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
38
39
40
41
42
43
44
45
require "openid/yadis/htmltokenizer"
require 'cgi'

module OpenID
  module Yadis
    def Yadis.html_yadis_location(html)
      parser = HTMLTokenizer.new(html)

      # to keep track of whether or not we are in the head element
      in_head = false

      begin
        while el = parser.getTag('head', '/head', 'meta', 'body', '/body',
                                 'html', 'script')

          # we are leaving head or have reached body, so we bail
          return nil if ['/head', 'body', '/body'].member?(el.tag_name)

          if el.tag_name == 'head'
            unless el.to_s[-2] == ?/ # tag ends with a /: a short tag
              in_head = true
            end
          end
          next unless in_head

          if el.tag_name == 'script'
            unless el.to_s[-2] == ?/ # tag ends with a /: a short tag
              parser.getTag('/script')
            end
          end

          return nil if el.tag_name == 'html'

          if el.tag_name == 'meta' and (equiv = el.attr_hash['http-equiv'])
            if ['x-xrds-location','x-yadis-location'].member?(equiv.downcase) &&
                el.attr_hash.member?('content')
              return CGI::unescapeHTML(el.attr_hash['content'])
            end
          end
        end
      rescue HTMLTokenizerError # just stop parsing if there's an error
      end
    end
  end
end