Skip to content

Commit 8c28cbe

Browse files
authored
Merge pull request #5631 from eclipse-ee4j/faces_1590
jakartaee/faces#1590: faces.util.chain must also support CSP; while at it bumped versions to 5
2 parents 1c1a250 + 6d34ecf commit 8c28cbe

1 file changed

Lines changed: 59 additions & 18 deletions

File tree

impl/src/main/resources/META-INF/resources/jakarta.faces/faces-uncompressed.js

Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818

1919
/**
2020
@project Faces JavaScript Library
21-
@version 4.1
21+
@version 5.0
2222
@description This is the standard implementation of the Faces JavaScript Library.
2323
*/
2424

2525
// Detect if this is already loaded, and if loaded, if it's a higher version
26-
if ( !( (window.faces && window.faces.specversion && window.faces.specversion >= 41000 )
27-
&& (window.faces.implversion && window.faces.implversion >= 4)) ) {
26+
if ( !( (window.faces && window.faces.specversion && window.faces.specversion >= 50000 )
27+
&& (window.faces.implversion && window.faces.implversion >= 5)) ) {
2828

2929
// --- JS Lang --------------------------------------------------------------------
3030
const UDEF = 'undefined';
@@ -34,6 +34,35 @@ if ( !( (window.faces && window.faces.specversion && window.faces.specversion >=
3434
const isNull = (value) => (typeof value === UDEF || (typeof value === "object" && !value));
3535
const isNotNull = (value) => !isNull(value);
3636

37+
/**
38+
* Get the head from document.
39+
* @ignore
40+
*/
41+
const getHead = () => {
42+
return document.head || document.getElementsByTagName('head')[0] || document.documentElement;
43+
};
44+
45+
/**
46+
* Get the nonce from faces.js script for CSP support.
47+
* @ignore
48+
*/
49+
const getNonce = () => {
50+
const thisScript = document.querySelector("script[src*='jakarta.faces.resource/faces.js']");
51+
return isNotNull(thisScript) ? thisScript.nonce : undefined;
52+
};
53+
54+
/**
55+
* Execute script with nonce for CSP support.
56+
* @ignore
57+
*/
58+
const executeScriptWithNonce = (head, script, nonce) => {
59+
const scriptNode = document.createElement('script'); // create script node
60+
scriptNode.nonce = nonce;
61+
scriptNode.text = script; // add the code to the script node
62+
head.appendChild(scriptNode); // add it to the head
63+
head.removeChild(scriptNode); // then remove it
64+
};
65+
3766
// --- Faces constants ------------------------------------------------------------
3867
const VIEW_STATE_PARAM = "jakarta.faces.ViewState";
3968
const CLIENT_WINDOW_PARAM = "jakarta.faces.ClientWindow";
@@ -318,7 +347,7 @@ if ( !( (window.faces && window.faces.specversion && window.faces.specversion >=
318347
if (url) loadedScriptUrls.push(url);
319348
}
320349

321-
const head = document.head || document.getElementsByTagName('head')[0] || document.documentElement;
350+
const head = getHead();
322351
runScript(head, loadedScriptUrls, scripts, 0);
323352
};
324353

@@ -346,8 +375,7 @@ if ( !( (window.faces && window.faces.specversion && window.faces.specversion >=
346375
const src = scriptStr[1].match(findsrc);
347376
let scriptLoadedViaUrl = false;
348377

349-
const thisScript = document.querySelector("script[src*='jakarta.faces.resource/faces.js']");
350-
const nonce = isNotNull(thisScript) ? thisScript.nonce : undefined;
378+
const nonce = getNonce();
351379

352380
if (!!src && src[1]) {
353381
// if this is a file, load it
@@ -377,12 +405,7 @@ if ( !( (window.faces && window.faces.specversion && window.faces.specversion >=
377405
const script = scriptStr[2].replace(stripStart, EMPTY);
378406

379407
if (!!script) {
380-
// create script node
381-
const scriptNode = document.createElement('script');
382-
scriptNode.nonce = nonce;
383-
scriptNode.text = script; // add the code to the script node
384-
head.appendChild(scriptNode); // add it to the head
385-
head.removeChild(scriptNode); // then remove it
408+
executeScriptWithNonce(head, script, nonce);
386409
}
387410
}
388411

@@ -406,7 +429,7 @@ if ( !( (window.faces && window.faces.specversion && window.faces.specversion >=
406429
const findhref = /href="([\S]*?)"/im;
407430

408431
// the head of the document, note that document.head do not always work
409-
const head = document.head || document.getElementsByTagName('head')[0] || document.documentElement;
432+
const head = getHead();
410433

411434
let loadedStylesheetUrls = null;
412435
let parserElement = null;
@@ -2943,13 +2966,31 @@ if ( !( (window.faces && window.faces.specversion && window.faces.specversion >=
29432966
// RELEASE_PENDING rogerk - shouldn't this be getElementById instead of null
29442967
const thisArg = (typeof source === 'object') ? source : null;
29452968

2969+
const head = getHead();
2970+
const nonce = getNonce();
2971+
29462972
// Call back any scripts that were passed in
29472973
for (let i = 2; i < arguments.length; i++) {
2974+
const facesChainThis = '__facesChainThis' + i;
2975+
const facesChainEvent = '__facesChainEvent' + i;
2976+
const facesChainResult = '__facesChainResult' + i;
2977+
2978+
let result = undefined;
29482979

2949-
const f = new Function("event", arguments[i]);
2950-
const returnValue = f.call(thisArg, event);
2980+
try {
2981+
window[facesChainThis] = thisArg;
2982+
window[facesChainEvent] = event;
2983+
const script = 'window.' + facesChainResult + ' = (function(event) { ' + arguments[i] + ' }).call(window.' + facesChainThis + ', window.' + facesChainEvent + ');';
2984+
executeScriptWithNonce(head, script, nonce);
2985+
result = window[facesChainResult];
2986+
}
2987+
finally {
2988+
delete window[facesChainThis];
2989+
delete window[facesChainEvent];
2990+
delete window[facesChainResult];
2991+
}
29512992

2952-
if (returnValue === false) {
2993+
if (result === false) {
29532994
return false;
29542995
}
29552996
}
@@ -2975,15 +3016,15 @@ if ( !( (window.faces && window.faces.specversion && window.faces.specversion >=
29753016
* minor release number, leftmost digits, major release number.
29763017
* This number may only be incremented by a new release of the specification.</p>
29773018
*/
2978-
faces.specversion = 41000;
3019+
faces.specversion = 50000;
29793020

29803021
/**
29813022
* <p>An integer specifying the implementation version that this file implements.
29823023
* It's a monotonically increasing number, reset with every increment of
29833024
* <code>faces.specversion</code>
29843025
* This number is implementation dependent.</p>
29853026
*/
2986-
faces.implversion = 4;
3027+
faces.implversion = 5;
29873028

29883029

29893030
} //end if version detection block

0 commit comments

Comments
 (0)