Class: W3CValidators::MarkupValidator

Inherits:
Validator
  • Object
show all
Defined in:
lib/w3c_validators/markup_validator.rb

Constant Summary collapse

MARKUP_VALIDATOR_URI =
'https://validator.w3.org/check'

Constants inherited from Validator

Validator::HEAD_ERROR_COUNT_HEADER, Validator::HEAD_STATUS_HEADER, Validator::SOAP_OUTPUT_PARAM, Validator::USER_AGENT

Instance Attribute Summary

Attributes inherited from Validator

#results, #validator_uri

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MarkupValidator

Create a new instance of the MarkupValidator.

Options

The options hash allows you to set request parameters (see validator.w3.org/docs/api.html#requestformat) quickly. Request parameters can also be set using set_charset!, set_debug! and set_doctype!.

You can pass in your own validator’s URI (i.e. MarkupValidator.new(:validator_uri => 'http://localhost/check')).

See Validator#new for proxy server options.



16
17
18
19
20
21
22
23
24
25
# File 'lib/w3c_validators/markup_validator.rb', line 16

def initialize(options = {})
  puts "The MarkupValidator class is deprecated (cf. https://validator.w3.org/docs/obsolete-api.html) as it cannot process HTML5 documents.\nPlease prefer the NuValidator class instead."
  if options[:validator_uri]
    @validator_uri = URI.parse(options[:validator_uri])
    options.delete(options[:validator_uri])
  else
    @validator_uri = URI.parse(MARKUP_VALIDATOR_URI)
  end
  super(options)
end

Instance Method Details

#set_charset!(charset, only_as_fallback = false) ⇒ Object

Specify the character encoding to use when parsing the document.

When only_as_fallback is true, the given encoding will only be used as a fallback value, in case the charset is absent or unrecognized.

charset can be a string (e.g. set_charset!('utf-8')) or a symbol (e.g. set_charset!(:utf_8)) from the W3CValidators::CHARSETS hash.

Has no effect when using validate_uri_quickly.



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/w3c_validators/markup_validator.rb', line 37

def set_charset!(charset, only_as_fallback = false)
  if charset.kind_of?(Symbol)
    if CHARSETS.has_key?(charset)
      charset = CHARSETS[charset]
    else
      return
    end
  end
  @options[:charset] = charset
  @options[:fbc] = only_as_fallback
end

#set_debug!(debug = true) ⇒ Object

When set the validator will output some extra debugging information on the validated resource (such as HTTP headers) and validation process (such as parser used, parse mode, etc.).

Debugging information is stored in the Results debug_messages hash. Custom debugging messages can be set with Results#add_debug_message.

Has no effect when using validate_uri_quickly.



80
81
82
# File 'lib/w3c_validators/markup_validator.rb', line 80

def set_debug!(debug = true)
  @options[:debug] = debug
end

#set_doctype!(doctype, only_as_fallback = false) ⇒ Object

Specify the Document Type (DOCTYPE) to use when parsing the document.

When only_as_fallback is true, the given document type will only be used as a fallback value, in case the document’s DOCTYPE declaration is missing or unrecognized.

doctype can be a string (e.g. set_doctype!('HTML 3.2')) or a symbol (e.g. set_doctype!(:html32)) from the W3CValidators::DOCTYPES hash.

Has no effect when using validate_uri_quickly.



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/w3c_validators/markup_validator.rb', line 60

def set_doctype!(doctype, only_as_fallback = false)
  if doctype.kind_of?(Symbol)
    if DOCTYPES.has_key?(doctype)
      doctype = DOCTYPES[doctype]
    else
      return
    end
  end
  @options[:doctype] = doctype
  @options[:fbd] = only_as_fallback
end

#validate_file(file_path) ⇒ Object

Validate the markup of a local file.

file_path may be either the fully-expanded path to the file or an IO object (like File).

Returns W3CValidators::Results.



111
112
113
114
115
116
117
118
119
# File 'lib/w3c_validators/markup_validator.rb', line 111

def validate_file(file_path)
  if file_path.respond_to? :read
    src = file_path.read
  else
    src = read_local_file(file_path)
  end

  return validate({:uploaded_file => src, :file_path => file_path}, false)
end

#validate_text(text) ⇒ Object

Validate the markup of a string.

Returns W3CValidators::Results.



101
102
103
# File 'lib/w3c_validators/markup_validator.rb', line 101

def validate_text(text)
  return validate({:fragment => text}, false)
end

#validate_uri(uri) ⇒ Object

Validate the markup of an URI using a SOAP request.

Returns W3CValidators::Results.



87
88
89
# File 'lib/w3c_validators/markup_validator.rb', line 87

def validate_uri(uri)
  return validate({:uri => uri}, false)
end

#validate_uri_quickly(uri) ⇒ Object

Validate the markup of an URI using a HEAD request.

Returns W3CValidators::Results with an error count, not full error messages.



94
95
96
# File 'lib/w3c_validators/markup_validator.rb', line 94

def validate_uri_quickly(uri)
  return validate({:uri => uri}, true)
end