summaryrefslogtreecommitdiff
path: root/specs/~dynamic-names.json
blob: c6aade62b491f796971db4142c077b032dd01fb0 (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
{
  "overview": "Dynamic Names are a special notation to dynamically determine a tag's content.\n\nDynamic Names MUST be a non-whitespace character sequence NOT containing\nthe current closing delimiter. A Dynamic Name consists of an asterisk,\nfollowed by a dotted name. The latter follows the same notation as in an\nInterpolation tag.\n\nThis tag's content refers to a key in the context whose value will be used in\nplace of the Dynamic Name itself as content of the tag. The name resolution is\nidentical to name resolution in Interpolation tags, 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.\nThe resolved data should be coerced into a string before being used as\ncontent.\n\nSet Delimiter tags MUST NOT affect the resolution of a Dynamic Name. The\nDynamic Names MUST be resolved against the context stack local to the tag.\nFailed resolution of the dynamic name should result in nothing being rendered.\n\nEngines that implement Dynamic Names MUST support their use in Partial tags.\nIn engines that also implement the optional inheritance spec, Dynamic Names\ninside Parent tags should be supported as well. Dynamic Names cannot be\nresolved more than once (Dynamic Names cannot be nested).\n",
  "tests": [
    {
      "name": "Basic Behavior - Partial",
      "desc": "The asterisk operator is used for dynamic partials.",
      "data": {
        "dynamic": "content"
      },
      "template": "\"{{>*dynamic}}\"",
      "partials": {
        "content": "Hello, world!"
      },
      "expected": "\"Hello, world!\""
    },
    {
      "name": "Context Misses - Partial",
      "desc": "Failed context lookups should be considered falsey.",
      "data": {},
      "template": "\"{{>*missing}}\"",
      "partials": {
        "missing": "Hello, world!"
      },
      "expected": "\"\""
    },
    {
      "name": "Failed Lookup - Partial",
      "desc": "The empty string should be used when the named partial is not found.",
      "data": {
        "dynamic": "content"
      },
      "template": "\"{{>*dynamic}}\"",
      "partials": {
        "foobar": "Hello, world!"
      },
      "expected": "\"\""
    },
    {
      "name": "Context",
      "desc": "The dynamic partial should operate within the current context.",
      "data": {
        "text": "Hello, world!",
        "example": "partial"
      },
      "template": "\"{{>*example}}\"",
      "partials": {
        "partial": "*{{text}}*"
      },
      "expected": "\"*Hello, world!*\""
    },
    {
      "name": "Dotted Names",
      "desc": "The dynamic partial should operate within the current context.",
      "data": {
        "text": "Hello, world!",
        "foo": {
          "bar": {
            "baz": "partial"
          }
        }
      },
      "template": "\"{{>*foo.bar.baz}}\"",
      "partials": {
        "partial": "*{{text}}*"
      },
      "expected": "\"*Hello, world!*\""
    },
    {
      "name": "Dotted Names - Failed Lookup",
      "desc": "The dynamic partial should operate within the current context.",
      "data": {
        "text": "Hello, world!",
        "foo": "test",
        "test": {
          "bar": {
            "baz": "partial"
          }
        }
      },
      "template": "\"{{>*foo.bar.baz}}\"",
      "partials": {
        "partial": "*{{text}}*"
      },
      "expected": "\"\""
    },
    {
      "name": "Dotted Names - Failed Lookup",
      "desc": "The dynamic partial should operate within the current context.",
      "data": {
        "foo": {
          "text": "Hello, world!",
          "bar": {
            "baz": "partial"
          }
        }
      },
      "template": "\"{{>*foo.bar.baz}}\"",
      "partials": {
        "partial": "*{{text}}*"
      },
      "expected": "\"**\""
    },
    {
      "name": "Dotted names - Context Stacking",
      "desc": "Dotted names should not push a new frame on the context stack.",
      "data": {
        "section1": {
          "value": "section1"
        },
        "section2": {
          "dynamic": "partial",
          "value": "section2"
        }
      },
      "template": "{{#section1}}{{>*section2.dynamic}}{{/section1}}",
      "partials": {
        "partial": "{{value}}"
      },
      "expected": "\"section1\""
    },
    {
      "name": "Dotted names - Context Stacking Under Repetition",
      "desc": "Dotted names should not push a new frame on the context stack.",
      "data": {
        "value": "test",
        "section1": [
          1,
          2
        ],
        "section2": {
          "dynamic": "partial",
          "value": "section2"
        }
      },
      "template": "{{#section1}}{{>*section2.dynamic}}{{/section1}}",
      "partials": {
        "partial": "{{value}}"
      },
      "expected": "testtest"
    },
    {
      "name": "Dotted names - Context Stacking Failed Lookup",
      "desc": "Dotted names should resolve against the proper context stack.",
      "data": {
        "section1": [
          1,
          2
        ],
        "section2": {
          "dynamic": "partial",
          "value": "section2"
        }
      },
      "template": "{{#section1}}{{>*section2.dynamic}}{{/section1}}",
      "partials": {
        "partial": "\"{{value}}\""
      },
      "expected": "\"\""
    },
    {
      "name": "Recursion",
      "desc": "Dynamic partials should properly recurse.",
      "data": {
        "template": "node",
        "content": "X",
        "nodes": [
          {
            "content": "Y",
            "nodes": []
          }
        ]
      },
      "template": "{{>*template}}",
      "partials": {
        "node": "{{content}}<{{#nodes}}{{>*template}}{{/nodes}}>"
      },
      "expected": "X<Y<>>"
    },
    {
      "name": "Surrounding Whitespace",
      "desc": "A dynamic partials should not alter surrounding whitespace; any\nwhitespace preceding the tag should treated as indentation while any\nwhitepsace succeding the tag should be left untouched.\n",
      "data": {
        "partial": "foobar"
      },
      "template": "| {{>*partial}} |",
      "partials": {
        "foobar": "\t|\t"
      },
      "expected": "| \t|\t |"
    },
    {
      "name": "Inline Indentation",
      "desc": "Whitespace should be left untouched: whitespaces preceding the tag\nshould be treated as indentation.\n",
      "data": {
        "dynamic": "partial",
        "data": "|"
      },
      "template": "  {{data}}  {{>* dynamic}}\n",
      "partials": {
        "partial": ">\n>"
      },
      "expected": "  |  >\n>\n"
    },
    {
      "name": "Standalone Line Endings",
      "desc": "\"\\r\\n\" should be considered a newline for standalone tags.",
      "data": {
        "dynamic": "partial"
      },
      "template": "|\r\n{{>*dynamic}}\r\n|",
      "partials": {
        "partial": ">"
      },
      "expected": "|\r\n>|"
    },
    {
      "name": "Standalone Without Previous Line",
      "desc": "Standalone tags should not require a newline to precede them.",
      "data": {
        "dynamic": "partial"
      },
      "template": "  {{>*dynamic}}\n>",
      "partials": {
        "partial": ">\n>"
      },
      "expected": "  >\n  >>"
    },
    {
      "name": "Standalone Without Newline",
      "desc": "Standalone tags should not require a newline to follow them.",
      "data": {
        "dynamic": "partial"
      },
      "template": ">\n  {{>*dynamic}}",
      "partials": {
        "partial": ">\n>"
      },
      "expected": ">\n  >\n  >"
    },
    {
      "name": "Standalone Indentation",
      "desc": "Each line of the partial should be indented before rendering.",
      "data": {
        "dynamic": "partial",
        "content": "<\n->"
      },
      "template": "\\\n {{>*dynamic}}\n/\n",
      "partials": {
        "partial": "|\n{{{content}}}\n|\n"
      },
      "expected": "\\\n |\n <\n->\n |\n/\n"
    },
    {
      "name": "Padding Whitespace",
      "desc": "Superfluous in-tag whitespace should be ignored.",
      "data": {
        "dynamic": "partial",
        "boolean": true
      },
      "template": "|{{> *dynamic }}|",
      "partials": {
        "partial": "[]"
      },
      "expected": "|[]|"
    }
  ]
}