0

(Q) How to copy (thousands of) carriage returns (not new lines) from one textarea to another?
I used the script below to check for "broken characters".
And apparently the carriage return is the only character that won't copy correctly.
I would like to avoid substituting.

(Edit 1)
Carriage returns (code 13) automatically get converted to "\n".
"\n".charCodeAt(0); returns 10.
I need it to return 13.
(Q) Is there a way to convert all carriage returns that got converted to new lines back to carriage returns, without converting new lines that are not converted from a carriage return?

(Edit 2)
It seems like I will have to use a substitute for carriage returns.
(Q) Any suggestions?

function getListOfChars()
{
    let arrayOfChars = [];

    for(let charCode = 0; charCode < 65536 /*1114112*/; charCode++)
    {
        arrayOfChars.push(String.fromCharCode(charCode));
        //arrayOfChars.push(String.fromCodePoint(charCode));
    }

    return arrayOfChars;
}

function getBrokenChars()
{
    let listOfChars = getListOfChars();
    let listOfBrokenChars = [];
    let char;
    let textareaValue;
    let textareaValueCharCode;

    for(let x = 0; x < listOfChars.length; x++)
    {
        char = listOfChars[x];
        document.getElementById('textarea').value = char;
        textareaValue = document.getElementById('textarea').value;
        textareaValueCharCode = textareaValue.charCodeAt(0);
        //textareaValueCharCode = textareaValue.codePointAt(0);

        if(x !== textareaValueCharCode)
        {
            listOfBrokenChars.push(char);
            console.log("\"" + char + "\"" + " (" + x + ")" + " -> " + "\"" + textareaValue + "\"" + " (" + textareaValueCharCode + ")");
        }
    }

    return listOfBrokenChars;
}

let brokenChars = getBrokenChars();
<!DOCTYPE html>
<html>
<head>
    <title>Fix Bug Char Codes</title>
</head>
<body>
    <textarea id="textarea">a</textarea>
</body>
<script src="fixBugCharCodes.js"></script>
</html>
Makyen
  • 31,849
  • 12
  • 86
  • 121
  • try first by adding in your head: `` – Mister Jojo Sep 05 '20 at 02:18
  • Does this answer your question? [Convert character to ASCII code in JavaScript](https://stackoverflow.com/questions/94037/convert-character-to-ascii-code-in-javascript) – Mister Jojo Sep 05 '20 at 02:48
  • 2
    Your problem can't be solved directly because `\r` cannot exist in the "API value" of a textarea. Textareas have 3 values, the raw value which is what the user pasted and may contain standalone `\r` (but you cannot access it in JS, only the browser itself has access to it), the API value which has all newlines normalized to `\n` (which is what you get in JS) and the submission value which has all newlines normalized to `\r\n` (which is what's sent in the POST body when the form is submitted). See https://stackoverflow.com/questions/14217101/what-character-represents-a-new-line-in-a-text-area – CherryDT Sep 05 '20 at 15:12
  • Re the edited question, you still can't copy carriage returns from one textarea to another because you won't find any in the textarea being used as the source. This is looking like it may be an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – traktor Sep 06 '20 at 00:17

1 Answers1

2

You can'tg copy \r to a text area (at least in Firefox when tested) because cross-platform browsers treat a carriage return as being used as a newline character under some O/S, and replace it with a newline character, '\n', so it gets treated as newline in JavaSdcript.

Similarly if you put an MSDOS new line pair CRLF ('\r\n`) into a text area, browsers will convert the pair to a single newline charavter.

traktor
  • 17,588
  • 4
  • 32
  • 53