Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow alternative path separators in references #130

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion dist/compiler.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions dist/compiler/extensions/references.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions dist/parser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/compiler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';
import Context from './compiler/context';
import escapableRaw from './compiler/extensions/escapableRaw';
import references from './compiler/extensions/references';
import builtinHelpers from './compiler/extensions/builtinHelpers';
import htmlEntities from './compiler/extensions/htmlEntities';
import adjustAttrs from './compiler/extensions/adjustAttrs';
Expand Down Expand Up @@ -48,6 +49,6 @@ let compiler = {
}
};

compiler.useExtensions([builtinHelpers, escapableRaw, htmlEntities, adjustAttrs, buildInstructions]);
compiler.useExtensions([references, builtinHelpers, escapableRaw, htmlEntities, adjustAttrs, buildInstructions]);

export default compiler;
22 changes: 22 additions & 0 deletions src/compiler/extensions/references.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';
import visitor from '../visitor';

let generatedWalker = visitor.build({
TORNADO_REFERENCE(item) {
let {node} = item;
node = node[1];
let sepParam = node.params.filter(param => param[1].key === 'sep')
.reduce(((acc, param) => param[1]), null);
if (sepParam && sepParam.val !== '.') {
node.key = node.key.join('.').split(sepParam.val);
}
}
});

let builtinHelpers = {
transforms: [function (ast, options) {
return generatedWalker(ast, options.context);
}]
};

export default builtinHelpers;
6 changes: 3 additions & 3 deletions src/grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ tornado_body_tag_end
}

tornado_reference
= lbrace r:tornado_key filters:tornado_filters rbrace {
= lbrace r:tornado_key p:tornado_params rbrace {
var key = r.split('.');
if (r === '.') {
key = [];
}
return ['TORNADO_REFERENCE', {key: key, filters: filters}]
return ['TORNADO_REFERENCE', {key: key, params: p}]
}

// TODO: tornado partial key can be a reference e.g. {#"{foo}"/}
Expand All @@ -167,7 +167,7 @@ tornado_partial
}

tornado_key
= first:[a-zA-Z_$\.] after:[a-zA-Z0-9_$-\.]* {
= first:[a-zA-Z_$\.,\|] after:[a-zA-Z0-9_$-\.,\|]* {
return first + after.join('');
}

Expand Down
11 changes: 11 additions & 0 deletions test/acceptance/reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,17 @@ let suite = {
}
},
expectedHTML: '<blockquote>The name is Bond.</blockquote>'
},
{
description: 'Reference with a non-dot separator, where dots in reference are not part of the path',
name: 'dots',
template: '{com.linkedin.dots.in.path|myVal sep="|"}',
context: {
'com.linkedin.dots.in.path': {
myVal: 'win!'
}
},
expectedHTML: 'win!'
}
]
};
Expand Down
2 changes: 1 addition & 1 deletion test/testRunner.es6
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ function runSuites(suites) {
test.setup(parser, compiler);
}
let html = test.template;
let ast = parser.parse(html);
for (let i = 0; i < 2; i++) {
let ast = parser.parse(html);
compiler.mode = compilerModes[i];
let compiledTemplate = compiler.compile(ast, test.name || 'abc');
let tl;
Expand Down