joomla / joomla-cms

Home of the Joomla! Content Management System
https://www.joomla.org
GNU General Public License v2.0
4.73k stars 3.64k forks source link

validate.js throws Uncaught SyntaxError: redeclaration of const errors #43714

Closed mfleeson closed 2 months ago

mfleeson commented 3 months ago

Steps to reproduce the issue

Display a form and use webassets to use script form.validate

Expected result

script works with no errors in console.log

Actual result

Uncaught SyntaxError: redeclaration of const errors is displayed in console.log

System information (as much as possible)

Tested on 5.0 and 5.1.1, discussed on Extension development room and @Fedik requested an issue raised.

Additional comments

Fedir Zinchuk It a bug in validate.js, the code should be wrapped in to anonimous function or use type module please open an issue

Fedik commented 3 months ago

Note: Strict mode should be restored, for the script, In joomla 4.4

mfleeson commented 3 months ago

I looked into how others have used punycode and modified validate.js to the following which appears to work and fixes the error. `'use strict'; let punycode = new function Punycode() { /* Highest positive signed 32-bit float value / const maxInt = 2147483647; // aka. 0x7FFFFFFF or 2^31-1

/* Bootstring parameters / const base = 36; const tMin = 1; const tMax = 26; const skew = 38; const damp = 700; const initialBias = 72; const initialN = 128; // 0x80 const delimiter = '-'; // '\x2D'

/* Regular expressions / const regexPunycode = /^xn--/; const regexNonASCII = /[^\0-\x7F]/; // Note: U+007F DEL is excluded too. const regexSeparators = /[\x2E\u3002\uFF0E\uFF61]/g; // RFC 3490 separators

/* Error messages / const errors = { 'overflow': 'Overflow: input needs wider integers to process', 'not-basic': 'Illegal input >= 0x80 (not a basic code point)', 'invalid-input': 'Invalid input' };

/* Convenience shortcuts / const baseMinusTMin = base - tMin; const floor = Math.floor; const stringFromCharCode = String.fromCharCode;

/--------------------------------------------------------------------------/

/**

dgrammatiko commented 3 months ago

Change https://github.com/joomla/joomla-cms/blob/69bca95cedab79ba1e3c074149ac254c6a31f04e/build/media_source/system/joomla.asset.json#L171 to

"type": "module"
dgrammatiko commented 3 months ago

Note: Strict mode should be restored, for the script, In joomla 4.4

Not really, the script/file was supposed to be ESM thus strict by default, reduced scoped (the reported issue), etc. but unfortunately the script was loaded as legacy js file with attribute defer instead of the proper attribute type="module"

Fedik commented 3 months ago

yes, and no. the script supposed to be wrapped in anonymous function with strict mode, and only removed when we change type to module. so for 4.4 it should be strict mode. there probably few more scripts which spills its variables globally. and that is realy bad.

type=module can be only in future 5.x or 6.x idk, as feature not bugfix

Fedik commented 2 months ago

okay, it is a horrible bug, also affect core js

dgrammatiko commented 2 months ago

There are PRs for this so it should be closed