RealityForge.org: Rails Plugin to Validate (X)HTML and CSS /articles/2006/03/15/rails-plugin-to-validate-x-html-and-css en-us 40 A little short for a storm trooper Rails Plugin to Validate (X)HTML and CSS <p>Here is an enahnced version of <a href="http://scottraymond.net">Scott Raymond&#8217;s</a> <a href="http://redgreenblu.com/svn/projects/assert_valid_markup/">assert_valid_markup</a> plugin that I use in my projects. Below are the directions for use;</p> <h3>HowTo Validate (X)HTML</h3> <div class="typocode"><pre><code class="typocode_ruby "><span class="comment"># Calling the assertion with no parameters validates </span> <span class="comment"># whatever is in @request.body, which is automatically </span> <span class="comment"># set by the existing get/post/etc helpers. For example:</span> <span class="keyword">class </span><span class="class">FooControllerTest</span> <span class="punct">&lt;</span> <span class="constant">Test</span><span class="punct">::</span><span class="constant">Unit</span><span class="punct">::</span><span class="constant">TestCase</span> <span class="keyword">def </span><span class="method">test_bar_markup</span> <span class="ident">get</span> <span class="symbol">:bar</span> <span class="ident">assert_valid_markup</span> <span class="keyword">end</span> <span class="keyword">end</span> <span class="comment"># Add a string parameter to the assertion to validate </span> <span class="comment"># any random fragment. For example:</span> <span class="keyword">class </span><span class="class">FooControllerTest</span> <span class="punct">&lt;</span> <span class="constant">Test</span><span class="punct">::</span><span class="constant">Unit</span><span class="punct">::</span><span class="constant">TestCase</span> <span class="keyword">def </span><span class="method">test_bar_markup</span> <span class="ident">assert_valid_markup</span> <span class="punct">&quot;</span><span class="string">&lt;div&gt;Hello, world.&lt;/div&gt;</span><span class="punct">&quot;</span> <span class="keyword">end</span> <span class="keyword">end</span> <span class="comment"># For the ultimate in convenience, use the class-level</span> <span class="comment"># method to validate a number of actions in one line. </span> <span class="keyword">class </span><span class="class">FooControllerTest</span> <span class="punct">&lt;</span> <span class="constant">Test</span><span class="punct">::</span><span class="constant">Unit</span><span class="punct">::</span><span class="constant">TestCase</span> <span class="ident">assert_valid_markup</span> <span class="symbol">:bar</span><span class="punct">,</span> <span class="symbol">:baz</span><span class="punct">,</span> <span class="symbol">:qux</span> <span class="keyword">end</span></code></pre></div> <h3>HowTo Validate <span class="caps">CSS</span></h3> <div class="typocode"><pre><code class="typocode_ruby "><span class="comment"># Pass a string parameter to the assertion to validate </span> <span class="comment"># a css fragment. For example:</span> <span class="keyword">class </span><span class="class">FooControllerTest</span> <span class="punct">&lt;</span> <span class="constant">Test</span><span class="punct">::</span><span class="constant">Unit</span><span class="punct">::</span><span class="constant">TestCase</span> <span class="keyword">def </span><span class="method">test_bar_css</span> <span class="ident">filename</span> <span class="punct">=</span> <span class="punct">&quot;</span><span class="string"><span class="expr">#{RAILS_ROOT}</span>/public/stylesheets/bar.css</span><span class="punct">&quot;</span> <span class="ident">assert_valid_css</span><span class="punct">(</span><span class="constant">File</span><span class="punct">.</span><span class="ident">open</span><span class="punct">(</span><span class="ident">filename</span> <span class="punct">,'</span><span class="string">rb</span><span class="punct">').</span><span class="ident">read</span><span class="punct">)</span> <span class="keyword">end</span> <span class="keyword">end</span> <span class="comment"># For the ultimate in convenience, use the class-level</span> <span class="comment"># method to validate a bunch of css files in one line.</span> <span class="comment"># Assumes that the CSS files are relative to </span> <span class="comment"># $RAILS_ROOT/public/stylesheets/ and end with '.css'. </span> <span class="comment"># The following example validates </span> <span class="comment"># $RAILS_ROOT/public/stylesheets/layout.css, </span> <span class="comment"># $RAILS_ROOT/public/stylesheets/standard.css and </span> <span class="comment"># $RAILS_ROOT/public/stylesheets/theme.css</span> <span class="keyword">class </span><span class="class">FooControllerTest</span> <span class="punct">&lt;</span> <span class="constant">Test</span><span class="punct">::</span><span class="constant">Unit</span><span class="punct">::</span><span class="constant">TestCase</span> <span class="ident">assert_valid_css_files</span> <span class="punct">'</span><span class="string">layout</span><span class="punct">',</span> <span class="punct">'</span><span class="string">standard</span><span class="punct">',</span> <span class="punct">'</span><span class="string">theme</span><span class="punct">'</span> <span class="keyword">end</span></code></pre></div> <p>Most of the credit for this plugin goes to Scott for the initial idea! The modifications that I made include;</p> <ul> <li>Validation of <span class="caps">CSS</span> files.</li> <li>Caching of fragments occurs in <code>$RAILS_ROOT/tmp</code> according to the name of the test class + test method. This avoids filling up the system temp folder with expired cache files.</li> <li>Ability to turn off validation by setting the &#8220;NONET&#8221; environment variable to &#8220;true&#8221;.</li> </ul> <p>You can grab it from subversion at;</p> <p><a href="http://www.realityforge.org/svn/code/assert-valid-asset/trunk/">http://www.realityforge.org/svn/code/assert-valid-asset/trunk/</a></p> <h4>Update 12th of May, 2008</h4> <p>Steve Sloan has also added a Git repo for the code which you can grab from:</p> <p><a href="http://github.com/CodeMonkeySteve/assert-valid-asset/tree/master">http://github.com/CodeMonkeySteve/assert-valid-asset/tree/master</a></p> Wed, 15 Mar 2006 19:18:00 +1100 urn:uuid:e93d2591-c8bb-4fe9-993e-f958140f5ad6 Peter Donald http://www.realityforge.org/articles/2006/03/15/rails-plugin-to-validate-x-html-and-css Rails rails plugin css html validate http://www.realityforge.org/articles/trackback/37 "Rails Plugin to Validate (X)HTML and CSS" by Peter Donald <p>Fixed, Thanks!</p> Thu, 16 Mar 2006 08:07:48 +1100 urn:uuid:99973183-6d3c-4e40-9ad2-d39e8cdbfb15 http://www.realityforge.org/articles/2006/03/15/rails-plugin-to-validate-x-html-and-css#comment-39 "Rails Plugin to Validate (X)HTML and CSS" by Steve Ross <p>In that last bit about the class method, you need to use:</p> <p>assert_valid_css_files &#8216;foo&#8217;, &#8216;bar&#8217;</p> Thu, 16 Mar 2006 03:48:21 +1100 urn:uuid:6502540d-9dd6-4d2f-882c-5bf978b1844e http://www.realityforge.org/articles/2006/03/15/rails-plugin-to-validate-x-html-and-css#comment-38