| 1 |
// Copyright 2007 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Rich text spell checker implementation. |
| 17 |
* |
| 18 |
* @author [email protected] (Emil A Eklund) |
| 19 |
* @see ../demos/richtextspellchecker.html |
| 20 |
*/ |
| 21 |
|
| 22 |
goog.provide('goog.ui.RichTextSpellChecker'); |
| 23 |
|
| 24 |
goog.require('goog.Timer'); |
| 25 |
goog.require('goog.asserts'); |
| 26 |
goog.require('goog.dom'); |
| 27 |
goog.require('goog.dom.NodeType'); |
| 28 |
goog.require('goog.dom.Range'); |
| 29 |
goog.require('goog.events.EventHandler'); |
| 30 |
goog.require('goog.events.EventType'); |
| 31 |
goog.require('goog.events.KeyCodes'); |
| 32 |
goog.require('goog.events.KeyHandler'); |
| 33 |
goog.require('goog.math.Coordinate'); |
| 34 |
goog.require('goog.spell.SpellCheck'); |
| 35 |
goog.require('goog.string.StringBuffer'); |
| 36 |
goog.require('goog.style'); |
| 37 |
goog.require('goog.ui.AbstractSpellChecker'); |
| 38 |
goog.require('goog.ui.Component'); |
| 39 |
goog.require('goog.ui.PopupMenu'); |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
/** |
| 44 |
* Rich text spell checker implementation. |
| 45 |
* |
| 46 |
* @param {goog.spell.SpellCheck} handler Instance of the SpellCheckHandler |
| 47 |
* support object to use. A single instance can be shared by multiple editor |
| 48 |
* components. |
| 49 |
* @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper. |
| 50 |
* @constructor |
| 51 |
* @extends {goog.ui.AbstractSpellChecker} |
| 52 |
*/ |
| 53 |
goog.ui.RichTextSpellChecker = function(handler, opt_domHelper) { |
| 54 |
goog.ui.AbstractSpellChecker.call(this, handler, opt_domHelper); |
| 55 |
|
| 56 |
/** |
| 57 |
* String buffer for use in reassembly of the original text. |
| 58 |
* @type {goog.string.StringBuffer} |
| 59 |
* @private |
| 60 |
*/ |
| 61 |
this.workBuffer_ = new goog.string.StringBuffer(); |
| 62 |
|
| 63 |
/** |
| 64 |
* Bound async function (to avoid rebinding it on every call). |
| 65 |
* @type {Function} |
| 66 |
* @private |
| 67 |
*/ |
| 68 |
this.boundContinueAsyncFn_ = goog.bind(this.continueAsync_, this); |
| 69 |
|
| 70 |
/** |
| 71 |
* Event handler for listening to events without leaking. |
| 72 |
* @private {!goog.events.EventHandler} |
| 73 |
*/ |
| 74 |
this.eventHandler_ = new goog.events.EventHandler(this); |
| 75 |
this.registerDisposable(this.eventHandler_); |
| 76 |
|
| 77 |
/** |
| 78 |
* The object handling keyboard events. |
| 79 |
* @private {!goog.events.KeyHandler} |
| 80 |
*/ |
| 81 |
this.keyHandler_ = new goog.events.KeyHandler(); |
| 82 |
this.registerDisposable(this.keyHandler_); |
| 83 |
}; |
| 84 |
goog.inherits(goog.ui.RichTextSpellChecker, goog.ui.AbstractSpellChecker); |
| 85 |
goog.tagUnsealableClass(goog.ui.RichTextSpellChecker); |
| 86 |
|
| 87 |
|
| 88 |
/** |
| 89 |
* Root node for rich editor. |
| 90 |
* @type {Node} |
| 91 |
* @private |
| 92 |
*/ |
| 93 |
goog.ui.RichTextSpellChecker.prototype.rootNode_; |
| 94 |
|
| 95 |
|
| 96 |
/** |
| 97 |
* Indicates whether the root node for the rich editor is an iframe. |
| 98 |
* @private {boolean} |
| 99 |
*/ |
| 100 |
goog.ui.RichTextSpellChecker.prototype.rootNodeIframe_ = false; |
| 101 |
|
| 102 |
|
| 103 |
/** |
| 104 |
* Current node where spell checker has interrupted to go to the next stack |
| 105 |
* frame. |
| 106 |
* @type {Node} |
| 107 |
* @private |
| 108 |
*/ |
| 109 |
goog.ui.RichTextSpellChecker.prototype.currentNode_; |
| 110 |
|
| 111 |
|
| 112 |
/** |
| 113 |
* Counter of inserted elements. Used in processing loop to attempt to preserve |
| 114 |
* existing nodes if they contain no misspellings. |
| 115 |
* @type {number} |
| 116 |
* @private |
| 117 |
*/ |
| 118 |
goog.ui.RichTextSpellChecker.prototype.elementsInserted_ = 0; |
| 119 |
|
| 120 |
|
| 121 |
/** |
| 122 |
* Number of words to scan to precharge the dictionary. |
| 123 |
* @type {number} |
| 124 |
* @private |
| 125 |
*/ |
| 126 |
goog.ui.RichTextSpellChecker.prototype.dictionaryPreScanSize_ = 1000; |
| 127 |
|
| 128 |
|
| 129 |
/** |
| 130 |
* Class name for word spans. |
| 131 |
* @type {string} |
| 132 |
*/ |
| 133 |
goog.ui.RichTextSpellChecker.prototype.wordClassName = |
| 134 |
goog.getCssName('goog-spellcheck-word'); |
| 135 |
|
| 136 |
|
| 137 |
/** |
| 138 |
* DomHelper to be used for interacting with the editable document/element. |
| 139 |
* |
| 140 |
* @type {goog.dom.DomHelper|undefined} |
| 141 |
* @private |
| 142 |
*/ |
| 143 |
goog.ui.RichTextSpellChecker.prototype.editorDom_; |
| 144 |
|
| 145 |
|
| 146 |
/** |
| 147 |
* Tag name portion of the marker for the text that does not need to be checked |
| 148 |
* for spelling. |
| 149 |
* |
| 150 |
* @type {Array<string|undefined>} |
| 151 |
*/ |
| 152 |
goog.ui.RichTextSpellChecker.prototype.excludeTags; |
| 153 |
|
| 154 |
|
| 155 |
/** |
| 156 |
* CSS Style text for invalid words. As it's set inside the rich edit iframe |
| 157 |
* classes defined in the parent document are not available, thus the style is |
| 158 |
* set inline. |
| 159 |
* @type {string} |
| 160 |
*/ |
| 161 |
goog.ui.RichTextSpellChecker.prototype.invalidWordCssText = |
| 162 |
'background: yellow;'; |
| 163 |
|
| 164 |
|
| 165 |
/** |
| 166 |
* Creates the initial DOM representation for the component. |
| 167 |
* |
| 168 |
* @throws {Error} Not supported. Use decorate. |
| 169 |
* @see #decorate |
| 170 |
* @override |
| 171 |
*/ |
| 172 |
goog.ui.RichTextSpellChecker.prototype.createDom = function() { |
| 173 |
throw new Error('Render not supported for goog.ui.RichTextSpellChecker.'); |
| 174 |
}; |
| 175 |
|
| 176 |
|
| 177 |
/** |
| 178 |
* Decorates the element for the UI component. |
| 179 |
* |
| 180 |
* @param {Element} element Element to decorate. |
| 181 |
* @override |
| 182 |
*/ |
| 183 |
goog.ui.RichTextSpellChecker.prototype.decorateInternal = function(element) { |
| 184 |
this.setElementInternal(element); |
| 185 |
this.rootNodeIframe_ = element.contentDocument || element.contentWindow; |
| 186 |
if (this.rootNodeIframe_) { |
| 187 |
var doc = element.contentDocument || element.contentWindow.document; |
| 188 |
this.rootNode_ = doc.body; |
| 189 |
this.editorDom_ = goog.dom.getDomHelper(doc); |
| 190 |
} else { |
| 191 |
this.rootNode_ = element; |
| 192 |
this.editorDom_ = goog.dom.getDomHelper(element); |
| 193 |
} |
| 194 |
}; |
| 195 |
|
| 196 |
|
| 197 |
/** @override */ |
| 198 |
goog.ui.RichTextSpellChecker.prototype.enterDocument = function() { |
| 199 |
goog.ui.RichTextSpellChecker.superClass_.enterDocument.call(this); |
| 200 |
|
| 201 |
var rootElement = goog.asserts.assertElement( |
| 202 |
this.rootNode_, |
| 203 |
'The rootNode_ of a richtextspellchecker must be an Element.'); |
| 204 |
this.keyHandler_.attach(rootElement); |
| 205 |
|
| 206 |
this.initSuggestionsMenu(); |
| 207 |
}; |
| 208 |
|
| 209 |
|
| 210 |
/** @override */ |
| 211 |
goog.ui.RichTextSpellChecker.prototype.initSuggestionsMenu = function() { |
| 212 |
goog.ui.RichTextSpellChecker.base(this, 'initSuggestionsMenu'); |
| 213 |
|
| 214 |
var menu = goog.asserts.assertInstanceof( |
| 215 |
this.getMenu(), goog.ui.PopupMenu, |
| 216 |
'The menu of a richtextspellchecker must be a PopupMenu.'); |
| 217 |
this.eventHandler_.listen( |
| 218 |
menu, goog.ui.Component.EventType.HIDE, this.onCorrectionHide_); |
| 219 |
}; |
| 220 |
|
| 221 |
|
| 222 |
/** |
| 223 |
* Checks spelling for all text and displays correction UI. |
| 224 |
* @override |
| 225 |
*/ |
| 226 |
goog.ui.RichTextSpellChecker.prototype.check = function() { |
| 227 |
this.blockReadyEvents(); |
| 228 |
this.preChargeDictionary_(this.rootNode_, this.dictionaryPreScanSize_); |
| 229 |
this.unblockReadyEvents(); |
| 230 |
|
| 231 |
this.eventHandler_.listen( |
| 232 |
this.spellCheck, goog.spell.SpellCheck.EventType.READY, |
| 233 |
this.onDictionaryCharged_, true); |
| 234 |
this.spellCheck.processPending(); |
| 235 |
}; |
| 236 |
|
| 237 |
|
| 238 |
/** |
| 239 |
* Processes nodes recursively. |
| 240 |
* |
| 241 |
* @param {Node} node Node to start with. |
| 242 |
* @param {number} words Max number of words to process. |
| 243 |
* @private |
| 244 |
*/ |
| 245 |
goog.ui.RichTextSpellChecker.prototype.preChargeDictionary_ = function( |
| 246 |
node, words) { |
| 247 |
while (node) { |
| 248 |
var next = this.nextNode_(node); |
| 249 |
if (this.isExcluded_(node)) { |
| 250 |
node = next; |
| 251 |
continue; |
| 252 |
} |
| 253 |
if (node.nodeType == goog.dom.NodeType.TEXT) { |
| 254 |
if (node.nodeValue) { |
| 255 |
words -= this.populateDictionary(node.nodeValue, words); |
| 256 |
if (words <= 0) { |
| 257 |
return; |
| 258 |
} |
| 259 |
} |
| 260 |
} else if (node.nodeType == goog.dom.NodeType.ELEMENT) { |
| 261 |
if (node.firstChild) { |
| 262 |
next = node.firstChild; |
| 263 |
} |
| 264 |
} |
| 265 |
node = next; |
| 266 |
} |
| 267 |
}; |
| 268 |
|
| 269 |
|
| 270 |
/** |
| 271 |
* Starts actual processing after the dictionary is charged. |
| 272 |
* @param {goog.events.Event} e goog.spell.SpellCheck.EventType.READY event. |
| 273 |
* @private |
| 274 |
*/ |
| 275 |
goog.ui.RichTextSpellChecker.prototype.onDictionaryCharged_ = function(e) { |
| 276 |
e.stopPropagation(); |
| 277 |
this.eventHandler_.unlisten( |
| 278 |
this.spellCheck, goog.spell.SpellCheck.EventType.READY, |
| 279 |
this.onDictionaryCharged_, true); |
| 280 |
|
| 281 |
// Now actually do the spell checking. |
| 282 |
this.clearWordElements(); |
| 283 |
this.initializeAsyncMode(); |
| 284 |
this.elementsInserted_ = 0; |
| 285 |
var result = this.processNode_(this.rootNode_); |
| 286 |
if (result == goog.ui.AbstractSpellChecker.AsyncResult.PENDING) { |
| 287 |
goog.Timer.callOnce(this.boundContinueAsyncFn_); |
| 288 |
return; |
| 289 |
} |
| 290 |
this.finishAsyncProcessing(); |
| 291 |
this.finishCheck_(); |
| 292 |
}; |
| 293 |
|
| 294 |
|
| 295 |
/** |
| 296 |
* Continues asynchrnonous spell checking. |
| 297 |
* @private |
| 298 |
*/ |
| 299 |
goog.ui.RichTextSpellChecker.prototype.continueAsync_ = function() { |
| 300 |
var result = this.continueAsyncProcessing(); |
| 301 |
if (result == goog.ui.AbstractSpellChecker.AsyncResult.PENDING) { |
| 302 |
goog.Timer.callOnce(this.boundContinueAsyncFn_); |
| 303 |
return; |
| 304 |
} |
| 305 |
result = this.processNode_(this.currentNode_); |
| 306 |
if (result == goog.ui.AbstractSpellChecker.AsyncResult.PENDING) { |
| 307 |
goog.Timer.callOnce(this.boundContinueAsyncFn_); |
| 308 |
return; |
| 309 |
} |
| 310 |
this.finishAsyncProcessing(); |
| 311 |
this.finishCheck_(); |
| 312 |
}; |
| 313 |
|
| 314 |
|
| 315 |
/** |
| 316 |
* Finalizes spelling check. |
| 317 |
* @private |
| 318 |
*/ |
| 319 |
goog.ui.RichTextSpellChecker.prototype.finishCheck_ = function() { |
| 320 |
delete this.currentNode_; |
| 321 |
this.spellCheck.processPending(); |
| 322 |
|
| 323 |
if (!this.isVisible()) { |
| 324 |
this.eventHandler_ |
| 325 |
.listen(this.rootNode_, goog.events.EventType.CLICK, this.onWordClick_) |
| 326 |
.listen( |
| 327 |
this.keyHandler_, goog.events.KeyHandler.EventType.KEY, |
| 328 |
this.handleRootNodeKeyEvent); |
| 329 |
} |
| 330 |
goog.ui.RichTextSpellChecker.superClass_.check.call(this); |
| 331 |
}; |
| 332 |
|
| 333 |
|
| 334 |
/** |
| 335 |
* Finds next node in our enumeration of the tree. |
| 336 |
* |
| 337 |
* @param {Node} node The node to which we're computing the next node for. |
| 338 |
* @return {Node} The next node or null if none was found. |
| 339 |
* @private |
| 340 |
*/ |
| 341 |
goog.ui.RichTextSpellChecker.prototype.nextNode_ = function(node) { |
| 342 |
while (node != this.rootNode_) { |
| 343 |
if (node.nextSibling) { |
| 344 |
return node.nextSibling; |
| 345 |
} |
| 346 |
node = node.parentNode; |
| 347 |
} |
| 348 |
return null; |
| 349 |
}; |
| 350 |
|
| 351 |
|
| 352 |
/** |
| 353 |
* Determines if the node is text node without any children. |
| 354 |
* |
| 355 |
* @param {Node} node The node to check. |
| 356 |
* @return {boolean} Whether the node is a text leaf node. |
| 357 |
* @private |
| 358 |
*/ |
| 359 |
goog.ui.RichTextSpellChecker.prototype.isTextLeaf_ = function(node) { |
| 360 |
return node != null && node.nodeType == goog.dom.NodeType.TEXT && |
| 361 |
!node.firstChild; |
| 362 |
}; |
| 363 |
|
| 364 |
|
| 365 |
/** @override */ |
| 366 |
goog.ui.RichTextSpellChecker.prototype.setExcludeMarker = function(marker) { |
| 367 |
if (marker) { |
| 368 |
if (typeof marker == 'string') { |
| 369 |
marker = [marker]; |
| 370 |
} |
| 371 |
|
| 372 |
this.excludeTags = []; |
| 373 |
this.excludeMarker = []; |
| 374 |
for (var i = 0; i < marker.length; i++) { |
| 375 |
var parts = marker[i].split('.'); |
| 376 |
if (parts.length == 2) { |
| 377 |
this.excludeTags.push(parts[0]); |
| 378 |
this.excludeMarker.push(parts[1]); |
| 379 |
} else { |
| 380 |
this.excludeMarker.push(parts[0]); |
| 381 |
this.excludeTags.push(undefined); |
| 382 |
} |
| 383 |
} |
| 384 |
} |
| 385 |
}; |
| 386 |
|
| 387 |
|
| 388 |
/** |
| 389 |
* Determines if the node is excluded from checking. |
| 390 |
* |
| 391 |
* @param {Node} node The node to check. |
| 392 |
* @return {boolean} Whether the node is excluded. |
| 393 |
* @private |
| 394 |
*/ |
| 395 |
goog.ui.RichTextSpellChecker.prototype.isExcluded_ = function(node) { |
| 396 |
if (this.excludeMarker && node.className) { |
| 397 |
for (var i = 0; i < this.excludeMarker.length; i++) { |
| 398 |
var excludeTag = this.excludeTags[i]; |
| 399 |
var excludeClass = this.excludeMarker[i]; |
| 400 |
var isExcluded = |
| 401 |
!!(excludeClass && node.className.indexOf(excludeClass) != -1 && |
| 402 |
(!excludeTag || node.tagName == excludeTag)); |
| 403 |
if (isExcluded) { |
| 404 |
return true; |
| 405 |
} |
| 406 |
} |
| 407 |
} |
| 408 |
return false; |
| 409 |
}; |
| 410 |
|
| 411 |
|
| 412 |
/** |
| 413 |
* Processes nodes recursively. |
| 414 |
* |
| 415 |
* @param {Node} node Node where to start. |
| 416 |
* @return {goog.ui.AbstractSpellChecker.AsyncResult|undefined} Result code. |
| 417 |
* @private |
| 418 |
*/ |
| 419 |
goog.ui.RichTextSpellChecker.prototype.processNode_ = function(node) { |
| 420 |
delete this.currentNode_; |
| 421 |
while (node) { |
| 422 |
var next = this.nextNode_(node); |
| 423 |
if (this.isExcluded_(node)) { |
| 424 |
node = next; |
| 425 |
continue; |
| 426 |
} |
| 427 |
if (node.nodeType == goog.dom.NodeType.TEXT) { |
| 428 |
var deleteNode = true; |
| 429 |
if (node.nodeValue) { |
| 430 |
var currentElements = this.elementsInserted_; |
| 431 |
var result = this.processTextAsync(node, node.nodeValue); |
| 432 |
if (result == goog.ui.AbstractSpellChecker.AsyncResult.PENDING) { |
| 433 |
// This marks node for deletion (empty nodes get deleted couple |
| 434 |
// of lines down this function). This is so our algorithm terminates. |
| 435 |
// In this case the node may be needlessly recreated, but it |
| 436 |
// happens rather infrequently and saves a lot of code. |
| 437 |
node.nodeValue = ''; |
| 438 |
this.currentNode_ = node; |
| 439 |
return result; |
| 440 |
} |
| 441 |
// If we did not add nodes in processing, the current element is still |
| 442 |
// valid. Let's preserve it! |
| 443 |
if (currentElements == this.elementsInserted_) { |
| 444 |
deleteNode = false; |
| 445 |
} |
| 446 |
} |
| 447 |
if (deleteNode) { |
| 448 |
goog.dom.removeNode(node); |
| 449 |
} |
| 450 |
} else if (node.nodeType == goog.dom.NodeType.ELEMENT) { |
| 451 |
// If this is a spell checker element... |
| 452 |
if (node.className == this.wordClassName) { |
| 453 |
// First, reconsolidate the text nodes inside the element - editing |
| 454 |
// in IE splits them up. |
| 455 |
var runner = node.firstChild; |
| 456 |
while (runner) { |
| 457 |
if (this.isTextLeaf_(runner)) { |
| 458 |
while (this.isTextLeaf_(runner.nextSibling)) { |
| 459 |
// Yes, this is not super efficient in IE, but it will almost |
| 460 |
// never happen. |
| 461 |
runner.nodeValue += runner.nextSibling.nodeValue; |
| 462 |
goog.dom.removeNode(runner.nextSibling); |
| 463 |
} |
| 464 |
} |
| 465 |
runner = runner.nextSibling; |
| 466 |
} |
| 467 |
// Move its contents out and reprocess it on the next iteration. |
| 468 |
if (node.firstChild) { |
| 469 |
next = node.firstChild; |
| 470 |
while (node.firstChild) { |
| 471 |
node.parentNode.insertBefore(node.firstChild, node); |
| 472 |
} |
| 473 |
} |
| 474 |
// get rid of the empty shell. |
| 475 |
goog.dom.removeNode(node); |
| 476 |
} else { |
| 477 |
if (node.firstChild) { |
| 478 |
next = node.firstChild; |
| 479 |
} |
| 480 |
} |
| 481 |
} |
| 482 |
node = next; |
| 483 |
} |
| 484 |
}; |
| 485 |
|
| 486 |
|
| 487 |
/** |
| 488 |
* Processes word. |
| 489 |
* |
| 490 |
* @param {Node} node Node containing word. |
| 491 |
* @param {string} word Word to process. |
| 492 |
* @param {goog.spell.SpellCheck.WordStatus} status Status of the word. |
| 493 |
* @protected |
| 494 |
* @override |
| 495 |
*/ |
| 496 |
goog.ui.RichTextSpellChecker.prototype.processWord = function( |
| 497 |
node, word, status) { |
| 498 |
node.parentNode.insertBefore(this.createWordElement(word, status), node); |
| 499 |
this.elementsInserted_++; |
| 500 |
}; |
| 501 |
|
| 502 |
|
| 503 |
/** |
| 504 |
* Processes recognized text and separators. |
| 505 |
* |
| 506 |
* @param {Node} node Node containing separator. |
| 507 |
* @param {string} text Text to process. |
| 508 |
* @protected |
| 509 |
* @override |
| 510 |
*/ |
| 511 |
goog.ui.RichTextSpellChecker.prototype.processRange = function(node, text) { |
| 512 |
// The text does not change, it only gets split, so if the lengths are the |
| 513 |
// same, the text is the same, so keep the existing node. |
| 514 |
if (node.nodeType == goog.dom.NodeType.TEXT && |
| 515 |
node.nodeValue.length == text.length) { |
| 516 |
return; |
| 517 |
} |
| 518 |
|
| 519 |
node.parentNode.insertBefore(this.editorDom_.createTextNode(text), node); |
| 520 |
this.elementsInserted_++; |
| 521 |
}; |
| 522 |
|
| 523 |
|
| 524 |
/** @override */ |
| 525 |
goog.ui.RichTextSpellChecker.prototype.getElementByIndex = function(id) { |
| 526 |
return this.editorDom_.getElement(this.makeElementId(id)); |
| 527 |
}; |
| 528 |
|
| 529 |
|
| 530 |
/** |
| 531 |
* Updates or replaces element based on word status. |
| 532 |
* @see goog.ui.AbstractSpellChecker.prototype.updateElement_ |
| 533 |
* |
| 534 |
* Overridden from AbstractSpellChecker because we need to be mindful of |
| 535 |
* deleting the currentNode_ - this can break our pending processing. |
| 536 |
* |
| 537 |
* @param {Element} el Word element. |
| 538 |
* @param {string} word Word to update status for. |
| 539 |
* @param {goog.spell.SpellCheck.WordStatus} status Status of word. |
| 540 |
* @protected |
| 541 |
* @override |
| 542 |
*/ |
| 543 |
goog.ui.RichTextSpellChecker.prototype.updateElement = function( |
| 544 |
el, word, status) { |
| 545 |
if (status == goog.spell.SpellCheck.WordStatus.VALID && |
| 546 |
el != this.currentNode_ && el.nextSibling != this.currentNode_) { |
| 547 |
this.removeMarkup(el); |
| 548 |
} else { |
| 549 |
goog.dom.setProperties(el, this.getElementProperties(status)); |
| 550 |
} |
| 551 |
}; |
| 552 |
|
| 553 |
|
| 554 |
/** |
| 555 |
* Hides correction UI. |
| 556 |
* @override |
| 557 |
*/ |
| 558 |
goog.ui.RichTextSpellChecker.prototype.resume = function() { |
| 559 |
goog.ui.RichTextSpellChecker.superClass_.resume.call(this); |
| 560 |
|
| 561 |
this.restoreNode_(this.rootNode_); |
| 562 |
|
| 563 |
this.eventHandler_ |
| 564 |
.unlisten(this.rootNode_, goog.events.EventType.CLICK, this.onWordClick_) |
| 565 |
.unlisten( |
| 566 |
this.keyHandler_, goog.events.KeyHandler.EventType.KEY, |
| 567 |
this.handleRootNodeKeyEvent); |
| 568 |
}; |
| 569 |
|
| 570 |
|
| 571 |
/** |
| 572 |
* Processes nodes recursively, removes all spell checker markup, and |
| 573 |
* consolidates text nodes. |
| 574 |
* |
| 575 |
* @param {Node} node node on which to recurse. |
| 576 |
* @private |
| 577 |
*/ |
| 578 |
goog.ui.RichTextSpellChecker.prototype.restoreNode_ = function(node) { |
| 579 |
while (node) { |
| 580 |
if (this.isExcluded_(node)) { |
| 581 |
node = node.nextSibling; |
| 582 |
continue; |
| 583 |
} |
| 584 |
// Contents of the child of the element is usually 1 text element, but the |
| 585 |
// user can actually add multiple nodes in it during editing. So we move |
| 586 |
// all the children out, prepend, and reprocess (pointer is set back to |
| 587 |
// the first node that's been moved out, and the loop repeats). |
| 588 |
if (node.nodeType == goog.dom.NodeType.ELEMENT && |
| 589 |
node.className == this.wordClassName) { |
| 590 |
var firstElement = node.firstChild; |
| 591 |
var next; |
| 592 |
for (var child = firstElement; child; child = next) { |
| 593 |
next = child.nextSibling; |
| 594 |
node.parentNode.insertBefore(child, node); |
| 595 |
} |
| 596 |
next = firstElement || node.nextSibling; |
| 597 |
goog.dom.removeNode(node); |
| 598 |
node = next; |
| 599 |
continue; |
| 600 |
} |
| 601 |
// If this is a chain of text elements, we're trying to consolidate it. |
| 602 |
var textLeaf = this.isTextLeaf_(node); |
| 603 |
if (textLeaf) { |
| 604 |
var textNodes = 1; |
| 605 |
var next = node.nextSibling; |
| 606 |
while (this.isTextLeaf_(node.previousSibling)) { |
| 607 |
node = node.previousSibling; |
| 608 |
++textNodes; |
| 609 |
} |
| 610 |
while (this.isTextLeaf_(next)) { |
| 611 |
next = next.nextSibling; |
| 612 |
++textNodes; |
| 613 |
} |
| 614 |
if (textNodes > 1) { |
| 615 |
this.workBuffer_.append(node.nodeValue); |
| 616 |
while (this.isTextLeaf_(node.nextSibling)) { |
| 617 |
this.workBuffer_.append(node.nextSibling.nodeValue); |
| 618 |
goog.dom.removeNode(node.nextSibling); |
| 619 |
} |
| 620 |
node.nodeValue = this.workBuffer_.toString(); |
| 621 |
this.workBuffer_.clear(); |
| 622 |
} |
| 623 |
} |
| 624 |
// Process child nodes, if any. |
| 625 |
if (node.firstChild) { |
| 626 |
this.restoreNode_(node.firstChild); |
| 627 |
} |
| 628 |
node = node.nextSibling; |
| 629 |
} |
| 630 |
}; |
| 631 |
|
| 632 |
|
| 633 |
/** |
| 634 |
* Returns desired element properties for the specified status. |
| 635 |
* |
| 636 |
* @param {goog.spell.SpellCheck.WordStatus} status Status of the word. |
| 637 |
* @return {!Object} Properties to apply to word element. |
| 638 |
* @protected |
| 639 |
* @override |
| 640 |
*/ |
| 641 |
goog.ui.RichTextSpellChecker.prototype.getElementProperties = function(status) { |
| 642 |
return { |
| 643 |
'class': this.wordClassName, |
| 644 |
'style': (status == goog.spell.SpellCheck.WordStatus.INVALID) ? |
| 645 |
this.invalidWordCssText : |
| 646 |
'' |
| 647 |
}; |
| 648 |
}; |
| 649 |
|
| 650 |
|
| 651 |
/** |
| 652 |
* Handler for click events. |
| 653 |
* |
| 654 |
* @param {goog.events.BrowserEvent} event Event object. |
| 655 |
* @private |
| 656 |
*/ |
| 657 |
goog.ui.RichTextSpellChecker.prototype.onWordClick_ = function(event) { |
| 658 |
var target = /** @type {Element} */ (event.target); |
| 659 |
if (event.target.className == this.wordClassName && |
| 660 |
this.spellCheck.checkWord(goog.dom.getTextContent(target)) == |
| 661 |
goog.spell.SpellCheck.WordStatus.INVALID) { |
| 662 |
this.showSuggestionsMenu(target, event); |
| 663 |
|
| 664 |
// Prevent document click handler from closing the menu. |
| 665 |
event.stopPropagation(); |
| 666 |
} |
| 667 |
}; |
| 668 |
|
| 669 |
|
| 670 |
/** @override */ |
| 671 |
goog.ui.RichTextSpellChecker.prototype.disposeInternal = function() { |
| 672 |
goog.ui.RichTextSpellChecker.superClass_.disposeInternal.call(this); |
| 673 |
this.rootNode_ = null; |
| 674 |
this.editorDom_ = null; |
| 675 |
}; |
| 676 |
|
| 677 |
|
| 678 |
/** |
| 679 |
* Returns whether the editor node is an iframe. |
| 680 |
* |
| 681 |
* @return {boolean} true the editor node is an iframe, otherwise false. |
| 682 |
* @protected |
| 683 |
*/ |
| 684 |
goog.ui.RichTextSpellChecker.prototype.isEditorIframe = function() { |
| 685 |
return this.rootNodeIframe_; |
| 686 |
}; |
| 687 |
|
| 688 |
|
| 689 |
/** |
| 690 |
* Handles keyboard events inside the editor to allow keyboard navigation |
| 691 |
* between misspelled words and activation of the suggestion menu. |
| 692 |
* |
| 693 |
* @param {goog.events.BrowserEvent} e the key event. |
| 694 |
* @return {boolean} The handled value. |
| 695 |
* @protected |
| 696 |
*/ |
| 697 |
goog.ui.RichTextSpellChecker.prototype.handleRootNodeKeyEvent = function(e) { |
| 698 |
var handled = false; |
| 699 |
switch (e.keyCode) { |
| 700 |
case goog.events.KeyCodes.RIGHT: |
| 701 |
if (e.ctrlKey) { |
| 702 |
handled = this.navigate(goog.ui.AbstractSpellChecker.Direction.NEXT); |
| 703 |
} |
| 704 |
break; |
| 705 |
|
| 706 |
case goog.events.KeyCodes.LEFT: |
| 707 |
if (e.ctrlKey) { |
| 708 |
handled = |
| 709 |
this.navigate(goog.ui.AbstractSpellChecker.Direction.PREVIOUS); |
| 710 |
} |
| 711 |
break; |
| 712 |
|
| 713 |
case goog.events.KeyCodes.DOWN: |
| 714 |
if (this.getFocusedElementIndex()) { |
| 715 |
var el = this.editorDom_.getElement( |
| 716 |
this.makeElementId(this.getFocusedElementIndex())); |
| 717 |
if (el) { |
| 718 |
var position = goog.style.getClientPosition(el); |
| 719 |
|
| 720 |
if (this.isEditorIframe()) { |
| 721 |
var iframePosition = |
| 722 |
goog.style.getClientPosition(this.getElementStrict()); |
| 723 |
position = goog.math.Coordinate.sum(iframePosition, position); |
| 724 |
} |
| 725 |
|
| 726 |
var size = goog.style.getSize(el); |
| 727 |
position.x += size.width / 2; |
| 728 |
position.y += size.height / 2; |
| 729 |
this.showSuggestionsMenu(el, position); |
| 730 |
handled = true; |
| 731 |
} |
| 732 |
} |
| 733 |
break; |
| 734 |
} |
| 735 |
|
| 736 |
if (handled) { |
| 737 |
e.preventDefault(); |
| 738 |
} |
| 739 |
|
| 740 |
return handled; |
| 741 |
}; |
| 742 |
|
| 743 |
|
| 744 |
/** @override */ |
| 745 |
goog.ui.RichTextSpellChecker.prototype.onCorrectionAction = function(event) { |
| 746 |
goog.ui.RichTextSpellChecker.base(this, 'onCorrectionAction', event); |
| 747 |
|
| 748 |
// In case of editWord base class has already set the focus (on the input), |
| 749 |
// otherwise set the focus back on the word. |
| 750 |
if (event.target != this.getMenuEdit()) { |
| 751 |
this.reFocus_(); |
| 752 |
} |
| 753 |
}; |
| 754 |
|
| 755 |
|
| 756 |
/** |
| 757 |
* Restores focus when the suggestion menu is hidden. |
| 758 |
* |
| 759 |
* @param {goog.events.BrowserEvent} event Blur event. |
| 760 |
* @private |
| 761 |
*/ |
| 762 |
goog.ui.RichTextSpellChecker.prototype.onCorrectionHide_ = function(event) { |
| 763 |
this.reFocus_(); |
| 764 |
}; |
| 765 |
|
| 766 |
|
| 767 |
/** |
| 768 |
* Sets the focus back on the previously focused word element. |
| 769 |
* @private |
| 770 |
*/ |
| 771 |
goog.ui.RichTextSpellChecker.prototype.reFocus_ = function() { |
| 772 |
this.getElementStrict().focus(); |
| 773 |
|
| 774 |
var el = this.getElementByIndex(this.getFocusedElementIndex()); |
| 775 |
if (el) { |
| 776 |
this.focusOnElement(el); |
| 777 |
} |
| 778 |
}; |
| 779 |
|
| 780 |
|
| 781 |
/** @override */ |
| 782 |
goog.ui.RichTextSpellChecker.prototype.focusOnElement = function(element) { |
| 783 |
goog.dom.Range.createCaret(element, 0).select(); |
| 784 |
}; |