#! rnews 3610 Path: news.demon.co.uk!mutlu.news.demon.net!peer-uk.news.demon.net!kibo.news.demon.net!demon!newsfeed.icl.net!newsfeed.fjserv.net!news.tele.dk!news.tele.dk!small.news.tele.dk!uninett.no!news.net.uni-c.dk!sunsite.dk!sonofon.dk!194.19.194.2.MISMATCH!newsfeed101.telia.com!nf02.dk.telia.net!news104.dk.telia.net!not-for-mail Newsgroups: comp.lang.javascript Subject: Re: Javascript formatter? References: From: Lasse Reichstein Nielsen Date: Thu, 07 Apr 2005 22:12:52 +0200 Message-ID: User-Agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Rational FORTRAN, windows-nt) Cancel-Lock: sha1:ktb4H/2RTgx2EbZ+hzc6qXoQdHw= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Lines: 73 NNTP-Posting-Host: 62.107.31.6 X-Trace: 1112904764 news.stofanet.dk 644 62.107.31.6:38312 X-Complaints-To: Telia Stofa Abuse Xref: news.demon.co.uk comp.lang.javascript:107452 Dr John Stockton writes: >> var text = func.toString(); > Code display within my javascript pages is largely based on > function.toString(); I only see the results in MSIE. > > I write my code (E&OE) to fit within 69-character-wide boxes (as seen in > IE 4), and I choose the height of each box to suit the code. Yes, checking in IE, I can see that the code doesn't have to overflow the box :) > Are the results generally acceptable in other browsers, and could > anything be done to make the average better (given the wide use of IE)? The boxes overflow in both Mozilla and Opera. One of the problems is the number of empty lines between functions in boxes with more than one function. In IE, there is one line, in both Mozilla and Opera, there are three (an extra line before and after each function). It shouldn't hurt to remove these empty lines, if they exist. Another difference is that IE doesn't reformat the body of the function at all, whereas the other browsers do. In particular, they move "}"'s to a line of their own. Opera also moves "{"'s to their own line (the thing that makes me prefer Mozilla's format). I don't see a simple solution to this. The non-IE browsers also doesn't include comments in the output. All in all, lines are probably shorter than in IE. A non-simple solution would be to make the ShowFF function calculate the necessary number of lines for the textarea, instead of providing it as an argument. E.g.: --- function trim(str) { var match = /\S([\s\S]*\S)*/.exec(str); return match ? match[0] : ""; } function countLines(str, lineLength) {//counts lines when wrapped at lineLength var lines = str.split(/[\n\r]/g); var lineCount = lines.length; for (var i = 0; i < lines.length; i++) { lineCount += Math.floor(lines[i].length / lineLength); } return lineCount; } function ShowFF() { // Args are functions, last Arg is unused var Len = arguments.length-1, S = "" for (var j=0 ; j0) { S += "\n\n"; } S += trim(arguments[j].toString()); } var numLines = countLines(S, BoxX); Depict(BoxX, numLines, S, "lightgreen") ; return "" } function ShowDo(Fn, Ht) { // N.B. this calls Fn() var string = trim(Fn.toString()); Depict(BoxX, countLines(string, BoxX), string, "red" ) ; Fn() ; return "" } --- Good luck /L -- Lasse Reichstein Nielsen - lrn@hotpop.com DHTML Death Colors: 'Faith without judgement merely degrades the spirit divine.'