15.02.2013 Views

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

JavaScript Examples Bible - UserWorks Technologies

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

308<br />

<strong>JavaScript</strong> <strong>Examples</strong> <strong>Bible</strong>: The Essential Companion to <strong>JavaScript</strong> <strong>Bible</strong><br />

Listing 19-11 includes an undo buffer that accurately undoes only the changes made<br />

in the initial replacement actions.<br />

The undo buffer stores its data in two global variables. The first,<br />

origSearchString, is simply the string used to perform the original search. This<br />

variable is the string that has to be put back in the places where it had been<br />

replaced. The second global variable is an array that stores TextRange bookmarks<br />

(see getBookmark() later in this chapter). These references are string values that<br />

don’t mean much to humans, but the browser can use them to recreate a range<br />

with its desired start and end points. Values for both the global search string and<br />

bookmark specifications are stored in calls to the pushUndoNew() method each<br />

time text is replaced.<br />

A perhaps unexpected action of setting the text property of a text range is that<br />

the start and end points collapse to the end of the new text. Because the stored<br />

bookmark must include the replaced text as part of its specification, the start point<br />

of the current range must be adjusted back to the beginning of the replacement text<br />

before the bookmark can be saved. Thus, the pushUndoNew() function receives the<br />

replacement text string so that the moveStart() method can be adjusted by the<br />

number of characters matching the length of the replacement string.<br />

After all of the bookmarks are stored in the array, the undo action can do its job<br />

in a rather simple for loop inside the undoReplace() function. After verifying that<br />

the undo buffer has data stored in it, the function loops through the array of bookmarks<br />

and replaces the bookmarked text with the old string. The benefit of using<br />

the bookmarks rather than using the replacement function again is that only those<br />

ranges originally affected by the search-and-replace operation are touched in the<br />

undo operation. For example, in this document if you replace a case-sensitive<br />

“states” with “States” two replacements are performed. At that point, however, the<br />

document has four instances of “States,” two of which existed before. Redoing the<br />

replacement function by inverting the search-and-replace strings would convert all<br />

four back to the lowercase version — not the desired effect.<br />

getBookmark()<br />

TextRange.getBookmark()<br />

NN2 NN3 NN4 NN6 IE3/J1 IE3/J2 IE4 IE5 IE5.5<br />

Compatibility ✓ ✓ ✓<br />

Example<br />

Listing 19-11 earlier in this chapter shows how the getBookmark() method is<br />

used to preserve specifications for text ranges so that they can be called upon<br />

again to be used to undo changes made to the text range. The getBookmark()<br />

method is used to save the snapshots, while the moveToBookmark() method is<br />

used during the undo process.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!