webForumDet fria alternativet

Patch till htmlarea.js

4 svar · 247 visningar · startad av kolas

kolasMedlem sedan mars 2001142 inlägg
#1

Jag försöker få att webbeditorn HTMLAREA inte ska koda om mina &.nbsp; och har hittat en patch som ska lösa detta, men jag får det inte att fungera, är rädd att det beror på mina usla javascriptkunskaper.

Denna kod försöker jag få in:

--- htmlarea.js.rc3	2005-03-15 12:44:56.664398136 +0000
+++ htmlarea.js	2005-03-15 12:44:22.442600640 +0000
@@ -2346,6 +2346,10 @@
     }
 }
 
+HTMLArea.encodeEntities = function(value) {
+  return value.replace(/&/g,'&amp;').replace(/\"/g,'&quot;').replace(/</g,'^lt;').replace(/>/g,'&gt;');
+}
+
 HTMLArea.getHTMLWrapper = function(root, outputRoot, editor) {
   var html = "";
   switch (root.nodeType) {
@@ -2422,7 +2426,7 @@
           // here; we don't need them.
           continue;
         }
-        html += " " + name + '="' + value + '"';
+        html += " " + name + '="' + HTMLArea.encodeEntities(value) + '"';
       }
       if (html != "") {
         html += closed ? " />" : ">";

i htmlarea.js nå´n stans här (rad 1947) antar jag, (jag bifogar hela htmlarea.js ifall jag är ute och cyklar)

// performs HTML encoding of some given string
HTMLArea.htmlEncode = function(str) {
	// we don't need regexp for that, but.. so be it for now.
	str = str.replace(/&/ig, "&amp;");
	str = str.replace(/</ig, "&lt;");
	str = str.replace(/>/ig, "&gt;");
	str = str.replace(/\x22/ig, "&quot;");
	// \x22 means '"' -- we use hex reprezentation so that we don't disturb
	// JS compressors (well, at least mine fails.. ;)
	return str;
};

// Retrieves the HTML code from the given node.	 This is a replacement for
// getting innerHTML, using standard DOM calls.
HTMLArea.getHTML = function(root, outputRoot, editor) {
	var html = "";
	switch (root.nodeType) {
	    case 1: // Node.ELEMENT_NODE
	    case 11: // Node.DOCUMENT_FRAGMENT_NODE
		var closed;
		var i;
		var root_tag = (root.nodeType == 1) ? root.tagName.toLowerCase() : '';
		if (HTMLArea.is_ie && root_tag == "head") {
			if (outputRoot)
				html += "<head>";
			// lowercasize
			var save_multiline = RegExp.multiline;
			RegExp.multiline = true;
			var txt = root.innerHTML.replace(HTMLArea.RE_tagName, function(str, p1, p2) {
				return p1 + p2.toLowerCase();
			});
			RegExp.multiline = save_multiline;
			html += txt;
			if (outputRoot)
				html += "</head>";
			break;
		} else if (outputRoot) {
			closed = (!(root.hasChildNodes() || HTMLArea.needsClosingTag(root)));
			html = "<" + root.tagName.toLowerCase();
			var attrs = root.attributes;
			for (i = 0; i < attrs.length; ++i) {
     var a = attrs.item(i); 
    if (!a.specified && attrs.item(i).nodeName.toLowerCase().indexOf("shape") == -1 && attrs.item(i).nodeName.toLowerCase().indexOf("coords") == -1) { 
     continue; 
    } 
				var name = a.nodeName.toLowerCase();
				if (/_moz|contenteditable|_msh/.test(name)) {
					// avoid certain attributes
					continue;
				}
				var value;
				if (name != "style") {
					// IE5.5 reports 25 when cellSpacing is
					// 1; other values might be doomed too.
					// For this reason we extract the
					// values directly from the root node.
					// I'm starting to HATE JavaScript
					// development.  Browser differences
					// suck.
					//
					// Using Gecko the values of href and src are converted to absolute links
					// unless we get them using nodeValue()
					if (typeof root[a.nodeName] != "undefined" && name != "href" && name != "src") {
						value = root[a.nodeName];
					} else {
						value = a.nodeValue;
						// IE seems not willing to return the original values - it converts to absolute
						// links using a.nodeValue, a.value, a.stringValue, root.getAttribute("href")
						// So we have to strip the baseurl manually -/
						if (HTMLArea.is_ie && (name == "href" || name == "src")) {
							value = editor.stripBaseURL(value);
						}
					}
				} else { // IE fails to put style in attributes list
					// FIXME: cssText reported by IE is UPPERCASE
					value = root.style.cssText;
				}
				if (/(_moz|^$)/.test(value)) {
					// Mozilla reports some special tags
					// here; we don't need them.
					continue;
				}
				html += " " + name + '="' + value + '"';
			}
			html += closed ? " />" : ">";
		}
		for (i = root.firstChild; i; i = i.nextSibling) {
			html += HTMLArea.getHTML(i, true, editor);
		}
		if (outputRoot && !closed) {
			html += "</" + root.tagName.toLowerCase() + ">";
		}
		break;
	    case 3: // Node.TEXT_NODE
		// If a text node is alone in an element and all spaces, replace it with an non breaking one
		// This partially undoes the damage done by moz, which translates '&nbsp;'s into spaces in the data element
		if ( !root.previousSibling && !root.nextSibling && root.data.match(/^\s*$/i) ) html = '&nbsp;';
		else html = HTMLArea.htmlEncode(root.data);
		break;
	    case 8: // Node.COMMENT_NODE
		html = "<!--" + root.data + "-->";
		break;		// skip comments, for now.
	}
	return html;
};
Peter SMedlem sedan dec. 20025 483 inlägg
#2

Testa följande:

HTMLArea.htmlEncode = function(str) {
	// we don't need regexp for that, but.. so be it for now.
	[b]str = str.replace(/&(?!nbsp;)/ig, "&amp;");[/b]
	str = str.replace(/</ig, "&lt;");
	str = str.replace(/>/ig, "&gt;");
	str = str.replace(/\x22/ig, "&quot;");
	// \x22 means '"' -- we use hex reprezentation so that we don't disturb
	// JS compressors (well, at least mine fails.. 
	return str;
};
kolasMedlem sedan mars 2001142 inlägg
#3

Det fungerade tyvärr inte, verkade inte göra nå´n skillnad alls.

Peter SMedlem sedan dec. 20025 483 inlägg
#4

Kan du bifoga ett färdigt exempel på editorn så att man själv kan se problemet?

kolasMedlem sedan mars 2001142 inlägg
#5

demoversion

Skriver man direkt & nbsp så kodar den ju om &, men om man har färdig text med & nbsp; som man klistrar in i html-läget <> så ersätt det mot ett vanligt mellanslag.

länken till patchen är här

133 ms totalt · 3 externa anrop · v20260731065814-full.86db40fa
0 ms — hämta forumlista (cache)
0 ms — hämta statistik (cache)
131 ms — hämta tråd, inlägg och bilagor (db)