Page 1 of 2
Paste HTML using pBear HtmlImport
Posted: Wed Sep 02, 2009 12:08 am
by Marsianin
Is there a demo? Tried a different methods but doesn't works.
Also I must know is there a HTML in clipboard - did it by
Code: Select all
CF_HTML:=RegisterClipboardFormat('HTML Format');
It seems bBear component is not so flexible to get clipboard content and then export it. Or I'm doing something wrong...
Posted: Tue Sep 08, 2009 5:43 pm
by Sergey Tkachenko
THTMLViewer does not have methods for loading from the Clipboard.
I'll try to explain why it's difficult to implement pasting, if it was not implemented in the component itself.
HTML Clipboard format is a plain text.
It looks like this:
Code: Select all
Version:1.0
StartHTML:000000188
EndHTML:000001603
StartFragment:000001434
EndFragment:000001451
StartSelection:000001434
EndSelection:000001447
SourceURL:http://www.trichview.com/
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>Delphi RichEdit Alternative: RichView Components for Delphi and C++Builder</TITLE>
<SCRIPT language=JavaScript type=text/javascript src="scripts.js"></SCRIPT>
<LINK rel=stylesheet type=text/css href="trv.css"><LINK title=RSS rel=alternate
type=application/rss+xml
href="http://www.trichview.com/forums/rss.php?f=6"><LINK title=Atom
rel=alternate type=application/atom+xml
href="http://www.trichview.com/forums/rss.php?atom&f=6">
<STYLE type=text/css>
<!--
.dllist { list-style-image: url('images/icons/download2.gif'); }
.helplist { list-style-image: url('images/icons/help2.gif'); }
-->
</STYLE>
</HEAD>
<BODY onload="MM_preloadAllImages('')" leftMargin=0 topMargin=0 marginheight="0"
marginwidth="0">
<TABLE border=0 cellSpacing=0 cellPadding=0 width="100%" height="100%">
<TBODY>
<TR>
<TD vAlign=top rowSpan=2 width="100%" align=left>
<TABLE border=0 cellSpacing=5 cellPadding=5 width="100%">
<TBODY>
<TR>
<TD class=main>
<DIV id=trichviewnews class=hslice>
<TABLE border=0 cellSpacing=1 cellPadding=5 width="100%" bgColor=#ffbe0b>
<TBODY>
<TR>
<TD class=news1>
<P><!--StartFragment-->[color=red]<B>September </B>[/color]<!--EndFragment--></P>
</TD>
</TR>
</TBODY>
</TABLE>
</DIV>
</TD>
</TR>
</TBODY>
</TABLE>
</TD>
</TR>
</TBODY>
</TABLE>
</BODY>
</HTML>
(I copied "September " from the main page).
HTML is always represented in UTF-8.
StartHTML..EndHTML define a range of bytes for HTML content.
StartFragment..EndFragment (or StartSelection..EndSelection, I forgot the difference) defines the selected part.
If you extract and load to HTMLViewer a part between StartHTML..EndHTML, this document will contain too many nested tables.
If you extract only selection (marked with red), you lost almost all HTML formatting.
Probably the best compromise is extracting <head> from this HTML, concatenating with the selected part, and loading in THTMLViewer. At least you will have a CSS. But you will lose all context where the selected fragment is inserted.
Posted: Tue Sep 15, 2009 12:38 am
by Marsianin
If you extract and load to HTMLViewer a part between StartHTML..EndHTML, this document will contain too many nested tables.
This is a not big problem...
Is there a sample on how to do it?
Posted: Tue Sep 15, 2009 10:15 am
by Sergey Tkachenko
I think these tables makes HTML pasting almost useless. Like in my example, the user copied only one word from the page, but 3 nested tables will be inserted.
Wait, it's an idea. We can remove all 1x1 tables after pasting, I'll make an example.
Posted: Thu Sep 17, 2009 3:45 pm
by Sergey Tkachenko
I just uploaded a new version of THTMLViewImporter with new LoadFromClipboard method.
Consider it is as a beta.
It uses some tricks
1) It loads the whole HTML from the Clipboard, then removes unnecessary tables (all 1x1 root tables).
2) It uses THTMLViewer.LoadFromString method. This method has a Reference parameter containing a path to HTML file. Unfortunately, THTMLViewer does not expect URL in this parameter. THTMLViewImporter tricks it, passing 'Z:\' in this parameter, when replacing 'Z:\' to the actual URL in paths to image. At least, it works for <img>.
Posted: Wed Sep 23, 2009 12:54 am
by Marsianin
Yeah it works but CLEARS whole document WITHOUT undo.
Here is my code:
Code: Select all
if RVHTMLViewImporter1.LoadFromClipboard(RichViewEdit1) then begin
RVHTMLViewImporter1.AppendHtmlViewer(HTMLViewer1,RichViewEdit1);
NormalizeRichView(RichViewEdit1.RVData);
RichViewEdit1.Format;
RichViewEdit1.Modified:=True;
end;
So how can I paste from cursor position?
Posted: Wed Sep 23, 2009 4:45 pm
by Sergey Tkachenko
Do it how RichViewAction do for TrvHtmlImporter: load in a hidden temporal RichView, save to RVF stream (SaveRVFToStream), insert in the caret position of the main editor (InsertRVFFromStreamEd).
Posted: Sun Dec 20, 2009 10:18 pm
by Marsianin
There is a problem with images when trying to paste from clipboard.
I did as you advised and everything works fine except images.
I've downloaded image in HTMLViewer1ImageRequest and that image appeared in HTMLViewer1 but this method RVHTMLViewImporter1.LoadFromClipboard(rve) gives me error:
---------------------------
Debugger Exception Notification
---------------------------
Project Project.exe raised exception class EFOpenError with message 'Cannot open file "C:\Users\User\Documents\Delphi\Project\http:\www.google.com\intl\en_ALL\images\logo.gif". The filename, directory name, or volume label syntax is incorrect'.
---------------------------
Break Continue Help
---------------------------
Posted: Mon Dec 21, 2009 12:51 pm
by Sergey Tkachenko
Does this url really have http:/ instead of http:// ?
If not, please send me a sample html file, I'll check it when I return
Posted: Mon Dec 21, 2009 11:42 pm
by Marsianin
I have no idea why it appears as
http:\ in the error description but my code loads picture into THTMLViewer and I can see it.
But transferring this HTML page from HTMLViewer to RVE causes this error (the problem is in the RvHtmlViewImporter).
And the source is
www.google.com copied from Firefox.
Posted: Sun Dec 27, 2009 2:28 pm
by Sergey Tkachenko
Sorry, I cannot reproduce this problem.
When I copy from English version of Google from FireFox, the project tries loading the image from a correct location, i.e.
http://www.google.com/intl/en_ALL/images/logo.gif
I use this code:
Code: Select all
RVHTMLViewImporter1.LoadFromClipboard(RichViewEdit1, HTMLViewer1);
NormalizeRichView(RichViewEdit1.RVData);
RichViewEdit1.Format;
Posted: Mon Dec 28, 2009 9:07 pm
by Marsianin
This code generates such error:
Code: Select all
RVHTMLViewImporter1.LoadFromClipboard(rve);
Calling this function fires
Code: Select all
HTMLViewer1ImageRequest(Sender: TObject; const SRC: string; var Stream: TMemoryStream);
My code handles it and loads image from the SRC and passes it into Stream.
That loaded image APPEARS in HTMLViewer1 after that above error raised.
And pushing continue copies webpage from HTMLViewer1 to RVE without Image.
So I'm getting HTMLViewer1 with loaded image and RVE without.
Posted: Tue Dec 29, 2009 12:44 pm
by Sergey Tkachenko
Probably, the code in htmlviewer.OnImageRequest causes this problem, because without it all looks ok.
There is one more reason for not using this event (and using RichView.OnImportPicture instead): relative paths.
Unfortunately, I was not able to find a way for specifying a URL (like '
http://www.google.com/') as a base path. Because of this, when loading from the clipboard, I specify a fake path ('Z:\') and replace it to URL when converting. If you process events of THTMLViewer, you will get paths with Z:\ when pasting. If you process RichView.OnImportPicture, you must get correct paths.
Posted: Wed Dec 30, 2009 7:22 am
by Marsianin
But how HTMLViewer.OnImageRequest can cause this problem if it loads image into HTMLViewer itself?
Posted: Wed Dec 30, 2009 7:07 pm
by Sergey Tkachenko
I do not know. Please create a test project and send it to me, because I cannot reproduce this problem myself.