fabiospampinato / cash

An absurdly small jQuery alternative for modern browsers.
MIT License
6.51k stars 266 forks source link

Execute scripts when using html function. #398

Closed vovayatsyuk closed 1 year ago

vovayatsyuk commented 2 years ago

This commit makes .html() to execute supplied scripts. Fixes https://github.com/fabiospampinato/cash/issues/366

jQuery realization is somewhat similar: https://github.com/jquery/jquery/blob/main/src/manipulation.js#L273-L297

fabiospampinato commented 1 year ago

Thank you! I'll get a few other changes in and make a release.

fabiospampinato commented 1 year ago

Btw I'm in the process of deleting pacco and its million of other dependencies from the dependencies list of Cash, so Cash should become easier to maintain and contribute to in the future.

I'd be happy if you'd like to contribute some more in the future. Sorry for the huge delay in getting stuff merged.

fabiospampinato commented 1 year ago

In hindsight getting this PR to work seems tricker than it seems 🤔 like if the string is a <script there are no script tags, or it could be <scripts /> or <script whatever="/>", like it's probably good enough, but I'm not sure what a perfect implementation looks like. I should check what jQuery is doing in this regard.

vovayatsyuk commented 1 year ago

Here is what jquery logic looks like: https://github.com/jquery/jquery/blob/main/src/manipulation.js#L273-L297

Made a test on sites with jquery and cash:

function jQuery Cash
$('header').html('<script').html() empty string Exception is thrown: Failed to execute 'querySelectorAll' on 'Document': '<script' is not a valid selector.
$('header').html('<script>').html() \x3Cscript>\x3C/script> \x3Cscript>\x3C/script>
$('header').html('<scripts/>').html() \x3Cscripts>\x3C/scripts> \x3Cscripts>\x3C/scripts>
$('header').html('<script whatever="/>"').html() empty string empty string