From 2a2e5e4e395895d132596cea21ed41266d04455f Mon Sep 17 00:00:00 2001 From: fbennett Date: Wed, 4 Jan 2017 11:08:44 +0900 Subject: [PATCH 1/3] Preserve format tags and entities in bibliography editor - Fixes errors in previous commits 55bfe54a and 9431e0de, to wit ... - Apply unescapeHTML after RTF tag and entity conversions - Remove high-bit character escape from entity converter, and apply separately after remaining HTML entities are decoded by unescapeHTML --- .../zotero/bindings/styled-textbox.xml | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/chrome/content/zotero/bindings/styled-textbox.xml b/chrome/content/zotero/bindings/styled-textbox.xml index 67ae09f360..0ccdcd9de0 100644 --- a/chrome/content/zotero/bindings/styled-textbox.xml +++ b/chrome/content/zotero/bindings/styled-textbox.xml @@ -51,19 +51,26 @@ this._onInitCallbacks = []; this._iframe = document.getAnonymousElementByAttribute(this, "anonid", "rt-view"); + // Atomic units, HTML -> RTF (cleanup) + //[/<\/p>(?!\s*$)/g, "\\par{}"], + //[/ /g, " "], + //[/\u00A0/g, " "], this._htmlRTFmap = [ - // Atomic units, HTML -> RTF (cleanup) [/
/g, "\x0B"], + [/ <\/span>/g, "\\tab{}"], + [/‘/g, "‘"], + [/’/g, "’"], + [/“/g, "“"], + [/”/g, "”"], + [/ /g, "\u00A0"], [/"(\w)/g, "“$1"], [/([\w,.?!])"/g, "$1”"], [/

/g, ""], - //[/<\/p>(?!\s*$)/g, "\\par{}"], - [/<\/?div[^>]*>/g, ""], - [/[\x7F-\uFFFF]/g, function(aChar) { return "\\uc0\\u"+aChar.charCodeAt(0).toString()+"{}"}] + [/<\/?div[^>]*>/g, ""] ]; + // Atomic units, RTF -> HTML (cleanup) this._rtfHTMLmap = [ - // Atomic units, RTF -> HTML (cleanup) [/\\uc0\{?\\u([0-9]+)\}?(?:{}| )?/g, function(wholeStr, aCode) { return String.fromCharCode(aCode) }], [/\\tab(?:\{\}| )/g, ' '], [/(?:\\par{}|\\\r?\n)/g, "

