From b45f1d95185822b2b21f12418bfe9acd7dfc8c97 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Mon, 22 Jan 2018 20:29:00 +0800 Subject: [PATCH] fix font-family issue in WeChat Public Account editor It could not recognize " in font-family. --- .../themes/v_moonlight/v_moonlight.css | 2 +- src/resources/themes/v_pure/v_pure.css | 2 +- src/resources/themes/v_white/v_white.css | 2 +- src/resources/vnote.ini | 4 +- src/utils/vwebutils.cpp | 112 +++++++++++++++++- src/utils/vwebutils.h | 6 + 6 files changed, 123 insertions(+), 5 deletions(-) diff --git a/src/resources/themes/v_moonlight/v_moonlight.css b/src/resources/themes/v_moonlight/v_moonlight.css index 6147f7b4..8e389dda 100644 --- a/src/resources/themes/v_moonlight/v_moonlight.css +++ b/src/resources/themes/v_moonlight/v_moonlight.css @@ -84,7 +84,7 @@ pre { } code { - font-family: Consolas, Monaco, Andale Mono, Monospace, Courier New; + font-family: Consolas, Monaco, Monospace, Courier; font-size: 16px; color: #98C379; } diff --git a/src/resources/themes/v_pure/v_pure.css b/src/resources/themes/v_pure/v_pure.css index 6e587f29..c93d8f78 100644 --- a/src/resources/themes/v_pure/v_pure.css +++ b/src/resources/themes/v_pure/v_pure.css @@ -84,7 +84,7 @@ pre { } code { - font-family: Consolas, Monaco, Andale Mono, Monospace, Courier New; + font-family: Consolas, Monaco, Monospace, Courier; font-size: 16px; color: #8E24AA; } diff --git a/src/resources/themes/v_white/v_white.css b/src/resources/themes/v_white/v_white.css index 3d43deb7..1d1b7da4 100644 --- a/src/resources/themes/v_white/v_white.css +++ b/src/resources/themes/v_white/v_white.css @@ -83,7 +83,7 @@ pre { } code { - font-family: Consolas, Monaco, Andale Mono, Monospace, Courier New; + font-family: Consolas, Monaco, Monospace, Courier; font-size: 16px; color: #8E24AA; } diff --git a/src/resources/vnote.ini b/src/resources/vnote.ini index 11fdcda1..727dc18c 100644 --- a/src/resources/vnote.ini +++ b/src/resources/vnote.ini @@ -219,7 +219,9 @@ styles_to_inline_when_copied=all$border:color:display:font-family:font-size:font ; p - replace the background color of
 with that of its child 
 ; n - replace the \n in 
 with 
; g - replace local relative/absolute tag with a warning label -copy_targets="Without Background"$s:b(mark):c:i:x,OneNote$s:b(mark):c:i:m:a:x,"Microsoft Word"$s:p:b(mark|pre):c(pre):i:m:a:x,"WeChat Public Account"$s:p:b(mark|pre):c(pre):g:m:x:n,"Web Editor"$s:p:b(mark|pre):c(pre):m:x:n,"Raw HTML"$r:x +; d - add to which is not inside
+; f - replace " with ' in font-family style
+copy_targets="Without Background"$s:b(mark):c:i:x,OneNote$s:b(mark):c:i:m:a:x,"Microsoft Word"$s:p:b(mark|pre):c(pre):i:m:a:x,"WeChat Public Account"$s:p:b(mark|pre):c(pre):g:m:x:n:f,"Web Editor"$s:p:b(mark|pre):c(pre):m:x:n,"Raw HTML"$r:x
 
 [shortcuts]
 ; Define shortcuts here, with each item in the form "operation=keysequence".
diff --git a/src/utils/vwebutils.cpp b/src/utils/vwebutils.cpp
index 2fdd60bc..60399573 100644
--- a/src/utils/vwebutils.cpp
+++ b/src/utils/vwebutils.cpp
@@ -221,6 +221,14 @@ bool VWebUtils::alterHtmlByTargetAction(const QUrl &p_baseUrl, QString &p_html,
         altered = replaceLocalImgWithWarningLabel(p_html);
         break;
 
+    case 'd':
+        altered = addSpanInsideCode(p_html);
+        break;
+
+    case 'f':
+        altered = replaceQuoteInFontFamily(p_html);
+        break;
+
     default:
         break;
     }
@@ -228,7 +236,10 @@ bool VWebUtils::alterHtmlByTargetAction(const QUrl &p_baseUrl, QString &p_html,
     return altered;
 }
 
