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

$pretd='';
$tval='';
$styleextra='';
$linesc=[];
$lineso=[];
$clinesc='';
$clineso='';


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) {
    if (isset($_POST['justrc'])) {
    $linesc=explode("\r", $tval);
    $clinesc='"' . str_replace("\r",'","',str_replace('"','`',$tval)) . '"';
    $styleextra=' #justrc { background-color: yellow; }  ';
    } else if (isset($_POST['justro'])) {
    $pretd='junk';
    $lineso=explode("\r", $tval);
    $clineso='"' . str_replace("\r",'","',str_replace('"','`',$tval)) . '"';
    $styleextra=' #justro { background-color: yellow; }  ';
    } else {
    $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) {
    if (isset($_GET['justrc'])) {
    $linesc=explode("\r", $tval);
    $clinesc='"' . str_replace("\r",'","',str_replace('"','`',$tval)) . '"';
    $styleextra=' #justrc { background-color: yellow; }  ';
    } else if (isset($_GET['justro'])) {
    $pretd='junk';
    $lineso=explode("\r", $tval);
    $clineso='"' . str_replace("\r",'","',str_replace('"','`',$tval)) . '"';
    $styleextra=' #justro { background-color: yellow; }  ';
    } else {
    $styleextra=' #justr { background-color: yellow; }  ';
    }
  }
}


$prebit="<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>";
if ($pretd != '') {
  $prebit="<br><pre id=mypre style='border:1px solid red;'>" . $tval . "</pre><br><br>";
}
$divbit=str_replace('pre', 'div', $prebit);

echo "<html>
<head>
<style>
  " . $pretd . "td {
    vertical-align:top;
  }