"] @@ -339,14 +346,13 @@ } this.htmlToRTF = function(txt) { - // Catch this one before   is clobbered by unescape - txt = txt.replace(/ <\/span>/g, "\\tab{}"); - txt = Zotero.Utilities.unescapeHTML(txt); + txt = this.convert("htmlRTF", txt); for (var i=0,ilen=this._htmlRTFmap.length; i < ilen; i++) { var entry = this._htmlRTFmap[i]; txt = txt.replace(entry[0], entry[1]); } - txt = this.convert("htmlRTF", txt); + txt = Zotero.Utilities.unescapeHTML(txt); + txt = txt.replace(/[\x7F-\uFFFF]/g, function(aChar) { return "\\uc0\\u"+aChar.charCodeAt(0).toString()+"{}"}); return txt.trim(); } From 2538e258b65354024cfb79181ba7edfd15611432 Mon Sep 17 00:00:00 2001 From: fbennett Date: Wed, 4 Jan 2017 21:34:53 +0900 Subject: [PATCH 2/3] Accept variations in spacing, and single-quoted XML serialization --- .../zotero/bindings/styled-textbox.xml | 39 ++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/chrome/content/zotero/bindings/styled-textbox.xml b/chrome/content/zotero/bindings/styled-textbox.xml index 0ccdcd9de0..1ac3805179 100644 --- a/chrome/content/zotero/bindings/styled-textbox.xml +++ b/chrome/content/zotero/bindings/styled-textbox.xml @@ -84,7 +84,7 @@ var _rexData = [ [ [ - [""], + [""], ["{\\scaps ", "{\\scaps{}"] ], [ @@ -94,7 +94,7 @@ ], [ [ - [""], + [""], ["{\\ul{}", "{\\ul "] ], [ @@ -164,7 +164,7 @@ ], [ [ - [""], + [""], ["{\\scaps0{}", "{\\scaps0 "] ], [ @@ -174,7 +174,7 @@ ], [ [ - [""], + [""], ["{\\i0{}", "{\\i0 "] ], [ @@ -184,7 +184,7 @@ ], [ [ - [""], + [""], ["{\\b0{}", "{\\b0 "] ], [ @@ -204,6 +204,16 @@ } } + function normalizeRegExpString(str) { + if (!str) return str; + return str.replace(/\s+/g, " ") + .replace(/(?:[\+]|\s[\*])/g, "") + .replace(/[\']/g, '\"') + .replace(/:\s/g, ":"); + } + + this.normalizeRegExpString = normalizeRegExpString; + function composeRex(rexes, noGlobal) { var lst = []; for (var rex in rexes) { @@ -247,13 +257,15 @@ function openTagRemapMaker(segment) { var ret = {}; for (var i=0,ilen=_rexData.length; i < ilen; i++) { - var master = _rexData[i][0][segment][0]; + var primaryVal = normalizeRegExpString(_rexData[i][0][segment][0]); for (var j=0,jlen=_rexData[i][0][segment].length; j < jlen; j++) { - ret[_rexData[i][0][segment][j]] = master; + var key = normalizeRegExpString(_rexData[i][0][segment][j]); + ret[key] = primaryVal; } } return ret; } + this.rtfHTMLopenTagRemap = openTagRemapMaker(1); this.htmlRTFopenTagRemap = openTagRemapMaker(0); @@ -262,11 +274,11 @@ var ret = {}; var rexes = {}; for (var i=0,ilen=_rexData.length; i < ilen; i++) { - var master = _rexData[i][0][segment][0]; + var primaryVal = _rexData[i][0][segment][0]; for (var j=0,jlen=_rexData[i][1][segment].length; j < jlen; j++) { rexes[_rexData[i][1][segment][j]] = true; } - ret[master] = composeRex(rexes); + ret[primaryVal] = composeRex(rexes); } return ret; } @@ -281,14 +293,15 @@ } var ret = {}; for (var i=0,ilen=_rexData.length; i < ilen; i++) { - var master = _rexData[i][0][segment][0]; - ret[master] = { - open: _rexData[i][0][antisegment][0], + var primaryVal = normalizeRegExpString(_rexData[i][0][segment][0]); + ret[primaryVal] = { + open: normalizeRegExpString(_rexData[i][0][antisegment][0]), close: _rexData[i][1][antisegment][0] } } return ret; } + this.rtfHTMLtagRegistry = tagRegistryMaker(1); this.htmlRTFtagRegistry = tagRegistryMaker(0); @@ -311,7 +324,7 @@ this.getOpenTag = function(mode, str) { var m = str.match(this[mode + "openSniffRex"]); if (m) { - m = this[mode + "openTagRemap"][m[0]]; + m = this[mode + "openTagRemap"][this.normalizeRegExpString(m[0])]; } return m; } From 4d8d4bcdbcfb2c97658c5e2e74c710a909e80260 Mon Sep 17 00:00:00 2001 From: fbennett Date: Thu, 5 Jan 2017 09:57:37 +0900 Subject: [PATCH 3/3] Preserve formatting on Hide/Show editor --- .../content/zotero/integration/addCitationDialog.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/chrome/content/zotero/integration/addCitationDialog.js b/chrome/content/zotero/integration/addCitationDialog.js index 83818b59d0..7ec90e5215 100644 --- a/chrome/content/zotero/integration/addCitationDialog.js +++ b/chrome/content/zotero/integration/addCitationDialog.js @@ -41,6 +41,7 @@ var Zotero_Citation_Dialog = new function () { var _previewShown = false; var _suppressNextTreeSelect = false; var _suppressNextListSelect = false; + var _customHTML = false; var _locatorIndexArray = {}; var _locatorNameArray = {}; var _autoRegeneratePref; @@ -556,6 +557,9 @@ var Zotero_Citation_Dialog = new function () { if(_previewShown) { document.documentElement.getButton("extra2").label = Zotero.getString("citation.hideEditor"); + if (!text && _customHTML) { + text = _customHTML; + } if(text) { io.preview().then(function(preview) { _originalHTML = preview; @@ -565,6 +569,11 @@ var Zotero_Citation_Dialog = new function () { _updatePreview(); } } else { + if (editor.initialized) { + if (editor.value) { + _customHTML = editor.value; + } + } document.documentElement.getButton("extra2").label = Zotero.getString("citation.showEditor"); } } @@ -795,4 +804,4 @@ var Zotero_Citation_Dialog = new function () { function _clearCitationList() { while(_citationList.firstChild) _citationList.removeChild(_citationList.firstChild); } -} \ No newline at end of file +}