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

Partials useParams fix to allow square bracket properties #110

Merged
merged 2 commits into from
Jul 26, 2020

Conversation

joelpurra
Copy link

Was trying to use doT 1.0.0+ partials with parameters, but ran into problems trying to pass in something like myJsonObject["property-with-dashes"]. The original useParams looked weird (and it's undocumented), so rewrote the parameter part, and fixed some related problems at the same time.

  • Square bracket object properties, it['bar'], it['bar baz']
  • Arrays with index, it[1]
  • Square bracket object properties with variables it[myvar] (untested)
  • Deep object addressing, it['bar baz'].qux[1]
  • Uppercase or mixed case variable names

Commited new Mocha tests separately.

Usage example

{{##def.tmp:input:<div>{{!input.foo}}</div>#}}

{{#def.tmp:myJsonObject["property-with-dashes"]}}

If this patch doesn't get accepted, you can still use it by changing the settings:

doT.templateSettings.useParams = /(^|[^\w$])def(?:\.|\[[\'\"])([\w$\.]+)(?:[\'\"]\])?\s*\:\s*((?:[_a-z$][\w$]+)(?:\.[_a-z$][\w$]+|\[(?:(?:[_a-z$][\w$]+)|\d+|(["'])(?![^\\]\4).+\4)\])*)\s*$/i;

Appendices

Regexp with test data. Use for example regexpal.com with Case insensitive (i) and ^$ match at line breaks (m).

Original useParams regex

(^|[^\w$])def(?:\.|\[[\'\"])([\w$\.]+)(?:[\'\"]\])?\s*\:\s*([\w$\.]+|\"[^\"]+\"|\'[^\']+\'|\{[^\}]+\})

Naïve fix, matches too many rows

(^|[^\w$])def(?:\.|\[[\'\"])([\w$\.]+)(?:[\'\"]\])?\s*\:\s*((?:[\w$\.]+|\[[^\]]+\])+)

Better fix

(^|[^\w$])def(?:\.|\[[\'\"])([\w$\.]+)(?:[\'\"]\])?\s*\:\s*((?:[_a-z$][\w$]+)(?:\.[_a-z$][\w$]+|\[(?:(?:[_a-z$][\w$]+)|\d+|(["'])(?![^\\]\4).+\4)\])*)\s*$

Test data

// This block should match the entire row for all rows
def.fnname:param
#def.fnname:param
def.fnname:param.foo
def.fnname:param['foo']
def.fnname:param["foo"]
def.fnname:param['foo bar']
def.fnname:param["foo bar"]
def.fnname:param[01234]
def.fnname:param[varname]
def.fnname:param.foo.qux["quux"][43210]
def.fnname:param['foo'].qux["quux"][43210]
def.fnname:param["foo"].qux["quux"][43210]
def.fnname:param['foo bar'].qux["quux"][43210]
def.fnname:param["foo bar"].qux["quux"][43210]
def.fnname:param[01234].qux["quux"][43210]
def.fnname:param[varname].qux["quux"][43210]
def.fnname:param[varname].qux["quåäö+ππππux"][43210]
def.fnname:Param
def.fnname:Param.Foo
def.fnname:Param['Foo Bar']


// This block should not match any part of any rows
def.fnname:param'foo'
def.fnname:param"foo bar"
def.fnname:param.01234
def.fnname:param.foo.qux."quux".43210
def.fnname:param'foo bar'.qux"quux".43210
def.fnname:param.
#def.fnname:param.
def.fnname:param.foo.
def.fnname:param['foo bar'].

joelpurra added a commit to joelpurra/ep-the-corrections that referenced this pull request Feb 13, 2014
@epoberezkin epoberezkin changed the base branch from master to v2 July 26, 2020 08:34
@epoberezkin epoberezkin merged commit 273510f into olado:v2 Jul 26, 2020
@joelpurra joelpurra deleted the useParams-square-bracket-properties branch July 26, 2020 09:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants