Skip to content
This repository
Browse code

Add Jasmine specs for the hamlcoffee helpers.

Interestingly whether `preserve` nor `findAndPreserve` have ever
worked as intended.
  • Loading branch information...
commit c9e63507d671136f01e48be3521ecd8b097c95af 1 parent f583a27
Michael Kessler authored
2  .travis.yml
@@ -7,6 +7,8 @@ rvm:
7 7 branches:
8 8 only:
9 9 - master
  10 +script: 'rake spec'
  11 +after_script: 'bundle exec guard-jasmine'
10 12 notifications:
11 13 recipients:
12 14 - michi@netzpiraten.ch
31 Gemfile
@@ -14,3 +14,34 @@ end
14 14 platform :jruby do
15 15 gem 'therubyrhino'
16 16 end
  17 +
  18 +group :assets do
  19 + gem 'coffee-script'
  20 +end
  21 +
  22 +group :development, :test do
  23 + gem 'actionpack', '~> 3.2'
  24 + gem 'railties', '~> 3.2'
  25 + gem 'tzinfo'
  26 +
  27 + gem 'thin'
  28 + gem 'jasminerice'
  29 + gem 'jquery-rails'
  30 +
  31 + gem 'guard-jasmine'
  32 + gem 'guard-rspec'
  33 +
  34 + gem 'rake'
  35 + gem 'pry'
  36 +
  37 + gem 'jasminerice'
  38 + gem 'rspec'
  39 +
  40 + gem 'guard-rspec'
  41 + gem 'guard-jasmine'
  42 + gem 'rb-fsevent'
  43 + gem 'ruby_gntp'
  44 +
  45 + gem 'yard'
  46 + gem 'redcarpet'
  47 +end
6 Guardfile
... ... @@ -1,5 +1,11 @@
1 1 # coding: UTF-8
2 2
  3 +guard :jasmine, :server => :thin do
  4 + watch(%r{spec/javascripts/spec\.(js\.coffee|js|coffee)$}) { "spec/javascripts" }
  5 + watch(%r{spec/javascripts/.+_spec\.(js\.coffee|js|coffee)$})
  6 + watch(%r{vendor/assets/javascripts/(.+?)\.(js\.coffee\.erb|js\.coffee|js|coffee)$}) { |m| "spec/javascripts/#{m[1]}_spec.#{m[2]}" }
  7 +end
  8 +
