niutech / showModalDialog

window.showModalDialog polyfill using a <dialog> element
https://niutech.github.io/showModalDialog/demo.html
Apache License 2.0
177 stars 88 forks source link

showModalDialog calls inside another function causes script to break #41

Open u4yk opened 5 years ago

u4yk commented 5 years ago

If window.showModalDialog call is nested inside another function call, your script will break since there will be a parenthesis at the end that will have already been escaped. For example, when trying to apply this polyfill to the following line:

var val = String(window.showModalDialog(url, "",feature));`

causes an error since it's replacing everything from (window.)showModalDialog onward even though there's a parenthesis before it for the outer function call. Replacing the regex on line 55 to the below will handle this use case:

nextStmts[0] = nextStmts[0].replace(/(window\.)?showModalDialog\([^\)]*\)/g,JSON.stringify(returnValue));

https://github.com/niutech/showModalDialog/blob/198b9f8d5be73eed1e36ebbfa811ba5f13caaa4d/showModalDialog.js#L55

macmessa commented 5 years ago

You can use yield and await version instead of eval, like this:

var val = String(await window.showModalDialog(url, "", feature));

or:

var val = window.showModalDialog(url, "", feature);
val = String(val)
u4yk commented 5 years ago

Yes, I understand that yield and await are options, but we still have to support older versions of IE (we're talking IE5) as well -- and Babel is out of the question. Using generators and async will just mean we'd have to build two separate codebases at that point.

Regardless, I mentioned the issue in case someone else ran into it with a possible means to fix it. We have it fixed in our version of the polyfill.

ziamohamed commented 3 years ago

Is nested calls more than 1 level working? If not how to make it work?