Using CR only if interruption inside pre pre does not work

At work, we stumbled upon Bugzilla, creating HTML output that resulted in lines that were too long because the browser did not break the lines. This happened in Chrome, but not in Firefox 3.5, so we don't care. But Firefox 4 behaves exactly like Chrome, so we had to find another workaround.

Example:

<html> <body> <pre> Lorem ipsum dolor sit amet, consetetur sadipscing elitr,&#013;sed diam nonumy eirmod tempor invidunt ut labore et&#013;dolore magna aliquyam erat, sed diam voluptua. At vero eos&#013;et accusam et justo duo dolores et ea rebum. Stet clita kasd&#013;gubergren, no sea takimata sanctus est Lorem ipsum dolor sit&#013;amet.&#013; </pre> </body> </html> 

The server uses only CR as a string, which is very unusual, and the usual alternatives (CR + LF, only LF) work correctly, so the correct way to fix this is to tell the Bugzilla server to use one of these lines methods. Anyway, I'm curious why not working and ignoring line breaks seems to be the β€œright” way for browsers.

Also, I found a weird local solution for Chrome and FF 4 using the Greasemonkey script (a modified version of this ):

 var els = document.getElementsByTagName("*"); for(var i = 0, l = els.length; i < l; i++) { var el = els[i]; el.innerHTML = el.innerHTML; } 

It didn't seem to affect the page, but with this script, line-to-line lines suddenly display correctly.

So my questions are:

  • Is the Chrome / FF 4 method the β€œright” way to handle these string types inside <pre> ?
  • Why does this Greasemonkey script work?
+4
source share
2 answers

The GM script works because, apparently, JS converts CR ( \r ) to LF ( \n ), dynamically writing to the DOM.

See this test in jsFiddle . Notice how CR (decimal number 13), at the end of the second line, is converted to LF (decimal 10).

+3
source

Yes, HTML RFC defines line breaks as: http://www.w3.org/TR/html401/struct/text.html#line-breaks

A line break is defined to be a carriage return (& # x000D;), a line feed (& # x000A;), or a carriage return / line feed pair. All line breaks constitute white space.

However, bare carriage returns are extremely rare. I am not surprised that this will not work. But technically, I would say that FF4 and Chrome are wrong.

Not sure why your greasemonkey script is working. I assume that getting el.innerHTML will convert CR to CR-LF or LF.

+3
source

All Articles