2012-05-30 20:48:24 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2008-09-03 17:35:06 +04:00
|
|
|
|
2019-04-09 02:57:14 +03:00
|
|
|
/* import-globals-from ../../composer/content/editorUtilities.js */
|
|
|
|
/* import-globals-from EdDialogCommon.js */
|
|
|
|
|
2008-09-03 17:35:06 +04:00
|
|
|
var gActiveEditor;
|
|
|
|
var anchorElement = null;
|
|
|
|
var imageElement = null;
|
|
|
|
var insertNew = false;
|
|
|
|
var replaceExistingLink = false;
|
|
|
|
var insertLinkAtCaret;
|
|
|
|
var needLinkText = false;
|
|
|
|
var href;
|
|
|
|
var newLinkText;
|
|
|
|
var gHNodeArray = {};
|
|
|
|
var gHaveNamedAnchors = false;
|
|
|
|
var gHaveHeadings = false;
|
|
|
|
var gCanChangeHeadingSelected = true;
|
|
|
|
var gCanChangeAnchorSelected = true;
|
|
|
|
|
|
|
|
// NOTE: Use "href" instead of "a" to distinguish from Named Anchor
|
|
|
|
// The returned node is has an "a" tagName
|
|
|
|
var tagName = "href";
|
|
|
|
|
|
|
|
// dialog initialization code
|
2019-04-04 08:58:52 +03:00
|
|
|
|
|
|
|
document.addEventListener("dialogaccept", onAccept);
|
|
|
|
document.addEventListener("dialogcancel", onCancel);
|
|
|
|
|
2019-04-09 02:57:12 +03:00
|
|
|
function Startup() {
|
2008-09-03 17:35:06 +04:00
|
|
|
gActiveEditor = GetCurrentEditor();
|
2019-04-09 02:57:12 +03:00
|
|
|
if (!gActiveEditor) {
|
2008-09-03 17:35:06 +04:00
|
|
|
dump("Failed to get active editor!\n");
|
|
|
|
window.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Message was wrapped in a <label> or <div>, so actual text is a child text node
|
2019-08-30 22:45:11 +03:00
|
|
|
gDialog.linkTextCaption = document.getElementById("linkTextCaption");
|
|
|
|
gDialog.linkTextMessage = document.getElementById("linkTextMessage");
|
|
|
|
gDialog.linkTextInput = document.getElementById("linkTextInput");
|
|
|
|
gDialog.hrefInput = document.getElementById("hrefInput");
|
|
|
|
gDialog.makeRelativeLink = document.getElementById("MakeRelativeLink");
|
2008-09-03 17:35:06 +04:00
|
|
|
gDialog.AdvancedEditSection = document.getElementById("AdvancedEdit");
|
|
|
|
|
|
|
|
// See if we have a single selected image
|
|
|
|
imageElement = gActiveEditor.getSelectedElement("img");
|
|
|
|
|
2019-04-09 02:57:12 +03:00
|
|
|
if (imageElement) {
|
2008-09-03 17:35:06 +04:00
|
|
|
// Get the parent link if it exists -- more efficient than GetSelectedElement()
|
2019-08-30 22:45:11 +03:00
|
|
|
anchorElement = gActiveEditor.getElementOrParentByTagName(
|
|
|
|
"href",
|
|
|
|
imageElement
|
|
|
|
);
|
2019-04-09 02:57:12 +03:00
|
|
|
if (anchorElement) {
|
|
|
|
if (anchorElement.childNodes.length > 1) {
|
2008-09-03 17:35:06 +04:00
|
|
|
// If there are other children, then we want to break
|
|
|
|
// this image away by inserting a new link around it,
|
|
|
|
// so make a new node and copy existing attributes
|
|
|
|
anchorElement = anchorElement.cloneNode(false);
|
2019-04-09 02:57:12 +03:00
|
|
|
// insertNew = true;
|
2008-09-03 17:35:06 +04:00
|
|
|
replaceExistingLink = true;
|
|
|
|
}
|
|
|
|
}
|
2019-04-09 02:57:12 +03:00
|
|
|
} else {
|
2008-09-03 17:35:06 +04:00
|
|
|
// Get an anchor element if caret or
|
|
|
|
// entire selection is within the link.
|
|
|
|
anchorElement = gActiveEditor.getSelectedElement(tagName);
|
|
|
|
|
2019-04-09 02:57:12 +03:00
|
|
|
if (anchorElement) {
|
2008-09-03 17:35:06 +04:00
|
|
|
// Select the entire link
|
|
|
|
gActiveEditor.selectElement(anchorElement);
|
2019-04-09 02:57:12 +03:00
|
|
|
} else {
|
2008-09-03 17:35:06 +04:00
|
|
|
// If selection starts in a link, but extends beyond it,
|
|
|
|
// the user probably wants to extend existing link to new selection,
|
|
|
|
// so check if either end of selection is within a link
|
|
|
|
// POTENTIAL PROBLEM: This prevents user from selecting text in an existing
|
2018-03-31 13:50:02 +03:00
|
|
|
// link and making 2 links.
|
2008-09-03 17:35:06 +04:00
|
|
|
// Note that this isn't a problem with images, handled above
|
|
|
|
|
2019-08-30 22:45:11 +03:00
|
|
|
anchorElement = gActiveEditor.getElementOrParentByTagName(
|
|
|
|
"href",
|
|
|
|
gActiveEditor.selection.anchorNode
|
|
|
|
);
|
|
|
|
if (!anchorElement) {
|
|
|
|
anchorElement = gActiveEditor.getElementOrParentByTagName(
|
|
|
|
"href",
|
|
|
|
gActiveEditor.selection.focusNode
|
|
|
|
);
|
|
|
|
}
|
2008-09-03 17:35:06 +04:00
|
|
|
|
2019-04-09 02:57:12 +03:00
|
|
|
if (anchorElement) {
|
2008-09-03 17:35:06 +04:00
|
|
|
// But clone it for reinserting/merging around existing
|
|
|
|
// link that only partially overlaps the selection
|
|
|
|
anchorElement = anchorElement.cloneNode(false);
|
2019-04-09 02:57:12 +03:00
|
|
|
// insertNew = true;
|
2008-09-03 17:35:06 +04:00
|
|
|
replaceExistingLink = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-09 02:57:12 +03:00
|
|
|
if (!anchorElement) {
|
2008-09-03 17:35:06 +04:00
|
|
|
// No existing link -- create a new one
|
|
|
|
anchorElement = gActiveEditor.createElementWithDefaults(tagName);
|
|
|
|
insertNew = true;
|
|
|
|
// Hide message about removing existing link
|
2019-04-09 02:57:12 +03:00
|
|
|
// document.getElementById("RemoveLinkMsg").hidden = true;
|
2008-09-03 17:35:06 +04:00
|
|
|
}
|
2019-04-09 02:57:12 +03:00
|
|
|
if (!anchorElement) {
|
2008-09-03 17:35:06 +04:00
|
|
|
dump("Failed to get selected element or create a new one!\n");
|
|
|
|
window.close();
|
|
|
|
return;
|
2018-03-31 13:50:02 +03:00
|
|
|
}
|
2008-09-03 17:35:06 +04:00
|
|
|
|
|
|
|
// We insert at caret only when nothing is selected
|
|
|
|
insertLinkAtCaret = gActiveEditor.selection.isCollapsed;
|
2018-03-31 13:50:02 +03:00
|
|
|
|
2008-09-03 17:35:06 +04:00
|
|
|
var selectedText;
|
2019-04-09 02:57:12 +03:00
|
|
|
if (insertLinkAtCaret) {
|
2008-09-03 17:35:06 +04:00
|
|
|
// Groupbox caption:
|
|
|
|
gDialog.linkTextCaption.setAttribute("label", GetString("LinkText"));
|
|
|
|
|
|
|
|
// Message above input field:
|
|
|
|
gDialog.linkTextMessage.setAttribute("value", GetString("EnterLinkText"));
|
2019-08-30 22:45:11 +03:00
|
|
|
gDialog.linkTextMessage.setAttribute(
|
|
|
|
"accesskey",
|
|
|
|
GetString("EnterLinkTextAccessKey")
|
|
|
|
);
|
2019-04-09 02:57:12 +03:00
|
|
|
} else {
|
|
|
|
if (!imageElement) {
|
2008-09-03 17:35:06 +04:00
|
|
|
// We get here if selection is exactly around a link node
|
|
|
|
// Check if selection has some text - use that first
|
|
|
|
selectedText = GetSelectionAsText();
|
2019-04-09 02:57:12 +03:00
|
|
|
if (!selectedText) {
|
2008-09-03 17:35:06 +04:00
|
|
|
// No text, look for first image in the selection
|
|
|
|
var children = anchorElement.childNodes;
|
2019-04-09 02:57:12 +03:00
|
|
|
if (children) {
|
|
|
|
for (var i = 0; i < children.length; i++) {
|
2008-09-03 17:35:06 +04:00
|
|
|
var nodeName = children.item(i).nodeName.toLowerCase();
|
2019-04-09 02:57:12 +03:00
|
|
|
if (nodeName == "img") {
|
2008-09-03 17:35:06 +04:00
|
|
|
imageElement = children.item(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Set "caption" for link source and the source text or image URL
|
2019-04-09 02:57:12 +03:00
|
|
|
if (imageElement) {
|
2008-09-03 17:35:06 +04:00
|
|
|
gDialog.linkTextCaption.setAttribute("label", GetString("LinkImage"));
|
|
|
|
// Link source string is the source URL of image
|
|
|
|
// TODO: THIS DOESN'T HANDLE MULTIPLE SELECTED IMAGES!
|
|
|
|
gDialog.linkTextMessage.setAttribute("value", imageElement.src);
|
|
|
|
} else {
|
|
|
|
gDialog.linkTextCaption.setAttribute("label", GetString("LinkText"));
|
2019-04-09 02:57:12 +03:00
|
|
|
if (selectedText) {
|
2008-09-03 17:35:06 +04:00
|
|
|
// Use just the first 60 characters and add "..."
|
2019-08-30 22:45:11 +03:00
|
|
|
gDialog.linkTextMessage.setAttribute(
|
|
|
|
"value",
|
|
|
|
TruncateStringAtWordEnd(
|
|
|
|
ReplaceWhitespace(selectedText, " "),
|
|
|
|
60,
|
|
|
|
true
|
|
|
|
)
|
|
|
|
);
|
2008-09-03 17:35:06 +04:00
|
|
|
} else {
|
2019-08-30 22:45:11 +03:00
|
|
|
gDialog.linkTextMessage.setAttribute(
|
|
|
|
"value",
|
|
|
|
GetString("MixedSelection")
|
|
|
|
);
|
2008-09-03 17:35:06 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a copy to use for AdvancedEdit and onSaveDefault
|
|
|
|
globalElement = anchorElement.cloneNode(false);
|
|
|
|
|
|
|
|
// Get the list of existing named anchors and headings
|
|
|
|
FillLinkMenulist(gDialog.hrefInput, gHNodeArray);
|
|
|
|
|
|
|
|
// We only need to test for this once per dialog load
|
|
|
|
gHaveDocumentUrl = GetDocumentBaseUrl();
|
|
|
|
|
|
|
|
// Set data for the dialog controls
|
|
|
|
InitDialog();
|
2018-03-31 13:50:02 +03:00
|
|
|
|
2008-09-03 17:35:06 +04:00
|
|
|
// Search for a URI pattern in the selected text
|
|
|
|
// as candidate href
|
2018-03-31 13:50:02 +03:00
|
|
|
selectedText = TrimString(selectedText);
|
2019-08-30 22:45:11 +03:00
|
|
|
if (!gDialog.hrefInput.value && TextIsURI(selectedText)) {
|
|
|
|
gDialog.hrefInput.value = selectedText;
|
|
|
|
}
|
2008-09-03 17:35:06 +04:00
|
|
|
|
|
|
|
// Set initial focus
|
|
|
|
if (insertLinkAtCaret) {
|
|
|
|
// We will be using the HREF inputbox, so text message
|
|
|
|
SetTextboxFocus(gDialog.linkTextInput);
|
|
|
|
} else {
|
|
|
|
SetTextboxFocus(gDialog.hrefInput);
|
|
|
|
|
|
|
|
// We will not insert a new link at caret, so remove link text input field
|
|
|
|
gDialog.linkTextInput.hidden = true;
|
|
|
|
gDialog.linkTextInput = null;
|
|
|
|
}
|
2018-03-31 13:50:02 +03:00
|
|
|
|
2008-09-03 17:35:06 +04:00
|
|
|
// This sets enable state on OK button
|
|
|
|
doEnabling();
|
|
|
|
|
|
|
|
SetWindowLocation();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set dialog widgets with attribute data
|
|
|
|
// We get them from globalElement copy so this can be used
|
|
|
|
// by AdvancedEdit(), which is shared by all property dialogs
|
2019-04-09 02:57:12 +03:00
|
|
|
function InitDialog() {
|
2018-03-31 13:50:02 +03:00
|
|
|
// Must use getAttribute, not "globalElement.href",
|
2018-03-21 01:57:33 +03:00
|
|
|
// or foreign chars aren't converted correctly!
|
2008-09-03 17:35:06 +04:00
|
|
|
gDialog.hrefInput.value = globalElement.getAttribute("href");
|
|
|
|
|
|
|
|
// Set "Relativize" checkbox according to current URL state
|
|
|
|
SetRelativeCheckbox(gDialog.makeRelativeLink);
|
|
|
|
}
|
|
|
|
|
2019-04-09 02:57:12 +03:00
|
|
|
function doEnabling() {
|
2008-09-03 17:35:06 +04:00
|
|
|
// We disable Ok button when there's no href text only if inserting a new link
|
2019-08-30 22:45:11 +03:00
|
|
|
var enable = insertNew
|
|
|
|
? TrimString(gDialog.hrefInput.value).length > 0
|
|
|
|
: true;
|
2018-03-31 13:50:02 +03:00
|
|
|
|
2008-09-03 17:35:06 +04:00
|
|
|
// anon. content, so can't use SetElementEnabledById here
|
|
|
|
var dialogNode = document.getElementById("linkDlg");
|
|
|
|
dialogNode.getButton("accept").disabled = !enable;
|
|
|
|
|
2019-04-09 02:57:12 +03:00
|
|
|
SetElementEnabledById("AdvancedEditButton1", enable);
|
2008-09-03 17:35:06 +04:00
|
|
|
}
|
|
|
|
|
2019-04-09 02:57:12 +03:00
|
|
|
function ChangeLinkLocation() {
|
2008-09-03 17:35:06 +04:00
|
|
|
SetRelativeCheckbox(gDialog.makeRelativeLink);
|
|
|
|
// Set OK button enable state
|
|
|
|
doEnabling();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get and validate data from widgets.
|
|
|
|
// Set attributes on globalElement so they can be accessed by AdvancedEdit()
|
2019-04-09 02:57:12 +03:00
|
|
|
function ValidateData() {
|
2008-09-03 17:35:06 +04:00
|
|
|
href = TrimString(gDialog.hrefInput.value);
|
2019-04-09 02:57:12 +03:00
|
|
|
if (href) {
|
2008-09-03 17:35:06 +04:00
|
|
|
// Set the HREF directly on the editor document's anchor node
|
|
|
|
// or on the newly-created node if insertNew is true
|
2019-04-09 02:57:12 +03:00
|
|
|
globalElement.setAttribute("href", href);
|
|
|
|
} else if (insertNew) {
|
2008-09-03 17:35:06 +04:00
|
|
|
// We must have a URL to insert a new link
|
2019-04-09 02:57:12 +03:00
|
|
|
// NOTE: We accept an empty HREF on existing link to indicate removing the link
|
2008-09-03 17:35:06 +04:00
|
|
|
ShowInputErrorMessage(GetString("EmptyHREFError"));
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-09 02:57:12 +03:00
|
|
|
if (gDialog.linkTextInput) {
|
2008-09-03 17:35:06 +04:00
|
|
|
// The text we will insert isn't really an attribute,
|
|
|
|
// but it makes sense to validate it
|
|
|
|
newLinkText = TrimString(gDialog.linkTextInput.value);
|
2019-04-09 02:57:12 +03:00
|
|
|
if (!newLinkText) {
|
2019-04-09 02:57:14 +03:00
|
|
|
if (href) {
|
2019-04-09 02:57:12 +03:00
|
|
|
newLinkText = href;
|
2019-04-09 02:57:14 +03:00
|
|
|
} else {
|
2008-09-03 17:35:06 +04:00
|
|
|
ShowInputErrorMessage(GetString("EmptyLinkTextError"));
|
|
|
|
SetTextboxFocus(gDialog.linkTextInput);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-09 02:57:12 +03:00
|
|
|
function onAccept(event) {
|
|
|
|
if (ValidateData()) {
|
|
|
|
if (href.length > 0) {
|
2008-09-03 17:35:06 +04:00
|
|
|
// Copy attributes to element we are changing or inserting
|
|
|
|
gActiveEditor.cloneAttributes(anchorElement, globalElement);
|
|
|
|
|
|
|
|
// Coalesce into one undo transaction
|
|
|
|
gActiveEditor.beginTransaction();
|
|
|
|
|
|
|
|
// Get text to use for a new link
|
2019-04-09 02:57:12 +03:00
|
|
|
if (insertLinkAtCaret) {
|
2018-03-31 13:50:02 +03:00
|
|
|
// Append the link text as the last child node
|
2008-09-03 17:35:06 +04:00
|
|
|
// of the anchor node
|
|
|
|
var textNode = gActiveEditor.document.createTextNode(newLinkText);
|
2019-08-30 22:45:11 +03:00
|
|
|
if (textNode) {
|
2008-09-03 17:35:06 +04:00
|
|
|
anchorElement.appendChild(textNode);
|
2019-08-30 22:45:11 +03:00
|
|
|
}
|
2008-09-03 17:35:06 +04:00
|
|
|
try {
|
|
|
|
gActiveEditor.insertElementAtSelection(anchorElement, false);
|
|
|
|
} catch (e) {
|
2018-03-21 01:57:33 +03:00
|
|
|
dump("Exception occurred in InsertElementAtSelection\n");
|
2019-04-05 05:59:06 +03:00
|
|
|
return;
|
2008-09-03 17:35:06 +04:00
|
|
|
}
|
2019-04-09 02:57:12 +03:00
|
|
|
} else if (insertNew || replaceExistingLink) {
|
2008-09-03 17:35:06 +04:00
|
|
|
// Link source was supplied by the selection,
|
|
|
|
// so insert a link node as parent of this
|
|
|
|
// (may be text, image, or other inline content)
|
|
|
|
try {
|
|
|
|
gActiveEditor.insertLinkAroundSelection(anchorElement);
|
|
|
|
} catch (e) {
|
2018-03-21 01:57:33 +03:00
|
|
|
dump("Exception occurred in InsertElementAtSelection\n");
|
2019-04-05 05:59:06 +03:00
|
|
|
return;
|
2008-09-03 17:35:06 +04:00
|
|
|
}
|
|
|
|
}
|
2018-03-31 13:50:02 +03:00
|
|
|
// Check if the link was to a heading
|
2019-04-09 02:57:12 +03:00
|
|
|
if (href in gHNodeArray) {
|
2008-09-03 17:35:06 +04:00
|
|
|
var anchorNode = gActiveEditor.createElementWithDefaults("a");
|
2019-04-09 02:57:12 +03:00
|
|
|
if (anchorNode) {
|
2008-09-03 17:35:06 +04:00
|
|
|
anchorNode.name = href.substr(1);
|
|
|
|
|
2018-03-31 13:50:02 +03:00
|
|
|
// Insert the anchor into the document,
|
2008-09-03 17:35:06 +04:00
|
|
|
// but don't let the transaction change the selection
|
|
|
|
gActiveEditor.setShouldTxnSetSelection(false);
|
|
|
|
gActiveEditor.insertNode(anchorNode, gHNodeArray[href], 0);
|
|
|
|
gActiveEditor.setShouldTxnSetSelection(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
gActiveEditor.endTransaction();
|
2019-04-09 02:57:12 +03:00
|
|
|
} else if (!insertNew) {
|
2008-09-03 17:35:06 +04:00
|
|
|
// We already had a link, but empty HREF means remove it
|
|
|
|
EditorRemoveTextProperty("href", "");
|
|
|
|
}
|
|
|
|
SaveWindowLocation();
|
2019-04-05 05:59:06 +03:00
|
|
|
return;
|
2008-09-03 17:35:06 +04:00
|
|
|
}
|
2019-04-05 05:59:06 +03:00
|
|
|
event.preventDefault();
|
2008-09-03 17:35:06 +04:00
|
|
|
}
|