Question

The application I'm working on allows users to upload HTML templates with CSS, which the app then processes out for various tasks. The upload process works fine, but I'm trying to find a way to validate the HTML and CSS files that get uploaded. I've found several gems and plugins that are meant to be integrated into unit tests (such as https://github.com/realityforge/rails-assert-valid-asset), but I'm looking for something I can run the files against on upload (in a production environment).

Does something like this exist?

Thanks

Was it helpful?

Solution

You should be able to use the World Wide Web Consortium’s online validation service through the w3c_validators gem: https://github.com/alexdunae/w3c_validators .

Here's an example from the Readme for CSS:

require 'w3c_validators'

include W3CValidators

@validator = CSSValidator.new

results = @validator.validate_text('body { margin: 0px; }')

if results.errors.length > 0
  results.errors.each do |err|
    puts err.to_s
  end
else
  puts 'Valid!'
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top