- qml/js/linkify.js: escape di tutto il testo, poi URL e indirizzi diventano link; la punteggiatura finale resta fuori; una sola passata (niente link annidati). ES5, provato con node: tests/test_linkify.js, 9 controlli - MessagePage: RichText e onLinkActivated -> Qt.openUrlExternally
57 lines
2.3 KiB
JavaScript
57 lines
2.3 KiB
JavaScript
#!/usr/bin/env node
|
|
// Test del linkificatore: quello che arriva dal mittente non deve mai
|
|
// diventare HTML attivo, e i link devono essere quelli giusti.
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
|
|
const src = fs.readFileSync(path.join(__dirname, '..', 'qml', 'js', 'linkify.js'), 'utf8');
|
|
eval(src);
|
|
|
|
let fail = 0;
|
|
function check(cond, msg, extra) {
|
|
if (!cond) { console.log('FALLITO:', msg, extra === undefined ? '' : '-> ' + extra); fail++; }
|
|
}
|
|
|
|
// 1. un URL semplice diventa un link
|
|
let out = html("Vedi https://example.org/pagina per i dettagli");
|
|
check(out.indexOf('<a href="https://example.org/pagina">https://example.org/pagina</a>') >= 0,
|
|
'URL non linkificato', out);
|
|
|
|
// 2. la punteggiatura finale resta fuori dal link
|
|
out = html("Scrivi a https://example.org/x. Poi altro.");
|
|
check(out.indexOf('href="https://example.org/x"') >= 0, 'punto finale dentro l\'URL', out);
|
|
check(out.indexOf('</a>.') >= 0, 'punto finale non fuori dal link', out);
|
|
|
|
// 3. indirizzo email
|
|
out = html("scrivi a mario.rossi@example.it, grazie");
|
|
check(out.indexOf('href="mailto:mario.rossi@example.it"') >= 0, 'email non linkificata', out);
|
|
|
|
// 4. HTML nel testo: mai attivo
|
|
out = html("<script>alert(1)</script>");
|
|
check(out.indexOf('<script') < 0, 'tag HTML passato intatto', out);
|
|
check(out.indexOf('<script>') >= 0, 'escape mancante', out);
|
|
|
|
// 5. le virgolette nel testo non rompono l'attributo
|
|
out = html('un "ciao" e https://example.org/a?b=1&c=2');
|
|
check(out.indexOf('"ciao"') >= 0, 'virgolette non esplicate', out);
|
|
check(out.indexOf('&c=2') >= 0, 'ampersand non esplicato nell\'href', out);
|
|
|
|
// 6. a capo conservati
|
|
out = html("prima\nseconda");
|
|
check(out.indexOf('<br/>') >= 0, 'a capo persi', out);
|
|
|
|
// 7. testo senza link: nessun tag <a>
|
|
out = html("solo testo, niente da aprire");
|
|
check(out.indexOf('<a ') < 0, 'link inventato', out);
|
|
|
|
// 8. due link nella stessa riga
|
|
out = html("https://a.example e https://b.example");
|
|
check((out.match(/<a /g) || []).length === 2, 'numero di link sbagliato', out);
|
|
|
|
// 9. un link dentro un altro non si forma
|
|
out = html("https://example.org/?u=a@b.example");
|
|
check((out.match(/<a /g) || []).length === 1, 'link annidato', out);
|
|
|
|
console.log(fail === 0 ? 'OK: linkificatore' : `${fail} controlli falliti`);
|
|
process.exit(fail === 0 ? 0 : 1);
|