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.
This article covers the following topics:
Overview of CF_HTML
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 , 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.
The following is an example of a clipboard fragment:
Version:0.9 StartHTML:71 EndHTML:170 StartFragment:140 EndFragment:160 StartSelection:140 EndSelection:160The HTML Clipboard The Fragment
Description
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 (:).
Version vv | Version number of the clipboard. Starting version is 0.9. |
StartHTML | Byte count from the beginning of the clipboard to the start of the context, or -1 if no context. |
EndHTML | Byte count from the beginning of the clipboard to the end of the context, or -1 if no context. |
StartFragment | Byte count from the beginning of the clipboard to the start of the fragment. |
EndFragment | Byte count from the beginning of the clipboard to the end of the fragment. |
StartSelection | Byte count from the beginning of the clipboard to the start of the selection. |
EndSelection | Byte count from the beginning of the clipboard to the end of the selection. |
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.
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. Also, Windows Internet Explorer places a in the description section. This allows handlers of CF_HTML to resolve relative links within a file (such as when CF_HTML text is pasted into a DHTML Edit Control host).
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).
Context
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 element and allows the and 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.
An application copying a fragment of HTML to the clipboard may choose to create a base element to include in the context so that partial URLs in the fragment can be resolved.
Fragment
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:
- Selected text.
- Opening tags and attributes of any element that has an end tag within the selected text.
- End tags that match the included opening tags.
The fragment should be preceded and followed by the HTML comments <!--StartFragment--> and <!--EndFragment--> (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.
Scenarios
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.
Simple Fragment of HTML
HTML text:
This is normal. This is bold. This is bold italic. This is italic.
This appears as:
This is normal. This is bold. This is bold italic. This is italic.
The text between the ** is selected and copied to the clipboard:
This is normal. This is **bold. This is bold italic. This** is italic.
In this scenario only the element and the 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).
Version:0.9 StartHTML:71 EndHTML:160 StartFragment:130 EndFragment:150 StartSelection:130 EndSelection:150 bold. This is bold italic. This
Fragment of a Table in HTML
HTML text:
Head1 | Item 1 | Item 2 | Item 3 | Item 4 |
---|---|---|---|---|
Item 5 | Item 6 | Item 7 | Item 8 | |
Head2 | Item 9 | Item 10 | Item 11 | Item 12 |
This appears as:
Head1 | Item 1 | Item 2 | Item 3 | Item 4 |
---|---|---|---|---|
Item 5 | Item 6 | Item 7 | Item 8 | |
Head2 | Item 9 | Item 10 | Item 11 | Item 12 |
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.
Item 6 | Item 7 |
Item 10 | Item 11 |
Pasting a Fragment of an Ordered List into Plain Text
HTML text:
- Item 1
- Item 2
- Item 3
- Item 4
- Item 5
- Item 6
This appears as:
- Item 1
- Item 2
- Item 3
- Item 4
- Item 5
- Item 6
The user selects and copies items 3 through 5 to the clipboard. The following HTML is in the clipboard.
- Item 3
- Item 4
- Item 5
If this fragment is now pasted into an empty document, the following HTML will be created:
- Item 3
- Item 4
- Item 5
This appears as:
- Item 3
- Item 4
- Item 5
Pasting a Partially Selected Region
HTML text:
MSHTML is a WYSIWYG Editor that supports:
- Cut
- Copy
- Paste
This is a great tool!
This appears as:
MSHTML is a WYSIWYG Editor that supports:
- Cut
- Copy
- Paste
This is a great tool!
The user selects from "WYSIWYG" to "Cop". The following HTML is in the clipboard.
WYSIWYG Editor that supports:
- Cut
- Cop
The user selects from "opy" to "great". The following HTML is in the clipboard.
- opy
- Paste
This is a great
参考资料: