w3c-validators / w3c_validators Goto Github PK
View Code? Open in Web Editor NEWA Ruby wrapper for the World Wide Web Consortium’s online validation services.
Home Page: http://code.dunae.ca/w3c_validators/
License: Other
A Ruby wrapper for the World Wide Web Consortium’s online validation services.
Home Page: http://code.dunae.ca/w3c_validators/
License: Other
This gem uses the online W3C validator. That's great! But:
How about using the offline version of the same tool?
I cannot validate any CSS. The following code:
require 'w3c_validators'
include W3CValidators
@validator = CSSValidator.new
results = @validator.validate_uri('http://www.csszengarden.com/examples/style.css')
if results.errors.length > 0
results.errors.each do |err|
puts err.to_s
end
else
puts 'Valid!'
end
results in the following error:
/Users/me/.rubies/ruby-2.3.1/lib/ruby/2.3.0/net/http/response.rb:120:in `error!': 500"Internal Server Error" (Net::HTTPFatalError)
from /Users/me/.rubies/ruby-2.3.1/lib/ruby/2.3.0/net/http/response.rb:129:in `value'
from /Users/me/.gem/ruby/2.3.1/gems/w3c_validators-1.2/lib/w3c_validators/validator.rb:88:in `send_request'
from /Users/me/.gem/ruby/2.3.1/gems/w3c_validators-1.2/lib/w3c_validators/css_validator.rb:87:in `validate'
from /Users/me/.gem/ruby/2.3.1/gems/w3c_validators-1.2/lib/w3c_validators/css_validator.rb:58:in `validate_uri'
from validate.rb:7:in `<main>'
is is possible to pass an options hash to validator to filter out errors that aren't relevant to your use case?
e.g. if I don't care about missing alt
attributes on <img>
tags, can I ask w3c_validator to ignore errors of type `Required attribute 'X' not specified'?
check https://validator.nu/ it returns 502.
Currently, the W3C validation service seems to be down (error 503 when trying to validate a page using the web form or the gem). Hence, the gem cannot validate any page (which is okay).
However, this also uncovered use of a deprecated constant:
…/lib/w3c_validators/validator.rb:177: warning: constant Net::HTTPServerException is deprecated
At the time of writing, It is not possible to validate files with vendor-prefixes using this (excellent) tool. For this to happen, the css validator must be extended with the ability to set "Vendor Extensions" as described in the link below.
The feed validator class is failing to properly validate RSS files due to the W3C website dropping support for HTTP requests:
POSTing any kind of data to http://validator.w3.org/feed/check.cgi results in a 301 Moved Permanently
(even when sending a valid RSS feed file):
➜ ~ cat sample_feed.rss
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>W3Schools Home Page</title>
<link>https://www.w3schools.com</link>
<description>Free web building tutorials</description>
<item>
<title>RSS Tutorial</title>
<link>https://www.w3schools.com/xml/xml_rss.asp</link>
<description>New RSS tutorial on W3Schools</description>
</item>
<item>
<title>XML Tutorial</title>
<link>https://www.w3schools.com/xml</link>
<description>New XML tutorial on W3Schools</description>
</item>
</channel>
</rss>
➜ ~ gem list w3c
*** LOCAL GEMS ***
w3c_validators (1.3.6)
➜ ~ irb
irb(main):001:0> require 'w3c_validators'
=> true
irb(main):002:0> v = W3CValidators::FeedValidator.new
=> #<W3CValidators::FeedValidator:0x000055c69221b598 @validator_uri=#<URI::HTTP http://validator.w3.org/feed/check.cgi>, @options={:proxy_host=>nil, :proxy_po...
irb(main):003:0> v.validate_file("sample_feed.rss")
Traceback (most recent call last):
11: from /usr/bin/irb:23:in `<main>'
10: from /usr/bin/irb:23:in `load'
9: from /usr/lib/ruby/gems/2.7.0/gems/irb-1.2.6/exe/irb:11:in `<top (required)>'
8: from (irb):3
7: from /var/lib/gems/2.7.0/gems/w3c_validators-1.3.6/lib/w3c_validators/feed_validator.rb:48:in `validate_file'
6: from /var/lib/gems/2.7.0/gems/w3c_validators-1.3.6/lib/w3c_validators/feed_validator.rb:33:in `validate_text'
5: from /var/lib/gems/2.7.0/gems/w3c_validators-1.3.6/lib/w3c_validators/feed_validator.rb:59:in `validate'
4: from /var/lib/gems/2.7.0/gems/w3c_validators-1.3.6/lib/w3c_validators/validator.rb:110:in `send_request'
3: from /var/lib/gems/2.7.0/gems/w3c_validators-1.3.6/lib/w3c_validators/validator.rb:113:in `send_request'
2: from /usr/lib/ruby/2.7.0/net/http/response.rb:133:in `value'
1: from /usr/lib/ruby/2.7.0/net/http/response.rb:124:in `error!'
Net::HTTPRetriableError (301 "Moved Permanently")
Upon further inspection, it became clear that the method send_request
is at fault:
options[:url]
containing the new URI is never used by the following call to send_request
: it only uses @validator_uri
, which still contains the old URI subject to redirection. A simple fix would be to replace this line by something like @validator_uri = URI.parse(response['location'])
to update the instance variable.I also noticed a probably poorly named variable:
not following_redirect
become just following_redirect
to actually follow redirects. The default boolean value used in the method signature would also need to become true
to default to following redirects, if that is the desired behaviour.While on 1.0.2 it works fine, now with 1.1.1 I can't see the full error messages, they get truncated. For example:
MarkupValidator.new.validate_uri('http://ryanair.com').errors.first.to_s
this will output on 1.1.1:
"ERROR; URI: http://www.ryanair.com/en; line "
while on 1.0.2 it will be fine:
"ERROR; URI: http://www.ryanair.com/en; line 98: general entity \"Bs\" not defined and no default entity"
I've tried this on ruby 1.8.6 and ruby 1.9.2, same results.
Thanks!
What was previously known as json, a gem used here, is now called json_pure.
And the json gem was changed be a high-performance binary module.
Don't we want high performance? Well we don't use a ton of JSON processing here, so switching to the json_pure package will be easier for people to use. For example, as current w3c_validators cannot be used on Apple+DevContainers platforms, discussed at rubygems/rubygems#6212
Caused by the API returning HTTP 400 Bad Request. Calling #validate_uri
makes a successful request.
W3CValidators::ValidatorUnavailable: unable to connect to the validator at https://validator.w3.org/nu/ (response was 400 "Bad request").
from ~/.gem/ruby/2.4.1/gems/w3c_validators-1.3.3/lib/w3c_validators/validator.rb:116:in `rescue in send_request'
from ~/.gem/ruby/2.4.1/gems/w3c_validators-1.3.3/lib/w3c_validators/validator.rb:43:in `send_request'
from ~/.gem/ruby/2.4.1/gems/w3c_validators-1.3.3/lib/w3c_validators/nu_validator.rb:68:in `validate'
from ~/.gem/ruby/2.4.1/gems/w3c_validators-1.3.3/lib/w3c_validators/nu_validator.rb:34:in `validate_uri'
Executing the sample code from the readme produces a traceback if pkg-config isn't installed. pkg-config isn't listed as a dependency, at least on this package's rubygems page.
The validator gem can fail withNet::HTTPRetriableError: 302 "Found"
.
require 'w3c_validators'
File.write('lol.html', '<!DOCTYPE html><html><head><meta charset="utf-8"><title>Hello</title></head><body><h1>Hi!</h1></body>')
::W3CValidators::MarkupValidator.new.validate_file('lol.html')
Error:
Net::HTTPRetriableError: 302 "Found"
from /usr/local/Cellar/ruby/2.3.3/lib/ruby/2.3.0/net/http/response.rb:120:in `error!'
from /usr/local/Cellar/ruby/2.3.3/lib/ruby/2.3.0/net/http/response.rb:129:in `value'
from /usr/local/lib/ruby/gems/2.3.0/gems/w3c_validators-1.3/lib/w3c_validators/validator.rb:93:in `send_request'
from /usr/local/lib/ruby/gems/2.3.0/gems/w3c_validators-1.3/lib/w3c_validators/validator.rb:90:in `send_request'
from /usr/local/lib/ruby/gems/2.3.0/gems/w3c_validators-1.3/lib/w3c_validators/markup_validator.rb:131:in `validate'
from /usr/local/lib/ruby/gems/2.3.0/gems/w3c_validators-1.3/lib/w3c_validators/markup_validator.rb:117:in `validate_file'
from (irb):6
from /usr/local/bin/irb:11:in `<main>'
w3c_validators
1.3Hi Alex,
Just found a small error in your README document.
The document gives the proxy example as:
validator = MarkupValidator.new(:proxy_server => 'proxy.example.com',
:proxy_port => 80,
:proxy_user => 'optional',
:proxy_pass => 'optional')
Whereas looking at the code this should be "proxy_host" instead of "proxy_server":
validator = MarkupValidator.new(:proxy_host => 'proxy.example.com',
:proxy_port => 80,
:proxy_user => 'optional',
:proxy_pass => 'optional')
Trying to update to Rails 4.2 while using w3c_validators results in this error:
Bundler could not find compatible versions for gem "nokogiri":
In Gemfile:
rails (~> 4.2.0) ruby depends on
actionpack (= 4.2.0) ruby depends on
rails-dom-testing (>= 1.0.5, ~> 1.0) ruby depends on
nokogiri (~> 1.6.0) ruby
w3c_validators (= 1.2) ruby depends on
nokogiri (1.5.11)
Seems not to work with 1.9.2? I'm using via Nanoc. Ok with 1.8.6:
rake aborted!
502 "Proxy Error"
V:/x/Ruby-192-p0/lib/ruby/1.9.1/net/http.rb:2295:in error!' V:/x/Ruby-192-p0/lib/ruby/1.9.1/net/http.rb:2304:in
value'
V:/x/Ruby-192-p0/lib/ruby/gems/1.9.1/gems/alexdunae-w3c_validators-1.0.0/lib/w3c_validators/validator.rb:57:in send_request' V:/x/Ruby-192-p0/lib/ruby/gems/1.9.1/gems/alexdunae-w3c_validators-1.0.0/lib/w3c_validators/markup_validator.rb:129:in
validate'
V:/x/Ruby-192-p0/lib/ruby/gems/1.9.1/gems/alexdunae-w3c_validators-1.0.0/lib/w3c_validators/markup_validator.rb:115:in `validate_file'
I'm getting a W3CValidators::ParsingError: unable to parse the response from the validator.
when trying to use the NuValidator like so:
validator = ::W3CValidators::NuValidator.new
results = validator.validate_file(file)
file
being a temp file containing HTML5 markup. I also tried validate_text, but got the very same error.
Details of ParsingError raised:
# --- Caused by: ---
# JSON::ParserError:
# 757: unexpected token at '<!DOCTYPE html>
# <html lang="en"><head><link href="icon.png" rel="icon"><link href="style.css" rel="stylesheet"><title>(X)HTML5 validation results</title></head><body><h1>(X)HTML5 validation results</h1><form method="get" class="simple">....
I already checked the request sent to validator.nu, which contains the out=json
parameter, so should be fine. Any idea what's wrong with the service?
Thanks so much for providing this awesome gem, btw. Looking forward to get this to work properly soon :)
It looks like w3.org is now redirecting.
validator.validate_text(rendered)
Net::HTTPRetriableError: 302 "Found"
W3CValidators::NuValidator.new.validate_text('<html>')
Regular output.
Net::OpenTimeout: execution expired
from /usr/local/Cellar/ruby/2.4.0/lib/ruby/2.4.0/net/http.rb:904:in `initialize'
from /usr/local/Cellar/ruby/2.4.0/lib/ruby/2.4.0/net/http.rb:904:in `open'
from /usr/local/Cellar/ruby/2.4.0/lib/ruby/2.4.0/net/http.rb:904:in `block in connect'
from /usr/local/Cellar/ruby/2.4.0/lib/ruby/2.4.0/timeout.rb:103:in `timeout'
from /usr/local/Cellar/ruby/2.4.0/lib/ruby/2.4.0/net/http.rb:902:in `connect'
from /usr/local/Cellar/ruby/2.4.0/lib/ruby/2.4.0/net/http.rb:887:in `do_start'
from /usr/local/Cellar/ruby/2.4.0/lib/ruby/2.4.0/net/http.rb:876:in `start'
from /usr/local/Cellar/ruby/2.4.0/lib/ruby/2.4.0/net/http.rb:1407:in `request'
from /usr/local/Cellar/ruby/2.4.0/lib/ruby/2.4.0/net/http.rb:1361:in `request_post'
from /usr/local/lib/ruby/gems/2.4.0/gems/w3c_validators-1.3.2/lib/w3c_validators/validator.rb:102:in `send_request'
from /usr/local/lib/ruby/gems/2.4.0/gems/w3c_validators-1.3.2/lib/w3c_validators/nu_validator.rb:72:in `validate'
from /usr/local/lib/ruby/gems/2.4.0/gems/w3c_validators-1.3.2/lib/w3c_validators/nu_validator.rb:42:in `validate_text'
from (irb):2
from /usr/local/bin/irb:11:in `<main>'
MarkupValidator
works fine.This gem does not have a licenses in its gemspec: https://github.com/w3c-validators/w3c_validators/blob/b227ae81a861fbdb92dbe04c795979badb0fab19/w3c_validators.gemspec
This would be good to have for organisations that need to verify the licences of the tools they use!
I can see the license file, but we track licences automatically driven from the gemspec using this: https://github.com/pivotal/LicenseFinder
Looks like it has been a while since the last release. There have been a couple important bug fixes since 1.1.1 -- is there any chance that you can push a new release.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.