Class: W3CValidators::CSSValidator

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

Constant Summary collapse

CSS_VALIDATOR_URI =
'https://jigsaw.w3.org/css-validator/validator'

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 = {}) ⇒ CSSValidator

Create a new instance of the CSSValidator.

Options

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

See Validator#new for proxy server options.



12
13
14
15
16
17
18
19
20
21
# File 'lib/w3c_validators/css_validator.rb', line 12

def initialize(options = {})
  if options[:validator_uri]
    @validator_uri = URI.parse(options[:validator_uri])
    options.delete(options[:validator_uri])
  else
    @validator_uri = URI.parse(CSS_VALIDATOR_URI)
  end
  options[:profile] ||= 'css3'
  super(options)
end

Instance Method Details

#set_language!(lang = 'en') ⇒ Object

The language used for the response.



50
51
52
# File 'lib/w3c_validators/css_validator.rb', line 50

def set_language!(lang = 'en')
  @options[:lang] = lang
end

#set_profile!(profile) ⇒ Object

The CSS profile used for the validation.

charset can be a string or a symbl from the W3CValidators::CSS_PROFILES hash.

Example

set_profile!('css1')
set_profile!(:css1)


30
31
32
33
34
35
36
37
38
39
# File 'lib/w3c_validators/css_validator.rb', line 30

def set_profile!(profile)
  if profile.kind_of?(Symbol)
    if CSS_PROFILES.has_key?(profile)
      profile = profile.to_s
    else
      return
    end
  end
  @options[:profile] = profile
end

#set_vendor_extension_warning!(level = 'Default') ⇒ Object

whether to treat presence of CSS vendor extension as error or merely a warning



55
56
57
58
59
# File 'lib/w3c_validators/css_validator.rb', line 55

def set_vendor_extension_warning!(level = 'Default')
  @options[:vextwarning] = nil     if level.to_s.downcase == 'default'
  @options[:vextwarning] = 'true'  if level.to_s.downcase == 'warnings'
  @options[:vextwarning] = 'false' if level.to_s.downcase == 'errors'
end

#set_warn_level!(level = 2) ⇒ Object

The warning level, no for no warnings, 0 for less warnings, 1or 2 for more warnings



42
43
44
45
46
47
# File 'lib/w3c_validators/css_validator.rb', line 42

def set_warn_level!(level = 2)
  warn_levels = ['0','1','2','no']
  return unless warn_levels.include?(level.to_s.downcase)

  @options[:warning] = level
end

#validate_file(file_path) ⇒ Object

Validate the CSS 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.



81
82
83
84
85
86
87
88
89
90
# File 'lib/w3c_validators/css_validator.rb', line 81

def validate_file(file_path)
  if file_path.respond_to? :read
    src = file_path.read
  else
    src = read_local_file(file_path)
  end 
  # we force the :post mode otherwise it fails for
  # big files
  return validate_text(src, :post)
end

#validate_text(text, request_mode = :get) ⇒ Object

Validate the CSS of a string.

Returns W3CValidators::Results.



71
72
73
# File 'lib/w3c_validators/css_validator.rb', line 71

def validate_text(text, request_mode = :get)
  return validate({:text => text}, request_mode)
end

#validate_uri(uri) ⇒ Object

Validate the CSS of an URI.

Returns W3CValidators::Results.



64
65
66
# File 'lib/w3c_validators/css_validator.rb', line 64

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