<?php
// cr_andor_lf.php
// Carriage Return and/or Linefeed
// RJM Programming
// April, 2022

$tval='';
$styleextra='';
if (isset($_POST['thistitle'])) {
  $tval=str_replace('+',' ',urldecode($_POST['thistitle']));
  if (strpos($tval, "\r\n") !== false) {
    $styleextra=' #justrn { background-color: yellow; }  ';
  } else if (strpos($tval, "\n") !== false) {
    $styleextra=' #justn { background-color: yellow; }  ';
  } else if (strpos($tval, "\r") !== false) {
    $styleextra=' #justr { background-color: yellow; }  ';
  }
} else if (isset($_GET['thistitle'])) {
  $tval=str_replace('+',' ',urldecode($_GET['thistitle']));
  if (strpos($tval, "\r\n") !== false) {
    $styleextra=' #justrn { background-color: yellow; }  ';
  } else if (strpos($tval, "\n") !== false) {
    $styleextra=' #justn { background-color: yellow; }  ';
  } else if (strpos($tval, "\r") !== false) {
    $styleextra=' #justr { background-color: yellow; }  ';
  }
}

echo "<html>
<head>
<style>
  td {
    vertical-align:top;
  }
" . $styleextra . "
</style>
<scr" . "ipt type=text/javascript>
 var justr=false, justn=false, justrn=false;
 var twentythree=-1, tts='';
 
 function whichd(ofo) {
   //alert(90);
   var dv=document.getElementById('thistitle').value;
   var dvs=document.getElementById('thistitle').value.split(String.fromCharCode(13) + String.fromCharCode(10));
   twentythree=23;
   tts=String.fromCharCode(13) + String.fromCharCode(10);
   //alert('' + dvs.length);
   if (eval('' + dvs.length) <= 1) { dvs=document.getElementById('thistitle').value.split(String.fromCharCode(10)); if (eval('' + dvs.length) != 0) { twentythree=10;  tts=String.fromCharCode(10); }  }
   if (eval('' + dvs.length) <= 1) { dvs=document.getElementById('thistitle').value.split(String.fromCharCode(13)); if (eval('' + dvs.length) != 0) { twentythree=13;  tts=String.fromCharCode(13); }  }
   if (eval('' + encodeURIComponent(dv).length) < 800) {
     //alert(91);
     setTimeout(whichdofo, 2000);
     return false;
   }
   return whichdofo();
 }
 
 function whichdofo() {
   var dv=document.getElementById('thistitle').value;
   var wasv=dv;
   var dvs=document.getElementById('thistitle').value.split(String.fromCharCode(13) + String.fromCharCode(10));
   twentythree=23;
   tts=String.fromCharCode(13) + String.fromCharCode(10);
   if (eval('' + dvs.length) <= 1) { dvs=document.getElementById('thistitle').value.split(String.fromCharCode(10)); if (eval('' + dvs.length) != 0) { twentythree=10;  tts=String.fromCharCode(10); }  }
   if (eval('' + dvs.length) <= 1) { dvs=document.getElementById('thistitle').value.split(String.fromCharCode(13)); if (eval('' + dvs.length) != 0) { twentythree=13;  tts=String.fromCharCode(13); }  }
   if (eval('' + encodeURIComponent(document.getElementById('thistitle').value).length) < 800) {
   document.getElementById('myform').method='GET';
   if (justrn && twentythree == 23) { location.href=document.URL.split('#')[0].split('?')[0] + '?thistitle=' + encodeURIComponent(wasv); return false; }
   if (justr && twentythree == 13) { location.href=document.URL.split('#')[0].split('?')[0] + '?thistitle=' + encodeURIComponent(wasv); return false; }
   if (justn && twentythree == 10) { location.href=document.URL.split('#')[0].split('?')[0] + '?thistitle=' + encodeURIComponent(wasv); return false; }

   if (justrn) {

   if (twentythree == 13) {
   while (wasv.indexOf(String.fromCharCode(13)) != -1) { wasv=wasv.replace(String.fromCharCode(13),'~!@#');  }
   wasv=wasv.replace(/\~\!\@\#/g, String.fromCharCode(13) + String.fromCharCode(10));

   } else if (twentythree == 10) {
   while (wasv.indexOf(String.fromCharCode(10)) != -1) { wasv=wasv.replace(String.fromCharCode(10),'~!@#');  }
   wasv=wasv.replace(/\~\!\@\#/g, String.fromCharCode(13) + String.fromCharCode(10));

   }
   
   } else if (justr) {

   if (twentythree == 23) {
   while (wasv.indexOf(String.fromCharCode(13) + String.fromCharCode(10)) != -1) { wasv=wasv.replace(String.fromCharCode(13) + String.fromCharCode(10),'~!@#');  }
   wasv=wasv.replace(/\~\!\@\#/g, String.fromCharCode(13));

   } else if (twentythree == 10) {
   while (wasv.indexOf(String.fromCharCode(10)) != -1) { wasv=wasv.replace(String.fromCharCode(10),'~!@#');  }
   wasv=wasv.replace(/\~\!\@\#/g, String.fromCharCode(13));

   }
   
   } else if (justn) {

   if (twentythree == 23) {
   while (wasv.indexOf(String.fromCharCode(13) + String.fromCharCode(10)) != -1) { wasv=wasv.replace(String.fromCharCode(13) + String.fromCharCode(10),'~!@#');  }
   wasv=wasv.replace(/\~\!\@\#/g, String.fromCharCode(10));

   } else if (twentythree == 13) {
   while (wasv.indexOf(String.fromCharCode(13)) != -1) { wasv=wasv.replace(String.fromCharCode(13),'~!@#');  }
   wasv=wasv.replace(/\~\!\@\#/g, String.fromCharCode(10));

   }
   
   } 
    
   location.href=document.URL.split('#')[0].split('?')[0] + '?thistitle=' + encodeURIComponent(wasv);
   return false;
    
   }
   if (justrn && twentythree == 23) {    return true; }
   if (justr && twentythree == 13) {   return true; }
   if (justn && twentythree == 10) {   return true; }

   //alert(8);

   if (justrn) {

   if (twentythree == 13) {
   while (document.getElementById('thistitle').value.indexOf(String.fromCharCode(13)) != -1) { document.getElementById('thistitle').value=document.getElementById('thistitle').value.replace(String.fromCharCode(13),'~!@#$');  }
   document.getElementById('thistitle').value=document.getElementById('thistitle').value.replace(/\~\!\@\#\$/g, String.fromCharCode(13) + String.fromCharCode(10));

   } else if (twentythree == 10) {
   while (document.getElementById('thistitle').value.indexOf(String.fromCharCode(10)) != -1) { document.getElementById('thistitle').value=document.getElementById('thistitle').value.replace(String.fromCharCode(10),'~!@#$');  }
   document.getElementById('thistitle').value=document.getElementById('thistitle').value.replace(/\~\!\@\#\$/g, String.fromCharCode(13) + String.fromCharCode(10));

   }
   
   } else if (justr) {

   if (twentythree == 23) {
   while (document.getElementById('thistitle').value.indexOf(String.fromCharCode(13) + String.fromCharCode(10)) != -1) { document.getElementById('thistitle').value=document.getElementById('thistitle').value.replace(String.fromCharCode(13) + String.fromCharCode(10),'~!@#$');  }
   document.getElementById('thistitle').value=document.getElementById('thistitle').value.replace(/\~\!\@\#\$/g, String.fromCharCode(13));

   } else if (twentythree == 10) {
   while (document.getElementById('thistitle').value.indexOf(String.fromCharCode(10)) != -1) { document.getElementById('thistitle').value=document.getElementById('thistitle').value.replace(String.fromCharCode(10),'~!@#$');  }
   document.getElementById('thistitle').value=document.getElementById('thistitle').value.replace(/\~\!\@\#\$/g, String.fromCharCode(13));

   }
   
   } else if (justn) {

   if (twentythree == 23) {
   while (document.getElementById('thistitle').value.indexOf(String.fromCharCode(13) + String.fromCharCode(10)) != -1) { document.getElementById('thistitle').value=document.getElementById('thistitle').value.replace(String.fromCharCode(13) + String.fromCharCode(10),'~!@#$');  }
   document.getElementById('thistitle').value=document.getElementById('thistitle').value.replace(/\~\!\@\#\$/g, String.fromCharCode(10));

   } else if (twentythree == 13) {
   while (document.getElementById('thistitle').value.indexOf(String.fromCharCode(13)) != -1) { document.getElementById('thistitle').value=document.getElementById('thistitle').value.replace(String.fromCharCode(13),'~!@#$');  }
   document.getElementById('thistitle').value=document.getElementById('thistitle').value.replace(/\~\!\@\#\$/g, String.fromCharCode(10));

   }
   }
      
   return true;
 }
 
 function justrntrue() {
   justrn=true;
   justr=false;
   justn=false;
   //alert('here');
 }
 
 function justntrue() {
   justn=true;
   justr=false;
   justrn=false;
 }
 
 function justrtrue() {
   justr=true;
   justrn=false;
   justn=false;
 }
 
 function tc() {
   if (" . strlen($tval) . " != 0) {
   document.title=document.getElementById('mydiv').textContent;
   //alert(document.getElementById('mydiv').textContent);
   }
 }
  
</scr" . "ipt>
</head>
<body onload='setTimeout(tc);'>
<h1 onclick='alert(this.title);' id=myh1 title=\"" . $tval . "\">Carriage Return and/or Linefeed</h1>
<h3>RJM Programming - April, 2022 <br>Proving <a target=_blank title=\"HTML when it normalizes newlines will convert all \\r\\n sequences to \\n and then all remaining \\r to \\n, so effectively getting rid of all lonely \\r characters. However, Node.textContent doesn't call this normalize-newlines algorithm, so they're not converted to \\n and not interpreted as segment-break.\" href='https://stackoverflow.com/questions/57965834/why-carriage-return-not-rendered-as-a-line-break-when-assigned-to-textcontent-al'>javascript - Why carriage return not rendered as a line break when assigned to textContent although formatted using pre? - Stack Overflow</a></h3>
<h4>Hover or click main heading up the top to see, reflected, the textarea record delimiter arrangement the button you pressed suggests</h4>

<form id=myform onsubmit='return whichd(this);' method=POST action=./cr_andor_lf.php>
<textarea rows=4 cols=80 name=thistitle id=thistitle style=width:95%; value=\"" . $tval . "\">" . $tval . "</textarea>
<table cellpadding=5 cellspacing=5><tr><td>
<p> ... <i>textarea</i> above vs <i>pre</i> element below ...</p>
<br><table border=5 cellpadding=5 cellspacing=5><tr><th></th><th>display:inline-block;</th><th>display:block;</th></tr><tr><td><pre id=mypre style='border:1px solid red;'>" . $tval . "</pre></td><td><pre style='display:inline-block;border:1px solid red;' id=mypreib>" . $tval . "</pre></td><td><pre style='display:block;border:1px solid red;' id=mypreb>" . $tval . "</pre></td></tr></table><br><br>
</td><td>
<p> ... vs <i>span</i> element below ...</p>
<br><span id=myp style='border:1px solid purple;'>" . $tval . "</span><br><br>
</td><td>
<p> ... vs <i>div</i> element below ...</p>
<br><table border=5 cellpadding=5 cellspacing=5><tr><th></th><th>display:inline-block;</th><th>display:block;</th></tr><tr><td><div id=mydiv style='border:1px solid blue;'>" . $tval . "</div></td><td><div style='display:inline-block;border:1px solid blue;' id=mydivib>" . $tval . "</div></td><td><div style='display:block;border:1px solid blue;' id=mydivb>" . $tval . "</div></td></tr></table><br><br>
</td></tr></table>
<input onclick='justrtrue();' type=submit id=justr name=justr value='Carriage Return  ( " . "\\" . "r )'></input>&nbsp;&nbsp;<input onclick='justntrue();' type=submit id=justn name=justn value='Line Feed  ( " . "\\" . "n )'></input>&nbsp;&nbsp;<input onclick='justrntrue();' type=submit id=justrn name=justrn value='Carriage Return/Line Feed  ( " . "\\" . "r" . "\\" . "n )'></input> 
</form>
</body>
</html>";
?>
