Skip to content

Commit

Permalink
Prevent potential crash with viewFor bad use
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasbadia committed Aug 25, 2019
1 parent 7b9c076 commit a65ae98
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 18 deletions.
12 changes: 6 additions & 6 deletions frameworks/core_foundation/core.js
Expand Up @@ -181,17 +181,17 @@ SC.mixin(/** @lends SC */ {
*/
viewFor: function (element) {
//@if(debug)
// Debug mode only arrgument validation.
// Ensure that every argument is correct and that the proper number of arguments is given.
if (!(element instanceof Element)) {
SC.error("Developer Error: Attempt to retrieve the SC.View instance for a non-element in SC.viewFor(): %@".fmt(element));
}

if (arguments.length > 1) {
SC.warn("Developer Warning: SC.viewFor() is meant to be used with only one argument: element");
}
//@endif

// Ensure the element argument is correct.
if (!(element instanceof Element)) {
SC.error("Attempt to retrieve the SC.View instance for a non-element in SC.viewFor(): %@".fmt(element));
return null;
}

// Search for the view for the given element.
var viewCache = SC.View.views,
view = viewCache[element.getAttribute('id')];
Expand Down
16 changes: 4 additions & 12 deletions frameworks/core_foundation/tests/core_tests.js
Expand Up @@ -24,19 +24,11 @@ module("SC", {
});

test("SC.viewFor() Argument validation", function() {
try {
SC.viewFor();
ok(false, "Throws an exception when the argument is not given.");
} catch (ex) {
ok(true, "Throws an exception when the argument is not given.");
}
var ret = SC.viewFor();
equals(ret, null, "Return null when the argument is not given.");

try {
SC.viewFor("blarg");
ok(false, "Throws an exception when the argument is not an Element.");
} catch (ex) {
ok(true, "Throws an exception when the argument is not an Element.");
}
var ret = SC.viewFor("blarg");
equals(ret, null, "Return null when the argument is not an Element.");
});

test("SC.viewFor() Usage", function() {
Expand Down

0 comments on commit a65ae98

Please sign in to comment.