Skip to content

Commit

Permalink
Merge pull request rails#242 from sstephenson/replace-directive-with-…
Browse files Browse the repository at this point in the history
…empty-line

Preserve directive whitespace
  • Loading branch information
josh committed Nov 9, 2011
2 parents e0e0f68 + def9a81 commit 66ae1fc
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 28 deletions.
5 changes: 3 additions & 2 deletions lib/sprockets/directive_processor.rb
Expand Up @@ -103,9 +103,10 @@ def evaluate(context, locals, &block)
# Returns the header String with any directives stripped.
def processed_header
lineno = 0
@processed_header ||= header.lines.reject { |line|
@processed_header ||= header.lines.map { |line|
lineno += 1
directives.assoc(lineno)
# Replace directive line with a clean break
directives.assoc(lineno) ? "\n" : line
}.join.chomp
end

Expand Down
36 changes: 18 additions & 18 deletions test/test_asset.rb
Expand Up @@ -464,7 +464,7 @@ def setup
end

test "length" do
assert_equal 67, @asset.length
assert_equal 69, @asset.length
end

test "splat" do
Expand All @@ -476,21 +476,21 @@ def setup
end

test "to_s" do
assert_equal "\ndocument.on('dom:loaded', function() {\n $('search').focus();\n});\n", @asset.to_s
assert_equal "\n\n\ndocument.on('dom:loaded', function() {\n $('search').focus();\n});\n", @asset.to_s
end

test "each" do
body = ""
@asset.each { |part| body << part }
assert_equal "\ndocument.on('dom:loaded', function() {\n $('search').focus();\n});\n", body
assert_equal "\n\n\ndocument.on('dom:loaded', function() {\n $('search').focus();\n});\n", body
end

test "to_a" do
body = ""
@asset.to_a.each do |asset|
body << asset.body
end
assert_equal "\ndocument.on('dom:loaded', function() {\n $('search').focus();\n});\n", body
assert_equal "\n\n\ndocument.on('dom:loaded', function() {\n $('search').focus();\n});\n", body
end

test "asset is fresh if its mtime and contents are the same" do
Expand Down Expand Up @@ -556,21 +556,21 @@ def setup
end

test "length" do
assert_equal 157, @asset.length
assert_equal 159, @asset.length
end

test "to_s" do
assert_equal "var Project = {\n find: function(id) {\n }\n};\nvar Users = {\n find: function(id) {\n }\n};\n\ndocument.on('dom:loaded', function() {\n $('search').focus();\n});\n", @asset.to_s
assert_equal "var Project = {\n find: function(id) {\n }\n};\nvar Users = {\n find: function(id) {\n }\n};\n\n\n\ndocument.on('dom:loaded', function() {\n $('search').focus();\n});\n", @asset.to_s
end

test "each" do
body = ""
@asset.each { |part| body << part }
assert_equal "var Project = {\n find: function(id) {\n }\n};\nvar Users = {\n find: function(id) {\n }\n};\n\ndocument.on('dom:loaded', function() {\n $('search').focus();\n});\n", body
assert_equal "var Project = {\n find: function(id) {\n }\n};\nvar Users = {\n find: function(id) {\n }\n};\n\n\n\ndocument.on('dom:loaded', function() {\n $('search').focus();\n});\n", body
end

test "requiring the same file multiple times has no effect" do
assert_equal read("project.js")+"\n", asset("multiple.js").to_s
assert_equal read("project.js")+"\n\n\n", asset("multiple.js").to_s
end

test "requiring a file of a different format raises an exception" do
Expand Down Expand Up @@ -602,12 +602,12 @@ def setup
end

test "bundled asset body is just its own contents" do
assert_equal "\ndocument.on('dom:loaded', function() {\n $('search').focus();\n});\n",
assert_equal "\n\n\ndocument.on('dom:loaded', function() {\n $('search').focus();\n});\n",
asset("application.js").body
end

test "bundling joins files with blank line" do
assert_equal "var Project = {\n find: function(id) {\n }\n};\nvar Users = {\n find: function(id) {\n }\n};\n\ndocument.on('dom:loaded', function() {\n $('search').focus();\n});\n",
assert_equal "var Project = {\n find: function(id) {\n }\n};\nvar Users = {\n find: function(id) {\n }\n};\n\n\n\ndocument.on('dom:loaded', function() {\n $('search').focus();\n});\n",
asset("application.js").to_s
end

Expand All @@ -628,16 +628,16 @@ def setup
end

test "processing a source file with unknown extensions" do
assert_equal read("users.js") + "var jQuery;\n", asset("unknownexts.min.js").to_s
assert_equal read("users.js") + "var jQuery;\n\n\n", asset("unknownexts.min.js").to_s
end

test "processing a source file in compat mode" do
assert_equal read("project.js") + "\n" + read("users.js"),
assert_equal read("project.js") + "\n" + read("users.js") + "\n\n\n\n",
asset("compat.js").to_s
end

test "included dependencies are inserted after the header of the dependent file" do
assert_equal "# My Application\n" + read("project.js") + "\n\nhello();\n",
assert_equal "# My Application\n\n" + read("project.js") + "\n\nhello();\n",
asset("included_header.js").to_s
end

Expand All @@ -647,7 +647,7 @@ def setup
end

test "including a file with a relative path" do
assert_equal "// Included relatively\n" + read("project.js") + "\n\nhello();\n", asset("relative/include.js").to_s
assert_equal "// Included relatively\n\n" + read("project.js") + "\n\nhello();\n", asset("relative/include.js").to_s
end

test "can't require files outside the load path" do
Expand Down Expand Up @@ -679,7 +679,7 @@ def setup
test "require_tree requires all descendant files in alphabetical order" do
assert_equal(
asset("tree/all_with_require.js").to_s,
asset("tree/all_with_require_tree.js").to_s
asset("tree/all_with_require_tree.js").to_s + "\n\n\n\n\n\n"
)
end

Expand Down Expand Up @@ -723,7 +723,7 @@ def setup
end

test "require_self inserts the current file's body at the specified point" do
assert_equal "/* b.css */\n\nb { display: none }\n/*\n */\n.one {}\n\n\nbody {}\n.two {}\n.project {}\n", asset("require_self.css").to_s
assert_equal "/* b.css */\n\nb { display: none }\n/*\n\n\n\n\n\n */\n.one {}\n\n\nbody {}\n.two {}\n.project {}\n", asset("require_self.css").to_s
end

test "multiple require_self directives raises and error" do
Expand All @@ -745,7 +745,7 @@ def setup
end

test "unknown directives are ignored" do
assert_equal "var Project = {\n find: function(id) {\n }\n};\n//\n// = Foo\n//\n// == Examples\n//\n// Foo.bar()\n// => \"baz\"\nvar Foo;\n",
assert_equal "var Project = {\n find: function(id) {\n }\n};\n\n//\n// = Foo\n//\n// == Examples\n//\n// Foo.bar()\n// => \"baz\"\nvar Foo;\n",
asset("documentation.js").to_s
end

Expand Down Expand Up @@ -798,7 +798,7 @@ def setup
end

test "multiple charset defintions are stripped from css bundle" do
assert_equal "@charset \"UTF-8\";\n.foo {}\n\n.bar {}\n", asset("charset.css").to_s
assert_equal "@charset \"UTF-8\";\n.foo {}\n\n.bar {}\n\n\n", asset("charset.css").to_s
end

test "appends missing semicolons" do
Expand Down
12 changes: 6 additions & 6 deletions test/test_directive_processor.rb
Expand Up @@ -3,21 +3,21 @@
class DirectiveProcessorTest < Sprockets::TestCase
test "parsing double-slash comments" do
directive_parser("double_slash").tap do |parser|
assert_equal "// Header\n//\n//\n(function() {\n})();\n", parser.processed_source
assert_equal "// Header\n//\n\n\n//\n\n(function() {\n})();\n", parser.processed_source
assert_equal directives, parser.directives
end
end

test "parsing hash comments" do
directive_parser("hash").tap do |parser|
assert_equal "# Header\n#\n#\n(->)()\n", parser.processed_source
assert_equal "# Header\n#\n\n\n#\n\n(->)()\n", parser.processed_source
assert_equal directives, parser.directives
end
end

test "parsing slash-star comments" do
directive_parser("slash_star").tap do |parser|
assert_equal "/* Header\n *\n *\n */\n\n(function() {\n})();\n", parser.processed_source
assert_equal "/* Header\n *\n\n\n *\n\n */\n\n(function() {\n})();\n", parser.processed_source
assert_equal directives, parser.directives
end
end
Expand All @@ -31,7 +31,7 @@ class DirectiveProcessorTest < Sprockets::TestCase

test "parsing triple-hash comments" do
directive_parser("triple_hash").tap do |parser|
assert_equal "###\nHeader\n\n\n###\n\n(->)()\n", parser.processed_source
assert_equal "###\nHeader\n\n\n\n\n\n###\n\n(->)()\n", parser.processed_source
assert_equal directives(1), parser.directives
end
end
Expand All @@ -45,7 +45,7 @@ class DirectiveProcessorTest < Sprockets::TestCase

test "directives in comment after header are not parsed" do
directive_parser("directives_after_header").tap do |parser|
assert_equal "/*\n * Header\n */\n\n\n\n/* Not a directive */\n\n(function() {\n})();\n\n/*= require e */\n", parser.processed_source
assert_equal "/*\n * Header\n\n */\n\n\n\n\n\n\n/* Not a directive */\n\n(function() {\n})();\n\n/*= require e */\n", parser.processed_source
assert_equal [
[3, "require", "a"],
[6, "require", "b"],
Expand Down Expand Up @@ -90,7 +90,7 @@ class DirectiveProcessorTest < Sprockets::TestCase

test "documentation headers" do
directive_parser("documentation").tap do |parser|
assert_equal "//\n// = Foo\n//\n// == Examples\n//\n// Foo.bar()\n// => \"baz\"var Foo;\n", parser.processed_source
assert_equal "\n//\n// = Foo\n//\n// == Examples\n//\n// Foo.bar()\n// => \"baz\"var Foo;\n", parser.processed_source
assert_equal [[1, "require", "project"]], parser.directives
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/test_encoding.rb
Expand Up @@ -34,7 +34,7 @@ def setup

test "read ASCII + UTF-8 concatation asset" do
data = @env['ascii_utf8.js'].to_s
assert_equal "var foo = \"bar\";\nvar snowman = \"\342\230\203\";\n",
assert_equal "var foo = \"bar\";\nvar snowman = \"\342\230\203\";\n\n\n",
data
assert_equal Encoding.find('UTF-8'), data.encoding
end
Expand Down
2 changes: 1 addition & 1 deletion test/test_environment.rb
Expand Up @@ -122,7 +122,7 @@ def self.test(name, &block)
end

test "find index.css in directory" do
assert_equal ".c {}\n.d {}\n/*\n */\n\n", @env["mobile.css"].to_s
assert_equal ".c {}\n.d {}\n/*\n\n */\n\n", @env["mobile.css"].to_s
end

test "missing static path returns nil" do
Expand Down

0 comments on commit 66ae1fc

Please sign in to comment.