IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage Delphi Discussion :

CopyToClipboard caractères spéciaux


Sujet :

Langage Delphi

  1. #1
    Membre habitué Avatar de donnadieujulien
    Développeur informatique
    Inscrit en
    Avril 2008
    Messages
    433
    Détails du profil
    Informations personnelles :
    Âge : 39

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2008
    Messages : 433
    Points : 191
    Points
    191
    Par défaut CopyToClipboard caractères spéciaux
    Bonjour, je souhaite copier dans le clipboard une chaine html, en l'occurrence un tableau.

    Je me heurte à un problème, c'est à dire que lorsque je colle ma chaine sur open office, des caractères apparaissent à la fin du tableau alors qu'ils ne devraient pas y être.

    J'ai fait des tests simple, au lieu de passer un tableau je passe juste quelques caractères :
    si je copie la chaine x, j'obtient y dans openoffice en collant :

    x='é' y='é'
    x='éé' y='éé'
    x='ééé' y='éé'
    x='éééé' y='ééé'
    x='ééééé' y='ééé'
    x='éééééé' y='éééé'

    Il semblerait donc que certains caractères aient besoin d'être encodés avant de passer par le clipboard.

    J'ai donc cherché et trouvé htmlescape, mais comment gérer les caractères non occidentaux? Ca n'est pas pris en compte par cette fonction.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    function HTMLEscape(const Str: string): string;
    var
      i, l : integer;
    begin
      l := Length(Str);
      Result := '';
      for i := 1 to l do
      begin
        case Str[i]  of
        '<' : Result := Result + '&lt;';    { Do not localize }
        '>' : Result := Result + '&gt;';    { Do not localize }
        '&' : Result := Result + '&amp;';   { Do not localize }
        '"' : Result := Result + '&quot;';  { Do not localize }
        #92, #160 .. #255 : Result := Result + '&#' + IntToStr(Ord(Str[ i ])) +';';  { Do not localize }
        else
          Result := Result + Str[i];
        end;
      end;
    end;
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    procedure CopyHTMLDirectToClipBoard(const htmlStr: string = '');
    var
      gMem: HGLOBAL;
      lp: PChar;
      Strings: array[0..1] of UTF8String ;
      Formats: array[0..1] of UINT;
      i: Integer;
    begin
      gMem := 0;
      {$IFNDEF USEVCLCLIPBOARD}
      Win32Check(OpenClipBoard(0));
      {$ENDIF}
      try
        //most descriptive first as per api docs
        Strings[0] := FormatHTMLClipboardHeader(htmlStr);
        Strings[1] := htmlStr;
        Formats[0] := RegisterClipboardFormat('HTML Format');
        Formats[1] := CF_TEXT;
        {$IFNDEF USEVCLCLIPBOARD}
        Win32Check(EmptyClipBoard);
        {$ENDIF}
        for i := 0 to High(Strings) do
        begin
          if Strings[i] = '' then Continue;
          //an extra "1" for the null terminator
          gMem := GlobalAlloc(GMEM_DDESHARE + GMEM_MOVEABLE, Length(Strings[i]) + 1);
          {Succeeded, now read the stream contents into the memory the pointer points at}
          try
            Win32Check(gmem <> 0);
            lp := GlobalLock(gMem);
            Win32Check(lp <> nil);
            CopyMemory(lp, PChar(Strings[i]), Length(Strings[i]) + 1);
          finally
            GlobalUnlock(gMem);
          end;
          Win32Check(gmem <> 0);
          SetClipboardData(Formats[i], gMEm);
          Win32Check(gmem <> 0);
          gmem := 0;
        end;
      finally
        {$IFNDEF USEVCLCLIPBOARD}
        Win32Check(CloseClipBoard);
        {$ENDIF}
      end;
    end;
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    procedure ClickBoutonCopy();
    var
      html:String;
    begin
    html:='<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">';
    html:=html + '<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8" />';
    html:=html + '</head><body>';
    html:='<table>' + #13#10;
    html:=html + #9#9 + '<tr>' + #13#10#9#9#9#9 +'<td>' + MonLabel1.Text + '</td>' + #13#10#9#9 + '</tr>' + #13#10;
    html:=html + #9#9 + '<tr>' + #13#10#9#9#9#9 +'<td>' + MonLabelHTML1.HTMLText.Text + '</td>' + #13#10#9#9 + '</tr>' + #13#10;
    html:=html + '</table>' + #13#10;
    html:=html + '</body></html>';
    CopyHTMLDirectToClipBoard(html);
    end;

  2. #2
    Modérateur
    Avatar de Rayek
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Mars 2005
    Messages
    5 235
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 50
    Localisation : France, Haute Savoie (Rhône Alpes)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 5 235
    Points : 8 504
    Points
    8 504
    Par défaut
    Dans l'unité HTTPApp tu as HtmlEncode et HtmlDecode qui te faciliteront la vie

  3. #3
    Membre habitué Avatar de donnadieujulien
    Développeur informatique
    Inscrit en
    Avril 2008
    Messages
    433
    Détails du profil
    Informations personnelles :
    Âge : 39

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2008
    Messages : 433
    Points : 191
    Points
    191
    Par défaut
    j'ai essayé ca :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    CopyHTMLDirectToClipBoard(htmlencode(html));
    mais sans succès, si je mets 'éééééé' dans ma variable html, j'ai toujours ééé en sortie

  4. #4
    Membre habitué Avatar de donnadieujulien
    Développeur informatique
    Inscrit en
    Avril 2008
    Messages
    433
    Détails du profil
    Informations personnelles :
    Âge : 39

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2008
    Messages : 433
    Points : 191
    Points
    191
    Par défaut
    ca marche avec htmlescape qui se trouve dans l'unité httputil, mais ca ne gère pas tous les jeux de caractères non occidentaux je pense.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    function HTMLEscape(const Str: string): string;
    var
      i, l : integer;
    begin
      l := Length(Str);
      Result := '';
      for i := 1 to l do
      begin
        case Str[i]  of
        '<' : Result := Result + '&lt;';    { Do not localize }
        '>' : Result := Result + '&gt;';    { Do not localize }
        '&' : Result := Result + '&amp;';   { Do not localize }
        '"' : Result := Result + '&quot;';  { Do not localize }
        #92, #160 .. #255 : Result := Result + '&#' + IntToStr(Ord(Str[ i ])) +';';  { Do not localize }
        else
          Result := Result + Str[i];
        end;
      end;
    end;

  5. #5
    Membre habitué Avatar de donnadieujulien
    Développeur informatique
    Inscrit en
    Avril 2008
    Messages
    433
    Détails du profil
    Informations personnelles :
    Âge : 39

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2008
    Messages : 433
    Points : 191
    Points
    191
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    function HTMLEscape(const Str: string): string;
    var
      i, l : integer;
    begin
      l := Length(Str);
      Result := '';
      for i := 1 to l do
      begin
        case Str[i]  of
        '<' : Result := Result + '&lt;';    { Do not localize }
        '>' : Result := Result + '&gt;';    { Do not localize }
        '&' : Result := Result + '&amp;';   { Do not localize }
        '"' : Result := Result + '"';  { Do not localize }
        #92, #160 .. #65535: Result := Result + '&#' + IntToStr(Ord(Str[ i ])) +';';  { Do not localize }
        else
          Result := Result + Str[i];
        end;
      end;
    end;
    Je rajouterai meme la prise en compte des autres caractères jusqu'a 65535 sinon bugs sur les apostrophes etc...

  6. #6
    Membre habitué Avatar de donnadieujulien
    Développeur informatique
    Inscrit en
    Avril 2008
    Messages
    433
    Détails du profil
    Informations personnelles :
    Âge : 39

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Avril 2008
    Messages : 433
    Points : 191
    Points
    191
    Par défaut
    Pb résolu avec html escape et changement de 255 par 65535...

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. caractères spéciaux
    Par mat10000 dans le forum Flash
    Réponses: 16
    Dernier message: 23/10/2003, 13h22
  2. Où trouver la liste des caractères spéciaux?
    Par gamez dans le forum Langage
    Réponses: 3
    Dernier message: 19/08/2003, 17h54
  3. Transformation XSL et caractères spéciaux
    Par Sylvain Leray dans le forum XMLRAD
    Réponses: 4
    Dernier message: 28/04/2003, 10h38
  4. [Sybase] filtre sur caractères spéciaux
    Par montelieri dans le forum Sybase
    Réponses: 4
    Dernier message: 07/04/2003, 16h49
  5. Traiter les caractères spéciaux
    Par ricola dans le forum Langage
    Réponses: 2
    Dernier message: 20/02/2003, 09h23

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo