/* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * The contents of this file are subject to the Mozilla Public License Version * 1.1 (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" basis, * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License * for the specific language governing rights and limitations under the * License. * * The Original Code is Mozilla Communicator client code, released * March 31, 1998. * * The Initial Developer of the Original Code is * Netscape Communications Corporation. * Portions created by the Initial Developer are Copyright (C) 1998-1999 * the Initial Developer. All Rights Reserved. * * Contributor(s): * Charles Manske (cmanske@netscape.com) * * Alternatively, the contents of this file may be used under the terms of * either of the GNU General Public License Version 2 or later (the "GPL"), * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), * in which case the provisions of the GPL or the LGPL are applicable instead * of those above. If you wish to allow use of your version of this file only * under the terms of either the GPL or the LGPL, and not to allow others to * use your version of this file under the terms of the MPL, indicate your * decision by deleting the provisions above and replace them with the notice * and other provisions required by the GPL or the LGPL. If you do not delete * the provisions above, a recipient may use your version of this file under * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ var gIndex; var gCommaIndex = "0"; var gSpaceIndex = "1"; var gOtherIndex = "2"; // dialog initialization code function Startup() { if (!GetCurrentEditor()) { window.close(); return; } gDialog.sepRadioGroup = document.getElementById("SepRadioGroup"); gDialog.sepCharacterInput = document.getElementById("SepCharacterInput"); gDialog.deleteSepCharacter = document.getElementById("DeleteSepCharacter"); gDialog.collapseSpaces = document.getElementById("CollapseSpaces"); // We persist the user's separator character gDialog.sepCharacterInput.value = gDialog.sepRadioGroup.getAttribute("character"); gIndex = gDialog.sepRadioGroup.getAttribute("index"); switch (gIndex) { case gCommaIndex: default: gDialog.sepRadioGroup.selectedItem = document.getElementById("comma"); break; case gSpaceIndex: gDialog.sepRadioGroup.selectedItem = document.getElementById("space"); break; case gOtherIndex: gDialog.sepRadioGroup.selectedItem = document.getElementById("other"); break; } // Set initial enable state on character input and "collapse" checkbox SelectCharacter(gIndex); SetWindowLocation(); } function InputSepCharacter() { var str = gDialog.sepCharacterInput.value; // Limit input to 1 character if (str.length > 1) str = str.slice(0,1); // We can never allow tag or entity delimiters for separator character if (str == "<" || str == ">" || str == "&" || str == ";" || str == " ") str = ""; gDialog.sepCharacterInput.value = str; } function SelectCharacter(radioGroupIndex) { gIndex = radioGroupIndex; SetElementEnabledById("SepCharacterInput", gIndex == gOtherIndex); SetElementEnabledById("CollapseSpaces", gIndex == gSpaceIndex); } function onAccept() { var sepCharacter = ""; switch ( gIndex ) { case gCommaIndex: sepCharacter = ","; break; case gSpaceIndex: sepCharacter = " "; break; case gOtherIndex: sepCharacter = gDialog.sepCharacterInput.value.slice(0,1); break; } var editor = GetCurrentEditor(); var str; try { // 1 = OutputSelectionOnly, 1024 = OutputLFLineBreak str = editor.outputToString("text/html", 1+1024); } catch (e) {} if (!str) { SaveWindowLocation(); return true; } // Replace nbsp with spaces: str = str.replace(/\u00a0/g, " "); // Strip out
completely str = str.replace(/\s*<\/p>\s*/g, ""); // Trim whitespace adjacent to and
tags
// and replace
with
// (which will be replaced with below)
str = str.replace(/\s*
\s*|\s*
\s*/g, "
");
// Trim leading
s
str = str.replace(/^(
)+/, "");
// Trim trailing
s
str = str.replace(/(
)+$/, "");
// Reduce multiple internal
to just 1
// TODO: Maybe add a checkbox to let user decide
//str = str.replace(/(
)+/g, "
");
// Trim leading and trailing spaces
str = str.replace(/^\s+|\s+$/, "");
// Remove all tag contents so we don't replace
// separator character within tags
// Also converts lists to something usefull
var stack = [];
var start;
var end;
var searchStart = 0;
var listSeparator = "";
var listItemSeparator = "";
var endList = false;
do {
start = str.indexOf("<", searchStart);
if (start >= 0)
{
end = str.indexOf(">", start+1);
if (end > start)
{
var tagContent = TrimString(str.slice(start+1, end));
if ( /^ol|^ul|^dl/.test(tagContent) )
{
// Replace list tag with
to start new row
// at begining of second or greater list tag
str = str.slice(0, start) + listSeparator + str.slice(end+1);
if (listSeparator == "")
listSeparator = "
";
// Reset for list item separation into cells
listItemSeparator = "";
}
else if ( /^li|^dt|^dd/.test(tagContent) )
{
// Start a new row if this is first item after the ending the last list
if (endList)
listItemSeparator = "
";
// Start new cell at begining of second or greater list items
str = str.slice(0, start) + listItemSeparator + str.slice(end+1);
if (endList || listItemSeparator == "")
listItemSeparator = sepCharacter;
endList = false;
}
else
{
// Find end tags
endList = /^\/ol|^\/ul|^\/dl/.test(tagContent);
if ( endList || /^\/li|^\/dt|^\/dd/.test(tagContent) )
{
// Strip out tag
str = str.slice(0, start) + str.slice(end+1);
}
else
{
// Not a list-related tag: Store tag contents in an array
stack.push(tagContent);
// Keep the "<" and ">" while removing from source string
start++;
str = str.slice(0, start) + str.slice(end);
}
}
}
searchStart = start + 1;
}
} while (start >= 0);
// Replace separator characters with table cells
var replaceString;
if (gDialog.deleteSepCharacter.checked)
{
replaceString = "";
}
else
{
// Don't delete separator character,
// so include it at start of string to replace
replaceString = sepCharacter;
}
replaceString += "
" + str + " |