User Guide
A simple Command example 143
Next, changeCase() calls getSelectedNode() to retrieve the node that contains the selected
text. It also retrieves any child nodes (
theSelNode.childNodes) in case the selection is a tag that
contains text, such as
<b>text</b>:
// Get the node containing the selection
var theSelNode = theDOM.getSelectedNode();
// Get the children of the selected node
var theChildren = theSelNode.childNodes;
If there are child nodes (hasChildNodes() returns the value true), the command loops through
the children looking for a text node. If one is found, the text (
theChildren[i].data) is stored
in
selText, and the offsets of the text node are stored in theSel. The code looks like the
following example:
var i = 0;
if(theSelNode.hasChildNodes()){
while (i < theChildren.length){
if(theChildren[i].nodeType == Node.TEXT_NODE){
var selText = theChildren[i].data;
var theSel = theDOM.nodeToOffsets(theChildren[0]);
break;
}
++i;
}
}
If there are no child nodes, the command calls getSelection() and stores the beginning and
ending offsets of the selection in
theSel. It then extracts the string between those two offsets and
stores it in
selText. The code looks like the following example:
else {
// Get the offsets of the selection
var theSel = theDOM.getSelection();
// Extract the selection
var selText = theWholeDoc.substring(theSel[0],theSel[1]);
}
The following code checks the uorl variable to determine whether the user selected uppercase:
if(uorl == 'u'){
If so, the following code writes the HTML code back to the document in sections: first, the
beginning of the document to the beginning of the selection; then the selected text, converting
it to uppercase (
selText.toUppercase()); and last, the end of the selected text to the end of
the document.
theDocEl.outerHTML = theWholeDoc.substring(0,theSel[0]) +
selText.toUpperCase() + theWholeDoc.substring(theSel[1]);
}
If the user selects lowercase, the function performs the same operation but calls
selText.toLowerCase(), to convert the selected text to lowercase. The code looks like the
following example:
else {
theDocEl.outerHTML = theWholeDoc.substring(0,theSel[0]) +
selText.toLowerCase() + theWholeDoc.substring(theSel[1]);
}