Permalink
Browse files

Adding code formatting functions for better readability

  • Loading branch information...
1 parent 794f641 commit 24da8203e154cb30520121ada8f4a6e05672fe8b julmis committed Mar 24, 2005
Showing with 52 additions and 3 deletions.
  1. +52 −3 lib/editor/htmlarea.php
View
@@ -2185,13 +2185,13 @@ function (str, l1, l2, l3) {
}
html += " " + name + '="' + value + '"';
}
- html += closed ? " />" : ">";
+ html += closed ? " />\n" : ">";
}
for (i = root.firstChild; i; i = i.nextSibling) {
html += HTMLArea.getHTML(i, true, editor);
}
if (outputRoot && !closed) {
- html += "</" + root.tagName.toLowerCase() + ">";
+ html += "</" + root.tagName.toLowerCase() + ">\n";
}
break;
case 3: // Node.TEXT_NODE
@@ -2204,7 +2204,8 @@ function (str, l1, l2, l3) {
html = "<!--" + root.data + "-->";
break; // skip comments, for now.
}
- return html;
+ //html = html.replace(/^\n+/, '');
+ return HTMLArea.formathtml(html);
};
HTMLArea.prototype.stripBaseURL = function(string) {
@@ -2328,3 +2329,51 @@ function hex(d) {
return el;
return null;
};
+
+HTMLArea.formathtml = function (html) {
+ // Original idea from FCKeditor
+ // http://www.fckeditor.net/
+ // by Frederico Caldeira Knabben
+
+ var indentchar = ' ';
+ var format = new Object();
+ format.regex = new Object();
+
+ format.regex.tagopen = /\<[^\/](P|DIV|H1|H2|H3|H4|H5|H6|ADDRESS|PRE|OL|UL|LI|TITLE|META|LINK|BASE|SCRIPT|LINK|TD|AREA|OPTION)[^\>]*\>/gi ;
+ format.regex.tagclose = /\<\/{1}(P|DIV|H1|H2|H3|H4|H5|H6|ADDRESS|PRE|OL|UL|LI|TITLE|META|LINK|BASE|SCRIPT|LINK|TD|AREA|OPTION)[^\>]*\>/gi ;
+ format.regex.newlines = /\<(BR|HR)[^\>]\>/gi ;
+ format.regex.tagsmain = /\<\/?(HTML|HEAD|BODY|FORM|TABLE|TBODY|THEAD|TR)[^\>]*\>/gi ;
+ format.regex.splitter = /\s*\n+\s*/g ;
+
+ format.regex.indent = /^\<(HTML|HEAD|BODY|FORM|TABLE|TBODY|THEAD|TR|UL|OL)[ \/\>]/i ;
+ format.regex.unindent = /^\<\/(HTML|HEAD|BODY|FORM|TABLE|TBODY|THEAD|TR|UL|OL)[ \>]/i ;
+ format.regex.inremove = new RegExp( indentchar );
+
+ var formatted = html.replace( format.regex.tagopen, '\n$&' );
+ formatted = formatted.replace( format.regex.tagclose, '$&\n' );
+ formatted = formatted.replace( format.regex.taglines, '$&\n' );
+ formatted = formatted.replace( format.regex.tagsmain, '\n$&\n' );
+
+ var indentation = '';
+ var tolines = formatted.split(format.regex.splitter);
+ var formatted = '';
+
+ for (var i = 0; i < tolines.length; i++) {
+ var line = tolines[i];
+ if (line.length < 1) {
+ continue;
+ }
+ if (format.regex.unindent.test(line)) {
+ indentation = indentation.replace(format.regex.inremove, '') ;
+ }
+
+ line = line.replace(format.regex.tagclose, '$&\n');
+ line = line.replace(format.regex.tagsmain, '$&\n');
+ formatted += indentation + line;
+
+ if (format.regex.indent.test(line)) {
+ indentation += indentchar;
+ }
+ }
+ return formatted;
+}

0 comments on commit 24da820

Please sign in to comment.