From 995959b65f5184477f3dfab26eb75c24237ed435 Mon Sep 17 00:00:00 2001 From: Gabriel Scherer Date: Thu, 24 Dec 2020 23:06:22 +0100 Subject: clarify the behavior of context stacks / parent contexts The behavior of "context stacks" is not explained very clearly in the documentation outside the spec, but the following text from mustache(5) (http://mustache.github.io/mustache.5.html) is pretty clear: > A {{name}} tag in a basic template will try to find the name key in > the current context. If there is no name key, the parent contexts > will be checked recursively. If the top context is reached and the > name key is still not found, nothing will be rendered. In particular, when the documentation of section mentions that > When the value is a non-empty list [..]. The context of the block > will be set to the current item for each iteration. or > When the value is non-false but not a list, it will be used as the > context for a single rendering of the block. it should be understood that the new context becomes the "current context", and that the previous context becomes a "parent context", and its binding are still available. The new tests check that these properties hold, in particular for sections on non-object non-false values, where this was not previously tested in the spec. --- specs/sections.yml | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/specs/sections.yml b/specs/sections.yml index 30815c9..d204bea 100644 --- a/specs/sections.yml +++ b/specs/sections.yml @@ -53,14 +53,27 @@ tests: template: '"{{#context}}Hi {{name}}.{{/context}}"' expected: '"Hi Joe."' + - name: Parent contexts + desc: Names missing in the current context are looked up in the stack. + data: { a: "foo", b: "wrong", sec: { b: "bar" }, c : { d : "baz" } } + template: '"{{#sec}}{{a}}, {{b}}, {{c.d}}{{/sec}}"' + expected: '"foo, bar, baz"' + + - name: Variable test + desc: | + Non-false sections have their value at the top of context, + accessible as {{.}} or through the parent context. This gives + a simple way to display content conditionally if a variable exists. + data: { foo: "bar" } + template: '"{{#foo}} {{.}} is {{foo}} {{/foo}}"' + expected: '"bar is bar"' + - name: Deeply Nested Contexts desc: All elements on the context stack should be accessible. data: a: { one: 1 } b: { two: 2 } - c: { three: 3 } - d: { four: 4 } - e: { five: 5 } + c: { three: 3, d : { four : 4, five : 5 } } template: | {{#a}} {{one}} @@ -70,9 +83,11 @@ tests: {{one}}{{two}}{{three}}{{two}}{{one}} {{#d}} {{one}}{{two}}{{three}}{{four}}{{three}}{{two}}{{one}} - {{#e}} + {{#five}} + {{one}}{{two}}{{three}}{{four}}{{five}}{{four}}{{three}}{{two}}{{one}} + {{one}}{{two}}{{three}}{{four}}{{.}}6{{.}}{{four}}{{three}}{{two}}{{one}} {{one}}{{two}}{{three}}{{four}}{{five}}{{four}}{{three}}{{two}}{{one}} - {{/e}} + {{/five}} {{one}}{{two}}{{three}}{{four}}{{three}}{{two}}{{one}} {{/d}} {{one}}{{two}}{{three}}{{two}}{{one}} @@ -87,6 +102,8 @@ tests: 12321 1234321 123454321 + 12345654321 + 123454321 1234321 12321 121 -- cgit v1.2.1 From 7d11e61bf5f4bee890d55184089493d185e82974 Mon Sep 17 00:00:00 2001 From: Andrew Martin Date: Tue, 6 Oct 2015 08:45:00 -0400 Subject: Add List context check --- specs/sections.yml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/specs/sections.yml b/specs/sections.yml index d204bea..2d1e982 100644 --- a/specs/sections.yml +++ b/specs/sections.yml @@ -68,6 +68,21 @@ tests: template: '"{{#foo}} {{.}} is {{foo}} {{/foo}}"' expected: '"bar is bar"' + - name: List Contexts + desc: All elements on the context stack should be accessible within lists. + data: + tops: + - tname: + upper: "A" + lower: "a" + middles: + - mname: "1" + bottoms: + - bname: "x" + - bname: "y" + template: '{{#tops}}{{#middles}}{{tname.lower}}{{mname}}.{{#bottoms}}{{tname.upper}}{{mname}}{{bname}}.{{/bottoms}}{{/middles}}{{/tops}}' + expected: 'a1.A1x.A1y.' + - name: Deeply Nested Contexts desc: All elements on the context stack should be accessible. data: -- cgit v1.2.1 From b04861a39134ac11308c84ab93205f2b7b17151a Mon Sep 17 00:00:00 2001 From: Gabriel Scherer Date: Tue, 9 Mar 2021 09:44:38 +0100 Subject: update the JSON specs (rake build) --- specs/sections.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/sections.json b/specs/sections.json index af5d7cb..f22c188 100644 --- a/specs/sections.json +++ b/specs/sections.json @@ -1 +1 @@ -{"__ATTN__":"Do not edit this file; changes belong in the appropriate YAML file.","overview":"Section tags and End Section tags are used in combination to wrap a section\nof the template for iteration\n\nThese tags' content MUST be a non-whitespace character sequence NOT\ncontaining the current closing delimiter; each Section tag MUST be followed\nby an End Section tag with the same content within the same section.\n\nThis tag's content names the data to replace the tag. Name resolution is as\nfollows:\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 and the method with the given name has an\n arity of 1, the method SHOULD be called with a String containing the\n unprocessed contents of the sections; the data is the value returned.\n 5) Otherwise, the data is the value returned by calling the method with\n the given name.\n 6) 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.\nIf the data is not of a list type, it is coerced into a list as follows: if\nthe data is truthy (e.g. `!!data == true`), use a single-element list\ncontaining the data, otherwise use an empty list.\n\nFor each element in the data list, the element MUST be pushed onto the\ncontext stack, the section MUST be rendered, and the element MUST be popped\noff the context stack.\n\nSection and End Section tags SHOULD be treated as standalone when\nappropriate.\n","tests":[{"name":"Truthy","desc":"Truthy sections should have their contents rendered.","data":{"boolean":true},"template":"\"{{#boolean}}This should be rendered.{{/boolean}}\"","expected":"\"This should be rendered.\""},{"name":"Falsey","desc":"Falsey sections should have their contents omitted.","data":{"boolean":false},"template":"\"{{#boolean}}This should not be rendered.{{/boolean}}\"","expected":"\"\""},{"name":"Context","desc":"Objects and hashes should be pushed onto the context stack.","data":{"context":{"name":"Joe"}},"template":"\"{{#context}}Hi {{name}}.{{/context}}\"","expected":"\"Hi Joe.\""},{"name":"Deeply Nested Contexts","desc":"All elements on the context stack should be accessible.","data":{"a":{"one":1},"b":{"two":2},"c":{"three":3},"d":{"four":4},"e":{"five":5}},"template":"{{#a}}\n{{one}}\n{{#b}}\n{{one}}{{two}}{{one}}\n{{#c}}\n{{one}}{{two}}{{three}}{{two}}{{one}}\n{{#d}}\n{{one}}{{two}}{{three}}{{four}}{{three}}{{two}}{{one}}\n{{#e}}\n{{one}}{{two}}{{three}}{{four}}{{five}}{{four}}{{three}}{{two}}{{one}}\n{{/e}}\n{{one}}{{two}}{{three}}{{four}}{{three}}{{two}}{{one}}\n{{/d}}\n{{one}}{{two}}{{three}}{{two}}{{one}}\n{{/c}}\n{{one}}{{two}}{{one}}\n{{/b}}\n{{one}}\n{{/a}}\n","expected":"1\n121\n12321\n1234321\n123454321\n1234321\n12321\n121\n1\n"},{"name":"List","desc":"Lists should be iterated; list items should visit the context stack.","data":{"list":[{"item":1},{"item":2},{"item":3}]},"template":"\"{{#list}}{{item}}{{/list}}\"","expected":"\"123\""},{"name":"Empty List","desc":"Empty lists should behave like falsey values.","data":{"list":[]},"template":"\"{{#list}}Yay lists!{{/list}}\"","expected":"\"\""},{"name":"Doubled","desc":"Multiple sections per template should be permitted.","data":{"bool":true,"two":"second"},"template":"{{#bool}}\n* first\n{{/bool}}\n* {{two}}\n{{#bool}}\n* third\n{{/bool}}\n","expected":"* first\n* second\n* third\n"},{"name":"Nested (Truthy)","desc":"Nested truthy sections should have their contents rendered.","data":{"bool":true},"template":"| A {{#bool}}B {{#bool}}C{{/bool}} D{{/bool}} E |","expected":"| A B C D E |"},{"name":"Nested (Falsey)","desc":"Nested falsey sections should be omitted.","data":{"bool":false},"template":"| A {{#bool}}B {{#bool}}C{{/bool}} D{{/bool}} E |","expected":"| A E |"},{"name":"Context Misses","desc":"Failed context lookups should be considered falsey.","data":{},"template":"[{{#missing}}Found key 'missing'!{{/missing}}]","expected":"[]"},{"name":"Implicit Iterator - String","desc":"Implicit iterators should directly interpolate strings.","data":{"list":["a","b","c","d","e"]},"template":"\"{{#list}}({{.}}){{/list}}\"","expected":"\"(a)(b)(c)(d)(e)\""},{"name":"Implicit Iterator - Integer","desc":"Implicit iterators should cast integers to strings and interpolate.","data":{"list":[1,2,3,4,5]},"template":"\"{{#list}}({{.}}){{/list}}\"","expected":"\"(1)(2)(3)(4)(5)\""},{"name":"Implicit Iterator - Decimal","desc":"Implicit iterators should cast decimals to strings and interpolate.","data":{"list":[1.1,2.2,3.3,4.4,5.5]},"template":"\"{{#list}}({{.}}){{/list}}\"","expected":"\"(1.1)(2.2)(3.3)(4.4)(5.5)\""},{"name":"Implicit Iterator - Array","desc":"Implicit iterators should allow iterating over nested arrays.","data":{"list":[[1,2,3],["a","b","c"]]},"template":"\"{{#list}}({{#.}}{{.}}{{/.}}){{/list}}\"","expected":"\"(123)(abc)\""},{"name":"Dotted Names - Truthy","desc":"Dotted names should be valid for Section tags.","data":{"a":{"b":{"c":true}}},"template":"\"{{#a.b.c}}Here{{/a.b.c}}\" == \"Here\"","expected":"\"Here\" == \"Here\""},{"name":"Dotted Names - Falsey","desc":"Dotted names should be valid for Section tags.","data":{"a":{"b":{"c":false}}},"template":"\"{{#a.b.c}}Here{{/a.b.c}}\" == \"\"","expected":"\"\" == \"\""},{"name":"Dotted Names - Broken Chains","desc":"Dotted names that cannot be resolved should be considered falsey.","data":{"a":{}},"template":"\"{{#a.b.c}}Here{{/a.b.c}}\" == \"\"","expected":"\"\" == \"\""},{"name":"Surrounding Whitespace","desc":"Sections should not alter surrounding whitespace.","data":{"boolean":true},"template":" | {{#boolean}}\t|\t{{/boolean}} | \n","expected":" | \t|\t | \n"},{"name":"Internal Whitespace","desc":"Sections should not alter internal whitespace.","data":{"boolean":true},"template":" | {{#boolean}} {{! Important Whitespace }}\n {{/boolean}} | \n","expected":" | \n | \n"},{"name":"Indented Inline Sections","desc":"Single-line sections should not alter surrounding whitespace.","data":{"boolean":true},"template":" {{#boolean}}YES{{/boolean}}\n {{#boolean}}GOOD{{/boolean}}\n","expected":" YES\n GOOD\n"},{"name":"Standalone Lines","desc":"Standalone lines should be removed from the template.","data":{"boolean":true},"template":"| This Is\n{{#boolean}}\n|\n{{/boolean}}\n| A Line\n","expected":"| This Is\n|\n| A Line\n"},{"name":"Indented Standalone Lines","desc":"Indented standalone lines should be removed from the template.","data":{"boolean":true},"template":"| This Is\n {{#boolean}}\n|\n {{/boolean}}\n| A Line\n","expected":"| This Is\n|\n| A Line\n"},{"name":"Standalone Line Endings","desc":"\"\\r\\n\" should be considered a newline for standalone tags.","data":{"boolean":true},"template":"|\r\n{{#boolean}}\r\n{{/boolean}}\r\n|","expected":"|\r\n|"},{"name":"Standalone Without Previous Line","desc":"Standalone tags should not require a newline to precede them.","data":{"boolean":true},"template":" {{#boolean}}\n#{{/boolean}}\n/","expected":"#\n/"},{"name":"Standalone Without Newline","desc":"Standalone tags should not require a newline to follow them.","data":{"boolean":true},"template":"#{{#boolean}}\n/\n {{/boolean}}","expected":"#\n/\n"},{"name":"Padding","desc":"Superfluous in-tag whitespace should be ignored.","data":{"boolean":true},"template":"|{{# boolean }}={{/ boolean }}|","expected":"|=|"}]} \ No newline at end of file +{"__ATTN__":"Do not edit this file; changes belong in the appropriate YAML file.","overview":"Section tags and End Section tags are used in combination to wrap a section\nof the template for iteration\n\nThese tags' content MUST be a non-whitespace character sequence NOT\ncontaining the current closing delimiter; each Section tag MUST be followed\nby an End Section tag with the same content within the same section.\n\nThis tag's content names the data to replace the tag. Name resolution is as\nfollows:\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 and the method with the given name has an\n arity of 1, the method SHOULD be called with a String containing the\n unprocessed contents of the sections; the data is the value returned.\n 5) Otherwise, the data is the value returned by calling the method with\n the given name.\n 6) 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.\nIf the data is not of a list type, it is coerced into a list as follows: if\nthe data is truthy (e.g. `!!data == true`), use a single-element list\ncontaining the data, otherwise use an empty list.\n\nFor each element in the data list, the element MUST be pushed onto the\ncontext stack, the section MUST be rendered, and the element MUST be popped\noff the context stack.\n\nSection and End Section tags SHOULD be treated as standalone when\nappropriate.\n","tests":[{"name":"Truthy","desc":"Truthy sections should have their contents rendered.","data":{"boolean":true},"template":"\"{{#boolean}}This should be rendered.{{/boolean}}\"","expected":"\"This should be rendered.\""},{"name":"Falsey","desc":"Falsey sections should have their contents omitted.","data":{"boolean":false},"template":"\"{{#boolean}}This should not be rendered.{{/boolean}}\"","expected":"\"\""},{"name":"Context","desc":"Objects and hashes should be pushed onto the context stack.","data":{"context":{"name":"Joe"}},"template":"\"{{#context}}Hi {{name}}.{{/context}}\"","expected":"\"Hi Joe.\""},{"name":"Parent contexts","desc":"Names missing in the current context are looked up in the stack.","data":{"a":"foo","b":"wrong","sec":{"b":"bar"},"c":{"d":"baz"}},"template":"\"{{#sec}}{{a}}, {{b}}, {{c.d}}{{/sec}}\"","expected":"\"foo, bar, baz\""},{"name":"Variable test","desc":"Non-false sections have their value at the top of context,\naccessible as {{.}} or through the parent context. This gives\na simple way to display content conditionally if a variable exists.\n","data":{"foo":"bar"},"template":"\"{{#foo}} {{.}} is {{foo}} {{/foo}}\"","expected":"\"bar is bar\""},{"name":"List Contexts","desc":"All elements on the context stack should be accessible within lists.","data":{"tops":[{"tname":{"upper":"A","lower":"a"},"middles":[{"mname":"1","bottoms":[{"bname":"x"},{"bname":"y"}]}]}]},"template":"{{#tops}}{{#middles}}{{tname.lower}}{{mname}}.{{#bottoms}}{{tname.upper}}{{mname}}{{bname}}.{{/bottoms}}{{/middles}}{{/tops}}","expected":"a1.A1x.A1y."},{"name":"Deeply Nested Contexts","desc":"All elements on the context stack should be accessible.","data":{"a":{"one":1},"b":{"two":2},"c":{"three":3,"d":{"four":4,"five":5}}},"template":"{{#a}}\n{{one}}\n{{#b}}\n{{one}}{{two}}{{one}}\n{{#c}}\n{{one}}{{two}}{{three}}{{two}}{{one}}\n{{#d}}\n{{one}}{{two}}{{three}}{{four}}{{three}}{{two}}{{one}}\n{{#five}}\n{{one}}{{two}}{{three}}{{four}}{{five}}{{four}}{{three}}{{two}}{{one}}\n{{one}}{{two}}{{three}}{{four}}{{.}}6{{.}}{{four}}{{three}}{{two}}{{one}}\n{{one}}{{two}}{{three}}{{four}}{{five}}{{four}}{{three}}{{two}}{{one}}\n{{/five}}\n{{one}}{{two}}{{three}}{{four}}{{three}}{{two}}{{one}}\n{{/d}}\n{{one}}{{two}}{{three}}{{two}}{{one}}\n{{/c}}\n{{one}}{{two}}{{one}}\n{{/b}}\n{{one}}\n{{/a}}\n","expected":"1\n121\n12321\n1234321\n123454321\n12345654321\n123454321\n1234321\n12321\n121\n1\n"},{"name":"List","desc":"Lists should be iterated; list items should visit the context stack.","data":{"list":[{"item":1},{"item":2},{"item":3}]},"template":"\"{{#list}}{{item}}{{/list}}\"","expected":"\"123\""},{"name":"Empty List","desc":"Empty lists should behave like falsey values.","data":{"list":[]},"template":"\"{{#list}}Yay lists!{{/list}}\"","expected":"\"\""},{"name":"Doubled","desc":"Multiple sections per template should be permitted.","data":{"bool":true,"two":"second"},"template":"{{#bool}}\n* first\n{{/bool}}\n* {{two}}\n{{#bool}}\n* third\n{{/bool}}\n","expected":"* first\n* second\n* third\n"},{"name":"Nested (Truthy)","desc":"Nested truthy sections should have their contents rendered.","data":{"bool":true},"template":"| A {{#bool}}B {{#bool}}C{{/bool}} D{{/bool}} E |","expected":"| A B C D E |"},{"name":"Nested (Falsey)","desc":"Nested falsey sections should be omitted.","data":{"bool":false},"template":"| A {{#bool}}B {{#bool}}C{{/bool}} D{{/bool}} E |","expected":"| A E |"},{"name":"Context Misses","desc":"Failed context lookups should be considered falsey.","data":{},"template":"[{{#missing}}Found key 'missing'!{{/missing}}]","expected":"[]"},{"name":"Implicit Iterator - String","desc":"Implicit iterators should directly interpolate strings.","data":{"list":["a","b","c","d","e"]},"template":"\"{{#list}}({{.}}){{/list}}\"","expected":"\"(a)(b)(c)(d)(e)\""},{"name":"Implicit Iterator - Integer","desc":"Implicit iterators should cast integers to strings and interpolate.","data":{"list":[1,2,3,4,5]},"template":"\"{{#list}}({{.}}){{/list}}\"","expected":"\"(1)(2)(3)(4)(5)\""},{"name":"Implicit Iterator - Decimal","desc":"Implicit iterators should cast decimals to strings and interpolate.","data":{"list":[1.1,2.2,3.3,4.4,5.5]},"template":"\"{{#list}}({{.}}){{/list}}\"","expected":"\"(1.1)(2.2)(3.3)(4.4)(5.5)\""},{"name":"Implicit Iterator - Array","desc":"Implicit iterators should allow iterating over nested arrays.","data":{"list":[[1,2,3],["a","b","c"]]},"template":"\"{{#list}}({{#.}}{{.}}{{/.}}){{/list}}\"","expected":"\"(123)(abc)\""},{"name":"Dotted Names - Truthy","desc":"Dotted names should be valid for Section tags.","data":{"a":{"b":{"c":true}}},"template":"\"{{#a.b.c}}Here{{/a.b.c}}\" == \"Here\"","expected":"\"Here\" == \"Here\""},{"name":"Dotted Names - Falsey","desc":"Dotted names should be valid for Section tags.","data":{"a":{"b":{"c":false}}},"template":"\"{{#a.b.c}}Here{{/a.b.c}}\" == \"\"","expected":"\"\" == \"\""},{"name":"Dotted Names - Broken Chains","desc":"Dotted names that cannot be resolved should be considered falsey.","data":{"a":{}},"template":"\"{{#a.b.c}}Here{{/a.b.c}}\" == \"\"","expected":"\"\" == \"\""},{"name":"Surrounding Whitespace","desc":"Sections should not alter surrounding whitespace.","data":{"boolean":true},"template":" | {{#boolean}}\t|\t{{/boolean}} | \n","expected":" | \t|\t | \n"},{"name":"Internal Whitespace","desc":"Sections should not alter internal whitespace.","data":{"boolean":true},"template":" | {{#boolean}} {{! Important Whitespace }}\n {{/boolean}} | \n","expected":" | \n | \n"},{"name":"Indented Inline Sections","desc":"Single-line sections should not alter surrounding whitespace.","data":{"boolean":true},"template":" {{#boolean}}YES{{/boolean}}\n {{#boolean}}GOOD{{/boolean}}\n","expected":" YES\n GOOD\n"},{"name":"Standalone Lines","desc":"Standalone lines should be removed from the template.","data":{"boolean":true},"template":"| This Is\n{{#boolean}}\n|\n{{/boolean}}\n| A Line\n","expected":"| This Is\n|\n| A Line\n"},{"name":"Indented Standalone Lines","desc":"Indented standalone lines should be removed from the template.","data":{"boolean":true},"template":"| This Is\n {{#boolean}}\n|\n {{/boolean}}\n| A Line\n","expected":"| This Is\n|\n| A Line\n"},{"name":"Standalone Line Endings","desc":"\"\\r\\n\" should be considered a newline for standalone tags.","data":{"boolean":true},"template":"|\r\n{{#boolean}}\r\n{{/boolean}}\r\n|","expected":"|\r\n|"},{"name":"Standalone Without Previous Line","desc":"Standalone tags should not require a newline to precede them.","data":{"boolean":true},"template":" {{#boolean}}\n#{{/boolean}}\n/","expected":"#\n/"},{"name":"Standalone Without Newline","desc":"Standalone tags should not require a newline to follow them.","data":{"boolean":true},"template":"#{{#boolean}}\n/\n {{/boolean}}","expected":"#\n/\n"},{"name":"Padding","desc":"Superfluous in-tag whitespace should be ignored.","data":{"boolean":true},"template":"|{{# boolean }}={{/ boolean }}|","expected":"|=|"}]} \ No newline at end of file -- cgit v1.2.1