XHTMLBasic.js
Summary
The XHTMLBasic modules includes commands derived from the MozileCommand object which allow for editing of HTML and XHTML documents. ALso included is a revision of the Mozile.initializeToolbar() function, which creates a number of commands commonly used in XHTML editing.
This module does not include support for validation of XHTML documents. Instead it makes some assumptions about XHTML, which lead to a more light-weight implementation. Elements are distinguished into block and non-block (inline) elements, based on their CSS "display" property. Most commands function differently on blocks than they do on inline elements. While the distinction could allow the creation of invalid XHTML documents, in most cases the commands will preserve the validity of the XHTML.
Module options:
- semantic=[true|false] Default is "true". When this option is set to "true", "strong" and "em" will be used for the bold and italic commands. When it is false, "b" and "i" will be used instead.
Version: 0.7.0
Author: James A. Overton
Mozile.prototype.initializeToolbar = function() {
var f = new Array();
f["File"] = "XHTMLBasic/XHTMLBasic.js";
f["Function"] = "Mozile.initializeToolbar()";
this.debug(f,1,"Initializing toolbar");
var location;
if(this.moduleList["XHTMLBasic"] && this.moduleList["XHTMLBasic"]["semantic"] && this.moduleList["XHTMLBasic"]["semantic"] == "false") {
this.rootCommandList.createCommand("MozileWrapCommand: id=Mozile-XHTMLBasic-Strong, tag=b, mode=toggle, label=Bold, tooltip='Make text bold', accelerator='Command-B', image='"+this.root+"images/bold.png'");
this.rootCommandList.createCommand("MozileWrapCommand: id=Mozile-XHTMLBasic-Emphasis, tag=i, mode=toggle, label=Italic, tooltip='Italicize text', accelerator='Command-I', image='"+this.root+"images/italic.png'");
}
else {
this.rootCommandList.createCommand("MozileWrapCommand: id=Mozile-XHTMLBasic-Strong, tag=strong, mode=toggle, label=Strong, tooltip='Make text strong', accelerator='Command-B', image='"+this.root+"images/bold.png'");
this.rootCommandList.createCommand("MozileWrapCommand: id=Mozile-XHTMLBasic-Emphasis, tag=em, mode=toggle, label=Emphasis, tooltip='Emphasize text', accelerator='Command-I', image='"+this.root+"images/italic.png'");
}
this.rootCommandList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-Underline, style='text-decoration: underline', mode=toggle, label=Underline, tooltip='Underline text', accelerator='Command-U', image='"+this.root+"images/underline.png'");
this.rootCommandList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-Strikethrough, style='text-decoration: line-through', mode=toggle, label=Strikethrough, tooltip='Strikethough text',image='"+this.root+"images/strikethrough.png'");
this.rootCommandList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-Superscript, style='vertical-align: super; font-size: 80%', mode=toggle, label=Superscript, tooltip='Raise text',image='"+this.root+"images/superscript.png'");
this.rootCommandList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-Subscript, style='vertical-align: sub; font-size: 80%', mode=toggle, label=Subscript, tooltip='Lower text',image='"+this.root+"images/subscript.png'");
this.rootCommandList.createCommand("MozileUnformatCommand: id=Mozile-XHTMLBasic-Unformat, label=Unformat, tooltip='Remove formatting from selection', image='"+this.root+"images/unlink.png'");
var fontList = this.rootCommandList.createCommand("MozileCommandList: id=Mozile-XHTMLBasic-FontList, label=Font, image='"+this.root+"images/fonts.png'");
fontList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-Serif, style='font-family: serif', mode=toggle, label=Serif, tooltip='Use serif font'");
fontList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-Sans-Serif, style='font-family: sans-serif', mode=toggle, label=Sans-Serif, tooltip='Use sans-serif font'");
fontList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-Monospace, style='font-family: monospace', mode=toggle, label=Monospace, tooltip='Use monospace font'");
var sizeList = this.rootCommandList.createCommand("MozileCommandList: id=Mozile-XHTMLBasic-SizeList, label=Size, image='"+this.root+"images/size.png'");
sizeList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-Smaller, style='font-size: smaller', mode=inline, label=Smaller, tooltip='Make text smaller than surrounding text', accelerator='Command--'");
sizeList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-XX-Small, style='font-size: xx-small', mode=toggle, label=XX-Small, tooltip='Make text extremely small'");
sizeList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-X-Small, style='font-size: x-small', mode=toggle, label=X-Small, tooltip='Make text very small'");
sizeList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-Small, style='font-size: small', mode=toggle, label=Small, tooltip='Make text small'");
sizeList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-Medium, style='font-size: medium', mode=toggle, label=Medium, tooltip='Make text medium sized'");
sizeList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-Large, style='font-size: large', mode=toggle, label=Large, tooltip='Make text large'");
sizeList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-X-Large, style='font-size: x-large', mode=toggle, label=X-Large, tooltip='Make text very large'");
sizeList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-XX-Large, style='font-size: xx-large', mode=toggle, label=XX-Large, tooltip='Make text extremely large'");
sizeList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-Larger, style='font-size: larger', mode=inline, label=Larger, tooltip='Make text larger than surrounding text', accelerator='Command-+'");
var formatList = this.rootCommandList.createCommand("MozileCommandList: id=Mozile-XHTMLBasic-FormatList, label='Format', image='"+this.root+"images/format.png'");
formatList.createCommand("MozileBlockSetCommand: id=Mozile-XHTMLBasic-Heading1, tag=h1, label='Heading1', tooltip='Level 1 Heading', accesskey=1");
formatList.createCommand("MozileBlockSetCommand: id=Mozile-XHTMLBasic-Heading2, tag=h2, label='Heading2', tooltip='Level 2 Heading', accesskey=2");
formatList.createCommand("MozileBlockSetCommand: id=Mozile-XHTMLBasic-Heading3, tag=h3, label='Heading3', tooltip='Level 3 Heading', accesskey=3");
formatList.createCommand("MozileBlockSetCommand: id=Mozile-XHTMLBasic-Heading4, tag=h4, label='Heading4', tooltip='Level 4 Heading', accesskey=4");
formatList.createCommand("MozileBlockSetCommand: id=Mozile-XHTMLBasic-Heading5, tag=h5, label='Heading5', tooltip='Level 5 Heading', accesskey=5");
formatList.createCommand("MozileBlockSetCommand: id=Mozile-XHTMLBasic-Heading6, tag=h6, label='Heading6', tooltip='Level 6 Heading', accesskey=6");
formatList.createCommand("MozileBlockSetCommand: id=Mozile-XHTMLBasic-Paragraph, tag=p, label='Paragraph', tooltip='Paragraph', accesskey=P");
formatList.createCommand("MozileBlockSetCommand: id=Mozile-XHTMLBasic-ListItem, tag=li, label='List Item', tooltip='List Item', accesskey=L");
var justifyList = this.rootCommandList.createCommand("MozileCommandList: id=Mozile-XHTMLBasic-JustifyList, label=Justify, image='"+this.root+"images/justify-left.png'");
justifyList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-Justify-Left, style='text-align: left', mode=toggleBlock, label=Left, tooltip='Justify text left'");
justifyList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-Justify-Right, style='text-align: right', mode=toggleBlock, label=Right, tooltip='Justify text right'");
justifyList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-Justify-Conter, style='text-align: center', mode=toggleBlock, label=Center, tooltip='Center text'");
justifyList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-Justify-Full, style='text-align: justify', mode=toggleBlock, label=Full, tooltip='Justify left and right'");
var textColorList = this.rootCommandList.createCommand("MozileCommandList: id=Mozile-XHTMLBasic-TextColorList, label='Text Color', image='"+this.root+"images/text-color.png'");
textColorList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-Red, style='color: red', mode=toggle, label=Red, tooltip='Make text red' ");
textColorList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-Blue, style='color: blue', mode=toggle, label=Blue, tooltip='Make text blue' ");
textColorList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-Green, style='color: green', mode=toggle, label=Green, tooltip='Make text green' ");
textColorList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-Yellow, style='color: yellow', mode=toggle, label=Yellow, tooltip='Make text yellow' ");
textColorList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-Orange, style='color: orange', mode=toggle, label=Orange, tooltip='Make text orange' ");
textColorList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-Purple, style='color: purple', mode=toggle, label=Purple, tooltip='Make text purple' ");
textColorList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-White, style='color: white', mode=toggle, label=White, tooltip='Make text white' ");
textColorList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-Gray, style='color: gray', mode=toggle, label=Gray, tooltip='Make text gray' ");
textColorList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-Black, style='color: black', mode=toggle, label=Black, tooltip='Make text black' ");
var backgroundColorList = this.rootCommandList.createCommand("MozileCommandList: id=Mozile-XHTMLBasic-BackgroundColorList, label='Background Color', image='"+this.root+"images/background-color.png'");
backgroundColorList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-BGRed, style='background-color: red', mode=toggle, label=Red, tooltip='Make background red' ");
backgroundColorList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-BGBlue, style='background-color: blue', mode=toggle, label=Blue, tooltip='Make background blue' ");
backgroundColorList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-BGGreen, style='background-color: green', mode=toggle, label=Green, tooltip='Make background green' ");
backgroundColorList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-BGYellow, style='background-color: yellow', mode=toggle, label=Yellow, tooltip='Make background yellow' ");
backgroundColorList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-BGOrange, style='background-color: orange', mode=toggle, label=Orange, tooltip='Make background orange' ");
backgroundColorList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-BGPurple, style='cobackground-colorlor: purple', mode=toggle, label=Purple, tooltip='Make background purple' ");
backgroundColorList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-BGWhite, style='background-color: white', mode=toggle, label=White, tooltip='Make background white' ");
backgroundColorList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-BGGray, style='background-color: gray', mode=toggle, label=Gray, tooltip='Make background gray' ");
backgroundColorList.createCommand("MozileStyleCommand: id=Mozile-XHTMLBasic-BGBlack, style='background-color: black', mode=toggle, label=Black, tooltip='Make background black' ");
var objectList = this.rootCommandList.createCommand("MozileCommandList: id=Mozile-XHTMLBasic-ObjectList, label='Insert Objects', image='"+this.root+"images/image.png'");
var hr = objectList.createCommand("MozileInsertCommand: id=Mozile-XHTMLBasic-HorizontalRule, label='Horizontal Rule', tooltip='Insert a horizontal rule' ");
hr.createNode = function() {
return document.createElement("hr");
}
var link = objectList.createCommand("MozileInsertCommand: id=Mozile-XHTMLBasic-Link, label='Link', tooltip='Insert a hyperlink' ");
link.createNode = function() {
var node = document.createElement("a");
var href = prompt("What is the URL for the hyperlink?");
if(href) {
node.setAttribute("href", href);
node.appendChild(window.getSelection().getRangeAt(0).extractContents());
return node;
}
else {
return false;
}
}
var image = objectList.createCommand("MozileInsertCommand: id=Mozile-XHTMLBasic-Image, label='Image', tooltip='Insert an image' ");
image.createNode = function() {
var node = document.createElement("img");
var src = prompt("What is the URL for the image?");
if(src) {
node.setAttribute("src", src);
return node;
}
else {
return false;
}
}
var ul = objectList.createCommand("MozileCommand: id=Mozile-XHTMLBasic-UnorderedList, label='Unordered List', tooltip='Insert an unordered list' ");
ul.command = function(event) {
var ul = document.createElement("ul");
var li = document.createElement("li");
var textNode = document.createTextNode("");
li.appendChild(textNode);
ul.appendChild(li);
var selection = window.getSelection();
var range = selection.getRangeAt(0).cloneRange();
range.insertNode(ul);
range.selectNode(textNode);
range.collapse(true);
selection.removeAllRanges();
selection.addRange(range);
return true;
}
var ol = objectList.createCommand("MozileCommand: id=Mozile-XHTMLBasic-OrderedList, label='Ordered List', tooltip='Insert an ordered list' ");
ol.command = function(event) {
var ol = document.createElement("ol");
var li = document.createElement("li");
var textNode = document.createTextNode("");
li.appendChild(textNode);
ol.appendChild(li);
var selection = window.getSelection();
var range = selection.getRangeAt(0).cloneRange();
range.insertNode(ol);
range.selectNode(textNode);
range.collapse(true);
selection.removeAllRanges();
selection.addRange(range);
return true;
}
var title = objectList.createCommand("MozileInsertCommand: id=Mozile-XHTMLBasic-Title, label='Change Title', tooltip='Change the document title' ");
title.command = function(event) {
var title = document.getElementsByTagName("title")[0];
var value = prompt("What should the title be?", document.title);
if(title && value) {
while(title.childNodes.length) {
title.removeChild(title.firstChild);
}
title.appendChild(document.createTextNode(value));
document.title = value;
return true;
}
else {
return false;
}
}
var style = objectList.createCommand("MozileInsertCommand: id=Mozile-XHTMLBasic-Style, label='Change Style', tooltip='Change the element style', accelerator='Command-Alt-S' ");
style.command = function(event) {
var selection = window.getSelection();
var range = selection.getRangeAt(0);
var container = range.commonAncestorContainer;
if(container.nodeType==3) container = container.parentNode;
var value = prompt("What should the style for the "+ container.nodeName +" element be?", container.getAttribute("style"));
if(value==null) {
return false;
}
else {
container.setAttribute("style", value);
return true;
}
}
var attribute = objectList.createCommand("MozileInsertCommand: id=Mozile-XHTMLBasic-Attribute, label='Change Attribute', tooltip='Change an attribute of the element', accelerator='Command-Shift-A' ");
attribute.command = function(event) {
var selection = window.getSelection();
var range = selection.getRangeAt(0);
var container = range.commonAncestorContainer;
if(container.nodeType==3) container = container.parentNode;
var attribute = prompt("What attribute of the "+container.nodeName+" element should be changed be?", "");
if(attribute==null) return false;
var value = prompt("What should the value of the "+attribute+" attribute be?", container.getAttribute(attribute));
if(value==null) return false;
container.setAttribute(attribute, value);
return true;
}
this.rootCommandList.createBox();
this.toolbar.appendChild(this.rootCommandList.box);
return true;
}
MozileBlockSetCommand.prototype = new MozileCommand();
MozileBlockSetCommand.prototype.constructor = MozileBlockSetCommand;
MozileBlockSetCommand.superclass = MozileCommand.prototype;
function MozileBlockSetCommand() {
if(arguments.length > 0){
var configArray = arguments[0];
this.init(configArray);
}
else return true;
this.tag = null;
if(configArray['tag'] && typeof(configArray['tag'])!="null") {
this.tag = configArray['tag'];
}
else {
return "Error initializing MozileBlockSetCommand object -- invalid tag provided: "+configArray['tag'];
}
return true;
}
MozileBlockSetCommand.prototype.createMenuitem = function() {
var f = new Array();
f["File"] = "XHTMLBasic/XHTMLBasic.js";
f["Function"] = "MozileBlockSetCommand.createMenuitem()";
this.debug(f,1,"Creating menuitem");
var menuitem = document.createElementNS(XULNS, "menuitem");
menuitem.setAttribute("id", this.id+"-Menuitem");
menuitem.setAttribute("class", "mozileMenuitem");
menuitem.setAttribute("type", "checkbox");
menuitem.setAttribute("label", this.label);
if(this.tooltip) menuitem.setAttribute("tooltiptext", this.tooltip);
if(this.accesskey) menuitem.setAttribute("accesskey", this.accesskey);
this.menuitem = menuitem;
return menuitem;
}
MozileBlockSetCommand.prototype.isActive = function() {
var f = new Array();
f["File"] = "XHTMLBasic/XHTMLBasic.js";
f["Function"] = "MozileBlockSetCommand.isActive()";
this.debug(f,0,"Checking to see if this command is active");
var selection = window.getSelection();
var range = selection.getRangeAt(0);
var node = range.commonAncestorContainer;
var parentBlock = node.parentBlock;
if(parentBlock.nodeName.toLowerCase()==this.tag) {
return true;
}
else {
return false;
}
}
MozileBlockSetCommand.prototype.command = function(event) {
var f = new Array();
f["File"] = "XHTMLBasic/XHTMLBasic.js";
f["Function"] = "MozileBlockSetCommand.command()";
this.debug(f,1,"Executing command "+event);
var selection = window.getSelection();
var range = selection.getRangeAt(0).cloneRange();
if(!selection.isCollapsed) {
var element = document.createElement(this.tag);
element.appendChild(range.extractContents());
range.insertNode(element);
return true;
}
var node = range.commonAncestorContainer;
node = node.parentBlock;
if(node.parentNode) {
var anchorNode = selection.anchorNode;
var anchorOffset = selection.anchorOffset;
var focusNode = selection.focusNode;
var focusOffset = selection.focusOffset;
range.selectNodeContents(node);
var contents = range.extractContents();
var newNode = document.createElement(this.tag);
newNode.appendChild(contents);
node.parentNode.replaceChild(newNode, node);
range = document.createRange();
try {
range.setStart(newNode.firstChild, focusOffset);
} catch(e) {
range.setStart(newNode.firstChild, 0);
}
selection.removeAllRanges();
selection.addRange(range);
}
return true;
}
MozileUnformatCommand.prototype = new MozileCommand();
MozileUnformatCommand.prototype.constructor = MozileUnformatCommand;
MozileUnformatCommand.superclass = MozileCommand.prototype;
function MozileUnformatCommand() {
if(arguments.length > 0){
var configArray = arguments[0];
this.init(configArray);
}
else return true;
return true;
}
MozileUnformatCommand.prototype.command = function(event) {
var f = new Array();
f["File"] = "XHTMLBasic/XHTMLBasic.js";
f["Function"] = "MozileUnformatCommand.command()";
this.debug(f,1,"Executing command "+event);
var selection = window.getSelection();
var range = selection.getRangeAt(0).cloneRange();
var contents,text;
if(!selection.isCollapsed) {
contents = range.toString();
text = document.createTextNode(contents);
range.deleteContents();
range.insertNode(text);
range.selectNode(text);
selection.removeAllRanges();
selection.addRange(range);
}
else {
var focusOffset = selection.focusOffset;
var node = range.commonAncestorContainer;
if(node.nodeType!=1) node = node.parentNode;
range.selectNodeContents(node);
contents = range.extractContents();
var firstChild = contents.firstChild;
range.selectNode(node);
range.insertNode(contents);
try {
range.setStart(firstChild, focusOffset);
}
catch(e) {
range.setStart(firstChild,0);
}
range.collapse(true);
selection.removeAllRanges()
selection.addRange(range);
}
return true;
}
MozileWrapCommand.prototype = new MozileCommand();
MozileWrapCommand.prototype.constructor = MozileWrapCommand;
MozileWrapCommand.superclass = MozileCommand.prototype;
function MozileWrapCommand() {
if(arguments.length > 0){
var configArray = arguments[0];
this.init(configArray);
}
else return true;
this.mode = null;
this.tag = null;
if(configArray['mode'] && typeof(configArray['mode'])!="null") {
this.mode = configArray['mode'];
}
else {
return "Error initializing MozileWrapCommand object -- invalid mode provided: "+configArray['mode'];
}
if(configArray['tag'] && typeof(configArray['tag'])!="null") {
this.tag = configArray['tag'];
}
else {
return "Error initializing MozileWrapCommand object -- invalid tag provided: "+configArray['tag'];
}
return true;
}
MozileWrapCommand.prototype.createMenuitem = function() {
var f = new Array()
f["File"] = "XHTMLBasic/XHTMLBasic.js";
f["Function"] = "MozileWrapCommand.createMenuitem()";
this.debug(f,1,"Creating menuitem");
var menuitem = document.createElementNS(XULNS, "menuitem");
menuitem.setAttribute("id", this.id+"-Menuitem");
menuitem.setAttribute("class", "mozileMenuitem");
menuitem.setAttribute("type", "checkbox");
menuitem.setAttribute("label", this.label);
if(this.tooltip) menuitem.setAttribute("tooltiptext", this.tooltip);
if(this.accesskey) menuitem.setAttribute("accesskey", this.accesskey);
this.menuitem = menuitem;
return menuitem;
}
MozileWrapCommand.prototype.isActive = function() {
var f = new Array();
f["File"] = "XHTMLBasic/XHTMLBasic.js";
f["Function"] = "MozileWrapCommand.isActive()";
this.debug(f,0,"Checking to see if this command is active");
var selection = window.getSelection();
var range = selection.getRangeAt(0);
var node = range.commonAncestorContainer;
var flag = false;
while(node) {
if(node.nodeName.toLowerCase()==this.tag) {
flag = true;
break;
}
node = node.parentNode;
}
return flag;
}
MozileWrapCommand.prototype.command = function(event) {
var f = new Array();
f["File"] = "XHTMLBasic/XHTMLBasic.js";
f["Function"] = "MozileWrapCommand.command()";
this.debug(f,1,"Executing command "+event);
var selection = window.getSelection();
var range = selection.getRangeAt(0).cloneRange();
var container, element, text, oldRange, blocks, firstElement, lastElement, i, flag;
switch (this.mode) { // Inline Case
case "inline":
if(selection.isCollapsed) {
element = document.createElement(this.tag);
text = document.createTextNode("");
element.appendChild(text);
range.insertNode(element);
range.selectNode(text);
range.collapse(true);
selection.removeAllRanges();
selection.addRange(range);
}
else {
element = document.createElement(this.tag);
range = this.wrapRange(range, element)
selection.removeAllRanges();
selection.addRange(range);
}
return true;
break;
case "toggle":
if(this.isActive()) {
container = range.commonAncestorContainer;
flag = false;
while(container) {
if( container.nodeName.toLowerCase() == this.tag ) {
flag = true;
break;
}
container = container.parentNode;
}
element = document.createElement(this.tag);
range = this.unwrapRange(container, range, element);
selection.removeAllRanges();
selection.addRange(range);
}
else {
if(selection.isCollapsed) {
element = document.createElement(this.tag);
text = document.createTextNode("");
element.appendChild(text);
range.insertNode(element);
range.selectNode(text);
range.collapse(true);
selection.removeAllRanges();
selection.addRange(range);
}
else {
element = document.createElement(this.tag);
range = this.wrapRange(range, element);
selection.removeAllRanges();
selection.addRange(range);
}
}
return true;
break;
default:
element = document.createElement(this.tag);
element.appendChild(range.extractContents());
range.insertNode(element);
if(selection.isCollapsed) {
text = document.createTextNode("");
element.appendChild(text);
range.selectNodeContents(text);
selection.removeAllRanges();
selection.addRange(range);
selection.collapseToEnd();
}
else {
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
}
return true;
break;
}
return true;
}
MozileWrapCommand.prototype.wrapBlock = function(block, element) {
var f = new Array();
f["File"] = "XHTMLBasic/XHTMLBasic.js";
f["Function"] = "MozileWrapCommand.wrapBlock()";
this.debug(f,1,"Wrapping block "+ block +" with "+ element);
var range = document.createRange();
if(block.nodeType==1) {
range.selectNodeContents(block);
}
else {
range.selectNode(block);
}
element.appendChild(range.extractContents());
range.insertNode(element);
return element;
}
MozileWrapCommand.prototype.wrapInline = function(range, element) {
var f = new Array();
f["File"] = "XHTMLBasic/XHTMLBasic.js";
f["Function"] = "MozileWrapCommand.wrapInline()";
this.debug(f,1,"Wrapping inline "+ range +" with "+ element);
element.appendChild(range.extractContents());
range.insertNode(element);
return element;
}
MozileWrapCommand.prototype.wrapRange = function(range, elementTemplate) {
var f = new Array();
f["File"] = "XHTMLBasic/XHTMLBasic.js";
f["Function"] = "MozileWrapCommand.wrapRange()";
this.debug(f,1,"Wrapping range "+ range +" with "+ elementTemplate);
var blocks = range.getBlocks();
var element, firstElement, lastElement;
if(blocks.length<2) {
element = elementTemplate.cloneNode(true);
element = this.wrapInline(range, element);
range.selectNodeContents(element);
}
else {
var oldRange = range.cloneRange();
range = oldRange.cloneRange();
if(blocks[0].nodeType==1) {
range.setEnd(blocks[0].lastChild, blocks[0].lastChild.textContent.length);
}
else {
range.setEnd(blocks[0], blocks[0].textContent.length);
}
element = elementTemplate.cloneNode(true);
firstElement = this.wrapInline(range, element);
for(var i=1; i < blocks.length-1; i++) {
element = elementTemplate.cloneNode(true);
this.wrapBlock(blocks[i], element);
}
range = oldRange.cloneRange();
range.setStart(blocks[blocks.length-1], 0);
element = elementTemplate.cloneNode(true);
lastElement = this.wrapInline(range, element);
range = document.createRange();
range.setStart(firstElement.firstChild, 0);
range.setEnd(lastElement.lastChild, lastElement.lastChild.textContent.length);
}
return range;
}
MozileWrapCommand.prototype.unwrapRange = function(container, range, elementTemplate) {
var f = new Array();
f["File"] = "XHTMLBasic/XHTMLBasic.js";
f["Function"] = "MozileWrapCommand.unwrapRange()";
this.debug(f,1,"Unwrapping range "+ range +" in "+ container +" with "+ element);
var collapsed = range.collapsed;
var oldRange = range.cloneRange();
var element, firstElement, lastElement;
range.selectNodeContents(container);
range.setStart(oldRange.endContainer, oldRange.endOffset);
element = elementTemplate.cloneNode(true);
this.wrapInline(range, element);
range.selectNodeContents(container);
range.setEnd(oldRange.startContainer, oldRange.startOffset);
element = elementTemplate.cloneNode(true);
this.wrapInline(range, element);
range.selectNodeContents(container);
var contents = range.extractContents();
firstElement = contents.firstChild;
lastElement = contents.lastChild;
container.parentNode.replaceChild(contents, container);
if(collapsed) {
var text = document.createTextNode("");
text = lastElement.parentNode.insertBefore(text, lastElement);
range.selectNodeContents(text);
range.collapse(true);
}
else {
range = document.createRange();
range.setStartAfter(firstElement);
range.setEndBefore(lastElement);
}
return range;
}
MozileStyleCommand.prototype = new MozileWrapCommand();
MozileStyleCommand.prototype.constructor = MozileStyleCommand;
MozileStyleCommand.superclass = MozileWrapCommand.prototype;
function MozileStyleCommand() {
if(arguments.length > 0){
var configArray = arguments[0];
this.init(configArray);
}
else return true;
this.tag = "span";
this.attribute = "style";
this.mode = null;
this.style = null;
if(configArray['mode'] && typeof(configArray['mode'])!="null") {
this.mode = configArray['mode'];
}
else {
return "Error initializing MozileStyleCommand object -- invalid mode provided: "+configArray['mode'];
}
if(configArray['style'] && typeof(configArray['style'])!="null") {
this.style = configArray['style'];
}
else {
return "Error initializing MozileStyleCommand object -- invalid style provided: "+configArray['style'];
}
return true;
}
MozileStyleCommand.prototype.isActive = function() {
var f = new Array();
f["File"] = "XHTMLBasic/XHTMLBasic.js";
f["Function"] = "MozileStyleCommand.isActive()";
this.debug(f,0,"Checking to see if this command is active");
var selection = window.getSelection();
var range = selection.getRangeAt(0);
var node = range.commonAncestorContainer;
var flag = false;
while(node) {
if(node.nodeType==1 && node.getAttribute(this.attribute) && node.getAttribute(this.attribute).match("(^|\W)"+this.style) ) {
flag = true;
break;
}
node = node.parentNode;
}
return flag;
}
MozileStyleCommand.prototype.command = function(event) {
var f = new Array();
f["File"] = "XHTMLBasic/XHTMLBasic.js";
f["Function"] = "MozileStyleCommand.command()";
this.debug(f,1,"Executing command "+event);
var selection = window.getSelection();
var range = selection.getRangeAt(0).cloneRange();
var container, element, text, style, oldRange, blocks, firstElement, lastElement, i, flag;
switch (this.mode) {
case "inline":
if(selection.isCollapsed) {
element = document.createElement(this.tag);
element.setAttribute("style", this.style);
text = document.createTextNode("");
element.appendChild(text);
range.insertNode(element);
range.selectNode(text);
range.collapse(true);
selection.removeAllRanges();
selection.addRange(range);
}
else {
element = document.createElement(this.tag);
element.setAttribute("style", this.style);
range = this.wrapRange(range, element)
selection.removeAllRanges();
selection.addRange(range);
}
return true;
break;
case "toggle":
if(this.isActive()) {
container = range.commonAncestorContainer;
while(container) {
if(container.nodeType==1 && container.getAttribute("style") && container.getAttribute("style").match(this.style) ) {
break;
}
container = container.parentNode;
}
element = document.createElement(this.tag);
element.setAttribute("style", this.style);
range = this.unwrapRange(container, range, element);
selection.removeAllRanges();
selection.addRange(range);
}
else {
if(selection.isCollapsed) {
element = document.createElement(this.tag);
element.setAttribute("style", this.style);
text = document.createTextNode("");
element.appendChild(text);
range.insertNode(element);
range.selectNode(text);
range.collapse(true);
selection.removeAllRanges();
selection.addRange(range);
}
else {
element = document.createElement(this.tag);
element.setAttribute("style", this.style);
range = this.wrapRange(range, element);
selection.removeAllRanges();
selection.addRange(range);
}
}
return true;
break;
case "toggleBlock":
if(this.isActive()) {
container = range.commonAncestorContainer;
while(container) {
if(container.nodeType==1 && container.getAttribute(this.attribute) && container.getAttribute(this.attribute).match(this.style) ) {
break;
}
container = container.parentNode;
}
element = container;
style = element.getAttribute(this.attribute);
style = style.replace(this.style,"");
element.setAttribute(this.attribute, style);
}
else {
container = range.commonAncestorContainer;
element = container.parentBlock;
style = element.getAttribute(this.attribute);
if(style && style != "") style = style + " "+ this.style;
else style = this.style;
element.setAttribute(this.attribute, style);
}
return true;
break;
default:
container = range.commonAncestorNode;
element = container.parentBlock;
style = element.getAttribute(this.attribute);
if(style && style != "") style = style + " "+ this.style;
else style = this.style;
element.setAttribute(this.attribute, style);
return true;
break;
}
return true;
}
Range.prototype.getTextNodes = function() {
var container = this.commonAncestorContainer;
container.normalize();
var previousNode, startNode, finishNode;
if(this.startContainer.nodeType!=3) return false;
if(this.endContainer.nodeType!=3) return false;
finishNode = this.endContainer.splitText(this.endOffset);
startNode = this.startContainer.splitText(this.startOffset);
previousNode = startNode.previousSibling;
var treeWalker = document.createTreeWalker(container, NodeFilter.SHOW_TEXT, null, false);
while(treeWalker.currentNode!=treeWalker.lastNode && treeWalker.currentNode != startNode) {
treeWalker.nextNode();
}
var textNodes = new Array();
var currentNode = treeWalker.currentNode;
while(currentNode && currentNode != finishNode) {
if(currentNode.parentNode &&
currentNode.parentNode.nodeName.toLowerCase()!="a" &&
matchNonWS.test(currentNode.textContent) ) {
textNodes.push(currentNode);
}
currentNode = treeWalker.nextNode();
}
return textNodes;
}
Range.prototype.getBlocks = function() {
var blockNodes = new Array();
if(this.startContainer==this.endContainer || this.startContainer.parentNode == this.endContainer.parentNode) return blockNodes;
var container = this.commonAncestorContainer;
var treeWalker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT + NodeFilter.SHOW_TEXT, null, false);
var currentNode = treeWalker.currentNode;
var flag = false;
var textFlag = false;
var parentBlock;
while(currentNode) {
if(currentNode == this.startContainer) {
flag=true;
if(currentNode.nodeType==3 && this.startContainer.parentNode==container) {
blockNodes.push(currentNode);
}
else {
blockNodes.push(currentNode.parentBlock);
}
}
if(flag) {
if(currentNode.nodeType==1 &&
currentNode.nodeName.toLowerCase() != "ul" &&
currentNode.nodeName.toLowerCase() != "ol" &&
currentNode.isBlock() &&
!currentNode.isAncestorOf(this.endContainer) ) {
blockNodes.push(currentNode);
}
if(currentNode.nodeType==3 &&
currentNode!=this.startContainer &&
matchNonWS.test(currentNode.textContent) ) {
textFlag = true;
parentBlock = currentNode.parentBlock;
for(var i=0; i<blockNodes.length; i++) {
if(parentBlock == blockNodes[i]) {
textFlag = false;
break;
}
}
if(textFlag) {
blockNodes.push(currentNode);
}
}
}
if(currentNode == this.endContainer) break;
currentNode = treeWalker.nextNode();
}
return blockNodes;
}
MozileInsertCommand.prototype = new MozileCommand();
MozileInsertCommand.prototype.constructor = MozileInsertCommand;
MozileInsertCommand.superclass = MozileCommand.prototype;
function MozileInsertCommand() {
if(arguments.length > 0){
var configArray = arguments[0];
this.init(configArray);
}
else return true;
this.insert = null;
return true;
}
MozileInsertCommand.prototype.command = function(event) {
var f = new Array();
f["File"] = "XHTMLBasic/XHTMLBasic.js";
f["Function"] = "MozileInsertCommand.command()";
this.debug(f,1,"Executing command "+event);
var node = this.createNode();
if(node) {
var range = window.getSelection().getRangeAt(0);
range.deleteContents();
range.insertNode(node);
return true;
}
else {
this.debug(f,2,"Nothing to insert!");
return false;
}
}
mozile.registerModule("XHTMLBasic","0.7.0");
Documentation generated by
JSDoc on Fri Feb 3 19:22:12 2006