Files
klz-cables.com/scripts/test-numeric-entities.js
2025-12-30 00:06:54 +01:00

68 lines
1.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
// Test numeric entity decoding
const testString = 'type=”in_container”';
console.log('Original:', testString);
// Method 1: Manual replacement
let method1 = testString
.replace(/”/g, '"')
.replace(/“/g, '"')
.replace(//g, "'")
.replace(//g, "'")
.replace(//g, '-')
.replace(/—/g, '—');
console.log('Method 1 (Unicode chars):', method1);
// Method 2: Numeric entity decoding
let method2 = testString
.replace(/”/g, '"')
.replace(/“/g, '"')
.replace(//g, "'")
.replace(//g, "'")
.replace(//g, '-')
.replace(/—/g, '—')
.replace(/…/g, '…')
.replace(/″/g, '"')
.replace(//g, "'");
console.log('Method 2 (Numeric entities):', method2);
// Method 3: Using a function to decode all numeric entities
function decodeHTMLEntities(str) {
return str.replace(/&#(\d+);/g, (match, dec) => {
return String.fromCharCode(dec);
});
}
let method3 = decodeHTMLEntities(testString);
console.log('Method 3 (All numeric):', method3);
// Method 4: Combined approach
function comprehensiveEntityDecode(str) {
return str
// First decode numeric entities
.replace(/&#(\d+);/g, (match, dec) => String.fromCharCode(dec))
// Then handle any remaining Unicode characters
.replace(/”/g, '"')
.replace(/“/g, '"')
.replace(//g, "'")
.replace(//g, "'")
.replace(//g, '-')
.replace(/—/g, '—')
.replace(/…/g, '…')
.replace(/″/g, '"')
.replace(//g, "'");
}
let method4 = comprehensiveEntityDecode(testString);
console.log('Method 4 (Combined):', method4);
// Test with the actual excerpt
const actualExcerpt = '<p>[vc_row type=”in_container” full_screen_row_position=”middle” column_margin=”default”]';
console.log('\n=== Real Test ===');
console.log('Original:', actualExcerpt);
console.log('Decoded:', comprehensiveEntityDecode(actualExcerpt));