-static int skipToTagEnd(const QString &p_html, int p_pos, const QString &p_tag)
+static int skipToTagEnd(const QString &p_html,
+                        int p_pos,
+                        const QString &p_tag,
+                        int *p_endTagIdx = NULL)
 {
     QRegExp beginReg(QString("<%1 ").arg(p_tag));
     QRegExp endReg(QString("").arg(p_tag));
@@ -243,7 +254,13 @@ static int skipToTagEnd(const QString &p_html, int p_pos, const QString &p_tag)
     }
 
     if (nEnd > -1) {
+        if (p_endTagIdx) {
+            *p_endTagIdx = nEnd;
+        }
+
         pos = nEnd + endReg.matchedLength();
+    } else if (p_endTagIdx) {
+        *p_endTagIdx = -1;
     }
 
     return pos;
@@ -675,3 +692,96 @@ bool VWebUtils::replaceLocalImgWithWarningLabel(QString &p_html)
 
     return altered;
 }
+
+bool VWebUtils::addSpanInsideCode(QString &p_html)
+{
+    bool altered = false;
+    int pos = 0;
+
+    while (pos < p_html.size()) {
+        int tagIdx = p_html.indexOf(m_tagReg, pos);
+        if (tagIdx == -1) {
+            break;
+        }
+
+        QString tagName = m_tagReg.cap(1);
+        QString lowerName = tagName.toLower();
+        if (lowerName == "pre") {
+            // Skip 
.
+            pos = skipToTagEnd(p_html, tagIdx + m_tagReg.matchedLength(), tagName);
+            continue;
+        }
+
+        if (lowerName != "code") {
+            pos = tagIdx + m_tagReg.matchedLength();
+            continue;
+        }
+
+        int idx = tagIdx + m_tagReg.matchedLength() - 1;
+        Q_ASSERT(p_html[idx] == '>');
+        QString span = QString(">").arg(m_tagReg.cap(2));
+        p_html.replace(idx, 1, span);
+
+        int codeEnd = skipToTagEnd(p_html, idx + span.size(), tagName, &idx);
+        Q_ASSERT(idx > -1);
+        Q_ASSERT(codeEnd - idx == 7);
+        Q_ASSERT(p_html[idx] == '<');
+        p_html.replace(idx, 1, "<");
+
+        pos = codeEnd;
+
+        altered = true;
+    }
+
+    return altered;
+}
+
+// @p_html is the style string.
+static bool replaceQuoteInFontFamilyInStyleString(QString &p_html)
+{
+    QRegExp reg("font-family:((")|[^;])+;");
+    int idx = p_html.indexOf(reg);
+    if (idx == -1) {
+        return false;
+    }
+
+    QString quote(""");
+    QString family = reg.cap(0);
+    if (family.indexOf(quote) == -1) {
+        return false;
+    }
+
+    QString newFamily = family.replace(quote, "'");
+    p_html.replace(idx, reg.matchedLength(), newFamily);
+    return true;
+}
+
+bool VWebUtils::replaceQuoteInFontFamily(QString &p_html)
+{
+    bool altered = false;
+    int pos = 0;
+
+    while (pos < p_html.size()) {
+        int idx = p_html.indexOf(m_styleTagReg, pos);
+        if (idx == -1) {
+            break;
+        }
+
+        QString styleStr = m_styleTagReg.cap(3);
+        if (replaceQuoteInFontFamilyInStyleString(styleStr)) {
+            QString newTag = QString("<%1%2style=\"%3\"%4>").arg(m_styleTagReg.cap(1))
+                                                            .arg(m_styleTagReg.cap(2))
+                                                            .arg(styleStr)
+                                                            .arg(m_styleTagReg.cap(4));
+            p_html.replace(idx, m_styleTagReg.matchedLength(), newTag);
+
+            pos = idx + newTag.size();
+
+            altered = true;
+        } else {
+            pos = idx + m_styleTagReg.matchedLength();
+        }
+    }
+
+    return altered;
+}
diff --git a/src/utils/vwebutils.h b/src/utils/vwebutils.h
index c811c553..8288020f 100644
--- a/src/utils/vwebutils.h
+++ b/src/utils/vwebutils.h
@@ -97,6 +97,12 @@ private:
     // Replace local absolute/relative  tag with a warning label.
     bool replaceLocalImgWithWarningLabel(QString &p_html);
 
+    // Add  inside  not in 
.
+    bool addSpanInsideCode(QString &p_html);
+
+    // Replace " in font-family with '.
+    bool replaceQuoteInFontFamily(QString &p_html);
+
     QVector m_copyTargets;
 
     // Custom styles to remove when copied.