| 1 |
// Copyright 2009 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 |
goog.provide('goog.editor.plugins.BlockquoteTest'); |
| 16 |
goog.setTestOnly('goog.editor.plugins.BlockquoteTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.dom.Range'); |
| 20 |
goog.require('goog.dom.TagName'); |
| 21 |
goog.require('goog.editor.BrowserFeature'); |
| 22 |
goog.require('goog.editor.plugins.Blockquote'); |
| 23 |
goog.require('goog.testing.editor.FieldMock'); |
| 24 |
goog.require('goog.testing.editor.TestHelper'); |
| 25 |
goog.require('goog.testing.jsunit'); |
| 26 |
|
| 27 |
var SPLIT = '<span id="split-point"></span>'; |
| 28 |
var root, helper, field, plugin; |
| 29 |
|
| 30 |
function setUp() { |
| 31 |
root = goog.dom.getElement('root'); |
| 32 |
helper = new goog.testing.editor.TestHelper(root); |
| 33 |
field = new goog.testing.editor.FieldMock(); |
| 34 |
|
| 35 |
helper.setUpEditableElement(); |
| 36 |
} |
| 37 |
|
| 38 |
function tearDown() { |
| 39 |
field.$verify(); |
| 40 |
helper.tearDownEditableElement(); |
| 41 |
} |
| 42 |
|
| 43 |
function createPlugin(requireClassname, opt_paragraphMode) { |
| 44 |
field.queryCommandValue('+defaultTag') |
| 45 |
.$anyTimes() |
| 46 |
.$returns(opt_paragraphMode ? goog.dom.TagName.P : undefined); |
| 47 |
|
| 48 |
plugin = new goog.editor.plugins.Blockquote(requireClassname); |
| 49 |
plugin.registerFieldObject(field); |
| 50 |
plugin.enable(field); |
| 51 |
} |
| 52 |
|
| 53 |
function execCommand() { |
| 54 |
field.$replay(); |
| 55 |
|
| 56 |
// With splitPoint we try to mimic the behavior of EnterHandler's |
| 57 |
// deleteCursorSelection_. |
| 58 |
var splitPoint = goog.dom.getElement('split-point'); |
| 59 |
var position = goog.editor.BrowserFeature.HAS_W3C_RANGES ? |
| 60 |
{node: splitPoint.nextSibling, offset: 0} : |
| 61 |
splitPoint; |
| 62 |
if (goog.editor.BrowserFeature.HAS_W3C_RANGES) { |
| 63 |
goog.dom.removeNode(splitPoint); |
| 64 |
goog.dom.Range.createCaret(position.node, 0).select(); |
| 65 |
} else { |
| 66 |
goog.dom.Range.createCaret(position, 0).select(); |
| 67 |
} |
| 68 |
|
| 69 |
var result = plugin.execCommand( |
| 70 |
goog.editor.plugins.Blockquote.SPLIT_COMMAND, position); |
| 71 |
if (!goog.editor.BrowserFeature.HAS_W3C_RANGES) { |
| 72 |
goog.dom.removeNode(splitPoint); |
| 73 |
} |
| 74 |
|
| 75 |
return result; |
| 76 |
} |
| 77 |
|
| 78 |
function testSplitBlockquoteDoesNothingWhenNotInBlockquote() { |
| 79 |
root.innerHTML = '<div>Test' + SPLIT + 'ing</div>'; |
| 80 |
|
| 81 |
createPlugin(false); |
| 82 |
assertFalse(execCommand()); |
| 83 |
helper.assertHtmlMatches('<div>Testing</div>'); |
| 84 |
} |
| 85 |
|
| 86 |
function testSplitBlockquoteDoesNothingWhenNotInBlockquoteWithClass() { |
| 87 |
root.innerHTML = '<blockquote>Test' + SPLIT + 'ing</blockquote>'; |
| 88 |
|
| 89 |
createPlugin(true); |
| 90 |
assertFalse(execCommand()); |
| 91 |
helper.assertHtmlMatches('<blockquote>Testing</blockquote>'); |
| 92 |
} |
| 93 |
|
| 94 |
function testSplitBlockquoteInBlockquoteWithoutClass() { |
| 95 |
root.innerHTML = '<blockquote>Test' + SPLIT + 'ing</blockquote>'; |
| 96 |
|
| 97 |
createPlugin(false); |
| 98 |
assertTrue(execCommand()); |
| 99 |
helper.assertHtmlMatches( |
| 100 |
'<blockquote>Test</blockquote>' + |
| 101 |
'<div>' + (goog.editor.BrowserFeature.HAS_W3C_RANGES ? ' ' : '') + |
| 102 |
'</div>' + |
| 103 |
'<blockquote>ing</blockquote>'); |
| 104 |
} |
| 105 |
|
| 106 |
function testSplitBlockquoteInBlockquoteWithoutClassInParagraphMode() { |
| 107 |
root.innerHTML = '<blockquote>Test' + SPLIT + 'ing</blockquote>'; |
| 108 |
|
| 109 |
createPlugin(false, true); |
| 110 |
assertTrue(execCommand()); |
| 111 |
helper.assertHtmlMatches( |
| 112 |
'<blockquote>Test</blockquote>' + |
| 113 |
'<p>' + (goog.editor.BrowserFeature.HAS_W3C_RANGES ? ' ' : '') + |
| 114 |
'</p>' + |
| 115 |
'<blockquote>ing</blockquote>'); |
| 116 |
} |
| 117 |
|
| 118 |
function testSplitBlockquoteInBlockquoteWithClass() { |
| 119 |
root.innerHTML = |
| 120 |
'<blockquote class="tr_bq">Test' + SPLIT + 'ing</blockquote>'; |
| 121 |
|
| 122 |
createPlugin(true); |
| 123 |
assertTrue(execCommand()); |
| 124 |
|
| 125 |
helper.assertHtmlMatches( |
| 126 |
'<blockquote class="tr_bq">Test</blockquote>' + |
| 127 |
'<div>' + (goog.editor.BrowserFeature.HAS_W3C_RANGES ? ' ' : '') + |
| 128 |
'</div>' + |
| 129 |
'<blockquote class="tr_bq">ing</blockquote>'); |
| 130 |
} |
| 131 |
|
| 132 |
function testSplitBlockquoteInBlockquoteWithClassInParagraphMode() { |
| 133 |
root.innerHTML = |
| 134 |
'<blockquote class="tr_bq">Test' + SPLIT + 'ing</blockquote>'; |
| 135 |
|
| 136 |
createPlugin(true, true); |
| 137 |
assertTrue(execCommand()); |
| 138 |
helper.assertHtmlMatches( |
| 139 |
'<blockquote class="tr_bq">Test</blockquote>' + |
| 140 |
'<p>' + (goog.editor.BrowserFeature.HAS_W3C_RANGES ? ' ' : '') + |
| 141 |
'</p>' + |
| 142 |
'<blockquote class="tr_bq">ing</blockquote>'); |
| 143 |
} |
| 144 |
|
| 145 |
function testIsSplittableBlockquoteWhenRequiresClassNameToSplit() { |
| 146 |
createPlugin(true); |
| 147 |
|
| 148 |
var blockquoteWithClassName = |
| 149 |
goog.dom.createDom(goog.dom.TagName.BLOCKQUOTE, 'tr_bq'); |
| 150 |
assertTrue( |
| 151 |
'blockquote should be detected as splittable', |
| 152 |
plugin.isSplittableBlockquote(blockquoteWithClassName)); |
| 153 |
|
| 154 |
var blockquoteWithoutClassName = |
| 155 |
goog.dom.createDom(goog.dom.TagName.BLOCKQUOTE, 'foo'); |
| 156 |
assertFalse( |
| 157 |
'blockquote should not be detected as splittable', |
| 158 |
plugin.isSplittableBlockquote(blockquoteWithoutClassName)); |
| 159 |
|
| 160 |
var nonBlockquote = goog.dom.createDom(goog.dom.TagName.SPAN, 'tr_bq'); |
| 161 |
assertFalse( |
| 162 |
'element should not be detected as splittable', |
| 163 |
plugin.isSplittableBlockquote(nonBlockquote)); |
| 164 |
} |
| 165 |
|
| 166 |
function testIsSplittableBlockquoteWhenNotRequiresClassNameToSplit() { |
| 167 |
createPlugin(false); |
| 168 |
|
| 169 |
var blockquoteWithClassName = |
| 170 |
goog.dom.createDom(goog.dom.TagName.BLOCKQUOTE, 'tr_bq'); |
| 171 |
assertTrue( |
| 172 |
'blockquote should be detected as splittable', |
| 173 |
plugin.isSplittableBlockquote(blockquoteWithClassName)); |
| 174 |
|
| 175 |
var blockquoteWithoutClassName = |
| 176 |
goog.dom.createDom(goog.dom.TagName.BLOCKQUOTE, 'foo'); |
| 177 |
assertTrue( |
| 178 |
'blockquote should be detected as splittable', |
| 179 |
plugin.isSplittableBlockquote(blockquoteWithoutClassName)); |
| 180 |
|
| 181 |
var nonBlockquote = goog.dom.createDom(goog.dom.TagName.SPAN, 'tr_bq'); |
| 182 |
assertFalse( |
| 183 |
'element should not be detected as splittable', |
| 184 |
plugin.isSplittableBlockquote(nonBlockquote)); |
| 185 |
} |
| 186 |
|
| 187 |
function testIsSetupBlockquote() { |
| 188 |
createPlugin(false); |
| 189 |
|
| 190 |
var blockquoteWithClassName = |
| 191 |
goog.dom.createDom(goog.dom.TagName.BLOCKQUOTE, 'tr_bq'); |
| 192 |
assertTrue( |
| 193 |
'blockquote should be detected as setup', |
| 194 |
plugin.isSetupBlockquote(blockquoteWithClassName)); |
| 195 |
|
| 196 |
var blockquoteWithoutClassName = |
| 197 |
goog.dom.createDom(goog.dom.TagName.BLOCKQUOTE, 'foo'); |
| 198 |
assertFalse( |
| 199 |
'blockquote should not be detected as setup', |
| 200 |
plugin.isSetupBlockquote(blockquoteWithoutClassName)); |
| 201 |
|
| 202 |
var nonBlockquote = goog.dom.createDom(goog.dom.TagName.SPAN, 'tr_bq'); |
| 203 |
assertFalse( |
| 204 |
'element should not be detected as setup', |
| 205 |
plugin.isSetupBlockquote(nonBlockquote)); |
| 206 |
} |
| 207 |
|
| 208 |
function testIsUnsetupBlockquote() { |
| 209 |
createPlugin(false); |
| 210 |
|
| 211 |
var blockquoteWithClassName = |
| 212 |
goog.dom.createDom(goog.dom.TagName.BLOCKQUOTE, 'tr_bq'); |
| 213 |
assertFalse( |
| 214 |
'blockquote should not be detected as unsetup', |
| 215 |
plugin.isUnsetupBlockquote(blockquoteWithClassName)); |
| 216 |
|
| 217 |
var blockquoteWithoutClassName = |
| 218 |
goog.dom.createDom(goog.dom.TagName.BLOCKQUOTE, 'foo'); |
| 219 |
assertTrue( |
| 220 |
'blockquote should be detected as unsetup', |
| 221 |
plugin.isUnsetupBlockquote(blockquoteWithoutClassName)); |
| 222 |
|
| 223 |
var nonBlockquote = goog.dom.createDom(goog.dom.TagName.SPAN, 'tr_bq'); |
| 224 |
assertFalse( |
| 225 |
'element should not be detected as unsetup', |
| 226 |
plugin.isUnsetupBlockquote(nonBlockquote)); |
| 227 |
} |