|
|
@@ -4,6 +4,10 @@
|
|
|
#include <QFileInfo>
|
|
|
#include <QDebug>
|
|
|
|
|
|
+#include "vpalette.h"
|
|
|
+
|
|
|
+extern VPalette *g_palette;
|
|
|
+
|
|
|
VWebUtils::VWebUtils()
|
|
|
{
|
|
|
}
|
|
|
@@ -56,3 +60,67 @@ bool VWebUtils::fixImageSrcInHtml(const QUrl &p_baseUrl, QString &p_html)
|
|
|
|
|
|
return changed;
|
|
|
}
|
|
|
+
|
|
|
+bool VWebUtils::removeBackgroundColor(QString &p_html)
|
|
|
+{
|
|
|
+ QRegExp reg("(<[^>]+\\sstyle=[^>]*(\\s|\"))background(-color)?:[^;]+;([^>]*>)");
|
|
|
+ int size = p_html.size();
|
|
|
+ p_html.replace(reg, "\\1\\4");
|
|
|
+ return p_html.size() != size;
|
|
|
+}
|
|
|
+
|
|
|
+bool VWebUtils::translateColors(QString &p_html)
|
|
|
+{
|
|
|
+ bool changed = false;
|
|
|
+
|
|
|
+ const QHash<QString, QString> &mapping = g_palette->getColorMapping();
|
|
|
+ if (mapping.isEmpty()) {
|
|
|
+ return changed;
|
|
|
+ }
|
|
|
+
|
|
|
+ QRegExp tagReg("(<[^>]+\\sstyle=[^>]*>)");
|
|
|
+ // Won't mixed up with background-color.
|
|
|
+ QRegExp colorReg("(\\s|\")color:([^;]+);");
|
|
|
+
|
|
|
+ int pos = 0;
|
|
|
+ while (pos < p_html.size()) {
|
|
|
+ int idx = p_html.indexOf(tagReg, pos);
|
|
|
+ if (idx == -1) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ QString styleStr = tagReg.cap(1);
|
|
|
+ QString alteredStyleStr = styleStr;
|
|
|
+
|
|
|
+ int posb = 0;
|
|
|
+ while (posb < alteredStyleStr.size()) {
|
|
|
+ int idxb = alteredStyleStr.indexOf(colorReg, posb);
|
|
|
+ if (idxb == -1) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ QString col = colorReg.cap(2).trimmed().toLower();
|
|
|
+ auto it = mapping.find(col);
|
|
|
+ if (it == mapping.end()) {
|
|
|
+ posb = idxb + colorReg.matchedLength();
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Replace the color.
|
|
|
+ QString newCol = it.value();
|
|
|
+ // Add one extra space between color and :.
|
|
|
+ QString newStr = QString("%1color : %2;").arg(colorReg.cap(1)).arg(newCol);
|
|
|
+ alteredStyleStr.replace(idxb, colorReg.matchedLength(), newStr);
|
|
|
+ posb = idxb + newStr.size();
|
|
|
+ changed = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ pos = idx + tagReg.matchedLength();
|
|
|
+ if (changed) {
|
|
|
+ pos = pos + alteredStyleStr.size() - styleStr.size();
|
|
|
+ p_html.replace(idx, tagReg.matchedLength(), alteredStyleStr);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return changed;
|
|
|
+}
|