-
Notifications
You must be signed in to change notification settings - Fork 5
/
drupal-fixture.js
183 lines (169 loc) · 5.91 KB
/
drupal-fixture.js
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// This module is used to provide the Drupal global in development and test mode.
// Functions taken from drupal.js.
import exampleData from '/test/unit/fixtures/example-data'
import initialData from '/test/unit/fixtures/initial-data'
const Drupal = {
settings: {
campaignion_email_to_target: (process.env.NODE_ENV === 'development')
? exampleData
: initialData
},
locale: {},
/**
* Encode special characters in a plain-text string for display as HTML.
*
* @ingroup sanitization
*/
checkPlain: function (str) {
var character; var regex
var replace = { '&': '&', '"': '"', '<': '<', '>': '>' }
str = String(str)
for (character in replace) {
if (replace.hasOwnProperty(character)) {
regex = new RegExp(character, 'g')
str = str.replace(regex, replace[character])
}
}
return str
},
/**
* Replace placeholders with sanitized values in a string.
*
* @param str
* A string with placeholders.
* @param args
* An object of replacements pairs to make. Incidences of any key in this
* array are replaced with the corresponding value. Based on the first
* character of the key, the value is escaped and/or themed:
* - !variable: inserted as is
* - @variable: escape plain text to HTML (Drupal.checkPlain)
* - %variable: escape text and theme as a placeholder for user-submitted
* content (checkPlain + Drupal.theme('placeholder'))
*
* @see Drupal.t()
* @ingroup sanitization
*/
formatString: function (str, args) {
// Transform arguments before inserting them.
for (var key in args) {
switch (key.charAt(0)) {
// Escaped only.
case '@':
args[key] = Drupal.checkPlain(args[key])
break
// Pass-through.
case '!':
break
// Escaped and placeholder.
case '%':
default:
args[key] = Drupal.theme('placeholder', args[key])
break
}
str = str.replace(key, args[key])
}
return str
},
/**
* Translate strings to the page language or a given language.
*
* See the documentation of the server-side t() function for further details.
*
* @param str
* A string containing the English string to translate.
* @param args
* An object of replacements pairs to make after translation. Incidences
* of any key in this array are replaced with the corresponding value.
* See Drupal.formatString().
*
* @param options
* - 'context' (defaults to the empty context): The context the source string
* belongs to.
*
* @return
* The translated string.
*/
t: function (str, args, options) {
options = options || {}
options.context = options.context || ''
// Fetch the localized version of the string.
if (Drupal.locale.strings && Drupal.locale.strings[options.context] && Drupal.locale.strings[options.context][str]) {
str = Drupal.locale.strings[options.context][str]
}
if (args) {
str = Drupal.formatString(str, args)
}
return str
},
/**
* Format a string containing a count of items.
*
* This function ensures that the string is pluralized correctly. Since Drupal.t() is
* called by this function, make sure not to pass already-localized strings to it.
*
* See the documentation of the server-side format_plural() function for further details.
*
* @param count
* The item count to display.
* @param singular
* The string for the singular case. Please make sure it is clear this is
* singular, to ease translation (e.g. use "1 new comment" instead of "1 new").
* Do not use @count in the singular string.
* @param plural
* The string for the plural case. Please make sure it is clear this is plural,
* to ease translation. Use @count in place of the item count, as in "@count
* new comments".
* @param args
* An object of replacements pairs to make after translation. Incidences
* of any key in this array are replaced with the corresponding value.
* See Drupal.formatString().
* Note that you do not need to include @count in this array.
* This replacement is done automatically for the plural case.
* @param options
* The options to pass to the Drupal.t() function.
* @return
* A translated string.
*/
formatPlural: function (count, singular, plural, args, options) {
var args = args || {}
args['@count'] = count
// Determine the index of the plural form.
var index = Drupal.locale.pluralFormula ? Drupal.locale.pluralFormula(args['@count']) : ((args['@count'] == 1) ? 0 : 1)
if (index == 0) {
return Drupal.t(singular, args, options)
}
else if (index == 1) {
return Drupal.t(plural, args, options)
}
else {
args['@count[' + index + ']'] = args['@count']
delete args['@count']
return Drupal.t(plural.replace('@count', '@count[' + index + ']'), args, options)
}
},
/**
* Generate the themed representation of a Drupal object.
*
* All requests for themed output must go through this function. It examines
* the request and routes it to the appropriate theme function. If the current
* theme does not provide an override function, the generic theme function is
* called.
*
* For example, to retrieve the HTML for text that should be emphasized and
* displayed as a placeholder inside a sentence, call
* Drupal.theme('placeholder', text).
*
* @param func
* The name of the theme function to call.
* @param ...
* Additional arguments to pass along to the theme function.
* @return
* Any data the theme function returns. This could be a plain HTML string,
* but also a complex object.
*/
theme: function (func) {
var args = Array.prototype.slice.apply(arguments, [1])
return (Drupal.theme[func] || Drupal.theme.prototype[func]).apply(this, args)
}
}
globalThis.Drupal = Drupal