-
Notifications
You must be signed in to change notification settings - Fork 146
/
hamlcoffee_amd.js.coffee.erb
118 lines (106 loc) · 3.63 KB
/
hamlcoffee_amd.js.coffee.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# HAML Coffee helper module
#
define ->
# HAML Coffee html escape function.
#
# @param text [String] the text to escape
# @return [String] the escaped text
#
escape: (text) ->
("" + text)
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'")
.replace(/\//g, "/")
# HAML Coffee clean value function. Beside just
# cleaning `null` and `undefined` values, it
# adds a hidden unicode marker character to
# boolean values, so that the render time boolean
# logic can distinguish between real booleans and
# boolean-like strings.
#
# @param text [String] the text to be cleaned
# @return [String] the cleaned text
#
cleanValue: (text) ->
switch text
when null, undefined then ''
when true, false then '\u0093' + text
else
text
# Preserve newlines in the text
#
# @param text [String] the text to preserve
# @return [String] the preserved text
#
preserve: (text) ->
text.replace /\n/g, '
'
# Find and preserve newlines in the preserving tags
#
# @param text [String] the text to preserve
# @return [String] the preserved text
#
findAndPreserve: (text) ->
tags = '<%= ::HamlCoffeeAssets.config.preserveTags %>'.split(',').join('|')
text = text.replace(/\r/g, '').replace ///<(#{ tags })>([\s\S]*?)<\/\1>///g, (str, tag, content) ->
"<#{ tag }>#{ <%= ::HamlCoffeeAssets.config.customPreserve %>(content) }</#{ tag }>"
# The surround helper surrounds the function output
# with the start and end string without whitespace in between.
#
# @param [String] start the text to prepend to the text
# @param [String] end the text to append to the text
# @param [Function] fn the text generating function
# @return [String] the surrounded text
#
surround: (start, end, fn) ->
start + fn.call(@)?.replace(/^\s+|\s+$/g, '') + end
# The succeed helper appends text to the function output
# without whitespace in between.
#
# @param [String] end the text to append to the text
# @param [Function] fn the text generating function
# @return [String] the succeeded text
#
succeed: (end, fn) ->
fn.call(@)?.replace(/\s+$/g, '') + end
# The precede helper prepends text to the function output
# without whitespace in between.
#
# @param [String] start the text to prepend to the text
# @param [Function] fn the text generating function
# @return [String] the preeceded text
#
precede: (start, fn) ->
start + fn.call(@)?.replace(/^\s+/g, '')
# Get the HAML template context. This merges the local
# and the global template context into a new context.
#
# @param locals [Object] the local template context
# @return [Object] the merged context
#
context: (locals) -> locals
# The reference helper creates id and class attributes
# for an object reference.
#
# @param [Object] object the referenced object
# @param [String] prefix the optional id and class prefix
# @return [String] the id and class attributes
#
reference: (object, prefix) ->
name = if prefix then prefix + '_' else ''
if typeof(object.hamlObjectRef) is 'function'
name += object.hamlObjectRef()
else
name += (object.constructor?.name || 'object').replace(/\W+/g, '_').replace(/([a-z\d])([A-Z])/g, '$1_$2').toLowerCase()
id = if typeof(object.to_key) is 'function'
object.to_key()
else if typeof(object.id) is 'function'
object.id()
else if object.id
object.id
else
object
result = "class='#{ name }'"
result += " id='#{ name }_#{ id }'" if i