Skip to content

Commit

Permalink
In set-attr, restrict on... attributes to empty string only
Browse files Browse the repository at this point in the history
As per feedback from https://github.com/distinctmondaylilac

Related commit:
3037ae5f04

Additionally, added logging ability to the scriptlet.
  • Loading branch information
gorhill committed Feb 14, 2024
1 parent 68186a9 commit 068b625
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions assets/resources/scriptlets.js
Original file line number Diff line number Diff line change
Expand Up @@ -3770,16 +3770,19 @@ builtinScriptlets.push({
world: 'ISOLATED',
dependencies: [
'run-at.fn',
'safe-self.fn',
],
});
function setAttr(
selector = '',
attr = '',
value = ''
) {
if ( typeof selector !== 'string' ) { return; }
if ( selector === '' ) { return; }
if ( attr === '' ) { return; }

const safe = safeSelf();
const logPrefix = safe.makeLogPrefix('set-attr', attr, value);
const validValues = [ '', 'false', 'true' ];
let copyFrom = '';

Expand All @@ -3797,7 +3800,6 @@ function setAttr(

const extractValue = elem => {
if ( copyFrom !== '' ) {
if ( copyFrom.startsWith('on') && copyFrom in elem ) { return; }
return elem.getAttribute(copyFrom) || '';
}
return value;
Expand All @@ -3814,9 +3816,10 @@ function setAttr(
for ( const elem of elems ) {
const before = elem.getAttribute(attr);
const after = extractValue(elem);
if ( after === undefined ) { continue; }
if ( after === before ) { continue; }
if ( attr.startsWith('on') && attr in elem && after !== '' ) { continue; }
elem.setAttribute(attr, after);
safe.uboLog(logPrefix, `${attr}="${after}"`);
}
return true;
};
Expand Down

0 comments on commit 068b625

Please sign in to comment.