// Before and After Comparison Test console.log("=== BEFORE vs AFTER: URL Parameter Decoding ===\n"); const problematicSearch = "?message=F:%20Header%20(1)%20sectiune%20Company%20(1)%20sectiune%20BankAccount%20(1)%20eroare%20structura:%20grupul%20%27%BankAccount_choice0%27%20ar%20fi%20trebuit%20sa%20apara%20de%20minimum%201%20ori,%20dar%20apare%20efectiv%20de%200%20ori"; // OLD IMPLEMENTATION (with the problematic line) function oldGetUrlParameter(name, searchString) { try { const urlParams = new URLSearchParams(searchString); const value = urlParams.get(name); if (value === null) { return ''; } return value; } catch (error) { // Old problematic fallback try { name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]'); var regex = new RegExp('[\\?&]' + name + '=([^&#]*)'); var results = regex.exec(searchString); if (results === null) { return ''; } let rawValue = results[1]; // THIS IS THE PROBLEMATIC PART - corrupts %B sequences let cleaned = rawValue .replace(/\+/g, ' ') .replace(/%20/g, ' ') .replace(/%27/g, "'") .replace(/%28/g, "(") .replace(/%29/g, ")") .replace(/%3A/g, ":") .replace(/%2C/g, ",") .replace(/%([\dA-Fa-f])/g, '$1') // PROBLEM: Replaces %B with B .replace(/%/g, ''); // Removes remaining % return cleaned; } catch (fallbackError) { return ''; } } } // NEW IMPLEMENTATION (improved) function newGetUrlParameter(name, searchString) { try { const urlParams = new URLSearchParams(searchString); const value = urlParams.get(name); if (value === null) { return ''; } return value; } catch (error) { // Improved safe fallback try { name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]'); var regex = new RegExp('[\\?&]' + name + '=([^&#]*)'); var results = regex.exec(searchString); if (results === null) { return ''; } let rawValue = results[1]; // Safe decode function function safeDecodeURIComponent(str) { str = str.replace(/\+/g, ' '); const chunks = str.split('%'); let result = chunks[0]; for (let i = 1; i < chunks.length; i++) { const chunk = chunks[i]; if (chunk.length >= 2) { const hexCode = chunk.substring(0, 2); const rest = chunk.substring(2); if (/^[0-9A-Fa-f]{2}$/.test(hexCode)) { try { result += decodeURIComponent('%' + hexCode) + rest; } catch (e) { result += '%' + chunk; } } else { // IMPROVEMENT: Preserves invalid sequences like %B result += '%' + chunk; } } else { result += '%' + chunk; } } return result; } return safeDecodeURIComponent(rawValue); } catch (fallbackError) { return ''; } } } // Test both implementations console.log("Problematic URL search parameter:"); console.log(problematicSearch); console.log(); console.log("=== OLD IMPLEMENTATION RESULT ==="); try { const oldResult = oldGetUrlParameter('message', problematicSearch); console.log("✅ Success (but corrupted content):"); console.log(oldResult); console.log(); console.log("🔍 Notice: '%BankAccount_choice0' became 'BankAccount_choice0' (lost the %B)"); } catch (error) { console.log("❌ Failed:"); console.log(error.message); } console.log(); console.log("=== NEW IMPLEMENTATION RESULT ==="); try { const newResult = newGetUrlParameter('message', problematicSearch); console.log("✅ Success (with preserved content):"); console.log(newResult); console.log(); console.log("🔍 Notice: '%BankAccount_choice0' is preserved correctly"); } catch (error) { console.log("❌ Failed:"); console.log(error.message); } console.log(); console.log("=== SUMMARY ==="); console.log("OLD: Corrupts %B sequences by converting them to single characters"); console.log("NEW: Preserves %B sequences as they are, preventing data loss"); console.log("BENEFIT: No more URI malformed errors and accurate message content");