" . $styleextra . "
</style>
<scr" . "ipt type=text/javascript>
 var justr=false, justn=false, justrn=false, justro=false, justrc=false;
 var twentythree=-1, tts='';
 var linesc=[" . $clinesc . "];
 var lineso=[" . $clineso . "];
 
 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 anymore='';
   var dv=document.getElementById('thistitle').value;
   var wasv=dv;
   var dvs=document.getElementById('thistitle').value.split(String.fromCharCode(13) + String.fromCharCode(10));
   if (justrc) { anymore='&justrc='; }
   if (justro) { anymore='&justro='; }
   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) + anymore; return false; }
   if (justr && twentythree == 13) { location.href=document.URL.split('#')[0].split('?')[0] + '?thistitle=' + encodeURIComponent(wasv) + anymore; return false; }
   if (justn && twentythree == 10) { location.href=document.URL.split('#')[0].split('?')[0] + '?thistitle=' + encodeURIComponent(wasv) + anymore; 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 || justro || justrc) {

   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) + anymore;
   return false;
    
   }
   
   
   if (justrn && twentythree == 23) {    return true; }
   if ((justr || justro || justrc) && 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 || justro || justrc) {

   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');
   justro=false;
   justrc=false;
 }
 
 function justntrue() {
   justn=true;
   justr=false;
   justrn=false;
   justro=false;
   justrc=false;
 }
 
 function justrtrue() {
   justr=true;
   justrn=false;
   justn=false;
   justro=false;
   justrc=false;
 }
 
 function justrotrue() {
   justr=false;
   justrn=false;
   justn=false;
   justro=true;
   justrc=false;
 }
 
 function justrctrue() {
   justr=false;
   justrn=false;
   justn=false;
   justro=false;
   justrc=true;
 }
 
 function tc() {
   var mm=0;
   var kk=0;
   var jj=0;
   var rect=null;
   var clone='';
   var tables=[];
   var threes=['div','pre'];
   if (" . strlen($tval) . " != 0) {
   document.title=document.getElementById('mydiv').textContent;
   //alert(document.getElementById('mydiv').textContent);
   if (eval('' + linesc.length) > 0) {
   document.getElementsByTagName('textarea')[0].value='' + linesc[eval(-1 + eval('' + linesc.length))];

   document.getElementsByTagName('pre')[0].innerHTML='' + linesc[eval(-1 + eval('' + linesc.length))];
   document.getElementsByTagName('pre')[1].innerHTML='' + linesc[eval(-1 + eval('' + linesc.length))];
   document.getElementsByTagName('pre')[2].innerHTML='' + linesc[eval(-1 + eval('' + linesc.length))];

   document.getElementsByTagName('span')[0].innerHTML='' + linesc[eval(-1 + eval('' + linesc.length))];

   document.getElementsByTagName('div')[0].innerHTML='' + linesc[eval(-1 + eval('' + linesc.length))];
   document.getElementsByTagName('div')[1].innerHTML='' + linesc[eval(-1 + eval('' + linesc.length))];
   document.getElementsByTagName('div')[2].innerHTML='' + linesc[eval(-1 + eval('' + linesc.length))];

   } else if (eval('' + lineso.length) > 0) {
   tables=document.getElementsByTagName('table');
   for (jj=eval(-1 + eval('' + tables.length)); jj>=0; jj--) {
     if (tables[jj].outerHTML.split('>')[0].indexOf(' border=') == -1 && tables[jj].outerHTML.split('>')[0].indexOf('table') == -1) {
     rect=tables[jj].getBoundingClientRect();
     tables[jj].style.position='absolute';
     tables[jj].style.top='' + rect.top + 'px';
     tables[jj].style.left='' + rect.left + 'px';
     tables[jj].style.width='' + rect.width + 'px';
     tables[jj].style.height='' + rect.height + 'px';
     }
   }

   document.getElementsByTagName('textarea')[0].value='' + lineso[0];
   if (1 == 1) {
   document.body.style.backgroundColor='transparent';
   document.getElementsByTagName('textarea')[0].style.backgroundColor='transparent';
   } else {
   document.getElementsByTagName('textarea')[0].style.opacity='0.5';
   }
   rect=document.getElementsByTagName('textarea')[0].getBoundingClientRect();
   document.getElementsByTagName('textarea')[0].style.position='absolute';
   document.getElementsByTagName('textarea')[0].style.top=('' + rect.top).split('.')[0] + 'px';
   document.getElementsByTagName('textarea')[0].style.left=('' + rect.left).split('.')[0] + 'px';
   document.getElementsByTagName('textarea')[0].style.width=('' + rect.width).split('.')[0] + 'px';
   document.getElementsByTagName('textarea')[0].style.height=('' + rect.height).split('.')[0] + 'px';
   clone=document.getElementsByTagName('textarea')[0].outerHTML.replace(' id=', ' data-id=').replace(' name=', ' data-name=').replace(' value=', ' value=\"\" data-value=').split('>')[0];
   for (jj=1; jj<lineso.length; jj++) {
     //alert(clone + '>' + lineso[jj] + '</textarea>');
     document.getElementById('overlays').innerHTML+=clone + '>' + lineso[jj] + '</textarea>';
   }
   
   document.getElementsByTagName('span')[0].innerHTML='' + lineso[0];
   if (1 == 1) {
   document.body.style.backgroundColor='transparent';
   document.getElementsByTagName('span')[0].style.backgroundColor='transparent';
   document.getElementsByTagName('span')[0].style.border='0px solid red';
   } else {
   document.getElementsByTagName('span')[0].style.opacity='0.5';
   }
   rect=document.getElementsByTagName('span')[0].getBoundingClientRect();
   document.getElementsByTagName('span')[0].style.position='absolute';
   document.getElementsByTagName('span')[0].style.top=('' + rect.top).split('.')[0] + 'px';
   document.getElementsByTagName('span')[0].style.left=('' + rect.left).split('.')[0] + 'px';
   document.getElementsByTagName('span')[0].style.width=('' + rect.width).split('.')[0] + 'px';
   document.getElementsByTagName('span')[0].style.height=('' + rect.height).split('.')[0] + 'px';
   clone=document.getElementsByTagName('span')[0].outerHTML.replace(' id=', ' data-id=').replace(' name=', ' data-name=').replace(' value=', ' value=\"\" data-value=').split('>')[0];
   for (jj=1; jj<lineso.length; jj++) {
     //alert(clone + '>' + lineso[jj] + '</span>');
     document.getElementById('overlays').innerHTML+=clone + '>' + lineso[jj] + '</span>';
   }

   var documentgetElementsByTagNamepre=[];
   
   for (mm=0; mm<threes.length; mm++) {
   documentgetElementsByTagNamepre=[];
   for (kk=0; kk<=0; kk++) {
    documentgetElementsByTagNamepre.push(document.getElementsByTagName(threes[mm])[kk]);
   }

   for (kk=0; kk<=0; kk++) {
   documentgetElementsByTagNamepre[kk].innerHTML='' + lineso[0];
   if (1 == 1) {
   document.body.style.backgroundColor='transparent';
   documentgetElementsByTagNamepre[kk].style.backgroundColor='transparent';
   documentgetElementsByTagNamepre[kk].style.border='0px solid red';
   } else {
   documentgetElementsByTagNamepre[kk].style.opacity='0.5';
   }
   rect=documentgetElementsByTagNamepre[kk].getBoundingClientRect();
   documentgetElementsByTagNamepre[kk].style.position='absolute';
   documentgetElementsByTagNamepre[kk].style.top=('' + rect.top).split('.')[0] + 'px';
   documentgetElementsByTagNamepre[kk].style.left=('' + rect.left).split('.')[0] + 'px';
   documentgetElementsByTagNamepre[kk].style.width=('' + rect.width).split('.')[0] + 'px';
   documentgetElementsByTagNamepre[kk].style.height=('' + rect.height).split('.')[0] + 'px';
   clone=documentgetElementsByTagNamepre[kk].outerHTML.replace(' id=', ' data-id=').replace(' name=', ' data-name=').replace(' value=', ' value=\"\" data-value=').split('>')[0];
   for (jj=1; jj<lineso.length; jj++) {
     //alert(clone + '>' + lineso[jj] + '</span>');
     if (1 == 2 && document.getElementById('td' + documentgetElementsByTagNamepre[kk].id)) {
     document.getElementById('td' + documentgetElementsByTagNamepre[kk].id).innerHTML+=clone + '>' + lineso[jj] + '</' + threes[mm] + '>';
     } else {
     document.getElementById('overlays').innerHTML+=clone + '>' + lineso[jj] + '</' + threes[mm] + '>';
     }
   }
   }
   }

   //document.getElementsByTagName('div')[0].innerHTML='' + lineso[0];
   //document.getElementsByTagName('div')[1].innerHTML='' + lineso[0];
   //document.getElementsByTagName('div')[2].innerHTML='' + lineso[0];
   //document.getElementsByTagName('div')[0].style.opacity='0.5';
   //document.getElementsByTagName('div')[1].style.opacity='0.5';
   //document.getElementsByTagName('div')[2].style.opacity='0.5';



   //alert('o=' + lineso.length);
   }
   }
 }
  
</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><br><br><br><br>
<table cellpadding=5 cellspacing=5><tr><td>
<p> ... <i>textarea</i> above vs <i>pre</i> element below ...</p>" . $prebit . "
</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>" . $divbit . "
</td></tr></table><br><br><br><br>
<table><tr><td><input onclick='justntrue();' type=submit id=justn name=justn value='Line Feed  ( " . "\\" . "n )'></input>&nbsp;&nbsp;</td><td><input onclick='justrntrue();' type=submit id=justrn name=justrn value='Carriage Return/Line Feed  ( " . "\\" . "r" . "\\" . "n )'></input>&nbsp;&nbsp;</td><td><input onclick='justrtrue();' type=submit id=justr name=justr value='Carriage Return  ( " . "\\" . "r )'></input><br><input onclick='justrotrue();' type=submit id=justro name=justro value='Our Overlay Carriage Return  ( " . "\\" . "r )'></input><br><input onclick='justrctrue();' type=submit id=justrc name=justrc value='Our Clobbering Carriage Return  ( " . "\\" . "r )'></input></td></tr></table>
</form>
<div id=overlays></div>
</body>
</html>";
?>
