Skip to content

Commit

Permalink
Adds rails stylesheet and javascript tags back
Browse files Browse the repository at this point in the history
This will support work for adding SRI to our templates. This will let us add an 'integrity' attribute to both the stylesheet_link_tag and the javascript_include_tag.

What this does

It adds the ability to understand and convert stylesheet_link_tag and javascript_include_tag to html for the other languages in which our erb template is being compiled to (e.g. Django, Mustache, Liquid, Jinja)
  • Loading branch information
Elena Tanasoiu committed May 12, 2017
1 parent d2cf462 commit 155a2fb
Show file tree
Hide file tree
Showing 11 changed files with 183 additions and 6 deletions.
64 changes: 64 additions & 0 deletions build_tools/compiler/template_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,69 @@ def asset_path(file, options={})
def method_missing(name, *args)
puts "#{name} #{args.inspect}"
end

def stylesheet_link_tag(*sources)
options = stringify_keys(extract_options!(sources))
sources.uniq.map { |source|
link_options = {
"rel" => "stylesheet",
"media" => "screen",
"href" => asset_path(source)
}.merge!(options)
tag(:link, tag_options(link_options))
}.join("\n")
end

def javascript_include_tag(*sources)
options = stringify_keys(extract_options!(sources))
sources.uniq.map { |source|
script_options = {
"src" => asset_path(source)
}.merge!(options)
content_tag(:script, tag_options(script_options))
}.join("\n")
end

def content_tag(name, options = nil)
"<#{name}#{options}></#{name}>"
end

def tag(name, options)
"<#{name}#{options}/>"
end

def extract_options!(options)
if options.last.is_a?(Hash) && extractable_options?(options.last)
options.pop
else
{}
end
end

def extractable_options?(options)
options.instance_of?(Hash)
end

def stringify_keys(options)
result = {}
options.each_key do |key|
result[key.to_s] = options[key]
end
result
end

