multirange.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. // Copyright 2008 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 Utilities for working with W3C multi-part ranges.
  16. *
  17. * @author robbyw@google.com (Robby Walker)
  18. */
  19. goog.provide('goog.dom.MultiRange');
  20. goog.provide('goog.dom.MultiRangeIterator');
  21. goog.require('goog.array');
  22. goog.require('goog.dom');
  23. goog.require('goog.dom.AbstractMultiRange');
  24. goog.require('goog.dom.AbstractRange');
  25. goog.require('goog.dom.RangeIterator');
  26. goog.require('goog.dom.RangeType');
  27. goog.require('goog.dom.SavedRange');
  28. goog.require('goog.dom.TextRange');
  29. goog.require('goog.iter');
  30. goog.require('goog.iter.StopIteration');
  31. goog.require('goog.log');
  32. /**
  33. * Creates a new multi part range with no properties. Do not use this
  34. * constructor: use one of the goog.dom.Range.createFrom* methods instead.
  35. * @constructor
  36. * @extends {goog.dom.AbstractMultiRange}
  37. * @final
  38. */
  39. goog.dom.MultiRange = function() {
  40. /**
  41. * Logging object.
  42. * @private {goog.log.Logger}
  43. */
  44. this.logger_ = goog.log.getLogger('goog.dom.MultiRange');
  45. /**
  46. * Array of browser sub-ranges comprising this multi-range.
  47. * @private {Array<Range>}
  48. */
  49. this.browserRanges_ = [];
  50. /**
  51. * Lazily initialized array of range objects comprising this multi-range.
  52. * @private {Array<goog.dom.TextRange>}
  53. */
  54. this.ranges_ = [];
  55. /**
  56. * Lazily computed sorted version of ranges_, sorted by start point.
  57. * @private {Array<goog.dom.TextRange>?}
  58. */
  59. this.sortedRanges_ = null;
  60. /**
  61. * Lazily computed container node.
  62. * @private {Node}
  63. */
  64. this.container_ = null;
  65. };
  66. goog.inherits(goog.dom.MultiRange, goog.dom.AbstractMultiRange);
  67. /**
  68. * Creates a new range wrapper from the given browser selection object. Do not
  69. * use this method directly - please use goog.dom.Range.createFrom* instead.
  70. * @param {Selection} selection The browser selection object.
  71. * @return {!goog.dom.MultiRange} A range wrapper object.
  72. */
  73. goog.dom.MultiRange.createFromBrowserSelection = function(selection) {
  74. var range = new goog.dom.MultiRange();
  75. for (var i = 0, len = selection.rangeCount; i < len; i++) {
  76. range.browserRanges_.push(selection.getRangeAt(i));
  77. }
  78. return range;
  79. };
  80. /**
  81. * Creates a new range wrapper from the given browser ranges. Do not
  82. * use this method directly - please use goog.dom.Range.createFrom* instead.
  83. * @param {Array<Range>} browserRanges The browser ranges.
  84. * @return {!goog.dom.MultiRange} A range wrapper object.
  85. */
  86. goog.dom.MultiRange.createFromBrowserRanges = function(browserRanges) {
  87. var range = new goog.dom.MultiRange();
  88. range.browserRanges_ = goog.array.clone(browserRanges);
  89. return range;
  90. };
  91. /**
  92. * Creates a new range wrapper from the given goog.dom.TextRange objects. Do
  93. * not use this method directly - please use goog.dom.Range.createFrom* instead.
  94. * @param {Array<goog.dom.TextRange>} textRanges The text range objects.
  95. * @return {!goog.dom.MultiRange} A range wrapper object.
  96. */
  97. goog.dom.MultiRange.createFromTextRanges = function(textRanges) {
  98. var range = new goog.dom.MultiRange();
  99. range.ranges_ = textRanges;
  100. range.browserRanges_ = goog.array.map(
  101. textRanges, function(range) { return range.getBrowserRangeObject(); });
  102. return range;
  103. };
  104. // Method implementations
  105. /**
  106. * Clears cached values. Should be called whenever this.browserRanges_ is
  107. * modified.
  108. * @private
  109. */
  110. goog.dom.MultiRange.prototype.clearCachedValues_ = function() {
  111. this.ranges_ = [];
  112. this.sortedRanges_ = null;
  113. this.container_ = null;
  114. };
  115. /**
  116. * @return {!goog.dom.MultiRange} A clone of this range.
  117. * @override
  118. */
  119. goog.dom.MultiRange.prototype.clone = function() {
  120. return goog.dom.MultiRange.createFromBrowserRanges(this.browserRanges_);
  121. };
  122. /** @override */
  123. goog.dom.MultiRange.prototype.getType = function() {
  124. return goog.dom.RangeType.MULTI;
  125. };
  126. /** @override */
  127. goog.dom.MultiRange.prototype.getBrowserRangeObject = function() {
  128. // NOTE(robbyw): This method does not make sense for multi-ranges.
  129. if (this.browserRanges_.length > 1) {
  130. goog.log.warning(
  131. this.logger_,
  132. 'getBrowserRangeObject called on MultiRange with more than 1 range');
  133. }
  134. return this.browserRanges_[0];
  135. };
  136. /** @override */
  137. goog.dom.MultiRange.prototype.setBrowserRangeObject = function(nativeRange) {
  138. // TODO(robbyw): Look in to adding setBrowserSelectionObject.
  139. return false;
  140. };
  141. /** @override */
  142. goog.dom.MultiRange.prototype.getTextRangeCount = function() {
  143. return this.browserRanges_.length;
  144. };
  145. /** @override */
  146. goog.dom.MultiRange.prototype.getTextRange = function(i) {
  147. if (!this.ranges_[i]) {
  148. this.ranges_[i] =
  149. goog.dom.TextRange.createFromBrowserRange(this.browserRanges_[i]);
  150. }
  151. return this.ranges_[i];
  152. };
  153. /** @override */
  154. goog.dom.MultiRange.prototype.getContainer = function() {
  155. if (!this.container_) {
  156. var nodes = [];
  157. for (var i = 0, len = this.getTextRangeCount(); i < len; i++) {
  158. nodes.push(this.getTextRange(i).getContainer());
  159. }
  160. this.container_ = goog.dom.findCommonAncestor.apply(null, nodes);
  161. }
  162. return this.container_;
  163. };
  164. /**
  165. * @return {!Array<goog.dom.TextRange>} An array of sub-ranges, sorted by start
  166. * point.
  167. */
  168. goog.dom.MultiRange.prototype.getSortedRanges = function() {
  169. if (!this.sortedRanges_) {
  170. this.sortedRanges_ = this.getTextRanges();
  171. this.sortedRanges_.sort(function(a, b) {
  172. var aStartNode = a.getStartNode();
  173. var aStartOffset = a.getStartOffset();
  174. var bStartNode = b.getStartNode();
  175. var bStartOffset = b.getStartOffset();
  176. if (aStartNode == bStartNode && aStartOffset == bStartOffset) {
  177. return 0;
  178. }
  179. /**
  180. * @suppress {missingRequire} Cannot depend on goog.dom.Range because
  181. * it creates a circular dependency.
  182. */
  183. return goog.dom.Range.isReversed(
  184. aStartNode, aStartOffset, bStartNode, bStartOffset) ?
  185. 1 :
  186. -1;
  187. });
  188. }
  189. return this.sortedRanges_;
  190. };
  191. /** @override */
  192. goog.dom.MultiRange.prototype.getStartNode = function() {
  193. return this.getSortedRanges()[0].getStartNode();
  194. };
  195. /** @override */
  196. goog.dom.MultiRange.prototype.getStartOffset = function() {
  197. return this.getSortedRanges()[0].getStartOffset();
  198. };
  199. /** @override */
  200. goog.dom.MultiRange.prototype.getEndNode = function() {
  201. // NOTE(robbyw): This may return the wrong node if any subranges overlap.
  202. return goog.array.peek(this.getSortedRanges()).getEndNode();
  203. };
  204. /** @override */
  205. goog.dom.MultiRange.prototype.getEndOffset = function() {
  206. // NOTE(robbyw): This may return the wrong value if any subranges overlap.
  207. return goog.array.peek(this.getSortedRanges()).getEndOffset();
  208. };
  209. /** @override */
  210. goog.dom.MultiRange.prototype.isRangeInDocument = function() {
  211. return goog.array.every(this.getTextRanges(), function(range) {
  212. return range.isRangeInDocument();
  213. });
  214. };
  215. /** @override */
  216. goog.dom.MultiRange.prototype.isCollapsed = function() {
  217. return this.browserRanges_.length == 0 ||
  218. this.browserRanges_.length == 1 && this.getTextRange(0).isCollapsed();
  219. };
  220. /** @override */
  221. goog.dom.MultiRange.prototype.getText = function() {
  222. return goog.array
  223. .map(this.getTextRanges(), function(range) { return range.getText(); })
  224. .join('');
  225. };
  226. /** @override */
  227. goog.dom.MultiRange.prototype.getHtmlFragment = function() {
  228. return this.getValidHtml();
  229. };
  230. /** @override */
  231. goog.dom.MultiRange.prototype.getValidHtml = function() {
  232. // NOTE(robbyw): This does not behave well if the sub-ranges overlap.
  233. return goog.array
  234. .map(
  235. this.getTextRanges(),
  236. function(range) { return range.getValidHtml(); })
  237. .join('');
  238. };
  239. /** @override */
  240. goog.dom.MultiRange.prototype.getPastableHtml = function() {
  241. // TODO(robbyw): This should probably do something smart like group TR and TD
  242. // selections in to the same table.
  243. return this.getValidHtml();
  244. };
  245. /** @override */
  246. goog.dom.MultiRange.prototype.__iterator__ = function(opt_keys) {
  247. return new goog.dom.MultiRangeIterator(this);
  248. };
  249. // RANGE ACTIONS
  250. /** @override */
  251. goog.dom.MultiRange.prototype.select = function() {
  252. var selection =
  253. goog.dom.AbstractRange.getBrowserSelectionForWindow(this.getWindow());
  254. selection.removeAllRanges();
  255. for (var i = 0, len = this.getTextRangeCount(); i < len; i++) {
  256. selection.addRange(this.getTextRange(i).getBrowserRangeObject());
  257. }
  258. };
  259. /** @override */
  260. goog.dom.MultiRange.prototype.removeContents = function() {
  261. goog.array.forEach(
  262. this.getTextRanges(), function(range) { range.removeContents(); });
  263. };
  264. // SAVE/RESTORE
  265. /** @override */
  266. goog.dom.MultiRange.prototype.saveUsingDom = function() {
  267. return new goog.dom.DomSavedMultiRange_(this);
  268. };
  269. // RANGE MODIFICATION
  270. /**
  271. * Collapses this range to a single point, either the first or last point
  272. * depending on the parameter. This will result in the number of ranges in this
  273. * multi range becoming 1.
  274. * @param {boolean} toAnchor Whether to collapse to the anchor.
  275. * @override
  276. */
  277. goog.dom.MultiRange.prototype.collapse = function(toAnchor) {
  278. if (!this.isCollapsed()) {
  279. var range = toAnchor ? this.getTextRange(0) :
  280. this.getTextRange(this.getTextRangeCount() - 1);
  281. this.clearCachedValues_();
  282. range.collapse(toAnchor);
  283. this.ranges_ = [range];
  284. this.sortedRanges_ = [range];
  285. this.browserRanges_ = [range.getBrowserRangeObject()];
  286. }
  287. };
  288. // SAVED RANGE OBJECTS
  289. /**
  290. * A SavedRange implementation using DOM endpoints.
  291. * @param {goog.dom.MultiRange} range The range to save.
  292. * @constructor
  293. * @extends {goog.dom.SavedRange}
  294. * @private
  295. */
  296. goog.dom.DomSavedMultiRange_ = function(range) {
  297. /**
  298. * Array of saved ranges.
  299. * @type {Array<goog.dom.SavedRange>}
  300. * @private
  301. */
  302. this.savedRanges_ = goog.array.map(
  303. range.getTextRanges(), function(range) { return range.saveUsingDom(); });
  304. };
  305. goog.inherits(goog.dom.DomSavedMultiRange_, goog.dom.SavedRange);
  306. /**
  307. * @return {!goog.dom.MultiRange} The restored range.
  308. * @override
  309. */
  310. goog.dom.DomSavedMultiRange_.prototype.restoreInternal = function() {
  311. var ranges = goog.array.map(
  312. this.savedRanges_, function(savedRange) { return savedRange.restore(); });
  313. return goog.dom.MultiRange.createFromTextRanges(ranges);
  314. };
  315. /** @override */
  316. goog.dom.DomSavedMultiRange_.prototype.disposeInternal = function() {
  317. goog.dom.DomSavedMultiRange_.superClass_.disposeInternal.call(this);
  318. goog.array.forEach(
  319. this.savedRanges_, function(savedRange) { savedRange.dispose(); });
  320. delete this.savedRanges_;
  321. };
  322. // RANGE ITERATION
  323. /**
  324. * Subclass of goog.dom.TagIterator that iterates over a DOM range. It
  325. * adds functions to determine the portion of each text node that is selected.
  326. *
  327. * @param {goog.dom.MultiRange} range The range to traverse.
  328. * @constructor
  329. * @extends {goog.dom.RangeIterator}
  330. * @final
  331. */
  332. goog.dom.MultiRangeIterator = function(range) {
  333. /**
  334. * The list of range iterators left to traverse.
  335. * @private {Array<goog.dom.RangeIterator>}
  336. */
  337. this.iterators_ = null;
  338. /**
  339. * The index of the current sub-iterator being traversed.
  340. * @private {number}
  341. */
  342. this.currentIdx_ = 0;
  343. if (range) {
  344. this.iterators_ = goog.array.map(range.getSortedRanges(), function(r) {
  345. return goog.iter.toIterator(r);
  346. });
  347. }
  348. goog.dom.MultiRangeIterator.base(
  349. this, 'constructor', range ? this.getStartNode() : null, false);
  350. };
  351. goog.inherits(goog.dom.MultiRangeIterator, goog.dom.RangeIterator);
  352. /** @override */
  353. goog.dom.MultiRangeIterator.prototype.getStartTextOffset = function() {
  354. return this.iterators_[this.currentIdx_].getStartTextOffset();
  355. };
  356. /** @override */
  357. goog.dom.MultiRangeIterator.prototype.getEndTextOffset = function() {
  358. return this.iterators_[this.currentIdx_].getEndTextOffset();
  359. };
  360. /** @override */
  361. goog.dom.MultiRangeIterator.prototype.getStartNode = function() {
  362. return this.iterators_[0].getStartNode();
  363. };
  364. /** @override */
  365. goog.dom.MultiRangeIterator.prototype.getEndNode = function() {
  366. return goog.array.peek(this.iterators_).getEndNode();
  367. };
  368. /** @override */
  369. goog.dom.MultiRangeIterator.prototype.isLast = function() {
  370. return this.iterators_[this.currentIdx_].isLast();
  371. };
  372. /** @override */
  373. goog.dom.MultiRangeIterator.prototype.next = function() {
  374. try {
  375. var it = this.iterators_[this.currentIdx_];
  376. var next = it.next();
  377. this.setPosition(it.node, it.tagType, it.depth);
  378. return next;
  379. } catch (ex) {
  380. if (ex !== goog.iter.StopIteration ||
  381. this.iterators_.length - 1 == this.currentIdx_) {
  382. throw ex;
  383. } else {
  384. // In case we got a StopIteration, increment counter and try again.
  385. this.currentIdx_++;
  386. return this.next();
  387. }
  388. }
  389. };
  390. /** @override */
  391. goog.dom.MultiRangeIterator.prototype.copyFrom = function(other) {
  392. this.iterators_ = goog.array.clone(other.iterators_);
  393. goog.dom.MultiRangeIterator.superClass_.copyFrom.call(this, other);
  394. };
  395. /**
  396. * @return {!goog.dom.MultiRangeIterator} An identical iterator.
  397. * @override
  398. */
  399. goog.dom.MultiRangeIterator.prototype.clone = function() {
  400. var copy = new goog.dom.MultiRangeIterator(null);
  401. copy.copyFrom(this);
  402. return copy;
  403. };