| 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 |
goog.provide('goog.editor.styleTest'); |
| 16 |
goog.setTestOnly('goog.editor.styleTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.dom.TagName'); |
| 20 |
goog.require('goog.editor.BrowserFeature'); |
| 21 |
goog.require('goog.editor.style'); |
| 22 |
goog.require('goog.events.EventHandler'); |
| 23 |
goog.require('goog.events.EventType'); |
| 24 |
goog.require('goog.style'); |
| 25 |
goog.require('goog.testing.LooseMock'); |
| 26 |
goog.require('goog.testing.jsunit'); |
| 27 |
goog.require('goog.testing.mockmatchers'); |
| 28 |
|
| 29 |
var parentNode = null; |
| 30 |
var childNode1 = null; |
| 31 |
var childNode2 = null; |
| 32 |
var childNode3 = null; |
| 33 |
var gChildWsNode1 = null; |
| 34 |
var gChildTextNode1 = null; |
| 35 |
var gChildNbspNode1 = null; |
| 36 |
var gChildMixedNode1 = null; |
| 37 |
var gChildWsNode2a = null; |
| 38 |
var gChildWsNode2b = null; |
| 39 |
var gChildTextNode3a = null; |
| 40 |
var gChildWsNode3 = null; |
| 41 |
var gChildTextNode3b = null; |
| 42 |
|
| 43 |
var $dom = goog.dom.createDom; |
| 44 |
var $text = goog.dom.createTextNode; |
| 45 |
|
| 46 |
function setUpGetNodeFunctions() { |
| 47 |
parentNode = $dom( |
| 48 |
goog.dom.TagName.P, {id: 'parentNode'}, |
| 49 |
childNode1 = $dom( |
| 50 |
goog.dom.TagName.DIV, null, gChildWsNode1 = $text(' \t\r\n'), |
| 51 |
gChildTextNode1 = $text('Child node'), |
| 52 |
gChildNbspNode1 = $text('\u00a0'), |
| 53 |
gChildMixedNode1 = $text('Text\n plus\u00a0')), |
| 54 |
childNode2 = $dom( |
| 55 |
goog.dom.TagName.DIV, null, gChildWsNode2a = $text(''), |
| 56 |
gChildWsNode2b = $text(' ')), |
| 57 |
childNode3 = $dom( |
| 58 |
goog.dom.TagName.DIV, null, |
| 59 |
gChildTextNode3a = $text('I am a grand child'), |
| 60 |
gChildWsNode3 = $text(' \t \r \n'), |
| 61 |
gChildTextNode3b = $text('I am also a grand child'))); |
| 62 |
|
| 63 |
document.body.appendChild(parentNode); |
| 64 |
} |
| 65 |
|
| 66 |
function tearDownGetNodeFunctions() { |
| 67 |
document.body.removeChild(parentNode); |
| 68 |
|
| 69 |
parentNode = null; |
| 70 |
childNode1 = null; |
| 71 |
childNode2 = null; |
| 72 |
childNode3 = null; |
| 73 |
gChildWsNode1 = null; |
| 74 |
gChildTextNode1 = null; |
| 75 |
gChildNbspNode1 = null; |
| 76 |
gChildMixedNode1 = null; |
| 77 |
gChildWsNode2a = null; |
| 78 |
gChildWsNode2b = null; |
| 79 |
gChildTextNode3a = null; |
| 80 |
gChildWsNode3 = null; |
| 81 |
gChildTextNode3b = null; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
/** |
| 86 |
* Test isBlockLevel with a node that is block style and a node that is not |
| 87 |
*/ |
| 88 |
function testIsDisplayBlock() { |
| 89 |
assertTrue( |
| 90 |
'Body is block style', goog.editor.style.isDisplayBlock(document.body)); |
| 91 |
var tableNode = $dom(goog.dom.TagName.TABLE); |
| 92 |
assertFalse( |
| 93 |
'Table is not block style', goog.editor.style.isDisplayBlock(tableNode)); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
/** |
| 98 |
* Test that isContainer returns true when the node is of non-inline HTML and |
| 99 |
* false when it is not |
| 100 |
*/ |
| 101 |
function testIsContainer() { |
| 102 |
var tableNode = $dom(goog.dom.TagName.TABLE); |
| 103 |
var liNode = $dom(goog.dom.TagName.LI); |
| 104 |
var textNode = $text('I am text'); |
| 105 |
document.body.appendChild(textNode); |
| 106 |
|
| 107 |
assertTrue('Table is a container', goog.editor.style.isContainer(tableNode)); |
| 108 |
assertTrue( |
| 109 |
'Body is a container', goog.editor.style.isContainer(document.body)); |
| 110 |
assertTrue('List item is a container', goog.editor.style.isContainer(liNode)); |
| 111 |
assertFalse( |
| 112 |
'Text node is not a container', goog.editor.style.isContainer(textNode)); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
/** |
| 117 |
* Test that getContainer properly returns the node itself if it is a |
| 118 |
* container, an ancestor node if it is a container, and null otherwise |
| 119 |
*/ |
| 120 |
function testGetContainer() { |
| 121 |
setUpGetNodeFunctions(); |
| 122 |
assertEquals( |
| 123 |
'Should return self', childNode1, |
| 124 |
goog.editor.style.getContainer(childNode1)); |
| 125 |
assertEquals( |
| 126 |
'Should return parent', childNode1, |
| 127 |
goog.editor.style.getContainer(gChildWsNode1)); |
| 128 |
assertNull( |
| 129 |
'Document has no ancestors', goog.editor.style.getContainer(document)); |
| 130 |
tearDownGetNodeFunctions(); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
function testMakeUnselectable() { |
| 135 |
var div = goog.dom.createElement(goog.dom.TagName.DIV); |
| 136 |
div.innerHTML = '<div>No input</div>' + |
| 137 |
'<p><input type="checkbox">Checkbox</p>' + |
| 138 |
'<span><input type="text"></span>'; |
| 139 |
document.body.appendChild(div); |
| 140 |
|
| 141 |
var eventHandler = new goog.testing.LooseMock(goog.events.EventHandler); |
| 142 |
if (goog.editor.BrowserFeature.HAS_UNSELECTABLE_STYLE) { |
| 143 |
eventHandler.listen( |
| 144 |
div, goog.events.EventType.MOUSEDOWN, |
| 145 |
goog.testing.mockmatchers.isFunction, true); |
| 146 |
} |
| 147 |
eventHandler.$replay(); |
| 148 |
|
| 149 |
|
| 150 |
var childDiv = div.firstChild; |
| 151 |
var p = div.childNodes[1]; |
| 152 |
var span = div.lastChild; |
| 153 |
var checkbox = p.firstChild; |
| 154 |
var text = span.firstChild; |
| 155 |
|
| 156 |
goog.editor.style.makeUnselectable(div, eventHandler); |
| 157 |
|
| 158 |
assertEquals( |
| 159 |
'For browsers with non-overridable selectability, the root should be ' + |
| 160 |
'selectable. Otherwise it should be unselectable.', |
| 161 |
!goog.editor.BrowserFeature.HAS_UNSELECTABLE_STYLE, |
| 162 |
goog.style.isUnselectable(div)); |
| 163 |
assertTrue(goog.style.isUnselectable(childDiv)); |
| 164 |
assertTrue(goog.style.isUnselectable(p)); |
| 165 |
assertTrue(goog.style.isUnselectable(checkbox)); |
| 166 |
|
| 167 |
assertEquals( |
| 168 |
'For browsers with non-overridable selectability, the span will be ' + |
| 169 |
'selectable. Otherwise it will be unselectable. ', |
| 170 |
!goog.editor.BrowserFeature.HAS_UNSELECTABLE_STYLE, |
| 171 |
goog.style.isUnselectable(span)); |
| 172 |
assertFalse(goog.style.isUnselectable(text)); |
| 173 |
|
| 174 |
eventHandler.$verify(); |
| 175 |
} |