Debian Codegraph @

closure-library/closure/goog/testing/editor/testhelper.js

raw
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 /**
16 * @fileoverview Class that allows for simple text editing tests.
17 *
18 * @author [email protected] (Robby Walker)
19 */
20
21 goog.setTestOnly('goog.testing.editor.TestHelper');
22 goog.provide('goog.testing.editor.TestHelper');
23
24 goog.require('goog.Disposable');
25 goog.require('goog.dom');
26 goog.require('goog.dom.Range');
27 goog.require('goog.editor.BrowserFeature');
28 goog.require('goog.editor.node');
29 goog.require('goog.editor.plugins.AbstractBubblePlugin');
30 goog.require('goog.testing.dom');
31
32
33
34 /**
35 * Create a new test controller.
36 * @param {Element} root The root editable element.
37 * @constructor
38 * @extends {goog.Disposable}
39 * @final
40 */
41 goog.testing.editor.TestHelper = function(root) {
42 if (!root) {
43 throw new Error('Null root');
44 }
45 goog.Disposable.call(this);
46
47 /**
48 * Convenience variable for root DOM element.
49 * @type {!Element}
50 * @private
51 */
52 this.root_ = root;
53
54 /**
55 * The starting HTML of the editable element.
56 * @type {string}
57 * @private
58 */
59 this.savedHtml_ = '';
60 };
61 goog.inherits(goog.testing.editor.TestHelper, goog.Disposable);
62
63
64 /**
65 * Selects a new root element.
66 * @param {Element} root The root editable element.
67 */
68 goog.testing.editor.TestHelper.prototype.setRoot = function(root) {
69 if (!root) {
70 throw new Error('Null root');
71 }
72 this.root_ = root;
73 };
74
75
76 /**
77 * Make the root element editable. Also saves its HTML to be restored
78 * in tearDown.
79 */
80 goog.testing.editor.TestHelper.prototype.setUpEditableElement = function() {
81 this.savedHtml_ = this.root_.innerHTML;
82 if (goog.editor.BrowserFeature.HAS_CONTENT_EDITABLE) {
83 this.root_.contentEditable = true;
84 } else {
85 this.root_.ownerDocument.designMode = 'on';
86 }
87 this.root_.setAttribute('g_editable', 'true');
88 };
89
90
91 /**
92 * Reset the element previously initialized, restoring its HTML and making it
93 * non editable.
94 * @suppress {accessControls} Private state of
95 * {@link goog.editor.plugins.AbstractBubblePlugin} is accessed for test
96 * purposes.
97 */
98 goog.testing.editor.TestHelper.prototype.tearDownEditableElement = function() {
99 if (goog.editor.BrowserFeature.HAS_CONTENT_EDITABLE) {
100 this.root_.contentEditable = false;
101 } else {
102 this.root_.ownerDocument.designMode = 'off';
103 }
104 goog.dom.removeChildren(this.root_);
105 this.root_.innerHTML = this.savedHtml_;
106 this.root_.removeAttribute('g_editable');
107
108 if (goog.editor.plugins && goog.editor.plugins.AbstractBubblePlugin) {
109 // Remove old bubbles.
110 for (var key in goog.editor.plugins.AbstractBubblePlugin.bubbleMap_) {
111 goog.editor.plugins.AbstractBubblePlugin.bubbleMap_[key].dispose();
112 }
113 // Ensure we get a new bubble for each test.
114 goog.editor.plugins.AbstractBubblePlugin.bubbleMap_ = {};
115 }
116 };
117
118
119 /**
120 * Assert that the html in 'root' is substantially similar to htmlPattern.
121 * This method tests for the same set of styles, and for the same order of
122 * nodes. Breaking whitespace nodes are ignored. Elements can be annotated
123 * with classnames corresponding to keys in goog.userAgent and will be
124 * expected to show up in that user agent and expected not to show up in
125 * others.
126 * @param {string} htmlPattern The pattern to match.
127 */
128 goog.testing.editor.TestHelper.prototype.assertHtmlMatches = function(
129 htmlPattern) {
130 goog.testing.dom.assertHtmlContentsMatch(htmlPattern, this.root_);
131 };
132
133
134 /**
135 * Finds the first text node descendant of root with the given content.
136 * @param {string|RegExp} textOrRegexp The text to find, or a regular
137 * expression to find a match of.
138 * @return {Node} The first text node that matches, or null if none is found.
139 */
140 goog.testing.editor.TestHelper.prototype.findTextNode = function(textOrRegexp) {
141 return goog.testing.dom.findTextNode(textOrRegexp, this.root_);
142 };
143
144
145 /**
146 * Select from the given {@code fromOffset} in the given {@code from} node to
147 * the given {@code toOffset} in the optionally given {@code to} node. If nodes
148 * are passed in, uses them, otherwise uses findTextNode to find the nodes to
149 * select. Selects a caret if opt_to and opt_toOffset are not given.
150 * @param {Node|string} from Node or text of the node to start the selection at.
151 * @param {number} fromOffset Offset within the above node to start the
152 * selection at.
153 * @param {Node|string=} opt_to Node or text of the node to end the selection
154 * at.
155 * @param {number=} opt_toOffset Offset within the above node to end the
156 * selection at.
157 * @return {!goog.dom.AbstractRange}
158 */
159 goog.testing.editor.TestHelper.prototype.select = function(
160 from, fromOffset, opt_to, opt_toOffset) {
161 var end;
162 var start = end = goog.isString(from) ? this.findTextNode(from) : from;
163 var endOffset;
164 var startOffset = endOffset = fromOffset;
165
166 if (opt_to && goog.isNumber(opt_toOffset)) {
167 end = goog.isString(opt_to) ? this.findTextNode(opt_to) : opt_to;
168 endOffset = opt_toOffset;
169 }
170
171 var range =
172 goog.dom.Range.createFromNodes(start, startOffset, end, endOffset);
173 range.select();
174 return range;
175 };
176
177
178 /** @override */
179 goog.testing.editor.TestHelper.prototype.disposeInternal = function() {
180 if (goog.editor.node.isEditableContainer(this.root_)) {
181 this.tearDownEditableElement();
182 }
183 delete this.root_;
184 goog.testing.editor.TestHelper.base(this, 'disposeInternal');
185 };