这是从M$主页上摘下来的
不知道html标记是否会被屏蔽掉...)
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>HTML Clipboard Format</TITLE>
<META NAME="description" CONTENT="HTML Clipboard Format" />
<META NAME="keywords" CONTENT="Internet Tools & Technologies" />
<META NAME="robots" CONTENT="all" />
<META NAME="MS.LOCALE" CONTENT="EN-US" />
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1" />
<META NAME="Author" CONTENT="InetSDK" />
<SCRIPT LANGUAGE="JavaScript"><!--
// -----------------------------------------------------------
// Client-side BrowserData constructor
// Populated using data from server-side oBD object to avoid redundancy
// -----------------------------------------------------------
function BrowserData()
{
this.userAgent = "Mozilla/4.0 (compatible; MSIE 4.01; Windows 98)";
this.browser = "MSIE";
this.majorVer = 4;
this.minorVer = "01";
this.betaVer = "0";
this.platform = "98";
this.getsNavBar = true;
this.doesActiveX = true;
}
var oBD = new BrowserData();
//--></SCRIPT>
<SCRIPT LANGUAGE="JavaScript"><!--
// if workshop and not vroot, assign "/workshop/" to sBucketRoot
// rename sVRoot
// var sBucketRoot = "/workshop/networking/";
//--></SCRIPT>
<LINK REL="stylesheet" TYPE="text/css" HREF="/msdn-online/shared/css/ie4.css" />
<SCRIPT LANGUAGE="JScript"><!--
function CommonLoad()
{
if ("function" == typeof(InitNavLinks)) InitNavLinks();
if ("function" == typeof(CheckToTop)) CheckToTop();
if ("function" == typeof(RemoveLastPipe)) RemoveLastPipe();
}
//--></SCRIPT>
<META NAME="MS-HAID" CONTENT="_inet_HTML_Clipboard_Format">
</HEAD>
<BODY TOPMARGIN="0" LEFTMARGIN="0" MARGINHEIGHT="0" MARGINWIDTH="0" BGCOLOR="#FFFFFF">
<A NAME="bk_clipboard"></A>
<!-- HEADER_START -->
<DIV CLASS="clsNavLinksDiv">
<TABLE CELLPADDING="1" CELLSPACING="0" BORDER="0">
<TR><TD COLSPAN="3">&nbsp;</TD></TR>
<TR>
<TD ROWSPAN="2"><A HREF="/workshop/networking/default.asp"><IMG HEIGHT="32" WIDTH="31" VSPACE="5" BORDER="0" ALT="Click to return to the Networking, Protocols &amp; Data Formats home page" SRC="/workshop/graphics/icons/networking.gif" /></A></TD><TD ROWSPAN="2" WIDTH="18">&nbsp;&nbsp;</TD>
<TD VALIGN="top">
<TABLE ID="tblNavLinks" CELLPADDING="0" CELLSPACING="0" BORDER="0"><TR>
<TD><A ID="lnkShowImg" HREF="/workshop/networking/toc.htm" TARGET="_top"><IMG ID="imgShow" CLASS="clsLeftMenu" HEIGHT="15" WIDTH="18" BORDER="0" SRC="/msdn-online/shared/graphics/icons/nl-show-0.gif" ALT="Show/Hide TOC in left frame" /></A>&nbsp;<BR></TD>
<TD NOWRAP><A ID="lnkShowText" CLASS="clsLeftMenu" HREF="/workshop/networking/toc.htm" TARGET="_top" TITLE="Show/Hide TOC in left frame">show toc</A> &nbsp; &nbsp; &nbsp; </TD>
<TD><A ID="lnkSyncImg" HREF="/workshop/c-frame.htm?934204577186#/workshop/networking/clipboard/htmlclipboard.asp" TARGET="_top"><IMG ID="imgSync" CLASS="clsLeftMenu" HEIGHT="15" WIDTH="18" BORDER="0" SRC="/msdn-online/shared/graphics/icons/nl-sync-0.gif" ALT="Synchronize TOC and Address Bar with this page" /></A>&nbsp;<BR></TD>
<TD NOWRAP><A ID="lnkSyncText" CLASS="clsLeftMenu" HREF="/workshop/c-frame.htm?934204577186#/workshop/networking/clipboard/htmlclipboard.asp" TARGET="_top" TITLE="Synchronize TOC and Address Bar with this page">sync toc</A> &nbsp; &nbsp; &nbsp; </TD>
<TD><A ID="lnkIndexImg" HREF="/workshop/index/default.asp"><IMG ID="imgIndex" CLASS="clsLeftMenu" HEIGHT="15" WIDTH="18" BORDER="0" SRC="/msdn-online/shared/graphics/icons/nl-index-0.gif" ALT="Go to an alphabetical keyword list" /></A>&nbsp;<BR></TD>
<TD NOWRAP><A ID="lnkIndexText" CLASS="clsLeftMenu" HREF="/workshop/index/default.asp" TITLE="Go to an alphabetical keyword list">index</A> &nbsp; &nbsp; &nbsp; </TD>
</TR></TABLE>
</TD></TR>
<TR><TD>
<TABLE ID="tblPrevNext" CELLPADDING="0" CELLSPACING="0" BORDER="0"><TR> <TD WIDTH="17"><A HREF="/workshop/networking/default.asp"><IMG ID="imgUp" CLASS="clsLeftMenu" HEIGHT="13" WIDTH="17" BORDER="0" TITLE="Networking, Protocols & Data Formats" SRC="/msdn-online/shared/graphics/icons/nl-up-0.gif" /></A></TD>
<TD NOWRAP><A ID="lnkUpText" CLASS="clsLeftMenu" HREF="/workshop/networking/default.asp" TITLE="Networking, Protocols & Data Formats">Networking, Protocols & D...</A> &nbsp; &nbsp; </TD>
</TR></TABLE>
</TD></TR>
</TABLE>
</DIV>
<DIV CLASS="clsBucketBranch"><A HREF="/workshop/default.asp" TARGET="_top">Web Workshop</A>&nbsp;&nbsp;|&nbsp;&nbsp;<A HREF="/workshop/c-frame.htm?934204577171#/workshop/networking/default.asp" TARGET="_top">Networking, Protocols &amp; Data Formats</A></DIV>
<!-- HEADER_END -->
<DIV CLASS="clsDocBody">
<!-- CONTENTS_START -->
<H1><A NAME="ch_htmlclipboard">HTML Clipboard Format</A></H1>
<HR SIZE="1">
<P>This article discusses cutting and pasting fragments of an HTML document. The CF_HTML clipboard format allows a fragment of raw HTML text and its context to be stored on the clipboard as ASCII. This allows the context of the HTML fragment, which consists of all preceding and surrounding tags, to be examined by an application so that the surrounding tags can be noted with their attributes. Although it is up to an application to interpret such fragments, some basic guidelines are included here based on MSHTML implementations.</P>
<P>
This article covers the following topics:</P>
<ul><li><A CLASS=tctop HREF="/workshop/networking/clipboard/htmlclipboard.asp#Overview of CF_HTML">Overview of CF_HTML</A>
<li><P><A CLASS=tctop HREF="/workshop/networking/clipboard/htmlclipboard.asp#Scenarios">Scenarios</A></ul>
<H2><A NAME="Overview of CF_HTML">Overview of CF_HTML</A></H2>
<P>CF_HTML is entirely text format and uses the transformation format UTF-8. It includes a description, a context, and within the context, the fragment. The format name of a clipboard containing CF_HTML data
is HTML Format. Therefore, if you use functions such as <A HREF="/workshop/external_Win32.asp#_win32_RegisterClipboardFormat">RegisterClipboardFormat</A>, pass "HTML Format" in the format name parameter. This section gives a code example of a clipboard using CF_HTML and discusses the three components of the CF_HTML clipboard format.</P>
<P>The following is an example of a clipboard:</P>
<PRE CLASS="clsCode">
Version:0.9
StartHTML:71
EndHTML:170
StartFragment:140
EndFragment:160
<B>StartSelection:140</B>
<B>EndSelection:160</B>
&lt;!DOCTYPE&gt;
&lt;HTML&gt;
&lt;HEAD&gt;
&lt;TITLE&gt;The HTML Clipboard&lt;/TITLE&gt;
&lt;BASE HREF="http://sample/specs"&gt;
&lt;/HEAD&gt;
&lt;BODY&gt;
&lt;!--StartFragment --&gt;
<B>&lt;P&gt;The Fragment&lt;/P&gt;</B>
&lt;!--EndFragment --&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;
</PRE>
<H3>Description</H3>
<P>The description includes the clipboard version number and offsets, indicating where the context and the fragment start and end. The description is a list of ASCII text keywords followed by a string and separated by a colon
).</P>
<TABLE CLASS="clsStd">
<tr><td>Version <i>vv</i></td><td>Version number of the clipboard. Starting version is 0.9. </td></tr>
<tr><td>StartHTML</td><td>Byte count from the beginning of the clipboard to the start of the context, or -1 if no context.</td></tr>
<tr><td>EndHTML</td><td>Byte count from the beginning of the clipboard to the end of the context, or -1 if no context.</td></tr>
<tr><td>StartFragment</td><td>Byte count from the beginning of the clipboard to the start of the fragment.</td></tr>
<tr><td>EndFragment</td><td>Byte count from the beginning of the clipboard to the end of the fragment.</td></tr>
<tr><td>StartSelection</td><td>Byte count from the beginning of the clipboard to the start of the selection. </td></tr>
<tr><td>EndSelection</td><td>Byte count from the beginning of the clipboard to the end of the selection.</td></tr> </table>
<P>The StartSelection and EndSelection keywords are optional
because sufficient information for basic pasting is included in the fragment description. However, the selection information indicates the exact HTML area the user has selected. This adds more information to the fragment description. </P>
<P>Other information may be added in the description section. For example, multiple pairs of StartFragment/EndFragment could be added to support noncontiguous selection of fragments.</P>
<P>The only character set supported by the clipboard is Unicode in its UTF-8 encoding. Because the first characters of UTF-8 and ASCII match, the description is always ASCII, but the bytes of the context (starting at StartHTML) may use any other characters coded in UTF-8. Ends of lines may be represented in a clipboard format header as Carriage Return (CR), Carriage Return/Line Feed (CR/LF), or Line Feed (LF).</P>
<H3>Context</H3>
<P>When working with a complete document, the context is the entire HTML document. Because this discussion is limited to copying and pasting a fragment of a document, the context is the selected fragment and all preceding and surrounding start and end tags. These tags represent all the parent nodes of the fragment, up to the HTML node. The context also contains the complete <A HREF="/workshop/author/dhtml/reference/objects/HEAD.asp">HEAD</a> element and allows the <A HREF="/workshop/author/dhtml/reference/objects/BASE.asp">BASE</a> and <A HREF="/workshop/author/dhtml/reference/objects/TITLE.asp">TITLE</a> elements to be included. Sufficient information is included in the fragment for a basic pasting operation. However, if your application requires information concerning the tags surrounding the fragment, you must store the context on the clipboard.
</P>
<P>An application copying a fragment of HTML to the clipboard may choose to create a <b>BASE</b> element to include in the context so that partial URLs in the fragment can be resolved.</P>
<H3>Fragment</H3>
<P>The fragment contains valid HTML representing the area the user has selected. This includes the information required for basic pasting of an HTML fragment, as follows:</P>
<UL>
<LI>Selected text.
<LI>Opening tags and attributes of any element that has an end tag within the selected text.
<LI>End tags that match the included opening tags.
</UL>
<P>The fragment should be preceded and followed by the HTML comments &lt;!--StartFragment--&gt; and &lt;!--EndFragment--&gt; (no space allowed between the !-- and the text) to indicate where the fragment starts and ends. So the start and end of the fragment are indicated by these comments as well as by the StartFragment and EndFragment byte counts. Though redundant, this makes it easier to find the start of the fragment (from the byte count) and mark the position of the fragment directly in the HTML tree.</P>
<H2><A NAME="Scenarios">Scenarios</A></H2>
<P>The following scenarios describe how the MSHTML HTML editor handles HTML cutting and pasting; other applications may or may not follow these scenarios. The clipboard format described here is intended to allow flexibility for the way an application functions. These scenarios show only well-formed HTML.</P>
<H3>Simple Fragment of HTML</H3>
<P>HTML text:</P>
<PRE CLASS="clsCode">
&lt;BODY&gt; This is normal. &lt;B&gt;This is bold.&lt;/B&gt; &lt;I&gt;&lt;B&gt;This is bold italic.&lt;/B&gt;&lt;/I&gt;
&lt;I&gt;This is italic.&lt;/I&gt; &lt;/BODY&gt;
</PRE>
<P>This appears as:</P>
<DIV Style='text-indent:"1cm"'>
<P>This is normal. <B>This is bold.</B> <i><B>This is bold italic.</B></i>
<I>This is italic.</I></P>
</DIV>
<P>The text between the ** is selected and copied to the
clipboard: </P>
<DIV Style='text-indent:"1cm"'>
<P>This is normal. <B>This is </B>**<B>bold.
</B> <I><B>This is bold italic.</B></I><I>
This</I>**<I> is italic.</I> </P>
</DIV>
<P>In this scenario only the <A HREF="/workshop/author/dhtml/reference/objects/BODY.asp">BODY</A> element and the <A HREF="/workshop/author/dhtml/reference/objects/HTML.asp">HTML</A> element appear in the context as it precedes the selected fragment. Note that start tags and end tags are included in the context. The selection, delimited by StartSelection and EndSelection, is shown in bold. This is what will be on the clipboard (note this is MSHTML's
interpretation).</P>
<PRE CLASS="clsCode">
Version:0.9
StartHTML:71
EndHTML:160
StartFragment:130
EndFragment:150
<B>StartSelection:130</B>
<B>EndSelection:150</B>
&lt;!DOCTYPE ...&gt;
&lt;HTML&gt;
&lt;BODY&gt;
&lt;!--StartFragment--&gt;
<B>&lt;B&gt;bold.&lt;/B&gt; &lt;I&gt;&lt;B&gt;This is bold italic.&lt;/B&gt;&lt;/I&gt; &lt;I&gt;This&lt;/I&gt; </B>
&lt;!--EndFragment--&gt;
&lt;/BODY&gt;
&lt;/HTML&gt;
</PRE>
<H3>Fragment of a Table in HTML</H3>
<P>HTML text:</P>
<PRE CLASS="clsCode">
&lt;BODY&gt;&lt;TABLE BORDER&gt;&lt;TR&gt;&lt;TH
ROWSPAN=2&gt;Head1&lt;/TH&gt;&lt;TD&gt;Item 1&lt;/TD&gt;
&lt;TD&gt;Item 2&lt;/TD&gt; &lt;TD&gt;Item 3&lt;/TD&gt;
&lt;TD&gt;Item 4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Item
5&lt;/TD&gt; &lt;TD&gt;Item 6&lt;/TD&gt; &lt;TD&gt;Item
7&lt;/TD&gt; &lt;TD&gt;Item
8&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TH&gt;Head2&lt;/TH&gt;&lt;TD&gt;Item
9&lt;/TD&gt; &lt;TD&gt;Item 10&lt;/TD&gt; &lt;TD&gt;Item
11&lt;/TD&gt; &lt;TD&gt;Item
12&lt;/TD&gt;&lt;/TR&gt;&lt;/TABLE&gt;&lt;/BODY&gt;
</PRE>
<P>This appears as:</P>
<DIV Style='text-indent:"1cm"'>
<TABLE CLASS="clsStd">
<TR><TH ROWSPAN=2>Head1</TH><TD>Item 1</TD> <TD>Item 2</TD> <TD>Item 3</TD> <TD>Item 4</TD></TR>
<TR><TD>Item 5</TD> <TD>Item 6</TD> <TD>Item 7</TD> <TD>Item 8</TD></TR>
<TR><TH>Head2</TH><TD>Item 9</TD> <TD>Item 10</TD> <TD>Item 11</TD> <TD>Item 12</TD></TR>
</TABLE>
</DIV>
<P>The Item 6, Item 7, Item 10, and Item 11 elements of the table are selected as a block and copied to the clipboard. The following is an MSHTML interpretation of what will be on the clipboard. The selection is shown in bold.</P>
<PRE CLASS="clsCode">
&lt;!DOCTYPE ...&gt;
&lt;HTML&gt;&lt;BODY&gt;&lt;TABLE BORDER&gt;
&lt;!--StartFragment--&gt;
<B>&lt;TR&gt;&lt;TD&gt;Item 6&lt;/TD&gt;
&lt;TD&gt;Item 7&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Item
10&lt;/TD&gt; &lt;TD&gt;Item 11&lt;/TD&gt;&lt;/TR&gt;</B>
&lt;!--EndFragment--&gt;
&lt;/TABLE&gt;
&lt;/BODY&gt;&lt;/HTML&gt;
</PRE>
<H3>Pasting a Fragment of an Ordered List into Plain Text</H3>
<P>HTML text:</P>
<PRE CLASS="clsCode">
&lt;BODY&gt;&lt;OL TYPE = a&gt;&lt;LI&gt;Item 1&lt;LI&gt;Item
2&lt;LI&gt;Item 3&lt;LI&gt;Item 4&lt;LI&gt;Item 5&lt;LI&gt;Item
6&lt;/OL&gt;&lt;/BODY&gt;
</PRE>
<P>This appears as:</P>
<OL>
<LI Style='text-indent:"1cm"'>Item 1
<LI Style='text-indent:"1cm"'>Item 2
<LI Style='text-indent:"1cm"'>Item 3
<LI Style='text-indent:"1cm"'>Item 4
<LI Style='text-indent:"1cm"'>Item 5
<LI Style='text-indent:"1cm"'>Item 6
</OL>
<P>The user selects and copies items 3 through 5 to the clipboard.
The following HTML is in the clipboard. The user's selection is shown in bold.</P>
<PRE CLASS="clsCode">
&lt;DOCTYPE...&gt;&lt;HTML&gt;&lt;BODY&gt;&lt;OL TYPE = a&gt;
&lt;!-- StartFragment--&gt;
<B>&lt;LI&gt;Item 3&lt;LI&gt;Item 4&lt;LI&gt;Item 5</B>
&lt;!-- EndFragment--&gt;
&lt;/OL&gt;&lt;/BODY&gt;&lt;/HTML&gt;
</PRE>
<P>If this fragment is now pasted into an empty document, the
following HTML will be created:</P>
<PRE CLASS="clsCode">
&lt;BODY&gt;&lt;OL TYPE = a&gt;&lt;LI&gt;Item 3&lt;LI&gt;Item4&lt;LI&gt;Item 5&lt;/OL&gt;&lt;/BODY&gt;
</PRE>
<P>This appears as:</P>
<UL>
<OL>
<LI Style='text-indent:"1cm"'>Item 3
<LI Style='text-indent:"1cm"'>Item 4
<LI Style='text-indent:"1cm"'>Item 5
</OL>
</UL>
<H3>Pasting a Partially Selected Region</H3>
<P>HTML text:</P>
<PRE CLASS="clsCode">
&lt;P&gt; MSHTML is a WYSIWYG Editor that supports:&lt;/P&gt;
&lt;UL&gt;&lt;LI&gt;Cut&lt;LI&gt;Copy&lt;LI&gt;Paste&lt;/UL&gt; &lt;P&gt;This is a great tool!&lt;/P&gt;
</PRE>
<P>This appears as:</P>
<DIV Style='text-indent:"1cm"'>
<P>MSHTML is a WYSIWYG Editor that supports:</P>
<UL>
<LI Style='text-indent:"1cm"'>Cut
<LI Style='text-indent:"1cm"'>Copy
<LI Style='text-indent:"1cm"'>Paste
</UL>
</DIV>
<DIV Style='text-indent:"1cm"'>
<P>This is a great tool!</P>
</DIV>
<P>The user selects from "WYSIWYG" to "Cop". The following HTML is in the clipboard. The user's selection is bold.</P>
<PRE CLASS="clsCode">
&lt;DOCTYPE...&gt;&lt;HTML&gt;&lt;BODY&gt;
&lt;!-- StartFragment--&gt;
<B>&lt;P&gt;WYSIWYG Editor that supports:&lt;/P&gt;
&lt;UL&gt;&lt;LI&gt;Cut&lt;LI&gt;Cop&lt;/UL&gt;</B>
&lt;!-- EndFragment--&gt;
&lt;/BODY&gt;&lt;/HTML&gt;
</PRE>
<P>The user selects from "opy" to "great". The following HTML is in the clipboard. The user's selection is bold.</P>
<PRE CLASS="clsCode">
&lt;DOCTYPE...&gt;&lt;HTML&gt;&lt;BODY&gt;
&lt;!-- StartFragment--&gt;
&lt;UL&gt;&lt;LI&gt;
<B>opy&lt;LI&gt;Paste&lt;/UL&gt;&lt;P&gt; This is a great</B>
&lt;/P&gt;
&lt;!-- EndFragment--&gt;
&lt;/BODY&gt;&lt;/HTML&gt;
</PRE>
<!-- CONTENTS_END -->
</DIV>
<!-- START_PAGE_FOOTER -->
<SCRIPT LANGUAGE="JavaScript1.2" DEFER SRC="/workshop/code/common.js"></SCRIPT>
<DIV CLASS="clsFooter">
<HR SIZE="1" /><BR />
<P ID="pBackTop"><A HREF="#top" CLASS="clsBackTop"><IMG SRC="/msdn-online/shared/graphics/top.gif" WIDTH="18" HEIGHT="8" BORDER="0" ALT="Back to top">Back to top</A></P>
<P><I>Did you find this topic useful? Suggestions for other topics? <A HREF="/siteguide/write-us.asp?author=InetSDK&area=Networking%2C+Protocols+%26amp%3B+Data+Formats" TARGET="_top">Write us!</A></I></P>
<P>&copy; <A CLASS="clsIncCpyRt" HREF="/isapi/gomscom.asp?TARGET=/misc/cpyright.htm" TARGET="_top">1999 Microsoft Corporation. All rights reserved. Terms of use</A>.</P>
<P>&nbsp;</P>
</DIV>
<SCRIPT LANGUAGE="JavaScript"><!--
function CheckToTop()
{
var eBody = document.body;
if (eBody.scrollHeight > eBody.offsetHeight) pBackTop.style.display = 'block';
}
//--></SCRIPT>
<SCRIPT LANGUAGE="JavaScript" SRC="/msdn-online/shared/js/navlinks.js"></SCRIPT>
<!-- END_PAGE_FOOTER -->
</BODY>
</HTML>