Link attivi nel corpo dei messaggi (0.5.0)
- 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
This commit is contained in:
1 parent
5bc53d1c18
commit
8e062adc34
5 files changed
+116
-5
No files matched your search
@@ -0,0 +1,45 @@
|
|||||||
|
// Trasforma il testo di un messaggio in HTML con i link attivi.
|
||||||
|
//
|
||||||
|
// Il testo arriva da chiunque: prima si escapa tutto, poi si inseriscono i
|
||||||
|
// propri tag. Una sola passata riconosce URL e indirizzi, così un link non
|
||||||
|
// finisce dentro l'href di un altro. ES5 puro: si prova con node.
|
||||||
|
|
||||||
|
var RE = /(https?:\/\/[^\s<>"']+)|([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,})/g;
|
||||||
|
|
||||||
|
function escapeHtml(s) {
|
||||||
|
return s.replace(/&/g, "&")
|
||||||
|
.replace(/</g, "<")
|
||||||
|
.replace(/>/g, ">")
|
||||||
|
.replace(/"/g, """);
|
||||||
|
}
|
||||||
|
|
||||||
|
// La punteggiatura che chiude una frase non fa parte dell'indirizzo.
|
||||||
|
function trimTail(url) {
|
||||||
|
var m = url.match(/[.,;:!?)\]]+$/);
|
||||||
|
if (!m) return [url, ""];
|
||||||
|
return [url.substring(0, url.length - m[0].length), m[0]];
|
||||||
|
}
|
||||||
|
|
||||||
|
function html(text) {
|
||||||
|
if (!text) return "";
|
||||||
|
var out = "";
|
||||||
|
var last = 0;
|
||||||
|
var m;
|
||||||
|
RE.lastIndex = 0;
|
||||||
|
while ((m = RE.exec(text)) !== null) {
|
||||||
|
out += escapeHtml(text.substring(last, m.index));
|
||||||
|
var token = m[0];
|
||||||
|
var tail = "";
|
||||||
|
if (m[1]) { // URL: la coda di punteggiatura resta fuori
|
||||||
|
var t = trimTail(token);
|
||||||
|
token = t[0];
|
||||||
|
tail = t[1];
|
||||||
|
}
|
||||||
|
var href = m[1] ? token : ("mailto:" + token);
|
||||||
|
out += '<a href="' + escapeHtml(href) + '">' + escapeHtml(token) + '</a>';
|
||||||
|
out += escapeHtml(tail);
|
||||||
|
last = m.index + m[0].length;
|
||||||
|
}
|
||||||
|
out += escapeHtml(text.substring(last));
|
||||||
|
return out.replace(/\n/g, "<br/>");
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import QtQuick 2.0
|
import QtQuick 2.0
|
||||||
import Sailfish.Silica 1.0
|
import Sailfish.Silica 1.0
|
||||||
|
import "../js/linkify.js" as Linkify
|
||||||
|
|
||||||
// Lettura di un messaggio, dal motore. Paginazione a tap: un tocco sul lato
|
// Lettura di un messaggio, dal motore. Paginazione a tap: un tocco sul lato
|
||||||
// destro passa al successivo, sul sinistro al precedente; il corpo resta
|
// destro passa al successivo, sul sinistro al precedente; il corpo resta
|
||||||
@@ -118,12 +119,16 @@ Page {
|
|||||||
|
|
||||||
Label {
|
Label {
|
||||||
width: parent.width
|
width: parent.width
|
||||||
text: page.corpo.length > 0 ? page.corpo : qsTr("(nessun testo)")
|
text: page.corpo.length > 0 ? Linkify.html(page.corpo)
|
||||||
|
: qsTr("(nessun testo)")
|
||||||
wrapMode: Text.Wrap
|
wrapMode: Text.Wrap
|
||||||
textFormat: Text.PlainText
|
// il testo arriva da chiunque: linkify escapa tutto e inserisce
|
||||||
|
// solo i propri tag (vedi qml/js/linkify.js, provato con node)
|
||||||
|
textFormat: Text.RichText
|
||||||
font.pixelSize: Theme.fontSizeSmall
|
font.pixelSize: Theme.fontSizeSmall
|
||||||
color: Theme.primaryColor
|
color: Theme.primaryColor
|
||||||
bottomPadding: Theme.paddingLarge
|
bottomPadding: Theme.paddingLarge
|
||||||
|
onLinkActivated: Qt.openUrlExternally(link)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Allegati: un tocco li salva in Download e li apre con l'app
|
// Allegati: un tocco li salva in Download e li apre con l'app
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
Name: harbour-grumpydove
|
Name: harbour-grumpydove
|
||||||
Summary: Eudora-style mail interface, mobile prototype
|
Summary: Eudora-style mail interface, mobile prototype
|
||||||
Version: 0.4.5
|
Version: 0.5.0
|
||||||
Release: 1
|
Release: 1
|
||||||
Group: Qt/Qt
|
Group: Qt/Qt
|
||||||
# Codice EUPL-1.2; le icone vengono dagli sprite di Eudora, BSD 3-Clause
|
# Codice EUPL-1.2; le icone vengono dagli sprite di Eudora, BSD 3-Clause
|
||||||
@@ -27,6 +27,11 @@ Icons come from the original project's Eudora bitmaps (BSD 3-Clause,
|
|||||||
Computer History Museum).
|
Computer History Museum).
|
||||||
|
|
||||||
%changelog
|
%changelog
|
||||||
|
* Wed Nov 11 2026 Carlo Baratto <carlo@carlobaratto.it> - 0.5.0-1
|
||||||
|
- Link nel corpo dei messaggi: URL e indirizzi diventano apribili (browser e
|
||||||
|
client di posta). Il testo e' escapato prima di inserire i tag, cosi' un
|
||||||
|
messaggio non puo' iniettare HTML; la punteggiatura finale resta fuori dal
|
||||||
|
link. Logica in qml/js/linkify.js, provata con node (tests/test_linkify.js)
|
||||||
* Wed Nov 11 2026 Carlo Baratto <carlo@carlobaratto.it> - 0.4.5-1
|
* Wed Nov 11 2026 Carlo Baratto <carlo@carlobaratto.it> - 0.4.5-1
|
||||||
- Fix: la posta veniva riscaricata da capo a ogni controllo. Dopo lo scarico
|
- Fix: la posta veniva riscaricata da capo a ogni controllo. Dopo lo scarico
|
||||||
mancava absorb, la chiamata con cui la vista prende i file nuovi
|
mancava absorb, la chiamata con cui la vista prende i file nuovi
|
||||||
|
|||||||
+2
-2
@@ -24,9 +24,9 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
|
|||||||
|
|
||||||
app->setOrganizationName(QStringLiteral("harbour"));
|
app->setOrganizationName(QStringLiteral("harbour"));
|
||||||
app->setApplicationName(QStringLiteral("grumpydove"));
|
app->setApplicationName(QStringLiteral("grumpydove"));
|
||||||
app->setApplicationVersion(QStringLiteral("0.4.5"));
|
app->setApplicationVersion(QStringLiteral("0.5.0"));
|
||||||
|
|
||||||
qDebug() << "harbour-grumpydove v0.4.5 build" << __DATE__ << __TIME__;
|
qDebug() << "harbour-grumpydove v0.5.0 build" << __DATE__ << __TIME__;
|
||||||
|
|
||||||
// La sandbox di Sailfish (SailJail) non lascia vedere ~/Mail, ma il motore
|
// La sandbox di Sailfish (SailJail) non lascia vedere ~/Mail, ma il motore
|
||||||
// legge il percorso del Maildir da GRUMPYDOVE_MAIL: si punta alla cartella
|
// legge il percorso del Maildir da GRUMPYDOVE_MAIL: si punta alla cartella
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
#!/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);
|
||||||
Reference in new issue
Block a user