Class: HTMLValidationResult

Inherits:
Object
  • Object
show all
Includes:
PageValidations
Defined in:
lib/html_validation/html_validation_result.rb

Constant Summary

Constants included from PageValidations

PageValidations::HTML_VALIDATOR_VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PageValidations

data_path, data_path=, #have_valid_html, included

Constructor Details

#initialize(resource, html, datapath, tidy_flags = [], options = {}) ⇒ HTMLValidationResult

options ex: options = [‘–show-warnings false’]



16
17
18
19
20
21
22
23
24
# File 'lib/html_validation/html_validation_result.rb', line 16

def initialize(resource, html, datapath, tidy_flags = [], options = {})
  @resource           = resource
  @html               = html
  @exceptions         = ''
  @datapath           = datapath
  @tidy_flags         = (HTMLValidation.default_tidy_flags + tidy_flags).uniq
  @options            = options
  valid?
end

Instance Attribute Details

#exceptionsObject

Returns the value of attribute exceptions.



6
7
8
# File 'lib/html_validation/html_validation_result.rb', line 6

def exceptions
  @exceptions
end

#htmlObject

Returns the value of attribute html.



6
7
8
# File 'lib/html_validation/html_validation_result.rb', line 6

def html
  @html
end

#optionsObject

Returns the value of attribute options.



6
7
8
# File 'lib/html_validation/html_validation_result.rb', line 6

def options
  @options
end

#resourceObject

Returns the value of attribute resource.



6
7
8
# File 'lib/html_validation/html_validation_result.rb', line 6

def resource
  @resource
end

Class Method Details

.load_from_files(filepath) ⇒ Object

takes a .url and loads the data into this object



34
35
36
37
38
# File 'lib/html_validation/html_validation_result.rb', line 34

def self.load_from_files(filepath)
  resource  = File.open("#{filepath}.resource.txt", 'r').read
  html      = File.open("#{filepath}.html.txt", 'r').read
 HTMLValidationResult.new(resource, html, filepath)
end

Instance Method Details

#accept!Object

Saves the exception string for the given url or file path. When next run, if the exception string is identical, valid? will return true. Note that #exceptions will still list the exception string, though, even if it is an accepted exception string.



56
57
58
# File 'lib/html_validation/html_validation_result.rb', line 56

def accept!
  File.open(data_path("accepted"), 'w') {|f| f.write(@exceptions) }
end

#reject!Object



60
61
62
63
64
# File 'lib/html_validation/html_validation_result.rb', line 60

def reject!
  if File.exists?(data_path("accepted"))
    File.delete data_path("accepted")
  end
end

#valid?Boolean

Validates an html string using html tidy. If there are no warnings or exceptions, or there is a previously accepted exception string that matches exactly, valid? returns true Line numbers of exceptions are likely to change with any edit, so our validation compares the exception strings with the lines and columns removed. Name can be a filename, file system path, or url, so long it is uniquely associated with the passed in html.

Returns:



45
46
47
48
49
50
51
# File 'lib/html_validation/html_validation_result.rb', line 45

def valid?
  @exceptions = validate
  File.delete(data_path("accepted")) if File.exists?(data_path("accepted")) if @exceptions == ''
  valid = (filter(@exceptions) == '' or accepted?(@exceptions))
  save_html_and_exceptions
  valid
end