summaryrefslogtreecommitdiff
path: root/specs/interpolation.json
blob: 1718e1c090fd9e72a0e0090ba2a042088759eedc (plain)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
{
  "__ATTN__": "Do not edit this file; changes belong in the appropriate YAML file.",
  "overview": "Interpolation tags are used to integrate dynamic content into the template.\n\nThe tag's content MUST be a non-whitespace character sequence NOT containing\nthe current closing delimiter.\n\nThis tag's content names the data to replace the tag.  A single period (`.`)\nindicates that the item currently sitting atop the context stack should be\nused; otherwise, name resolution is as follows:\n  1) Split the name on periods; the first part is the name to resolve, any\n  remaining parts should be retained.\n  2) Walk the context stack from top to bottom, finding the first context\n  that is a) a hash containing the name as a key OR b) an object responding\n  to a method with the given name.\n  3) If the context is a hash, the data is the value associated with the\n  name.\n  4) If the context is an object, the data is the value returned by the\n  method with the given name.\n  5) If any name parts were retained in step 1, each should be resolved\n  against a context stack containing only the result from the former\n  resolution.  If any part fails resolution, the result should be considered\n  falsey, and should interpolate as the empty string.\nData should be coerced into a string (and escaped, if appropriate) before\ninterpolation.\n\nThe Interpolation tags MUST NOT be treated as standalone.\n",
  "tests": [
    {
      "name": "No Interpolation",
      "desc": "Mustache-free templates should render as-is.",
      "data": {
      },
      "template": "Hello from {Mustache}!\n",
      "expected": "Hello from {Mustache}!\n"
    },
    {
      "name": "Basic Interpolation",
      "desc": "Unadorned tags should interpolate content into the template.",
      "data": {
        "subject": "world"
      },
      "template": "Hello, {{subject}}!\n",
      "expected": "Hello, world!\n"
    },
    {
      "name": "HTML Escaping",
      "desc": "Basic interpolation should be HTML escaped.",
      "data": {
        "forbidden": "& \" < >"
      },
      "template": "These characters should be HTML escaped: {{forbidden}}\n",
      "expected": "These characters should be HTML escaped: &amp; &quot; &lt; &gt;\n"
    },
    {
      "name": "Triple Mustache",
      "desc": "Triple mustaches should interpolate without HTML escaping.",
      "data": {
        "forbidden": "& \" < >"
      },
      "template": "These characters should not be HTML escaped: {{{forbidden}}}\n",
      "expected": "These characters should not be HTML escaped: & \" < >\n"
    },
    {
      "name": "Ampersand",
      "desc": "Ampersand should interpolate without HTML escaping.",
      "data": {
        "forbidden": "& \" < >"
      },
      "template": "These characters should not be HTML escaped: {{&forbidden}}\n",
      "expected": "These characters should not be HTML escaped: & \" < >\n"
    },
    {
      "name": "Basic Integer Interpolation",
      "desc": "Integers should interpolate seamlessly.",
      "data": {
        "mph": 85
      },
      "template": "\"{{mph}} miles an hour!\"",
      "expected": "\"85 miles an hour!\""
    },
    {
      "name": "Triple Mustache Integer Interpolation",
      "desc": "Integers should interpolate seamlessly.",
      "data": {
        "mph": 85
      },
      "template": "\"{{{mph}}} miles an hour!\"",
      "expected": "\"85 miles an hour!\""
    },
    {
      "name": "Ampersand Integer Interpolation",
      "desc": "Integers should interpolate seamlessly.",
      "data": {
        "mph": 85
      },
      "template": "\"{{&mph}} miles an hour!\"",
      "expected": "\"85 miles an hour!\""
    },
    {
      "name": "Basic Decimal Interpolation",
      "desc": "Decimals should interpolate seamlessly with proper significance.",
      "data": {
        "power": 1.21
      },
      "template": "\"{{power}} jiggawatts!\"",
      "expected": "\"1.21 jiggawatts!\""
    },
    {
      "name": "Triple Mustache Decimal Interpolation",
      "desc": "Decimals should interpolate seamlessly with proper significance.",
      "data": {
        "power": 1.21
      },
      "template": "\"{{{power}}} jiggawatts!\"",
      "expected": "\"1.21 jiggawatts!\""
    },
    {
      "name": "Ampersand Decimal Interpolation",
      "desc": "Decimals should interpolate seamlessly with proper significance.",
      "data": {
        "power": 1.21
      },
      "template": "\"{{&power}} jiggawatts!\"",
      "expected": "\"1.21 jiggawatts!\""
    },
    {
      "name": "Basic Context Miss Interpolation",
      "desc": "Failed context lookups should default to empty strings.",
      "data": {
      },
      "template": "I ({{cannot}}) be seen!",
      "expected": "I () be seen!"
    },
    {
      "name": "Triple Mustache Context Miss Interpolation",
      "desc": "Failed context lookups should default to empty strings.",
      "data": {
      },
      "template": "I ({{{cannot}}}) be seen!",
      "expected": "I () be seen!"
    },
    {
      "name": "Ampersand Context Miss Interpolation",
      "desc": "Failed context lookups should default to empty strings.",
      "data": {
      },
      "template": "I ({{&cannot}}) be seen!",
      "expected": "I () be seen!"
    },
    {
      "name": "Dotted Names - Basic Interpolation",
      "desc": "Dotted names should be considered a form of shorthand for sections.",
      "data": {
        "person": {
          "name": "Joe"
        }
      },
      "template": "\"{{person.name}}\" == \"{{#person}}{{name}}{{/person}}\"",
      "expected": "\"Joe\" == \"Joe\""
    },
    {
      "name": "Dotted Names - Triple Mustache Interpolation",
      "desc": "Dotted names should be considered a form of shorthand for sections.",
      "data": {
        "person": {
          "name": "Joe"
        }
      },
      "template": "\"{{{person.name}}}\" == \"{{#person}}{{{name}}}{{/person}}\"",
      "expected": "\"Joe\" == \"Joe\""
    },
    {
      "name": "Dotted Names - Ampersand Interpolation",
      "desc": "Dotted names should be considered a form of shorthand for sections.",
      "data": {
        "person": {
          "name": "Joe"
        }
      },
      "template": "\"{{&person.name}}\" == \"{{#person}}{{&name}}{{/person}}\"",
      "expected": "\"Joe\" == \"Joe\""
    },
    {
      "name": "Dotted Names - Arbitrary Depth",
      "desc": "Dotted names should be functional to any level of nesting.",
      "data": {
        "a": {
          "b": {
            "c": {
              "d": {
                "e": {
                  "name": "Phil"
                }
              }
            }
          }
        }
      },
      "template": "\"{{a.b.c.d.e.name}}\" == \"Phil\"",
      "expected": "\"Phil\" == \"Phil\""
    },
    {
      "name": "Dotted Names - Broken Chains",
      "desc": "Any falsey value prior to the last part of the name should yield ''.",
      "data": {
        "a": {
        }
      },
      "template": "\"{{a.b.c}}\" == \"\"",
      "expected": "\"\" == \"\""
    },
    {
      "name": "Dotted Names - Broken Chain Resolution",
      "desc": "Each part of a dotted name should resolve only against its parent.",
      "data": {
        "a": {
          "b": {
          }
        },
        "c": {
          "name": "Jim"
        }
      },
      "template": "\"{{a.b.c.name}}\" == \"\"",
      "expected": "\"\" == \"\""
    },
    {
      "name": "Dotted Names - Initial Resolution",
      "desc": "The first part of a dotted name should resolve as any other name.",
      "data": {
        "a": {
          "b": {
            "c": {
              "d": {
                "e": {
                  "name": "Phil"
                }
              }
            }
          }
        },
        "b": {
          "c": {
            "d": {
              "e": {
                "name": "Wrong"
              }
            }
          }
        }
      },
      "template": "\"{{#a}}{{b.c.d.e.name}}{{/a}}\" == \"Phil\"",
      "expected": "\"Phil\" == \"Phil\""
    },
    {
      "name": "Dotted Names - Context Precedence",
      "desc": "Dotted names should be resolved against former resolutions.",
      "data": {
        "a": {
          "b": {
          }
        },
        "b": {
          "c": "ERROR"
        }
      },
      "template": "{{#a}}{{b.c}}{{/a}}",
      "expected": ""
    },
    {
      "name": "Interpolation - Surrounding Whitespace",
      "desc": "Interpolation should not alter surrounding whitespace.",
      "data": {
        "string": "---"
      },
      "template": "| {{string}} |",
      "expected": "| --- |"
    },
    {
      "name": "Triple Mustache - Surrounding Whitespace",
      "desc": "Interpolation should not alter surrounding whitespace.",
      "data": {
        "string": "---"
      },
      "template": "| {{{string}}} |",
      "expected": "| --- |"
    },
    {
      "name": "Ampersand - Surrounding Whitespace",
      "desc": "Interpolation should not alter surrounding whitespace.",
      "data": {
        "string": "---"
      },
      "template": "| {{&string}} |",
      "expected": "| --- |"
    },
    {
      "name": "Interpolation - Standalone",
      "desc": "Standalone interpolation should not alter surrounding whitespace.",
      "data": {
        "string": "---"
      },
      "template": "  {{string}}\n",
      "expected": "  ---\n"
    },
    {
      "name": "Triple Mustache - Standalone",
      "desc": "Standalone interpolation should not alter surrounding whitespace.",
      "data": {
        "string": "---"
      },
      "template": "  {{{string}}}\n",
      "expected": "  ---\n"
    },
    {
      "name": "Ampersand - Standalone",
      "desc": "Standalone interpolation should not alter surrounding whitespace.",
      "data": {
        "string": "---"
      },
      "template": "  {{&string}}\n",
      "expected": "  ---\n"
    },
    {
      "name": "Interpolation With Padding",
      "desc": "Superfluous in-tag whitespace should be ignored.",
      "data": {
        "string": "---"
      },
      "template": "|{{ string }}|",
      "expected": "|---|"
    },
    {
      "name": "Triple Mustache With Padding",
      "desc": "Superfluous in-tag whitespace should be ignored.",
      "data": {
        "string": "---"
      },
      "template": "|{{{ string }}}|",
      "expected": "|---|"
    },
    {
      "name": "Ampersand With Padding",
      "desc": "Superfluous in-tag whitespace should be ignored.",
      "data": {
        "string": "---"
      },
      "template": "|{{& string }}|",
      "expected": "|---|"
    }
  ]
}