Class: Grell::Page::VisitedPage::Link

Inherits:
Object
  • Object
show all
Defined in:
lib/grell/page.rb

Overview

Private class to group all the methods related to links.

Instance Method Summary collapse

Constructor Details

#initialize(anchor) ⇒ Link

Returns a new instance of Link.



222
223
224
# File 'lib/grell/page.rb', line 222

def initialize(anchor)
  @anchor = anchor
end

Instance Method Details

#disabled?Boolean

Is the link disabled by either Javascript or CSS?

Returns:

  • (Boolean)


232
233
234
# File 'lib/grell/page.rb', line 232

def disabled?
  @anchor.disabled? || !!@anchor.native.attributes['disabled']
end

#hrefObject

Some links may use data-href + javascript to do interesting things



242
243
244
# File 'lib/grell/page.rb', line 242

def href
  @anchor['href'] || @anchor['data-href']
end

#inside_header?Boolean

<link> can only be used in the <head> as of: developer.mozilla.org/en/docs/Web/HTML/Element/link

Returns:

  • (Boolean)


227
228
229
# File 'lib/grell/page.rb', line 227

def inside_header?
  @anchor.tag_name == 'link'
end

#js_href?Boolean

Does the href use javascript?

Returns:

  • (Boolean)


237
238
239
# File 'lib/grell/page.rb', line 237

def js_href?
  href.start_with?('javascript:')
end

#to_url(host) ⇒ Object

We only accept links in this same host that start with a path



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/grell/page.rb', line 247

def to_url(host)
  uri = URI.parse(href)
  if uri.absolute?
    if uri.host != URI.parse(host).host
      Grell.logger.debug "GRELL does not follow links to external hosts: #{href}"
      nil
    else
      href # Absolute link to our own host
    end
  else
    if uri.path.nil?
      Grell.logger.debug "GRELL does not follow links without a path: #{uri}"
      nil
    end
    if uri.path.start_with?('/')
      host + href  # convert to full URL
    else # links like href="google.com" the browser would go to http://google.com like "http://#{link}"
      Grell.logger.debug "GRELL Bad formatted link: #{href}, assuming external"
      nil
    end
  end
rescue URI::InvalidURIError # Invalid links propagating till we navigate to them
  href
end