Fixed spaces in copy/paste of rich text (Patch from Phenom)

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@7035 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2014-01-18 22:55:14 +00:00
parent 741424dd72
commit 037abea5eb

View File

@ -333,6 +333,48 @@ void RsHtml::embedHtml(QTextDocument *textDocument, QDomDocument& doc, QDomEleme
} }
} }
/**
* Save space and tab out of bracket that XML loose.
*
* @param[in] text The text to save space.
* @return Text with space saved.
*/
static QString saveSpace(const QString text)
{
QString savedSpaceText=text;
bool outBrackets=false, echapChar=false;
QString keyName = "";
bool getKeyName = false;
for(int i=0;i<savedSpaceText.length();i++){
QChar cursChar=savedSpaceText.at(i);
if(getKeyName || (!outBrackets && keyName.isEmpty())){
if((cursChar==QLatin1Char(' ')) || (cursChar==QLatin1Char('>'))) {
getKeyName=keyName.isEmpty();
} else {
keyName.append(cursChar.toLower());
}
}
if(cursChar==QLatin1Char('>')) {
if(!echapChar && i>0) outBrackets=true;
} else if(cursChar==QLatin1Char('\t')) {
if(outBrackets && (keyName!="style")) savedSpaceText.replace(i, 1, "&nbsp;&nbsp;");
} else if(cursChar==QLatin1Char(' ')) {
if(outBrackets && (keyName!="style")) savedSpaceText.replace(i, 1, "&nbsp;");
} else if(cursChar==QChar(0xA0)) {
if(outBrackets && (keyName!="style")) savedSpaceText.replace(i, 1, "&nbsp;");
} else if(cursChar==QLatin1Char('<')) {
if(!echapChar) {outBrackets=false; getKeyName=true; keyName.clear();}
}
echapChar=(cursChar==QLatin1Char('\\'));
}
return savedSpaceText;
}
QString RsHtml::formatText(QTextDocument *textDocument, const QString &text, ulong flag, const QColor &backgroundColor, qreal desiredContrast) QString RsHtml::formatText(QTextDocument *textDocument, const QString &text, ulong flag, const QColor &backgroundColor, qreal desiredContrast)
{ {
if (flag == 0 || text.isEmpty()) { if (flag == 0 || text.isEmpty()) {
@ -340,12 +382,25 @@ QString RsHtml::formatText(QTextDocument *textDocument, const QString &text, ulo
return text; return text;
} }
QDomDocument doc; QString formattedText=text;
if (doc.setContent(text) == false) { //remove all prepend char that make doc.setContent() fail
formattedText.remove(0,text.indexOf("<"));
// Save Space and Tab because doc loose it.
formattedText=saveSpace(formattedText);
QString errorMsg; int errorLine; int errorColumn;
QDomDocument doc;
if (doc.setContent(formattedText, &errorMsg, &errorLine, &errorColumn) == false) {
// convert text with QTextBrowser // convert text with QTextBrowser
QTextBrowser textBrowser; QTextBrowser textBrowser;
textBrowser.setText(text); textBrowser.setText(text);
doc.setContent(textBrowser.toHtml()); formattedText=textBrowser.toHtml();
formattedText.remove(0,formattedText.indexOf("<"));
formattedText=saveSpace(formattedText);
doc.setContent(formattedText, &errorMsg, &errorLine, &errorColumn);
} }
QDomElement body = doc.documentElement(); QDomElement body = doc.documentElement();
@ -357,7 +412,7 @@ QString RsHtml::formatText(QTextDocument *textDocument, const QString &text, ulo
embedHtml(textDocument, doc, body, defEmbedAhref, flag); embedHtml(textDocument, doc, body, defEmbedAhref, flag);
} }
QString formattedText = doc.toString(-1); // -1 removes any annoying carriage return misinterpreted by QTextEdit formattedText = doc.toString(-1); // -1 removes any annoying carriage return misinterpreted by QTextEdit
if (flag & RSHTML_OPTIMIZEHTML_MASK) { if (flag & RSHTML_OPTIMIZEHTML_MASK) {
optimizeHtml(formattedText, flag & RSHTML_OPTIMIZEHTML_MASK, backgroundColor, desiredContrast); optimizeHtml(formattedText, flag & RSHTML_OPTIMIZEHTML_MASK, backgroundColor, desiredContrast);
@ -396,12 +451,13 @@ static void findElements(QDomDocument& doc, QDomElement& currentElement, const Q
bool RsHtml::findAnchors(const QString &text, QStringList& urls) bool RsHtml::findAnchors(const QString &text, QStringList& urls)
{ {
QString errorMsg; int errorLine; int errorColumn;
QDomDocument doc; QDomDocument doc;
if (doc.setContent(text) == false) { if (doc.setContent(text, &errorMsg, &errorLine, &errorColumn) == false) {
// convert text with QTextBrowser // convert text with QTextBrowser
QTextBrowser textBrowser; QTextBrowser textBrowser;
textBrowser.setText(text); textBrowser.setText(text);
doc.setContent(textBrowser.toHtml()); doc.setContent(textBrowser.toHtml(), &errorMsg, &errorLine, &errorColumn);
} }
QDomElement body = doc.documentElement(); QDomElement body = doc.documentElement();
@ -708,9 +764,14 @@ void RsHtml::optimizeHtml(QString &text, unsigned int flag, const QColor &backgr
// remove doctype // remove doctype
text.remove(QRegExp("<!DOCTYPE[^>]*>")); text.remove(QRegExp("<!DOCTYPE[^>]*>"));
//remove all prepend char that make doc.setContent() fail
text.remove(0,text.indexOf("<"));
// Save Space and Tab because doc loose it.
text=saveSpace(text);
QString errorMsg; int errorLine; int errorColumn;
QDomDocument doc; QDomDocument doc;
if (doc.setContent(text) == false) { if (doc.setContent(text, &errorMsg, &errorLine, &errorColumn) == false) {
return; return;
} }