Protect HTML-Tags at the converting RTF->HTML

General TRichView support forum. Please post your questions here
Post Reply
Bjoern
Posts: 21
Joined: Tue May 11, 2010 4:05 pm

Protect HTML-Tags at the converting RTF->HTML

Post by Bjoern »

My users enter text in a DBRichViewEdit which is stored in a database and then printed to a file as a part of a html-page.
For this, i convert the RTF to HTML with

Code: Select all

function RTF2Html(rtf:string): string;
var Stream: TStringStream;
    rv:TCustomRichView;
begin
 rv:=TCustomRichView.Create(nil);
 rv.Style:=MyRVStyle1;

 Stream := TStringStream.Create('');
 Stream.WriteString(rtf);
 Stream.Position := 0;
 rv.LoadFromStream(Stream,rvynaNo);
 Stream.Position := 0;
 rv.SaveHTMLToStream(Stream, '', '','',[rvsoMiddleOnly,rvsoDefault0Style]);

 result:= Stream.DataString;
 stream.free;
 rv.Free;
end;
That works fine :-) ... but sometimes the users must insert a complete html-line like

Code: Select all

<img src="http://www.abc.de/def.gif">
into the DBRichViewEdit.
(more complex than in this example, so they must insert them as source-code)

After the converting, there is of course

Code: Select all

<img src="http://www.abc.de/def.gif">
in the source-code.

Is there any possibility to protect all < and > from converting to their entitys?
Sergey Tkachenko
Site Admin
Posts: 17569
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Use a text style with rvteoHTMLCode in Options: http://www.trichview.com/help/idh_tcust ... tions.html
Bjoern
Posts: 21
Joined: Tue May 11, 2010 4:05 pm

Post by Bjoern »

Hello Sergey,

i've tried so, but i get a problem with my code, when i loop over the TextStyles to set +[rvteoHTMLCode] it clears my DBRichViewEdit after the first run.

The code is

Code: Select all

procedure TMainForm.SpeedButtonTest2Click(Sender: TObject);
var
  i: Integer;
  Stream: TStringStream;
  rv:TCustomRichView;
begin
  if MyTable.State <> dsBrowse then	MyTable.Post;
  for i := 0 to MyRVStyle.TextStyles.Count-1 do 
  	MyRVStyle.TextStyles[i].Options := MyRVStyle.TextStyles[i].Options+[rvteoHTMLCode];
  rv:=TCustomRichView.Create(nil);
  rv.Style:=MyRVStyle;
  Stream := TStringStream.Create('');
  Stream.WriteString(MyTable.FieldByName('Text').AsString);
  Stream.Position := 0;
  rv.LoadFromStream(Stream,rvynaNo);
  Stream.Position := 0;
  rv.SaveHTMLToStream(Stream, '', '','', [rvsoMiddleOnly,rvsoDefault0Style]);
  ShowMessage(Stream.DataString);
  stream.free;
  rv.Free;
end;
If i enter something into my DBRichViewEdit and click the button it works fine, but if i enter a second something and click the button a second time the showmessage shows only htmls-tags with no content between, and my DBRichViewEdit goes empty, and now every click clears the DBRichViewEdit
If i delete the "for i := 0 to MyRVStyle.TextStyles.Count-1..."-lines everthinks works fine again.
Sergey Tkachenko
Site Admin
Posts: 17569
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Do you store documents in DB in RTF format?
Text with rvteoHTMLCode style is not saved in RTF (as well as text with rvteoRTFCode is not saved in RTF).
Exclude this option from styles after saving HTML.
Bjoern
Posts: 21
Joined: Tue May 11, 2010 4:05 pm

Post by Bjoern »

yes, it is RTF ... and with excluding the option after saving HTML it works fine.
Thank you for your support!
Post Reply