OSDN Git Service

Unicode fix for editing source HTML.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / BrowserWindow.java
index 2741c49..2c2157c 100644 (file)
@@ -38,6 +38,7 @@ import java.util.List;
 import java.util.Locale;\r
 import java.util.StringTokenizer;\r
 \r
+import org.apache.commons.lang.StringEscapeUtils;\r
 import org.apache.commons.lang.StringUtils;\r
 \r
 import com.evernote.edam.limits.Constants;\r
@@ -3384,9 +3385,12 @@ public class BrowserWindow extends QWidget {
        // Source edited\r
        @SuppressWarnings("unused")\r
        private void sourceEdited() {\r
-               QByteArray data = new QByteArray(sourceEditHeader+sourceEdit.toPlainText()+"</body></html>");\r
+               QTextCodec codec = QTextCodec.codecForLocale();\r
+               codec = QTextCodec.codecForName("UTF-8");\r
+        String content =  codec.fromUnicode(sourceEdit.toHtml()).toString();\r
+               content = StringEscapeUtils.unescapeHtml(removeTags(content));\r
+               QByteArray data = new QByteArray(sourceEditHeader+content+"</body></html>");\r
                getBrowser().setContent(data);\r
-\r
                checkNoteTitle();\r
                noteSignal.noteChanged.emit(currentNote.getGuid(), sourceEdit.toPlainText()); \r
        }\r
@@ -3412,4 +3416,21 @@ public class BrowserWindow extends QWidget {
        public void showSource(boolean value) {\r
                sourceEdit.setVisible(value);\r
        }\r
+\r
+       // Remove HTML tags\r
+       private String removeTags(String text) {\r
+               StringBuffer buffer = new StringBuffer(text);\r
+               boolean inTag = false;\r
+               int bodyPosition = text.indexOf("<body");\r
+               for (int i=buffer.length()-1; i>=0; i--) {\r
+                       if (buffer.charAt(i) == '>')\r
+                               inTag = true;\r
+                       if (buffer.charAt(i) == '<')\r
+                               inTag = false;\r
+                       if (inTag || buffer.charAt(i) == '<' || i<bodyPosition)\r
+                               buffer.deleteCharAt(i);\r
+               }\r
+               \r
+               return buffer.toString();\r
+       }\r
 }\r