-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Resolve #589, and possibly fix other manifestations of this bug #618
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
78ed7a0
prevent value from being returned by Context.prototype.lookup if look…
raymond-lam a51e1c2
add test for renderability of Array.length via dot notation
raymond-lam 6382ec9
Remove `typeof obj === 'object'` constraint in prop lookup
dasilvacontin 8108045
pop lookup needs to use hasOwnProperty for non-objs
raymond-lam d2f3a48
re-add constraint in prop lookup, but make property lookups for prim…
raymond-lam 0e607fe
add test to address #589 specifically
raymond-lam fa75abc
enhance readability of primitiveHasOwnProperty and add comments to ex…
raymond-lam bd12d95
wrap code comments explaining , and use a different example
raymond-lam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,19 @@ | |
return obj != null && typeof obj === 'object' && (propName in obj); | ||
} | ||
|
||
/** | ||
* Safe way of detecting whether or not the given thing is a primitive and | ||
* whether it has the given property | ||
*/ | ||
function primitiveHasOwnProperty (primitive, propName) { | ||
return ( | ||
primitive != null | ||
&& typeof primitive !== 'object' | ||
&& primitive.hasOwnProperty | ||
&& primitive.hasOwnProperty(propName) | ||
); | ||
} | ||
|
||
// Workaround for https://issues.apache.org/jira/browse/COUCHDB-577 | ||
// See https://github.com/janl/mustache.js/issues/189 | ||
var regExpTest = RegExp.prototype.test; | ||
|
@@ -377,11 +390,11 @@ | |
if (cache.hasOwnProperty(name)) { | ||
value = cache[name]; | ||
} else { | ||
var context = this, names, index, lookupHit = false; | ||
var context = this, intermediateValue, names, index, lookupHit = false; | ||
|
||
while (context) { | ||
if (name.indexOf('.') > 0) { | ||
value = context.view; | ||
intermediateValue = context.view; | ||
names = name.split('.'); | ||
index = 0; | ||
|
||
|
@@ -395,20 +408,51 @@ | |
* | ||
* This is specially necessary for when the value has been set to | ||
* `undefined` and we want to avoid looking up parent contexts. | ||
* | ||
* In the case where dot notation is used, we consider the lookup | ||
* to be successful even if the last "object" in the path is | ||
* not actually an object but a primitive (e.g., a string, or an | ||
* integer), because it is sometimes useful to access a property | ||
* of an autoboxed primitive, such as the length of a string. | ||
**/ | ||
while (value != null && index < names.length) { | ||
while (intermediateValue != null && index < names.length) { | ||
if (index === names.length - 1) | ||
lookupHit = hasProperty(value, names[index]); | ||
lookupHit = ( | ||
hasProperty(intermediateValue, names[index]) | ||
|| primitiveHasOwnProperty(intermediateValue, names[index]) | ||
); | ||
|
||
value = value[names[index++]]; | ||
intermediateValue = intermediateValue[names[index++]]; | ||
} | ||
} else { | ||
value = context.view[name]; | ||
intermediateValue = context.view[name]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block could use some clarification on why we are only using |
||
|
||
/** | ||
* Only checking against `hasProperty`, which always returns `false` if | ||
* `context.view` is not an object. Deliberately omitting the check | ||
* against `primitiveHasOwnProperty` if dot notation is not used. | ||
* | ||
* Consider this example: | ||
* ``` | ||
* Mustache.render("The length of a football field is {{#length}}{{length}}{{/length}}.", {length: "100 yards"}) | ||
* ``` | ||
* | ||
* If we were to check also against `primitiveHasOwnProperty`, as we do | ||
* in the dot notation case, then render call would return: | ||
* | ||
* "The length of a football field is 9." | ||
* | ||
* rather than the expected: | ||
* | ||
* "The length of a football field is 100 yards." | ||
**/ | ||
lookupHit = hasProperty(context.view, name); | ||
} | ||
|
||
if (lookupHit) | ||
if (lookupHit) { | ||
value = intermediateValue; | ||
break; | ||
} | ||
|
||
context = context.parent; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,5 +19,6 @@ | |
truthy: { | ||
zero: 0, | ||
notTrue: false | ||
} | ||
}, | ||
singletonList: [{singletonItem: "singleton item"}] | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesomee. 🙌