def tag_options(options)
return if options.empty?
output = "".dup
sep = " "
options.each_pair do |key, value|
if !value.nil?
output << sep
output << %(#{key}="#{value}")
end
end
output unless output.empty?
end

end
end
8 changes: 4 additions & 4 deletions source/views/layouts/govuk_template.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
<meta charset="utf-8" />
<title><%= content_for?(:page_title) ? yield(:page_title) : "GOV.UK - The best place to find government services and information" %></title>

<!--[if gt IE 8]><!--><link href="<%= asset_path "govuk-template.css" %>" media="screen" rel="stylesheet" /><!--<![endif]-->
<!--[if gt IE 8]><!--><%= stylesheet_link_tag "govuk-template.css" %><!--<![endif]-->
<!--[if IE 6]><link href="<%= asset_path "govuk-template-ie6.css" %>" media="screen" rel="stylesheet" /><![endif]-->
<!--[if IE 7]><link href="<%= asset_path "govuk-template-ie7.css" %>" media="screen" rel="stylesheet" /><![endif]-->
<!--[if IE 8]><link href="<%= asset_path "govuk-template-ie8.css" %>" media="screen" rel="stylesheet" /><![endif]-->
<link href="<%= asset_path "govuk-template-print.css" %>" media="print" rel="stylesheet" />
<%= stylesheet_link_tag "govuk-template-print.css", media: "print" %>

<!--[if IE 8]><link href="<%= asset_path "fonts-ie8.css" %>" media="all" rel="stylesheet" /><![endif]-->
<!--[if gte IE 9]><!--><link href="<%= asset_path "fonts.css" %>" media="all" rel="stylesheet" /><!--<![endif]-->
<!--[if lt IE 9]><script src="<%= asset_path "ie.js" %>"></script><![endif]-->
<!--[if gte IE 9]><!--><%= stylesheet_link_tag "fonts.css", media: "all" %><!--<![endif]-->
<!--[if lt IE 9]><%= javascript_include_tag "ie.js" %><![endif]-->

<link rel="shortcut icon" href="<%= asset_path 'favicon.ico' %>" type="image/x-icon" />
<%# the colour used for mask-icon is the standard palette $black from
Expand Down
1 change: 1 addition & 0 deletions spec/build_tools/compiler/django_processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def valid_sections
let(:file) {"some/file.erb"}
subject {described_class.new(file)}

it_behaves_like "a processor"

describe "#handle_yield" do
valid_sections.each do |key, content|
Expand Down
1 change: 1 addition & 0 deletions spec/build_tools/compiler/ejs_processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def valid_sections
let(:file) {"some/file.erb"}
subject {described_class.new(file)}

it_behaves_like "a processor"

describe "#handle_yield" do
valid_sections.each do |key, content|
Expand Down
1 change: 1 addition & 0 deletions spec/build_tools/compiler/jinja_processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def valid_sections
let(:file) {"some/file.erb"}
subject {described_class.new(file)}

it_behaves_like "a processor"

describe "#handle_yield" do
valid_sections.each do |key, content|
Expand Down
11 changes: 11 additions & 0 deletions spec/build_tools/compiler/liquid_processor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'spec_helper'
require File.join(PROJECT_ROOT, 'build_tools/compiler/liquid_processor.rb')

describe Compiler::LiquidProcessor do

let(:file) {"some/file.erb"}
subject {described_class.new(file)}

it_behaves_like "a processor"

end
11 changes: 11 additions & 0 deletions spec/build_tools/compiler/mustache_inheritance_processor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'spec_helper'
require File.join(PROJECT_ROOT, 'build_tools/compiler/mustache_inheritance_processor.rb')

describe Compiler::MustacheInheritanceProcessor do

let(:file) {"some/file.erb"}
subject {described_class.new(file)}

it_behaves_like "a processor"

end
3 changes: 1 addition & 2 deletions spec/build_tools/compiler/mustache_processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ def valid_sections
end

describe Compiler::MustacheProcessor do

let(:file) {"some/file.erb"}
subject {described_class.new(file)}

it_behaves_like "a processor"

describe "#handle_yield" do
valid_sections.each do |key, content|
Expand Down Expand Up @@ -64,5 +64,4 @@ def valid_sections
end
end
end

end
11 changes: 11 additions & 0 deletions spec/build_tools/compiler/plain_processor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'spec_helper'
require File.join(PROJECT_ROOT, 'build_tools/compiler/plain_processor.rb')

describe Compiler::PlainProcessor do

let(:file) {"some/file.erb"}
subject {described_class.new(file)}

it_behaves_like "a processor"

end
2 changes: 2 additions & 0 deletions spec/build_tools/compiler/play_processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def expected_parameter_names
describe Compiler::PlayProcessor do
subject { described_class.new("dummy filename") }

it_behaves_like "a processor"

describe "top_of_page" do
it "declares all of the template parameters" do
expected_parameter_names.each do |parameter_name|
Expand Down
76 changes: 76 additions & 0 deletions spec/support/examples/processor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
require 'spec_helper'
require 'set'

shared_examples_for "a processor" do
let(:html_erb_file) {"a/file.css"}
let(:processor) { described_class.new(html_erb_file) }

describe "convert rails tags into html" do

let(:css_source) { "govuk-template.css" }
let(:js_source) { "ie.js" }

describe "#stylesheet_link_tag" do
let(:css_options) { {"media" => "print"} }

it "should parse the stylesheet tag" do
expect(processor.stylesheet_link_tag(css_source)).to eql("<link rel=\"stylesheet\" media=\"screen\" href=\"#{processor.asset_path(css_source)}\"/>")
end

context "if css file is for print" do
it "should parse the stylesheet tag for print" do
expect(processor.stylesheet_link_tag(css_source, css_options)).to eql("<link rel=\"stylesheet\" media=\"print\" href=\"#{processor.asset_path(css_source)}\"/>")
end
end
end

describe "#javascript_include_tag" do
let(:js_options) { {"charset" => "UTF-8"} }

it "should parse the javascript tag" do
expect(processor.javascript_include_tag(js_source)).to eql("<script src=\"#{processor.asset_path(js_source)}\"></script>")
end

it "should parse the javascript tag" do
expect(processor.javascript_include_tag(js_source, js_options)).to eql("<script src=\"#{processor.asset_path(js_source)}\" charset=\"UTF-8\"></script>")
end
end

describe "#content_tag" do
it "should return the correct html script tag" do
expect(processor.content_tag(:script, " src=\"#{processor.asset_path(js_source)}\"")).to eql("<script src=\"#{processor.asset_path(js_source)}\"></script>")
end
end

describe "#tag" do
it "should return the correct html link tag" do
expect(processor.tag(:link, " rel=\"stylesheet\" media=\"screen\" href=\"#{processor.asset_path(css_source)}\"")).to eql("<link rel=\"stylesheet\" media=\"screen\" href=\"#{processor.asset_path(css_source)}\"/>")
end
end

describe "#extract_options!" do
let(:options) { ["govuk-template.css", {"media" => "print"}]}

it "should extract the last part of the options" do
expect(processor.extract_options!(options)).to eql({"media" => "print"})
end
end

describe "#stringify_keys" do
let(:options) { {:media => "print"} }

it "should turn keys of a hash into strings" do
expect(processor.stringify_keys(options)).to eql({"media"=>"print"})
end
end

describe "#tag_options" do
let(:options) { {"rel"=>"stylesheet", "media"=>"screen", "href"=>processor.asset_path(css_source)} }

it "should parse the hash" do
expect(processor.tag_options(options)).to eql(" rel=\"stylesheet\" media=\"screen\" href=\"#{processor.asset_path(css_source)}\"")
end
end

end
end

0 comments on commit 155a2fb

Please sign in to comment.