Chris Maunder posted in the Lounge about what he considers an annoying feature in VS.NET, that it adds ID attribute to an HTML element when pasted from a clipboard. (Actually, it adds the ID attribute only to some subset of elements like the
INPUT element.) Here is what you need to see the feature in action:-
- Cut/Copy some HTML text like
<input name="test" value="">
- Paste it in the HTML source editor. The text that will be pasted will be
<input name="test" value="" id=Text1>. The text in the clipboard is still
<input name="test" value=""> which can be verified by pasting in any other text editor.
The purpose of this add-in is to not allow the above to happen.
Fortunately, Visual Studio .NET Extensibility API is quite rich and provides an amazing level of control. The solution turned out to be pretty simple. Visual Studio .NET fires two events before any command get executed
BeforeExecute event allows the handlers to optionally cancel the command execution. So the add-in can handle the
BeforeExecute event and write the text as it is and cancel the default command execution. Here is a detailed description of the add-in code.
- The first step is to add the
BeforeExecute event for the
Edit.Paste command. For this we need to find the Guid and ID of the command that Visual Studio .NET maintains for the command. This can be obtained from the command object corresponding the the
Edit.Paste command. The following code gets the
cmd = applicationObject.Commands.Item("Edit.Paste", 0);
- Next we need to handle the
BeforeExecute event of this command.
events = applicationObject.Events.get_CommandEvents(cmd.Guid, cmd.ID);
events.BeforeExecute += new
- Now before any text is pasted, the control goes to the
OnBeforePaste method which looks like:
void OnBeforePaste(string guid, int id,
object customIn, object customOut, ref bool cancel)
cancel parameter value is set to
true in the function, the command execution will be canceled.
OnBeforePaste function is called, we need to check whether the user is using the HTML editor. Here is how this is done.
HTMLWindow htmlWindow =
applicationObject.ActiveWindow.Object as HTMLWindow;
if (htmlWindow != null)
- Finally we need to add text on our own to the document and cancel the default paste operations.
if (htmlWindow.CurrentTab == vsHTMLTabs.vsHTMLTabsSource)
IDataObject dataObject = Clipboard.GetDataObject();
string text = (string)dataObject.GetData(DataFormats.Text);
TextWindow tw = (TextWindow)htmlWindow.CurrentTabObject;
mshtml.IHTMLDocument2 doc =
doc.execCommand("Paste", false, null);
- Finally the event handlers need to be removed in the
OnDisconnection event of the add-in.
events.BeforeExecute -= new
Using the add-in
Follow these simple steps to start using the add-in:-
- Download the add-in setup and install it.
- Enable the add-in using the add-in manager (by default it is not loaded). Any HTML cut-paste operations will not add the default ID attribute.
- Unloading the add-in will make cut-paste operations work in the default way.
- May 19, 2003 - Added support for VS.NET 2003 and also support for editing from HTML design view