Skip to content

Commit

Permalink
Test for null before doing JS ops
Browse files Browse the repository at this point in the history
  • Loading branch information
mmore500 committed Apr 18, 2024
1 parent b64ba00 commit c8d4b81
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 14 deletions.
2 changes: 1 addition & 1 deletion include/emp/web/Canvas.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ namespace web {
EM_ASM({
var cname = UTF8ToString($0);
var canvas = document.getElementById(cname);
emp_i.ctx = canvas.getContext('2d');
if (canvas) emp_i.ctx = canvas.getContext('2d');
}, id.c_str());
#endif
}
Expand Down
8 changes: 5 additions & 3 deletions include/emp/web/Div.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,11 @@ namespace web {
parent_id = UTF8ToString($0);
child_id = UTF8ToString($1);
let parent = document.getElementById(parent_id);
let span = document.createElement('span');
span.id = child_id;
parent.appendChild(span);
if (parent) {
let span = document.createElement('span');
span.id = child_id;
parent.appendChild(span);
}
}, id.c_str(), in.GetID().c_str());

// Now that the new widget has some place to hook in, activate it!
Expand Down
1 change: 1 addition & 0 deletions include/emp/web/Input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ namespace web {
function modifyInputs() {

var input_el = document.getElementById(UTF8ToString($0));
if (!input_el) return;
input_el.addEventListener("input", modifyOffset);
// the following taken from http://stackoverflow.com/questions/2856513/trigger-onchange-event-manually
if ("fireEvent" in input_el) {
Expand Down
3 changes: 2 additions & 1 deletion include/emp/web/Listeners.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ namespace web {
MAIN_THREAD_EM_ASM({
var id = UTF8ToString($0);
var name = UTF8ToString($1);
document.getElementById(id).addEventListener(name, function(evt) {
var element = document.getElementById(id);
if (element) element.addEventListener(name, function(evt) {
emp.Callback($2, evt);
});
}, widget_id.c_str(), event_name.c_str(), fun_id);
Expand Down
8 changes: 5 additions & 3 deletions include/emp/web/Table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,11 @@ namespace web {
parent_id = UTF8ToString($0);
child_id = UTF8ToString($1);
let parent = document.getElementById(parent_id);
let span = document.createElement('span');
span.id = child_id;
parent.appendChild(span);
if (parent) {
let span = document.createElement('span');
span.id = child_id;
parent.appendChild(span);
}
}, cell_id.c_str(), in.GetID().c_str());

// Now that the new widget has some place to hook in, activate it!
Expand Down
2 changes: 1 addition & 1 deletion include/emp/web/TextFeed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ namespace web {
MAIN_THREAD_ASYNC_EM_ASM({
var elementId = UTF8ToString($0); // Convert UTF-8 encoded string to JavaScript string
var targetElement = document.getElementById(elementId);
targetElement.removeChild(targetElement.firstChild);
if (targetElement) targetElement.removeChild(targetElement.firstChild);
}, id.c_str() );

}
Expand Down
19 changes: 14 additions & 5 deletions include/emp/web/Widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,8 @@ namespace web {
MAIN_THREAD_EM_ASM({
var widget_id = UTF8ToString($0);
var out_html = UTF8ToString($1);
document.getElementById(widget_id).outerHTML = out_html;
var element = document.getElementById(widget_id);
if (element) element.outerHTML = out_html;
}, id.c_str(), ss.str().c_str());

// If active update style, trigger JS, and recurse to children!
Expand Down Expand Up @@ -498,15 +499,19 @@ namespace web {
emp_assert(GetID() != ""); // Must have a name!
return MAIN_THREAD_EM_ASM_DOUBLE({
var id = UTF8ToString($0);
return document.getElementById(id).clientWidth;
var element = document.getElementById(id);
if (!element) return -1.0; // Check if element exists
return element.clientWidth;
}, GetID().c_str());
}
double Widget::GetHeight(){
if (!info) return -1.0;
emp_assert(GetID() != ""); // Must have a name!
return MAIN_THREAD_EM_ASM_DOUBLE({
var id = UTF8ToString($0);
return document.getElementById(id).clientHeight;
var element = document.getElementById(id);
if (!element) return -1.0; // Check if element exists
return element.clientHeight;
}, GetID().c_str());
}
double Widget::GetInnerWidth(){
Expand Down Expand Up @@ -544,15 +549,19 @@ namespace web {
emp_assert(GetID() != ""); // Must have a name!
return MAIN_THREAD_EM_ASM_DOUBLE({
var id = UTF8ToString($0);
return document.getElementById(id).offsetWidth;
var element = document.getElementById(id);
if (!element) return 0.0; // Check if element exists
return element.offsetWidth;
}, GetID().c_str());
}
double Widget::GetOuterHeight(){
if (!info) return -1.0;
emp_assert(GetID() != ""); // Must have a name!
return MAIN_THREAD_EM_ASM_DOUBLE({
var id = UTF8ToString($0);
return document.getElementById(id).offsetHeight;
var element = document.getElementById(id);
if (!element) return 0.0; // Check if element exists
return element.offsetHeight;
}, GetID().c_str());
}

Expand Down

0 comments on commit c8d4b81

Please sign in to comment.