aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/editor/style_test.html
blob: e1bebccc4081b9067c6b22d463efce8acaff498f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<!DOCTYPE html>
<!--
  All Rights Reserved.

  Author: nicksantos@google.com (Nick Santos)
-->
<html>
<!--
Copyright 2007 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.editor.style</title>
<script src="../base.js"></script>
<script>
  goog.require('goog.editor.BrowserFeature');
  goog.require('goog.editor.style');
  goog.require('goog.events.EventHandler');
  goog.require('goog.events.EventType');
  goog.require('goog.testing.LooseMock');
  goog.require('goog.testing.dom');
  goog.require('goog.testing.jsunit');
  goog.require('goog.testing.mockmatchers');
</script>
</head>
<body>
<script>
var parentNode = null;
var childNode1 = null;
var childNode2 = null;
var childNode3 = null;
var gChildWsNode1 = null;
var gChildTextNode1 = null;
var gChildNbspNode1 = null;
var gChildMixedNode1 = null;
var gChildWsNode2a = null;
var gChildWsNode2b = null;
var gChildTextNode3a = null;
var gChildWsNode3 = null;
var gChildTextNode3b = null;

var $dom = goog.dom.createDom;
var $text = goog.dom.createTextNode;

function setUpGetNodeFunctions() {
  parentNode = $dom(
      'p', {id: 'parentNode'},
      childNode1 = $dom('div', null,
          gChildWsNode1 = $text(' \t\r\n'),
          gChildTextNode1 = $text('Child node'),
          gChildNbspNode1 = $text('\u00a0'),
          gChildMixedNode1 = $text('Text\n plus\u00a0')),
      childNode2 = $dom('div', null,
          gChildWsNode2a = $text(''),
          gChildWsNode2b = $text(' ')),
      childNode3 = $dom('div', null,
          gChildTextNode3a = $text('I am a grand child'),
          gChildWsNode3 = $text('   \t  \r   \n'),
          gChildTextNode3b = $text('I am also a grand child')));

  document.body.appendChild(parentNode);
}

function tearDownGetNodeFunctions() {
  document.body.removeChild(parentNode);

  parentNode = null;
  childNode1 = null;
  childNode2 = null;
  childNode3 = null;
  gChildWsNode1 = null;
  gChildTextNode1 = null;
  gChildNbspNode1 = null;
  gChildMixedNode1 = null;
  gChildWsNode2a = null;
  gChildWsNode2b = null;
  gChildTextNode3a = null;
  gChildWsNode3 = null;
  gChildTextNode3b = null;
}

/**
 * Test isBlockLevel with a node that is block style and a node that is not
 */
function testIsDisplayBlock() {
  assertTrue('Body is block style',
      goog.editor.style.isDisplayBlock(document.body));
  var tableNode = $dom('table');
  assertFalse('Table is not block style',
      goog.editor.style.isDisplayBlock(tableNode));
}

/**
 * Test that isContainer returns true when the node is of non-inline HTML and
 * false when it is not
 */
function testIsContainer() {
  var tableNode = $dom('table');
  var liNode = $dom('li');
  var textNode = $text('I am text');
  document.body.appendChild(textNode);

  assertTrue('Table is a container',
      goog.editor.style.isContainer(tableNode));
  assertTrue('Body is a container',
      goog.editor.style.isContainer(document.body));
  assertTrue('List item is a container',
      goog.editor.style.isContainer(liNode));
  assertFalse('Text node is not a container',
      goog.editor.style.isContainer(textNode));
}

/**
 * Test that getContainer properly returns the node itself if it is a
 * container, an ancestor node if it is a container, and null otherwise
 */
function testGetContainer() {
  setUpGetNodeFunctions();
  assertEquals('Should return self', childNode1,
      goog.editor.style.getContainer(childNode1));
  assertEquals('Should return parent', childNode1,
      goog.editor.style.getContainer(gChildWsNode1));
  assertNull('Document has no ancestors',
      goog.editor.style.getContainer(document));
  tearDownGetNodeFunctions();
}


function testMakeUnselectable() {
  var div = goog.dom.createElement(goog.dom.TagName.DIV);
  div.innerHTML =
      '<div>No input</div>' +
      '<p><input type="checkbox">Checkbox</p>' +
      '<span><input type="text"></span>';
  document.body.appendChild(div);

  var eventHandler = new goog.testing.LooseMock(goog.events.EventHandler);
  if (goog.editor.BrowserFeature.HAS_UNSELECTABLE_STYLE) {
    eventHandler.listen(div, goog.events.EventType.MOUSEDOWN,
        goog.testing.mockmatchers.isFunction, true);
  }
  eventHandler.$replay();


  var childDiv = div.firstChild;
  var p = div.childNodes[1];
  var span = div.lastChild;
  var checkbox = p.firstChild;
  var text = span.firstChild;

  goog.editor.style.makeUnselectable(div, eventHandler);

  assertEquals(
      'For browsers with non-overridable selectability, the root should be ' +
      'selectable.  Otherwise it should be unselectable.',
      !goog.editor.BrowserFeature.HAS_UNSELECTABLE_STYLE,
      goog.style.isUnselectable(div));
  assertTrue(goog.style.isUnselectable(childDiv));
  assertTrue(goog.style.isUnselectable(p));
  assertTrue(goog.style.isUnselectable(checkbox));

  assertEquals(
      'For browsers with non-overridable selectability, the span will be ' +
      'selectable.  Otherwise it will be unselectable. ',
      !goog.editor.BrowserFeature.HAS_UNSELECTABLE_STYLE,
      goog.style.isUnselectable(span));
  assertFalse(goog.style.isUnselectable(text));

  eventHandler.$verify();
}

</script>
</body>
</html>