[LON-CAPA-cvs] cvs: loncom(version_2_11_X) /interface lonhtmlcommon.pm

raeburn raeburn at source.lon-capa.org
Thu Sep 15 13:31:01 EDT 2016


raeburn		Thu Sep 15 17:31:01 2016 EDT

  Modified files:              (Branch: version_2_11_X)
    /loncom/interface	lonhtmlcommon.pm 
  Log:
  - For 2.11
    - Backport 1.359, 1.360.
  
  
Index: loncom/interface/lonhtmlcommon.pm
diff -u loncom/interface/lonhtmlcommon.pm:1.358.2.6 loncom/interface/lonhtmlcommon.pm:1.358.2.7
--- loncom/interface/lonhtmlcommon.pm:1.358.2.6	Wed Aug 10 03:17:15 2016
+++ loncom/interface/lonhtmlcommon.pm	Thu Sep 15 17:31:00 2016
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # a pile of common html routines
 #
-# $Id: lonhtmlcommon.pm,v 1.358.2.6 2016/08/10 03:17:15 raeburn Exp $
+# $Id: lonhtmlcommon.pm,v 1.358.2.7 2016/09/15 17:31:00 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1342,6 +1342,41 @@
     }
     
     function startRichEditor(id) {
+        // fix character entities inside <m>
+        // NOTE: this is not fixing characters inside <parse>
+        // NOTE: < and > inside <chem> should fix automatically because there should not be a letter after <.
+        var ta = document.getElementById(id);
+        var value = ta.value;
+        var in_m = false; // in the m element
+        var in_text = false; // in the text inside the m element
+        var im = -1; // position of <m>
+        var it = -1; // position of the text inside
+        for (var i=0; i<value.length; i++) {
+            if (value.substr(i, 2) == "<m") {
+                // ignore previous <m> if found twice
+                in_m = true;
+                in_text = false;
+                im = i;
+                it = -1;
+            } else if (in_m) {
+                if (!in_text) {
+                    if (value.charAt(i) == ">") {
+                        in_text = true;
+                        it = i+1;
+                    }
+                } else if (value.substr(i, 4) == "</m>") {
+                    in_m = false;
+                    var text = value.substr(it, i-it);
+                    var l1 = text.length;
+                    text = text.replace(/</g, "<");
+                    text = text.replace(/>/g, ">");
+                    var l2 = text.length;
+                    value = value.substr(0, it) + text + "</m>" + value.substr(i+4);
+                    i = i + (l2-l1);
+                }
+            }
+        }
+        ta.value = value;
     	CKEDITOR.replace(id, 
     		{
     			customConfig: "/ckeditor/loncapaconfig.js",
@@ -1353,6 +1388,68 @@
     
     function destroyRichEditor(id) {
     	CKEDITOR.instances[id].destroy();
+        // replace character entities < and > in <m> and <chem>
+        // and "&fctname(" by "&fctname("
+        // and the quotes inside functions: "&fct(1, "a")" -> "&fct(1, "a")"
+        var ta = document.getElementById(id);
+        var value = ta.value;
+        var in_element = false; // in the m or chem element
+        var tagname = ""; // m or chem
+        var in_text = false; // in the text inside the element
+        var im = -1; // position of start tag
+        var it = -1; // position of the text inside
+        for (var i=0; i<value.length; i++) {
+            if (value.substr(i, 2) == "<m" || value.substr(i, 5) == "<chem") {
+                // ignore previous tags if found twice
+                in_element = true;
+                if (value.substr(i, 2) == "<m")
+                    tagname = "m";
+                else
+                    tagname = "chem";
+                in_text = false;
+                im = i;
+                it = -1;
+            } else if (in_element) {
+                if (!in_text) {
+                    if (value.charAt(i) == ">") {
+                        in_text = true;
+                        it = i+1;
+                    }
+                } else if (value.substr(i, 3+tagname.length) == "</"+tagname+">") {
+                    in_element = false;
+                    var text = value.substr(it, i-it);
+                    var l1 = text.length;
+                    text = text.replace(/</g, "<");
+                    text = text.replace(/>/g, ">");
+                    var l2 = text.length;
+                    value = value.substr(0, it) + text + value.substr(i);
+                    i = i + (l2-l1);
+                }
+            }
+        }
+        // fix function names
+        value = value.replace(/&([a-zA-Z_]+)\(/g, "&$1(");
+        // fix quotes in functions
+        var pos_next_fct = value.search(/&[a-zA-Z_]+\(/);
+        var depth = 0;
+        for (var i=0; i<value.length; i++) {
+            if (i == pos_next_fct) {
+                depth++;
+                var sub = value.substring(i+1);
+                var pos2 = sub.search(/&[a-zA-Z_]+\(/);
+                if (pos2 == -1)
+                    pos_next_fct = -1;
+                else
+                    pos_next_fct = i + 1 + pos2;
+            } else if (depth > 0) {
+                if (value.charAt(i) == ")")
+                    depth--;
+                else if (value.substr(i, 6) == """)
+                    value = value.substr(0, i) + "\"" + value.substr(i+6);
+            }
+        }
+        // replace the text value
+        ta.value = value;
     }
     
     function editorHandler(event) {




More information about the LON-CAPA-cvs mailing list