瀏覽代碼

[search] Template manager now strips html from inside functions

Enrico Berti 12 年之前
父節點
當前提交
65a5284

+ 8 - 5
apps/search/src/search/templates/admin_collection_template.mako

@@ -560,11 +560,14 @@ ${ commonheader(_('Search'), "search", user, "40px") | n,unicode }
     // Force refresh on tab change
     $("a[data-toggle='tab']").on("shown", function (e) {
       if ($(e.target).attr("href") == "#source") {
-        templateSourceMirror.setValue($("#content-editor").html());
+        templateSourceMirror.setValue(stripHtmlFromFunctions($("#content-editor").html()));
         templateSourceMirror.setSize("100%", 450);
-        for (var i = 0; i < templateSourceMirror.lineCount(); i++) {
-          templateSourceMirror.indentLine(i);
-        }
+        window.setTimeout(function(){
+          for (var i = 0; i < templateSourceMirror.lineCount(); i++) {
+            templateSourceMirror.indentLine(i);
+          }
+        }, 100);
+
         templateSourceMirror.refresh();
       }
       if ($(e.target).attr("href") == "#extra") {
@@ -585,7 +588,7 @@ ${ commonheader(_('Search'), "search", user, "40px") | n,unicode }
     templateSourceMirror.on("change", function () {
       clearTimeout(sourceDelay);
       sourceDelay = setTimeout(function () {
-        $("#content-editor").html(templateSourceMirror.getValue());
+        $("#content-editor").html(stripHtmlFromFunctions(templateSourceMirror.getValue()));
       }, 300);
     });
 

+ 18 - 2
apps/search/static/js/search.utils.js

@@ -142,7 +142,7 @@ function addTemplateFunctions(item) {
 }
 
 function fixTemplateDotsAndFunctionNames(template) {
-  var _mustacheTmpl = template;
+  var _mustacheTmpl = stripHtmlFromFunctions(template);
   var _mustacheTags = _mustacheTmpl.match(/{{(.*?)}}/g);
   $.each(_mustacheTags, function (cnt, tag) {
     if (tag.indexOf("{#") > -1) {
@@ -156,4 +156,20 @@ function fixTemplateDotsAndFunctionNames(template) {
     }
   });
   return _mustacheTmpl;
-}
+}
+
+function stripHtmlFromFunctions(template) {
+  // strips HTML from inside the functions
+  var _tmpl = template;
+  var _mustacheFunctions = _tmpl.match(/{{#(.[\s\S]*?){{\//g);
+  $.each(_mustacheFunctions, function (cnt, fn) {
+    _tmpl = _tmpl.replace(fn, fn.substr(0, fn.indexOf("}}") + 2) + $.trim(stripHtml(fn.substr(fn.indexOf("}}") + 2).slice(0, -3))) + "{{/");
+  });
+  return _tmpl;
+}
+
+function stripHtml(html) {
+  var tmp = document.createElement("DIV");
+  tmp.innerHTML = html;
+  return tmp.textContent || tmp.innerText;
+}