How to get the line separation of the system

Asked

Viewed 176 times

7

Some operating systems (i.e.: Windows and BSD) like to break lines with Carriage Return (CR) followed by Line Feed (LF), or \r\n.

Already GNU/Linux, OS X and other Unix-like usually break only with LF (\n).

Is there any way, in Javascript, to get the line break character of the client system? Some HTML 5 API or anything that allows me to get this without appealing to the header reading user agent of a request in order to verify the operating system?

  • 1

    I did not find an answer, but I did not understand what I meant by "appeal to the reading of the header user agent of a request". To my knowledge, this information is available via window.navigator (and from there oscpu, platform or userAgent - what is more reliable/easy to interpret), and can be used to infer the type of line break. Incidentally, just out of curiosity, what do you intend to do in possession of this information?

  • 1

    I’ll check the type of user line break to assemble a text and make a request to the server. From the request a file will be generated and I will redirect the user to that file. I wish the file had user system breaks.

  • Why the fact that the line break is with CR or LF is important in this case?

  • @gabrielhof wanted the breaks to be straight in the text editors (friends of mine THE X'istas complain when they have r, I Windows'ista complain when they do not have), but I confess that there is also a mixture of academic curiosity with pedance on my part.

  • Will this text be generated and displayed in the browser? As for the server, what do you do with the text in it?

  • 1

    As a curiosity, it is interesting. For practical use, I cannot imagine. Ex: A user with Linux records the output on a USB stick, delivers the USB stick to a friend who uses Windows, who will need the text. We have just lost all effort to rely on the OS of the request to decide the output format (remembering that if it is to use with http only, the line break is part of the standard specification, not OS).

  • @gabrielhof use as input to make a request that mounts a file, and then force the download of the file.

  • 1

    Well, in that case, I think you’d better just standardize \n due to the case @Bacco cited.

  • I’m coming to that conclusion too. But I was still wondering if it’s possible to get the line separator just out of curiosity right now.

Show 4 more comments

3 answers

3

You can do something very close to what you want without using Javascript. The HTTP header User-Agent includes information about your client’s browser and operating system and your server can look at this when receiving the client’s form. It’s not 100% accurate, since the value of the User-Agent header is user configurable, but at the same time I don’t believe there is any 100% accurate way to know the client’s OS.

Of course, another possibility is to add an extra field to your form and ask the client what kind of line break he prefers :P

2


In the specification of ECMA a section 7.3 only sets the grammar and possible tokens recognized as line break.

As there is no exact way to find out the characters of the line break. I log a heuristic for line break style detection:

function getLineBreakSequence() {
    var div, ta, text;

    div = document.createElement("div");
    div.innerHTML = "<textarea>one\ntwo</textarea>";
    ta = div.firstChild;
    text = ta.value;

    return text.indexOf("\r") >= 0 ? "\r\n" : "\n";
}

According to T.J. Crowder in that response from SO EN, this function works because the browsers that use the \r\n make the conversion on-the-fly of \n when they do the Parsing of the HTML string.

In addition it is necessary to emphasize that the results may vary for the same OS. Because different browsers do different things.

It is possible to test in the Jsbin.

1

As a practical application, I imagine the idea is to identify the line break (line ending) by the S.O. to be able to swap for an HTML tag, for example, to show in real time the output of a textarea field (as in Stackoverflow).

You can use a regular expression that identifies the \n (used by *NIX) or \r\n (used by Windows).

htmlstring = stringContainingNewLines.replace(/(\r\n|\n|\r)/gm, "<br>");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.