savedrange.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. * @fileoverview A generic interface for saving and restoring ranges.
  16. *
  17. * @author robbyw@google.com (Robby Walker)
  18. */
  19. goog.provide('goog.dom.SavedRange');
  20. goog.require('goog.Disposable');
  21. goog.require('goog.log');
  22. /**
  23. * Abstract interface for a saved range.
  24. * @constructor
  25. * @extends {goog.Disposable}
  26. */
  27. goog.dom.SavedRange = function() {
  28. goog.Disposable.call(this);
  29. };
  30. goog.inherits(goog.dom.SavedRange, goog.Disposable);
  31. /**
  32. * Logging object.
  33. * @type {goog.log.Logger}
  34. * @private
  35. */
  36. goog.dom.SavedRange.logger_ = goog.log.getLogger('goog.dom.SavedRange');
  37. /**
  38. * Restores the range and by default disposes of the saved copy. Take note:
  39. * this means the by default SavedRange objects are single use objects.
  40. * @param {boolean=} opt_stayAlive Whether this SavedRange should stay alive
  41. * (not be disposed) after restoring the range. Defaults to false (dispose).
  42. * @return {goog.dom.AbstractRange} The restored range.
  43. */
  44. goog.dom.SavedRange.prototype.restore = function(opt_stayAlive) {
  45. if (this.isDisposed()) {
  46. goog.log.error(
  47. goog.dom.SavedRange.logger_,
  48. 'Disposed SavedRange objects cannot be restored.');
  49. }
  50. var range = this.restoreInternal();
  51. if (!opt_stayAlive) {
  52. this.dispose();
  53. }
  54. return range;
  55. };
  56. /**
  57. * Internal method to restore the saved range.
  58. * @return {goog.dom.AbstractRange} The restored range.
  59. */
  60. goog.dom.SavedRange.prototype.restoreInternal = goog.abstractMethod;