Skip to content

Commit

Permalink
Test for null when calling getElement
Browse files Browse the repository at this point in the history
  • Loading branch information
mmore500 committed Apr 18, 2024
1 parent 71f2b87 commit 405faf9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
6 changes: 4 additions & 2 deletions include/emp/web/Attributes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ namespace web {
var id = UTF8ToString($0);
var setting = UTF8ToString($1);
var value = UTF8ToString($2);
document.getElementById(id).setAttribute(setting, value);
var element = document.getElementById(id);
if (element) element.setAttribute(setting, value);
}, widget_id.c_str(), setting.c_str(), settings[setting].c_str());
#else
std::cout << "Setting '" << widget_id << "' attribute '" << setting
Expand All @@ -152,7 +153,8 @@ namespace web {
var id = UTF8ToString($0);
var setting = UTF8ToString($1);
var value = UTF8ToString($2);
document.getElementById(id).setAttribute(setting, value);
var element = document.getElementById(id);
if (element) element.setAttribute(setting, value);
}, widget_id.c_str(), setting.c_str(), value.c_str());
#else
std::cout << "Setting '" << widget_id << "' attribute '" << setting
Expand Down
12 changes: 8 additions & 4 deletions include/emp/web/Style.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ namespace web {
var id = UTF8ToString($0);
var setting = UTF8ToString($1);
var value = UTF8ToString($2);
document.getElementById(id).style[setting] = value;
var element = document.getElementById(id);
if (element) element.style[setting] = value;
}, widget_id.c_str(), setting.c_str(), settings[setting].c_str());
#else
std::cout << "Setting '" << widget_id << "' attribute '" << setting
Expand All @@ -170,7 +171,8 @@ namespace web {
var id = UTF8ToString($0);
var setting = UTF8ToString($1);
var value = UTF8ToString($2);
document.getElementById(id).style[setting] = value;
var element = document.getElementById(id);
if (element) element.style[setting] = value;
}, widget_id.c_str(), setting.c_str(), value.c_str());
#else
std::cout << "Setting '" << widget_id << "' attribute '" << setting
Expand All @@ -184,7 +186,8 @@ namespace web {
EM_ASM_ARGS({
var id = UTF8ToString($0);
var name = UTF8ToString($1);
document.getElementById(id).classList.add(name);
var element = document.getElementById(id);
if (element) element.classList.add(name);
}, widget_id.c_str(), clss.c_str());
#else
std::cout << "Adding class to '" << widget_id << "': '" << clss;
Expand All @@ -196,7 +199,8 @@ namespace web {
EM_ASM_ARGS({
var id = UTF8ToString($0);
var name = UTF8ToString($1);
document.getElementById(id).classList.remove(name);
var element = document.getElementById(id);
if (element) element.classList.remove(name);
}, widget_id.c_str(), clss.c_str());
#else
std::cout << "Adding class to '" << widget_id << "': '" << clss;
Expand Down
4 changes: 2 additions & 2 deletions include/emp/web/TextFeed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ namespace web {
var content = document.createElement('span');
content.innerHTML = UTF8ToString($1);
var elementId = UTF8ToString($0);
var targetElement = document.getElementById(elementId);
targetElement.insertAdjacentHTML('beforeend', content);
var element = document.getElementById(elementId);
if (element) element.insertAdjacentHTML('beforeend', content);
}, id.c_str(), to_append.c_str() );
}

Expand Down

0 comments on commit 405faf9

Please sign in to comment.