Wikini

HackTabulationDansMozilla

PagePrincipale :: DerniersChangements :: DerniersCommentaires :: ParametresUtilisateur :: Vous êtes ec2-3-146-221-204.us-east-2.compute.amazonaws.com

Tentative de hack autour des tabulations dans textarea (IE/Mozilla)


Quand on est édite une page, le javascript qui gère l'insertion des tabulations sous IE fonctionne bien.

Mais pas sous Firefox/Mozilla. Il s'agit d'un bogue et voici quand je l'ai contourné :

Attention: le script comprend aussi un réajustement automatique du textarea, pour l'enlever, il suffit
de virer tous les appels a la fonction expand_tarea();



<FORM NAME="formu">
<TEXTAREA COLS="45" STYLE="width:99%;overflow:visible" name="content">contenu</TEXTAREA>
</FORM>

<script>

function addEvent(obj, evType, fn, useCapture) {
 if (obj.addEventListener) {
  obj.addEventListener(evType, fn, useCapture);
  return true;
 } else if (obj.attachEvent) {
  var r = obj.attachEvent("on" + evType, fn);
  return r;
 } else {
  alert("handler could not be attached");
 }
}

function addKeyEvent(obj,fnc_name) {
 var e = (document.addEventListener) ? 'keypress' : 'keydown';
 addEvent(obj,e,fnc_name,false);
}

function expand_tarea(tarea) {
 nb_line=escape(tarea.value).split("%0A").length+1;
 //nb_line=nb_line-1;
 if (nb_line<15) nb_line=15;
 tarea.rows=nb_line;
}

function manage_keys(e) {
 tarea = e.target || e.srcElement,

 // Get key code
 keyCode = e.keyCode || e.which;

 // Tabulation
 if (keyCode==9) {
  if (eval(document.selection)) {
   // For Internet Explorer
   document.selection.createRange().text = String.fromCharCode(9);
  } else {
   // For Mozilla/FireFox
   tbegin = tarea.value.substring( 0 , tarea.selectionStart );
   tend = tarea.value.substring( tarea.selectionEnd , tarea.textLength );
   tarea.value=tbegin.substring(0,tbegin.length)+'\t'+tend;
   tarea.setSelectionRange(tbegin.length+1,tbegin.length+1);
   e.preventDefault();
   e.stopPropagation();
 }
  e.returnValue=false;
 }
 // Adjust textarea
 expand_tarea(tarea);
}


expand_tarea(document.formu.content,0);
addKeyEvent(document.formu.content,manage_keys);
</script>




Mémo bug :
http://tabinta.mozdev.org/
https://bugzilla.mozilla.org/show_bug.cgi?id=29086

Mémo inspiration code
http://www.highdots.com/forums/archive/index.php/t-66235.html
http://www.editeurjavascript.com/scripts/scripts_formulaires_3_318.php

Y-aurait-il des âmes charitables pour tester cette bidouille sur différentes plateformes ?
- IE 6 OK
- Firefox 1.0.1 FR OK

Merci d'avance !

FranckExeprod


Il n'y a pas de commentaire sur cette page. [Afficher commentaires/formulaire]