简体   繁体   中英

Ruby on Rails: Validate HTML and CSS on file upload

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM