Comparing version 2.3.12 to 2.3.13
/** | ||
* The riot template engine | ||
* @version v2.3.12 | ||
* @version v2.3.13 | ||
*/ | ||
@@ -12,3 +12,3 @@ | ||
* `brackets.settings` Mirrors the `riot.settings` object | ||
* `brackets.set ` The recommended option to change the current tiot brackets | ||
* `brackets.set ` The recommended form to change the current riot brackets | ||
*/ | ||
@@ -68,3 +68,4 @@ | ||
} | ||
_pairs[4] = _regex(_pairs[1].length > 1 ? /(?:^|[^\\]){[\S\s]*?}/ : /(?:^|[^\\]){[^}]*}/) | ||
_pairs[4] = _regex(_pairs[1].length > 1 ? /{[\S\s]*?}/ : /{[^}]*}/) | ||
_pairs[5] = _regex(/\\({|})/g) | ||
@@ -71,0 +72,0 @@ _pairs[6] = _regex(/(\\?)({)/g) |
@@ -1,2 +0,2 @@ | ||
/* riot-tmpl v2.3.12, @license MIT, (c) 2015 Muut Inc. + contributors */ | ||
/* riot-tmpl v2.3.13, @license MIT, (c) 2015 Muut Inc. + contributors */ | ||
;(function (window) { | ||
@@ -10,3 +10,3 @@ 'use strict' // eslint-disable-line | ||
* `brackets.settings` Mirrors the `riot.settings` object | ||
* `brackets.set ` The recommended option to change the current tiot brackets | ||
* `brackets.set ` The recommended form to change the current riot brackets | ||
*/ | ||
@@ -66,3 +66,4 @@ | ||
} | ||
_pairs[4] = _regex(_pairs[1].length > 1 ? /(?:^|[^\\]){[\S\s]*?}/ : /(?:^|[^\\]){[^}]*}/) | ||
_pairs[4] = _regex(_pairs[1].length > 1 ? /{[\S\s]*?}/ : /{[^}]*}/) | ||
_pairs[5] = _regex(/\\({|})/g) | ||
@@ -69,0 +70,0 @@ _pairs[6] = _regex(/(\\?)({)/g) |
131
doc/API.md
@@ -21,35 +21,14 @@ | ||
### `tmpl.hasExpr` function | ||
### `hasExpr` function (alias) | ||
_usage:_ `tmpl.hasExpr( str )` | ||
This is an alias to [`brackets.hasExpr`](#hasxpr-function) | ||
Checks for an expression within a string, using the current brackets. | ||
_parameters_ | ||
### `loopKeys` function (alias, private) | ||
* `str` : string - String where to search | ||
This is an alias to [`brackets.loopKeys`](#loopkeys-function-private) | ||
_returns:_ boolean - `true` if the string contains an expression | ||
NOTE: This function only checks for a pair of unescaped riot brackets, does not validate | ||
the expression nor excludes brackets within quotes. | ||
### `errorHandler` property | ||
### `tmpl.loopKeys` function | ||
_usage:_ `tmpl.loopKeys( expr )` | ||
Parses the `each` expression to detect how to map the collection data to the child tags | ||
(used by browser/tag/each.js) | ||
If the received string does not contains an expression, the behavior is undefined. | ||
_parameters_ | ||
* `expr` : string - value of the 'each' attribute | ||
_returns:_ object - object needed to check how the items in the collection should be mapped to the child tags | ||
### `tmpl.errorHandler` property | ||
_type:_ function | ||
@@ -59,3 +38,3 @@ | ||
The `errorHandler` property allows to detect errors _in the evaluation_, by setting its value to a function that receives the generated Error object, augmented with an object `riotData` containing the properties `tagName` and `\_riot_id` of the context at error time. | ||
The `tmpl.errorHandler` property allows to detect errors _in the evaluation_, by setting its value to a function that receives the generated Error object, augmented with an object `riotData` containing the properties `tagName` and `_riot_id` of the context at error time. | ||
@@ -72,3 +51,3 @@ Other (usually fatal) errors, such as "Parse Error" generated by the Function constructor, are not intercepted. | ||
- Control characters from `\x00` to `\x1F` that can be changed by browsers or minifier tools | ||
- Control characters from `\x00` to `\x1F` that can be changed by browsers or minification tools | ||
- Alphanumeric `a-z`, `A-Z`, and `0-9`, wich are confused with JS variable names | ||
@@ -78,60 +57,119 @@ - Single and double quotes, comma, semicolon and backslash `'`, `"`, `,`, `;`, `\`, for obvious reasons | ||
See the [CHANGES](CHANGES.md) document for details. | ||
### `brackets` function | ||
_Syntax:_ `brackets( RegExp | number ) : RegExp | string` | ||
_Syntax:_ `brackets( reOrIdx ) : RegExp | string` | ||
The brackets function accepts a RegExp or numeric parameter. | ||
With a numeric parameter, brackets returns the current left (0) or right (1) bracket characters. | ||
_parameters_ | ||
With a regex, this function returns the original regex if the current brackets are the defaults, or a new one with the default brackets replaced by the current custom brackets. | ||
* `reOrIdx` : RegExp or number - regex to convert or index number of backets part | ||
_returns:_ RegExp or string - With a regex, this function returns the original regex if the current brackets are the defaults, or a new one with the default brackets replaced by the current custom brackets. | ||
With a numeric parameter, returns a value based on current brackets according to the following table (defaults are within parenthesis): | ||
### `brackets.set` function | ||
* 0: left bracket (`{`) | ||
* 1: right bracket (`}`) | ||
* 2: left escaped bracket (`{`)* | ||
* 3: right escaped bracket (`}`)* | ||
* 4: RegExp which matches an unescaped barckets pair (`/(?:^\|[^\\]){[^}]*}/`)\*\* | ||
_Syntax:_ `brackets.set( brackets_string )` | ||
\* only characters `[]()*+?.^$|` are escaped. | ||
Receives the new string for the brackets pair. This function checks their parameter and reconfigures the internal state immediately. | ||
\*\* not 100% accurate, because it does not recognize brackets within strings. | ||
### `brackets.settings` property | ||
### `set` function | ||
_Syntax:_ `brackets.set( brackets_pair )` | ||
Receives the new string for the brackets pair. If you pass a falsy value, brackets are reset to default. | ||
This function checks their parameter and reconfigures the internal state immediately. | ||
_parameters_ | ||
* `brackets_pair` : string - new custom brackets pair. The start and end is separated with a space character. | ||
### `settings` property | ||
_Type:_ object | ||
Mirror the `riot.settings` object or other user object with where read or write the current brackets string. Unlike with `brackets.set`, reconfiguration for changes in this property, called `brackets`, will be applied in the next use of the `brackets` function. | ||
Mirror the `riot.settings` object or other user object where read or write the current brackets string. Unlike with `brackets.set`, reconfiguration for changes in `brackets.settings.brackets` or `riot.settings.brackets` will be applied in the next use of the `brackets` function. | ||
### `brackets.split` function (private) | ||
### `split` function (private) | ||
_Syntax:_ `brackets.split( template [, brackets_array] ) : Array` | ||
Used by tmpl and riot-compiler. | ||
### `brackets.array` function (private) | ||
### `array` function (private) | ||
_Syntax:_ `brackets.array( [brackets_string] ) : Array` | ||
Used by riot-compiler. | ||
### `brackets.R_MLCOMMS` property | ||
### `hasExpr` function | ||
_usage:_ `brackets.hasExpr( str )` | ||
Checks for an expression within a string, using the current brackets. | ||
_parameters_ | ||
* `str` : string - String where to search | ||
_returns:_ boolean - `true` if the string contains an expression | ||
NOTE: This function only checks for a pair of unescaped riot brackets, does not validate | ||
the expression nor excludes brackets within quotes. | ||
### `loopKeys` function (private) | ||
_usage:_ `brackets.loopKeys( expr )` | ||
Used by browser/tag/each.js | ||
Parses the `expr` string to detect how to map the collection data to the child tags. | ||
If the received string does not contains an expression, the behavior is undefined. | ||
_parameters_ | ||
* `expr` : string - value of the `each` attribute | ||
_returns:_ object - object needed to check how the items in the collection should be mapped to the child tags | ||
### `R_MLCOMMS` property | ||
_Type:_ RegExp | ||
Used by internal functions and shared with the riot compiler, matches valid, multiline JavaScript comments in almost all forms. Can handle embedded sequences `/*`, `*\/` and `//` in these. Skips non-valid comments like `/*/`. | ||
Used by internal functions and shared with the riot compiler, matches valid, multiline JavaScript comments in almost all forms. Can handle embedded sequences `/*`, `*\/` and `//` in these. Skips non-valid comments like `/*/`. | ||
`R_MLCOMMS` does not make captures. | ||
### `brackets.R_STRINGS` property | ||
### `R_STRINGS` property | ||
_Type:_ RegExp | ||
Used by internal functions and shared with the riot compiler, matches single or double quoted strings, handles embedded quotes and multi-line strings (not in accordance with the JavaScript specs). It is not for ES6 template strings, these are too complex for a regex. | ||
Used by internal functions and shared with the riot compiler, matches single or double quoted strings, handles embedded quotes and multi-line strings (not in accordance with the JavaScript spec). It is not for ES6 template strings, these are too complex for a regex. | ||
`R_STRINGS` does not make captures. | ||
### `brackets.S_QBLOCK` property | ||
### `S_QBLOCK` property | ||
_Type:_ string | ||
Combines the `brackets.R_STRINGS` source with regexes for matching quoted strings, division symbols, and literal regexes. | ||
Combines the `brackets.R_STRINGS` source with regexes for matching division symbols and literal regexes. | ||
When dealing with clean JavaScript code, i.e. without comments, this is the only string you need to instantiate your RegExp object. For code containing comments, `S_QBLOCK` needs to be combined with other regexes for exclusion of multiline and single-line comments (`MLCOMMS` is one of both). | ||
When dealing with clean JavaScript code, i.e. without comments, this is the only string you need to instantiate your RegExp object. For code containing comments, `S_QBLOCK` needs to be combined with other regexes for exclusion of multiline and single-line comments (`MLCOMMS` can be one of both). | ||
@@ -143,2 +181,5 @@ The `S_QBLOCK` part captures in `$1` and `$2` a single slash, depending if it matches a division symbol ($1) or a regex ($2). If there's no matches for any of these, they have empty strings. | ||
```js | ||
// We can use riot.util.brackets if riot is in context | ||
var brackets = require('riot-tmpl').brackets | ||
// Creates the regex, $1 encloses the whole S_QBLOCK, for easier detection | ||
@@ -145,0 +186,0 @@ var JS_RMCOMMS = new RegExp( |
@@ -8,3 +8,3 @@ | ||
Escaped brackets _within expressions_ are left unescaped, except in JavaScript strings and regexes, where are preserved. So far, I have not found a case where the brackets must remain escaped. | ||
Escaped brackets _within expressions_ are left unescaped, except in JavaScript strings and regexes, where are preserved. So far, I have not found a case where brackets in expressions must remain escaped. | ||
@@ -17,3 +17,3 @@ Backslashes in the HTML parts are not touched. | ||
The new `tmpl.errorHandler` property allows to detect errors _in the evaluation_, by setting its value to a function that receives the generated Error object, augmented with an object `riotData` containing the properties `tagName` and `\_riot_id` of the context at error time. | ||
The new `tmpl.errorHandler` property allows to detect errors _in the evaluation_, by setting its value to a function that receives the generated Error object, augmented with an object `riotData` containing the properties `tagName` and `_riot_id` of the context at error time. | ||
@@ -24,22 +24,29 @@ Other (usually fatal) errors, such as "Parse Error" generated for the Function constructor, are not intercepted. | ||
Ref: [riot#1189](https://github.com/riot/riot/issues/1189) | ||
Example: | ||
```html | ||
<mytag></mytag> | ||
<script type="riot/tag"> | ||
<mytag><p>{ foo == 'bar' }</p></mytag> | ||
</script> | ||
<script> | ||
riot.util.tmpl.errorHandler = myLogger | ||
riot.mount('*') | ||
## Why to use tmpl.hasExpr and tmpl.loopKeys? | ||
function myLogger(err) { | ||
console.log(err.message + ' in ' + err.riotData.tagName) | ||
} | ||
</script> | ||
``` | ||
outputs "Cannot read property 'bar' of undefined in MYTAG" in the console. | ||
Encapsulation. Changes to the internal `tmpl` or `brackets` function are easy if other code don't depends on the format of parsed expressions. `hasExpr` and `loopKeys` has optimized regexes to do the work and the riot code can stay a bit clearer. | ||
Ref: [riot#871](https://github.com/riot/riot/issues/871), [riot#1189](https://github.com/riot/riot/issues/1189) | ||
`tmpl.hasExpr` is intended for general use, while `loopKeys` is useful only for riot. | ||
## The new brackets function | ||
In my personal opinion this function must have been designed as an array from the beginning, configured by riot at the instantiation time, and from the user via an explicit function call, without the possibility of further changes; plus an auxiliary function to convert custom regexes. | ||
brackets 2.3 combines the behavior of brackets 2.2 with a new one, based on a function to make immediate, more secure changes to custom brackets. There is a performance penalty in supporting both schemes, but compatibility is maintained. | ||
brackets 2.3 combines the behavior of brackets 2.2 with a new one, based on these idea. There is a performance penalty in supporting both, but compatibility is maintained. | ||
If riot is available when `brackets` is instantiated, `brackets` will use the configuration in `riot.settings`. If not, you can link a configuration later, through the new `brackets.settings` property, which accepts a reference to `riot.settings` or other object where read and write new brackets values. In this way, brackets works as in previous versions. | ||
If riot is available when `brackets` is instantiated, `brackets` will use the configuration in `riot.settings`. If not, you can link a configuration later, through the new `brackets.settings` property, which accepts a reference to `riot.settings` or other object where read and write new brackets values. | ||
The other, recommended option, is call to the new `breackets.set` function with the value for the brackets. The only difference is `brackets.set` checks and make the changes immediately, while using the `settings` property the reconfiguration is delayed to first use. | ||
There's more new functions and properties added to `brackets`, you can use the regexes, these will be maintained, but the additional functions are for internal use. | ||
It is all, syntax and behavior are the same as older versions: `brackets(regex_or_number)`. | ||
@@ -77,2 +84,13 @@ | ||
## Why to use brackets.hasExpr and brackets.loopKeys? | ||
Encapsulation. Changes to the internal `tmpl` or `brackets` function are easy if other code don't depends on the format of parsed expressions. `hasExpr` and `loopKeys` has optimized regexes to do the work and the riot code can stay a bit clearer. | ||
`brackets.hasExpr` is intended for general use, while `brackets.loopKeys` is useful only for riot. | ||
## Final Note | ||
There's more new functions and properties added to `brackets`, you can use [hasExpr](https://github.com/riot/tmpl/blob/dev/doc/API.md#hasexpr-function) and the [regexes](https://github.com/riot/tmpl/blob/dev/doc/API.md#r_mlcomms-property) which will be maintained, but the additional functions are for internal use. | ||
_@amarcruz_ |
@@ -6,3 +6,3 @@ /** | ||
* `brackets.settings` Mirrors the `riot.settings` object | ||
* `brackets.set ` The recommended option to change the current tiot brackets | ||
* `brackets.set ` The recommended form to change the current riot brackets | ||
*/ | ||
@@ -129,3 +129,4 @@ | ||
} | ||
_pairs[$_RIX_TEST] = _regex(_pairs[1].length > 1 ? /(?:^|[^\\]){[\S\s]*?}/ : /(?:^|[^\\]){[^}]*}/) | ||
//_pairs[$_RIX_TEST] = _regex(_pairs[1].length > 1 ? /(?:^|[^\\]){[\S\s]*?}/ : /(?:^|[^\\]){[^}]*}/) | ||
_pairs[$_RIX_TEST] = _regex(_pairs[1].length > 1 ? /{[\S\s]*?}/ : /{[^}]*}/) | ||
_pairs[$_RIX_ESC] = _regex(/\\({|})/g) | ||
@@ -132,0 +133,0 @@ _pairs[$_RIX_OPEN] = _regex(/(\\?)({)/g) // for _split() |
{ | ||
"name": "riot-tmpl", | ||
"version": "2.3.12", | ||
"version": "2.3.13", | ||
"description": "The riot template engine", | ||
@@ -32,3 +32,3 @@ "main": "dist/tmpl.js", | ||
"coveralls": "^2.11.4", | ||
"eslint": "^1.7.3", | ||
"eslint": "^1.10.1", | ||
"expect.js": "^0.3.1", | ||
@@ -41,3 +41,3 @@ "jspreproc": "^0.2.4", | ||
"karma-phantomjs-launcher": "^0.2.1", | ||
"mocha": "^2.3.3", | ||
"mocha": "^2.3.4", | ||
"phantomjs": "^1.9.18" | ||
@@ -44,0 +44,0 @@ }, |
/* eslint-env node, mocha */ | ||
/* eslint camelcase: false */ | ||
/* eslint camelcase: 0 */ | ||
@@ -26,5 +26,5 @@ globalVar = 5 | ||
describe('riot-tmpl', function() { | ||
describe('riot-tmpl', function () { | ||
describe('compiles specs', function() { | ||
describe('compiles specs', function () { | ||
@@ -221,2 +221,4 @@ //// return values | ||
expect(render('str = "/\\{}\\/\\n/"')).to.be('str = "/{}\\/\\n/"') | ||
expect(render('<p str2="\\{foo}">\\{ message }</p>')).to.be('<p str2="{foo}">{ message }</p>') | ||
expect(render('str="\\\\{foo}"')).to.be('str="\\{foo}"') | ||
}) | ||
@@ -465,3 +467,3 @@ | ||
it('tmpl.hasExpr: test for expression existence', function () { | ||
it('tmpl.hasExpr: test for expression (brackets) existence', function () { | ||
expect(tmpl.hasExpr('{}')).to.be(true) | ||
@@ -472,4 +474,4 @@ expect(tmpl.hasExpr(' {} ')).to.be(true) | ||
expect(tmpl.hasExpr('"{ " }')).to.be(true) | ||
expect(tmpl.hasExpr('\\{ 123 } ')).to.be(false) | ||
expect(tmpl.hasExpr(' \\{}')).to.be(false) | ||
expect(tmpl.hasExpr('\\{ 123 } ')).to.be(true) | ||
expect(tmpl.hasExpr(' \\{}')).to.be(true) | ||
expect(tmpl.hasExpr(' }{ ')).to.be(false) | ||
@@ -476,0 +478,0 @@ }) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
104417
2274