def to_absolute(link)
      return nil if link.nil?

      valid_link = link.to_s.encode('utf-8', 'binary', invalid: :replace,
                                                       undef: :replace, replace: '')

      # remove anchor
      link =
        begin
          URI.encode(URI.decode(valid_link.gsub(/#[a-zA-Z0-9_-]*$/, '')))
        rescue URI::Error
          return nil
        end

      relative = begin
                   URI(link)
                 rescue URI::Error
                   return nil
                 end
      absolute = base ? base.merge(relative) : @url.merge(relative)

      absolute.path = '/' if absolute.path.empty?

      absolute
    end
def to_absolute(link)
      return nil if link.nil?

      valid_link = link.to_s.encode('utf-8', 'binary', invalid: :replace,
                                                       undef: :replace, replace: '')

      # remove anchor
      link =
        begin
          URI.encode(URI.decode(valid_link.gsub(/#[a-zA-Z0-9_-]*$/, '')))
        rescue URI::Error
          return nil
        end

      relative = begin
                   URI(link)
                 rescue URI::Error
                   return nil
                 end
      absolute = base ? base.merge(relative) : @url.merge(relative)

      absolute.path = '/' if absolute.path.empty?

      absolute
    end
Converts relative URL *link* into an absolute URL based on the
 location of the page