mshtml: Fix inserting a hyperlink when text is already selected.
There is no need to use cmd_insertLinkNoUI when we have the code to do most of the action anyway. Make use of the handy InsertLinkAroundSelection to insert the "a" element when not inserting at the caret and don't create a text node in this case because it is redundant.
This commit is contained in:
parent
1991e9f940
commit
5209ed0c5e
|
@ -1160,11 +1160,11 @@ static HRESULT exec_hyperlink(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in,
|
||||||
{
|
{
|
||||||
BSTR url = NULL;
|
BSTR url = NULL;
|
||||||
INT ret;
|
INT ret;
|
||||||
nsAString ns_url;
|
|
||||||
PRBool insert_link_at_caret;
|
|
||||||
nsISelection *nsselection;
|
nsISelection *nsselection;
|
||||||
|
nsIDOMDocument *nsdoc;
|
||||||
|
nsresult nsres;
|
||||||
|
|
||||||
FIXME("%p, 0x%x, %p, %p\n", This, cmdexecopt, in, out);
|
TRACE("%p, 0x%x, %p, %p\n", This, cmdexecopt, in, out);
|
||||||
|
|
||||||
if (cmdexecopt == OLECMDEXECOPT_DONTPROMPTUSER)
|
if (cmdexecopt == OLECMDEXECOPT_DONTPROMPTUSER)
|
||||||
{
|
{
|
||||||
|
@ -1186,29 +1186,23 @@ static HRESULT exec_hyperlink(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in,
|
||||||
if (!nsselection)
|
if (!nsselection)
|
||||||
return E_FAIL;
|
return E_FAIL;
|
||||||
|
|
||||||
nsAString_Init(&ns_url, url);
|
nsres = nsIWebNavigation_GetDocument(This->nscontainer->navigation, &nsdoc);
|
||||||
|
if(NS_SUCCEEDED(nsres))
|
||||||
nsISelection_GetIsCollapsed(nsselection, &insert_link_at_caret);
|
|
||||||
|
|
||||||
if (insert_link_at_caret)
|
|
||||||
{
|
{
|
||||||
static const WCHAR wszA[] = {'a',0};
|
static const WCHAR wszA[] = {'a',0};
|
||||||
static const WCHAR wszHref[] = {'h','r','e','f',0};
|
static const WCHAR wszHref[] = {'h','r','e','f',0};
|
||||||
nsIHTMLEditor *html_editor;
|
nsIHTMLEditor *html_editor;
|
||||||
nsIDOMDocument *nsdoc;
|
|
||||||
nsIDOMNode *text_node;
|
nsIDOMNode *text_node;
|
||||||
nsIDOMElement *anchor_elem;
|
nsIDOMElement *anchor_elem;
|
||||||
nsIDOMNode *unused_node;
|
nsIDOMNode *unused_node;
|
||||||
nsAString a_str;
|
nsAString a_str;
|
||||||
nsAString href_str;
|
nsAString href_str;
|
||||||
nsresult nsres;
|
nsAString ns_url;
|
||||||
|
PRBool insert_link_at_caret;
|
||||||
nsres = nsIWebNavigation_GetDocument(This->nscontainer->navigation, &nsdoc);
|
|
||||||
if(NS_FAILED(nsres))
|
|
||||||
return E_FAIL;
|
|
||||||
|
|
||||||
nsAString_Init(&a_str, wszA);
|
nsAString_Init(&a_str, wszA);
|
||||||
nsAString_Init(&href_str, wszHref);
|
nsAString_Init(&href_str, wszHref);
|
||||||
|
nsAString_Init(&ns_url, url);
|
||||||
|
|
||||||
/* create an element for the link */
|
/* create an element for the link */
|
||||||
nsIDOMDocument_CreateElement(nsdoc, &a_str, &anchor_elem);
|
nsIDOMDocument_CreateElement(nsdoc, &a_str, &anchor_elem);
|
||||||
|
@ -1217,44 +1211,48 @@ static HRESULT exec_hyperlink(HTMLDocument *This, DWORD cmdexecopt, VARIANT *in,
|
||||||
nsAString_Finish(&href_str);
|
nsAString_Finish(&href_str);
|
||||||
nsAString_Finish(&a_str);
|
nsAString_Finish(&a_str);
|
||||||
|
|
||||||
/* create an element with text of URL */
|
nsISelection_GetIsCollapsed(nsselection, &insert_link_at_caret);
|
||||||
nsIDOMDocument_CreateTextNode(nsdoc, &ns_url, (nsIDOMText **)&text_node);
|
|
||||||
|
|
||||||
/* wrap the <a> tags around the text element */
|
/* create an element with text of URL */
|
||||||
nsIDOMElement_AppendChild(anchor_elem, text_node, &unused_node);
|
if (insert_link_at_caret)
|
||||||
nsIDOMNode_Release(text_node);
|
{
|
||||||
nsIDOMNode_Release(unused_node);
|
nsIDOMDocument_CreateTextNode(nsdoc, &ns_url, (nsIDOMText **)&text_node);
|
||||||
|
|
||||||
|
/* wrap the <a> tags around the text element */
|
||||||
|
nsIDOMElement_AppendChild(anchor_elem, text_node, &unused_node);
|
||||||
|
nsIDOMNode_Release(text_node);
|
||||||
|
nsIDOMNode_Release(unused_node);
|
||||||
|
}
|
||||||
|
|
||||||
|
nsAString_Finish(&ns_url);
|
||||||
|
|
||||||
nsIEditor_QueryInterface(This->nscontainer->editor, &IID_nsIHTMLEditor, (void **)&html_editor);
|
nsIEditor_QueryInterface(This->nscontainer->editor, &IID_nsIHTMLEditor, (void **)&html_editor);
|
||||||
if (html_editor)
|
if (html_editor)
|
||||||
{
|
{
|
||||||
/* add them to the document at the caret position */
|
if (insert_link_at_caret)
|
||||||
nsres = nsIHTMLEditor_InsertElementAtSelection(html_editor, anchor_elem, FALSE);
|
{
|
||||||
|
/* add them to the document at the caret position */
|
||||||
|
nsres = nsIHTMLEditor_InsertElementAtSelection(html_editor, anchor_elem, FALSE);
|
||||||
|
nsISelection_SelectAllChildren(nsselection, (nsIDOMNode*)anchor_elem);
|
||||||
|
}
|
||||||
|
else /* add them around the selection using the magic provided to us by nsIHTMLEditor */
|
||||||
|
nsres = nsIHTMLEditor_InsertLinkAroundSelection(html_editor, anchor_elem);
|
||||||
nsIHTMLEditor_Release(html_editor);
|
nsIHTMLEditor_Release(html_editor);
|
||||||
}
|
}
|
||||||
|
|
||||||
nsISelection_SelectAllChildren(nsselection, (nsIDOMNode*)anchor_elem);
|
|
||||||
|
|
||||||
nsIDOMElement_Release(anchor_elem);
|
nsIDOMElement_Release(anchor_elem);
|
||||||
nsIDOMDocument_Release(nsdoc);
|
nsIDOMDocument_Release(nsdoc);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
nsres = E_FAIL;
|
||||||
nsICommandParams *nsparam = create_nscommand_params();
|
|
||||||
|
|
||||||
nsICommandParams_SetStringValue(nsparam, NSSTATE_ATTRIBUTE, &ns_url);
|
|
||||||
do_ns_command(This->nscontainer, NSCMD_INSERTLINKNOUI, nsparam);
|
|
||||||
nsICommandParams_Release(nsparam);
|
|
||||||
}
|
|
||||||
|
|
||||||
nsAString_Finish(&ns_url);
|
|
||||||
|
|
||||||
nsISelection_Release(nsselection);
|
nsISelection_Release(nsselection);
|
||||||
|
|
||||||
if (cmdexecopt != OLECMDEXECOPT_DONTPROMPTUSER)
|
if (cmdexecopt != OLECMDEXECOPT_DONTPROMPTUSER)
|
||||||
SysFreeString(url);
|
SysFreeString(url);
|
||||||
|
|
||||||
return S_OK;
|
TRACE("-- 0x%08x\n", nsres);
|
||||||
|
return nsres;
|
||||||
}
|
}
|
||||||
|
|
||||||
static HRESULT query_selall_status(HTMLDocument *This, OLECMD *cmd)
|
static HRESULT query_selall_status(HTMLDocument *This, OLECMD *cmd)
|
||||||
|
|
Loading…
Reference in New Issue