From bb467445743dea010395c75a66951dec4e2ffa7d Mon Sep 17 00:00:00 2001 From: "glazman%netscape.com" Date: Thu, 11 Apr 2002 13:33:30 +0000 Subject: [PATCH] Allow Composer's users to remove named anchors w/o having to use Source View; b=80742; r=brade, r=jfrancis, sr=kin, a=asa git-svn-id: svn://10.0.0.236/trunk@118746 18797224-902f-48f8-a5cc-f745e15eee43 --- .../editor/libeditor/base/nsIEditProperty.h | 1 + .../editor/libeditor/html/nsEditProperty.cpp | 3 + mozilla/editor/libeditor/html/nsHTMLEditor.h | 1 + .../libeditor/html/nsHTMLEditorStyle.cpp | 77 +++++++++++++++++-- .../ui/composer/content/ComposerCommands.js | 18 +++++ .../ui/composer/content/EditorContextMenu.js | 5 +- .../content/EditorContextMenuOverlay.xul | 3 + mozilla/editor/ui/composer/content/editor.js | 10 ++- .../ui/composer/content/editorOverlay.xul | 23 ++++-- .../composer/locale/en-US/editorOverlay.dtd | 4 + 10 files changed, 128 insertions(+), 17 deletions(-) diff --git a/mozilla/editor/libeditor/base/nsIEditProperty.h b/mozilla/editor/libeditor/base/nsIEditProperty.h index b92ed431751..c5d876029b8 100644 --- a/mozilla/editor/libeditor/base/nsIEditProperty.h +++ b/mozilla/editor/libeditor/base/nsIEditProperty.h @@ -86,6 +86,7 @@ public: static nsIAtom *font; static nsIAtom *a; static nsIAtom *href; + static nsIAtom *name; static nsIAtom *img; static nsIAtom *object; static nsIAtom *br; diff --git a/mozilla/editor/libeditor/html/nsEditProperty.cpp b/mozilla/editor/libeditor/html/nsEditProperty.cpp index a1a087525ed..b32c5be77a4 100644 --- a/mozilla/editor/libeditor/html/nsEditProperty.cpp +++ b/mozilla/editor/libeditor/html/nsEditProperty.cpp @@ -73,6 +73,7 @@ nsIAtom * nsIEditProperty::acronym; nsIAtom * nsIEditProperty::font; nsIAtom * nsIEditProperty::a; nsIAtom * nsIEditProperty::href; +nsIAtom * nsIEditProperty::name; nsIAtom * nsIEditProperty::img; nsIAtom * nsIEditProperty::object; nsIAtom * nsIEditProperty::br; @@ -204,6 +205,7 @@ nsEditProperty::nsEditProperty() nsIEditProperty::font = NS_NewAtom("font"); nsIEditProperty::a = NS_NewAtom("a"); nsIEditProperty::href = NS_NewAtom("href"); // Use to differentiate between "a" for link, "a" for named anchor + nsIEditProperty::name = NS_NewAtom("name"); nsIEditProperty::img = NS_NewAtom("img"); nsIEditProperty::object = NS_NewAtom("object"); nsIEditProperty::br = NS_NewAtom("br"); @@ -319,6 +321,7 @@ nsEditProperty::~nsEditProperty() NS_IF_RELEASE(nsIEditProperty::font); NS_IF_RELEASE(nsIEditProperty::a); NS_IF_RELEASE(nsIEditProperty::href); + NS_IF_RELEASE(nsIEditProperty::name); NS_IF_RELEASE(nsIEditProperty::img); NS_IF_RELEASE(nsIEditProperty::object); NS_IF_RELEASE(nsIEditProperty::br); diff --git a/mozilla/editor/libeditor/html/nsHTMLEditor.h b/mozilla/editor/libeditor/html/nsHTMLEditor.h index bb11e876175..a19e01bc85c 100644 --- a/mozilla/editor/libeditor/html/nsHTMLEditor.h +++ b/mozilla/editor/libeditor/html/nsHTMLEditor.h @@ -676,6 +676,7 @@ protected: const nsAString *aValue); nsresult PromoteInlineRange(nsIDOMRange *inRange); + nsresult PromoteRangeIfStartsOrEndsInNamedAnchor(nsIDOMRange *inRange); nsresult SplitStyleAboveRange(nsIDOMRange *aRange, nsIAtom *aProperty, const nsAString *aAttribute); diff --git a/mozilla/editor/libeditor/html/nsHTMLEditorStyle.cpp b/mozilla/editor/libeditor/html/nsHTMLEditorStyle.cpp index 58b00edc4af..a15dc2ac8a3 100644 --- a/mozilla/editor/libeditor/html/nsHTMLEditorStyle.cpp +++ b/mozilla/editor/libeditor/html/nsHTMLEditorStyle.cpp @@ -646,7 +646,8 @@ nsresult nsHTMLEditor::RemoveStyleInside(nsIDOMNode *aNode, // then process the node itself if ( !aChildrenOnly && (aProperty && NodeIsType(aNode, aProperty) || // node is prop we asked for - (aProperty == nsIEditProperty::href && nsHTMLEditUtils::IsLink(aNode))) || // but check for link ( startNode, endNode, parent, tmp; + PRInt32 startOffset, endOffset, tmpOffset; + + res = inRange->GetStartContainer(getter_AddRefs(startNode)); + if (NS_FAILED(res)) return res; + res = inRange->GetStartOffset(&startOffset); + if (NS_FAILED(res)) return res; + res = inRange->GetEndContainer(getter_AddRefs(endNode)); + if (NS_FAILED(res)) return res; + res = inRange->GetEndOffset(&endOffset); + if (NS_FAILED(res)) return res; + + tmp = startNode; + while ( tmp && + !nsTextEditUtils::IsBody(tmp) && + !nsHTMLEditUtils::IsNamedAnchor(tmp)) + { + res = GetNodeLocation(tmp, address_of(parent), &tmpOffset); + if (NS_FAILED(res)) return res; + tmp = parent; + } + if (!tmp) return NS_ERROR_NULL_POINTER; + if (nsHTMLEditUtils::IsNamedAnchor(tmp)) + { + res = GetNodeLocation(tmp, address_of(parent), &tmpOffset); + if (NS_FAILED(res)) return res; + startNode = parent; + startOffset = tmpOffset; + } + + tmp = endNode; + while ( tmp && + !nsTextEditUtils::IsBody(tmp) && + !nsHTMLEditUtils::IsNamedAnchor(tmp)) + { + res = GetNodeLocation(tmp, address_of(parent), &tmpOffset); + if (NS_FAILED(res)) return res; + tmp = parent; + } + if (!tmp) return NS_ERROR_NULL_POINTER; + if (nsHTMLEditUtils::IsNamedAnchor(tmp)) + { + res = GetNodeLocation(tmp, address_of(parent), &tmpOffset); + if (NS_FAILED(res)) return res; + endNode = parent; + endOffset = tmpOffset + 1; + } + + res = inRange->SetStart(startNode, startOffset); + if (NS_FAILED(res)) return res; + res = inRange->SetEnd(endNode, endOffset); + return res; +} nsresult nsHTMLEditor::PromoteInlineRange(nsIDOMRange *inRange) { @@ -1161,7 +1219,8 @@ nsresult nsHTMLEditor::RemoveInlinePropertyImpl(nsIAtom *aProperty, const nsAStr // manipulating text attributes on a collapsed selection only sets state for the next text insertion // For links, aProperty uses "href", use "a" instead - if (aProperty == nsIEditProperty::href) + if (aProperty == nsIEditProperty::href || + aProperty == nsIEditProperty::name) aProperty = nsIEditProperty::a; if (aProperty) return mTypeInState->ClearProp(aProperty, nsAutoString(*aAttribute)); @@ -1195,10 +1254,18 @@ nsresult nsHTMLEditor::RemoveInlinePropertyImpl(nsIAtom *aProperty, const nsAStr nsCOMPtr range( do_QueryInterface(currentItem) ); - // adjust range to include any ancestors who's children are entirely selected - res = PromoteInlineRange(range); + if (aProperty == nsIEditProperty::name) + { + // promote range if it starts or end in a named anchor and we + // want to remove named anchors + res = PromoteRangeIfStartsOrEndsInNamedAnchor(range); + } + else { + // adjust range to include any ancestors who's children are entirely selected + res = PromoteInlineRange(range); + } if (NS_FAILED(res)) return res; - + // remove this style from ancestors of our range endpoints, // splitting them as appropriate res = SplitStyleAboveRange(range, aProperty, aAttribute); diff --git a/mozilla/editor/ui/composer/content/ComposerCommands.js b/mozilla/editor/ui/composer/content/ComposerCommands.js index ce2bf405dd4..ecc8cf1df2a 100644 --- a/mozilla/editor/ui/composer/content/ComposerCommands.js +++ b/mozilla/editor/ui/composer/content/ComposerCommands.js @@ -23,6 +23,7 @@ * Simon Fraser (sfraser@netscape.com) * Ryan Cassin (rcassin@supernova.org) * Kathleen Brade (brade@netscape.com) + * Daniel Glazman (glazman@netscape.com) * * * Alternatively, the contents of this file may be used under the terms of @@ -62,6 +63,7 @@ function SetupHTMLEditorCommands() controller.registerCommand("cmd_advancedProperties", nsAdvancedPropertiesCommand); controller.registerCommand("cmd_objectProperties", nsObjectPropertiesCommand); controller.registerCommand("cmd_removeLinks", nsRemoveLinksCommand); + controller.registerCommand("cmd_removeNamedAnchors", nsRemoveNamedAnchorsCommand); controller.registerCommand("cmd_editLink", nsEditLinkCommand); controller.registerCommand("cmd_form", nsFormCommand); @@ -2714,6 +2716,22 @@ var nsRemoveLinksCommand = }; +//----------------------------------------------------------------------------------- +var nsRemoveNamedAnchorsCommand = +{ + isCommandEnabled: function(aCommand, dummy) + { + // We could see if there's any link in selection, but it doesn't seem worth the work! + return (window.editorShell && window.editorShell.documentEditable && IsEditingRenderedHTML()); + }, + doCommand: function(aCommand) + { + window.editorShell.RemoveTextProperty("name", ""); + window._content.focus(); + } +}; + + //----------------------------------------------------------------------------------- var nsEditLinkCommand = { diff --git a/mozilla/editor/ui/composer/content/EditorContextMenu.js b/mozilla/editor/ui/composer/content/EditorContextMenu.js index 1100c44e17f..03f7d90b189 100644 --- a/mozilla/editor/ui/composer/content/EditorContextMenu.js +++ b/mozilla/editor/ui/composer/content/EditorContextMenu.js @@ -36,7 +36,7 @@ function EditorFillContextMenu(event, contextMenuNode) // Setup object property menuitem var objectName = InitObjectPropertiesMenuitem("objectProperties_cm"); - InitRemoveStylesMenuitems("removeStylesMenuitem_cm", "removeLinksMenuitem_cm"); + InitRemoveStylesMenuitems("removeStylesMenuitem_cm", "removeLinksMenuitem_cm", "removeNamedAnchorsMenuitem_cm"); // This item is present only in context menu: DisableItem("editLink_cm", objectName != "href"); @@ -76,7 +76,8 @@ function EditorFillContextMenu(event, contextMenuNode) var haveStyle = IsMenuItemShowing("removeStylesMenuitem_cm") || IsMenuItemShowing("createLink_cm") || - IsMenuItemShowing("removeLinksMenuitem_cm"); + IsMenuItemShowing("removeLinksMenuitem_cm") || + IsMenuItemShowing("removeNamedAnchorsMenuitem_cm"); var havePropsOrImage = IsMenuItemShowing("objectProperties_cm") || diff --git a/mozilla/editor/ui/composer/content/EditorContextMenuOverlay.xul b/mozilla/editor/ui/composer/content/EditorContextMenuOverlay.xul index 8cd22d3f926..6efcd62cd23 100644 --- a/mozilla/editor/ui/composer/content/EditorContextMenuOverlay.xul +++ b/mozilla/editor/ui/composer/content/EditorContextMenuOverlay.xul @@ -19,6 +19,7 @@ - Rights Reserved. - - Contributor(s): + - Daniel Glazman (glazman@netscape.com) --> @@ -53,6 +54,8 @@ observes="cmd_link" /> + diff --git a/mozilla/editor/ui/composer/content/editor.js b/mozilla/editor/ui/composer/content/editor.js index 769e5ba06d5..7638271a225 100644 --- a/mozilla/editor/ui/composer/content/editor.js +++ b/mozilla/editor/ui/composer/content/editor.js @@ -1754,7 +1754,7 @@ function EditorInitFormatMenu() { try { InitObjectPropertiesMenuitem("objectProperties"); - InitRemoveStylesMenuitems("removeStylesMenuitem", "removeLinksMenuitem"); + InitRemoveStylesMenuitems("removeStylesMenuitem", "removeLinksMenuitem", "removeNamedAnchorsMenuitem"); } catch(ex) {} // Set alignment check } @@ -2358,11 +2358,12 @@ function InitJoinCellMenuitem(id) menuItem.setAttribute("accesskey",GetString("JoinCellAccesskey")); } -function InitRemoveStylesMenuitems(removeStylesId, removeLinksId) +function InitRemoveStylesMenuitems(removeStylesId, removeLinksId, removeNamedAnchorsId) { // Change wording of menuitems depending on selection var stylesItem = document.getElementById(removeStylesId); var linkItem = document.getElementById(removeLinksId); + var namedAnchorsItem = document.getElementById(removeNamedAnchorsId); var isCollapsed = editorShell.editorSelection.isCollapsed; if (stylesItem) @@ -2380,6 +2381,11 @@ function InitRemoveStylesMenuitems(removeStylesId, removeLinksId) // if selection isn't collapsed since we only look at anchor node DisableItem(removeLinksId, isCollapsed && !window.editorShell.GetElementOrParentByTagName("href", null)); } + if (namedAnchorsItem) + { + // Disable if selection is collapsed + DisableItem(removeNamedAnchorsId, isCollapsed); + } } function goUpdateTableMenuItems(commandset) diff --git a/mozilla/editor/ui/composer/content/editorOverlay.xul b/mozilla/editor/ui/composer/content/editorOverlay.xul index 75e386966a3..d019182305a 100644 --- a/mozilla/editor/ui/composer/content/editorOverlay.xul +++ b/mozilla/editor/ui/composer/content/editorOverlay.xul @@ -65,6 +65,7 @@ + @@ -224,6 +225,7 @@ + @@ -483,7 +485,12 @@ - + + + position="10" onpopupshowing="InitParagraphMenu()"> @@ -512,7 +519,7 @@ + position="11" onpopupshowing="InitListMenu()"> @@ -523,16 +530,16 @@ - + + observes="cmd_indent" position="13"/> + observes="cmd_outdent" position="14"/> + position="15"> @@ -541,7 +548,7 @@ - + diff --git a/mozilla/editor/ui/composer/locale/en-US/editorOverlay.dtd b/mozilla/editor/ui/composer/locale/en-US/editorOverlay.dtd index 383921afb1d..65e501e3372 100644 --- a/mozilla/editor/ui/composer/locale/en-US/editorOverlay.dtd +++ b/mozilla/editor/ui/composer/locale/en-US/editorOverlay.dtd @@ -21,6 +21,7 @@ - Sammy Ford - Blake Ross - Ryan Cassin (rcassin@supernova.org) + - Daniel Glazman (glazman@netscape.com) --> @@ -404,6 +405,9 @@ + + +