3 9 guard 'rspec' do
4 10 watch(%r{^spec/.+_spec\.rb$})
5 11 watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{ m[1] }_spec.rb" }
4 README.md
Source Rendered
@@ -355,9 +355,9 @@ You can see the [default implementation](https://github.com/netzpirat/haml_coffe
355 355 and the [Haml Coffee documentation](https://github.com/9elements/haml-coffee#custom-helper-function-compiler-options)
356 356 for more information about each helper function.
357 357
358   -## Shameless self promotion
  358 +## Author
359 359
360   -Developed by Michael Kessler, sponsored by [mksoft.ch](https://mksoft.ch).
  360 +Developed by Michael Kessler, [mksoft.ch](https://mksoft.ch).
361 361
362 362 If you like Haml Coffee Assets, you can watch the repository at [GitHub](https://github.com/netzpirat/haml_coffee_assets) and
363 363 follow [@netzpirat](https://twitter.com/#!/netzpirat) on Twitter for project updates.
21 config.ru
... ... @@ -0,0 +1,21 @@
  1 +require 'rails'
  2 +require 'rails/all'
  3 +require 'jasminerice'
  4 +require 'sprockets/railtie'
  5 +require 'jquery-rails'
  6 +require 'haml_coffee_assets'
  7 +
  8 +class HamlCoffeeAssetsJasmineTest < Rails::Application
  9 + routes.append do
  10 + mount Jasminerice::Engine => '/jasmine'
  11 + end
  12 +
  13 + config.cache_classes = true
  14 + config.active_support.deprecation = :log
  15 + config.assets.enabled = true
  16 + config.assets.version = '1.0'
  17 + config.secret_token = '9696be98e32a5f213730cb7ed6161c79'
  18 +end
  19 +
  20 +HamlCoffeeAssetsJasmineTest.initialize!
  21 +run HamlCoffeeAssetsJasmineTest
10 haml_coffee_assets.gemspec
@@ -19,14 +19,6 @@ Gem::Specification.new do |s|
19 19 s.add_runtime_dependency "execjs", ">= 1.2.9"
20 20 s.add_runtime_dependency "tilt", ">= 1.3.3"
21 21 s.add_runtime_dependency "sprockets", ">= 2.0.3"
22   -
  22 +
23 23 s.add_development_dependency 'bundler'
24   -
25   - s.add_development_dependency 'railties', '>= 3.1'
26   - s.add_development_dependency 'rspec'
27   - s.add_development_dependency 'guard-rspec'
28   - s.add_development_dependency 'yard'
29   - s.add_development_dependency 'redcarpet'
30   - s.add_development_dependency 'pry'
31   - s.add_development_dependency 'rake'
32 24 end
2  lib/haml_coffee_assets/engine.rb
@@ -18,7 +18,7 @@ class Engine < Rails::Engine
18 18 :cleanValue => true,
19 19 :customHtmlEscape => 'HAML.escape',
20 20 :customCleanValue => 'HAML.cleanValue',
21   - :customPreserve => 'HAML.escape',
  21 + :customPreserve => 'HAML.preserve',
22 22 :customFindAndPreserve => 'HAML.findAndPreserve',
23 23 :customSurround => 'HAML.surround',
24 24 :customSucceed => 'HAML.succeed',
101 spec/javascripts/hamlcoffee_spec.js.coffee
... ... @@ -0,0 +1,101 @@
  1 +describe 'HAML', ->
  2 + describe '.escape', ->
  3 + it 'escapes an ampersand', ->
  4 + expect(HAML.escape('house & garden')).toEqual 'house &amp; garden'
  5 +
  6 + it 'escapes the less than sign', ->
  7 + expect(HAML.escape('money < fun')).toEqual 'money &lt; fun'
  8 +
  9 + it 'escapes the greater than sign', ->
  10 + expect(HAML.escape('love > hate')).toEqual 'love &gt; hate'
  11 +
  12 + it 'escapes a double quote', ->
  13 + expect(HAML.escape('Michael "netzpirat" Kessler')).toEqual 'Michael &quot;netzpirat&quot; Kessler'
  14 +
  15 + describe '.cleanValue', ->
  16 + it 'returns an empty string for null', ->
  17 + expect(HAML.cleanValue(null)).toEqual ''
  18 +
  19 + it 'returns an empty string for undefined', ->
  20 + expect(HAML.cleanValue(undefined)).toEqual ''
  21 +
  22 + it 'passes everything else unchanged', ->
  23 + expect(HAML.cleanValue('Still the same')).toEqual 'Still the same'
  24 +
  25 + describe '.extend', ->
  26 + it 'extends the given object from a source object', ->
  27 + object = HAML.extend {}, { a: 1, b: 2 }
  28 +
  29 + expect(object.a).toEqual 1
  30 + expect(object.b).toEqual 2
  31 +
  32 + it 'extends the given object from multipe source objects', ->
  33 + object = HAML.extend {}, { a: 1 }, { b: 2 }, { c: 3, d: 4 }
  34 +
  35 + expect(object.a).toEqual 1
  36 + expect(object.b).toEqual 2
  37 + expect(object.c).toEqual 3
  38 + expect(object.d).toEqual 4
  39 +
  40 + it 'overwrites existing properties', ->
  41 + object = HAML.extend {}, { a: 1 }, { b: 2 }, { a: 2, b: 4 }
  42 +
  43 + expect(object.a).toEqual 2
  44 + expect(object.b).toEqual 4
  45 +
  46 + describe '.globals', ->
  47 + it 'retuns the global object', ->
  48 + expect(HAML.globals).toEqual Object(HAML.globals)
  49 +
  50 + it 'returns an empty object', ->
  51 + expect(Object.keys(HAML.globals).length).toEqual 0
  52 +
  53 + describe '.context', ->
  54 + it 'merges the locals into the globals', ->
  55 + spyOn(HAML, 'globals').andCallFake -> { b: 2, d: 4 }
  56 + context = HAML.context({ a: 1, c: 3 })
  57 +
  58 + expect(context.a).toEqual 1
  59 + expect(context.b).toEqual 2
  60 + expect(context.c).toEqual 3
  61 + expect(context.d).toEqual 4
  62 +
  63 + describe '.preserve', ->
  64 + it 'preserves all newlines', ->
  65 + expect(HAML.preserve("Newlines\nall\nthe\nway\n")).toEqual "Newlines&#x000A;all&#x000A;the&#x000A;way&#x000A;"
  66 +
  67 + describe '.findAndPreserve', ->
  68 + it 'replaces newlines within the preserved tags', ->
  69 + expect(HAML.findAndPreserve("""
  70 + <pre>
  71 + This will
  72 + be preserved
  73 + </pre>
  74 + <p>
  75 + This will not
  76 + be preserved
  77 + </p>
  78 + <textarea>
  79 + This will
  80 + be preserved
  81 + </textarea>
  82 + """)).toEqual """
  83 + <pre>&#x000A; This will&#x000A; be preserved&#x000A;</pre>
  84 + <p>
  85 + This will not
  86 + be preserved
  87 + </p>
  88 + <textarea>&#x000A; This will&#x000A; be preserved&#x000A;</textarea>
  89 + """
  90 +
  91 + describe '.surround', ->
  92 + it 'surrounds the text to the function result', ->
  93 + expect(HAML.surround('Prefix', 'Suffix', -> '<p>text</p>')).toEqual 'Prefix<p>text</p>Suffix'
  94 +
  95 + describe '.succeed', ->
  96 + it 'appends the text to the function result', ->
  97 + expect(HAML.succeed('Suffix', -> '<p>text</p>')).toEqual '<p>text</p>Suffix'
  98 +
  99 + describe '.precede', ->
  100 + it 'prepends the text to the function result', ->
  101 + expect(HAML.precede('Prefix', -> '<p>text</p>')).toEqual 'Prefix<p>text</p>'
3  spec/javascripts/spec.js.coffee
... ... @@ -0,0 +1,3 @@
  1 +#= require hamlcoffee
  2 +#= require jquery
  3 +#= require_tree .
215 vendor/assets/javascripts/hamlcoffee.js.coffee.erb
@@ -2,120 +2,101 @@
2 2
3 3 # HAML Coffee namespace
4 4 #
5   -window.HAML ||= {}
6   -
7   -# HAML Coffee html escape function
8   -#
9   -# @param text [String] the text to escape
10   -# @return [String] the escaped text
11   -#
12   -window.HAML.escape ||= (text) ->
13   - ("" + text)
14   - .replace(/&/g, "&amp;")
15   - .replace(/</g, "&lt;")
16   - .replace(/>/g, "&gt;")
17   - .replace(/"/g, "&quot;")
18   -
19   -# HAML Coffee clean value function
20   -#
21   -# @param text [String] the text to be cleaned
22   -# @return [String] the cleaned text
23   -#
24   -window.HAML.cleanValue ||= (text) ->
25   - if text is null or text is undefined then '' else text
26   -
27   -# HAML Coffee extend function.
28   -#
29   -# This will reuse the extend function from either:
30   -#
31   -# - jQuery
32   -# - Underscore.js
33   -# - Prototype
34   -# - MooTools
35   -# - Zepto.js
36   -#
37   -# You can assign a custom extend function if your framework
38   -# is not supported out of the box.
39   -#
40   -window.HAML.extend ||= (globals, locals) ->
41   - if jQuery?.extend
42   - jQuery.extend {}, globals, locals
43   -
44   - else if _?.extend
45   - _.extend {}, globals, locals
46   -
47   - else if Zepto?.extend
48   - Zepto.extend(Zepto.extend({}, globals), locals)
49   -
50   - else if Object.append
51   - Object.append(Object.append({}, globals), locals)
52   -
53   - else if Object.extend
54   - Object.extend(Object.extend({}, globals), locals)
55   -
56   - else
57   - locals
58   -
59   -# HAML Coffee global template context.
60   -#
61   -# @return [Object] the global template context
62   -#
63   -window.HAML.globals ||= -> {}
64   -
65   -# Get the HAML template context. This merges the local
66   -# and the global template context into a new context.
67   -#
68   -# @param locals [Object] the local template context
69   -# @return [Object] the merged context
70   -#
71   -window.HAML.context ||= (locals) ->
72   - HAML.extend(HAML.globals(), locals)
73   -
74   -# Preserve newlines in the text
75   -#
76   -# @param text [String] the text to preserve
77   -# @return [String] the preserved text
78   -#
79   -window.HAML.preserve ||= (text) ->
80   - text.replace /\\n/g, '&#x000A;'
81   -
82   -# Find and preserve newlines in the preserving tags
83   -#
84   -# @param text [String] the text to preserve
85   -# @return [String] the preserved text
86   -#
87   -window.HAML.findAndPreserve ||= (text) ->
88   - tags = '<%= configuration.preserveTags %>'.split(',').join('|')
89   - text.replace ///<(#{ tags })>([^]*?)<\/\1>///g, (str, tag, content) ->
90   - "<#{ tag }>#{ <%= configuration.customPreserve %>(content) }</#{ tag }>"
91   -
92   -# The surround helper surrounds the function output
93   -# with the start and end string without whitespace in between.
94   -#
95   -# @param [String] start the text to prepend to the text
96   -# @param [String] end the text to append to the text
97   -# @param [Function] fn the text generating function
98   -# @return [String] the surrounded text
99   -#
100   -window.HAML.surround ||= (start, end, fn) ->
101   - start + fn() + end
102   -
103   -# The succeed helper appends text to the function output
104   -# without whitespace in between.
105   -#
106   -# @param [String] end the text to append to the text
107   -# @param [Function] fn the text generating function
108   -# @return [String] the succeeded text
109   -#
110   -window.HAML.succeed ||= (end, fn) ->
111   - fn() + end
112   -
113   -# The precede helper prepends text to the function output
114   -# without whitespace in between.
115   -#
116   -# @param [String] start the text to prepend to the text
117   -# @param [Function] fn the text generating function
118   -# @return [String] the preeceded text
119   -#
120   -window.HAML.precede ||= (start, fn) ->
121   - start + fn()
  5 +class window.HAML
  6 +
  7 + # HAML Coffee html escape function
  8 + #
  9 + # @param text [String] the text to escape
  10 + # @return [String] the escaped text
  11 + #
  12 + @escape: (text) ->
  13 + ("" + text)
  14 + .replace(/&/g, "&amp;")
  15 + .replace(/</g, "&lt;")
  16 + .replace(/>/g, "&gt;")
  17 + .replace(/"/g, "&quot;")
  18 +
  19 + # HAML Coffee clean value function
  20 + #
  21 + # @param text [String] the text to be cleaned
  22 + # @return [String] the cleaned text
  23 + #
  24 + @cleanValue: (text) ->
  25 + if text is null or text is undefined then '' else text
  26 +
  27 + # Extends an object with the properties from
  28 + # the source objects.
  29 + #
  30 + # @param [Object] obj the object to extended
  31 + # @param [Object] sources the objects to copy from
  32 + # @return [Object] the extended object
  33 + #
  34 + @extend: (obj, sources...) ->
  35 + for source in sources
  36 + obj[key] = val for key, val of source
  37 +
  38 + obj
  39 +
  40 + # HAML Coffee global template context.
  41 + #
  42 + # @return [Object] the global template context
  43 + #
  44 + @globals: -> {}
  45 +
  46 + # Get the HAML template context. This merges the local
  47 + # and the global template context into a new context.
  48 + #
  49 + # @param locals [Object] the local template context
  50 + # @return [Object] the merged context
  51 + #
  52 + @context: (locals) ->
  53 + @extend {}, HAML.globals(), locals
  54 +
  55 + # Preserve newlines in the text
  56 + #
  57 + # @param text [String] the text to preserve
  58 + # @return [String] the preserved text
  59 + #
  60 + @preserve: (text) ->
  61 + text.replace /\n/g, '&#x000A;'
  62 +
  63 + # Find and preserve newlines in the preserving tags
  64 + #
  65 + # @param text [String] the text to preserve
  66 + # @return [String] the preserved text
  67 + #
  68 + @findAndPreserve: (text) ->
  69 + tags = '<%= configuration.preserveTags %>'.split(',').join('|')
  70 + text = text.replace ///<(#{ tags })>([^]*?)<\/\1>///g, (str, tag, content) ->
  71 + "<#{ tag }>#{ <%= configuration.customPreserve %>(content) }</#{ tag }>"
  72 +
  73 + # The surround helper surrounds the function output
  74 + # with the start and end string without whitespace in between.
  75 + #
  76 + # @param [String] start the text to prepend to the text
  77 + # @param [String] end the text to append to the text
  78 + # @param [Function] fn the text generating function
  79 + # @return [String] the surrounded text
  80 + #
  81 + @surround: (start, end, fn) ->
  82 + start + fn() + end
  83 +
  84 + # The succeed helper appends text to the function output
  85 + # without whitespace in between.
  86 + #
  87 + # @param [String] end the text to append to the text
  88 + # @param [Function] fn the text generating function
  89 + # @return [String] the succeeded text
  90 + #
  91 + @succeed: (end, fn) ->
  92 + fn() + end
  93 +
  94 + # The precede helper prepends text to the function output
  95 + # without whitespace in between.
  96 + #
  97 + # @param [String] start the text to prepend to the text
  98 + # @param [Function] fn the text generating function
  99 + # @return [String] the preeceded text
  100 + #
  101 + @precede: (start, fn) ->
  102 + start + fn()

0 comments on commit c9e6350

Please sign in to comment.
Something went wrong with that request. Please try again.