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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
|
// Copyright 2010 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
goog.provide('goog.cssom.iframe.styleTest');
goog.setTestOnly('goog.cssom.iframe.styleTest');
goog.require('goog.cssom');
goog.require('goog.cssom.iframe.style');
goog.require('goog.dom');
goog.require('goog.dom.DomHelper');
goog.require('goog.dom.TagName');
goog.require('goog.testing.jsunit');
goog.require('goog.userAgent');
// unit tests
var propertiesToTest = [
'color', 'font-family', 'font-style', 'font-size', 'font-variant',
'border-top-style', 'border-top-width', 'border-top-color',
'background-color', 'margin-bottom'
];
function crawlDom(startNode, func) {
if (startNode.nodeType != 1) {
return;
}
func(startNode);
for (var i = 0; i < startNode.childNodes.length; i++) {
crawlDom(startNode.childNodes[i], func);
}
}
function getCurrentCssProperties(node, propList) {
var props = {};
if (node.nodeType != 1) {
return;
}
for (var i = 0; i < propList.length; i++) {
var prop = propList[i];
if (node.currentStyle) { // IE
var propCamelCase = '';
var propParts = prop.split('-');
for (var j = 0; j < propParts.length; j++) {
propCamelCase += propParts[j].charAt(0).toUpperCase() +
propParts[j].substring(1, propParts[j].length);
}
props[prop] = node.currentStyle[propCamelCase];
} else { // standards-compliant browsers
props[prop] = node.ownerDocument.defaultView.getComputedStyle(node, '')
.getPropertyValue(prop);
}
}
return props;
}
function CssPropertyCollector() {
var propsList = [];
this.propsList = propsList;
this.collectProps = function(node) {
var nodeProps = getCurrentCssProperties(node, propertiesToTest);
if (nodeProps) {
propsList.push([nodeProps, node]);
}
};
}
function recursivelyListCssProperties(el) {
var collector = new CssPropertyCollector();
crawlDom(el, collector.collectProps);
return collector.propsList;
}
function testMatchCssSelector() {
var container = goog.dom.createElement(goog.dom.TagName.DIV);
container.className = 'container';
var el = goog.dom.createElement(goog.dom.TagName.DIV);
x = el;
el.id = 'mydiv';
el.className = 'colorful foo';
// set some arbirtrary content
el.innerHTML = '<div><ul><li>One</li><li>Two</li></ul></div>';
container.appendChild(el);
document.body.appendChild(container);
var elementAncestry = new goog.cssom.iframe.style.NodeAncestry_(el);
assertEquals(5, elementAncestry.nodes.length);
// list of input/output results. Output is the index of the selector
// that we expect to match - for example, in 'body div div.colorful',
// 'div.colorful' has an index of 2.
var expectedResults = [
['body div', [4, 1]], ['h1', null], ['body div h1', [4, 1]],
['body div.colorful h1', [4, 1]], ['body div div', [4, 2]],
['body div div div', [4, 2]], ['body div div.somethingelse div', [4, 1]],
['body div.somethingelse div', [2, 0]], ['div.container', [3, 0]],
['div.container div', [4, 1]], ['#mydiv', [4, 0]], ['div#mydiv', [4, 0]],
['div.colorful', [4, 0]], ['div#mydiv .colorful', [4, 0]],
['.colorful', [4, 0]], ['body * div', [4, 2]], ['body * *', [4, 2]]
];
for (var i = 0; i < expectedResults.length; i++) {
var input = expectedResults[i][0];
var expectedResult = expectedResults[i][1];
var selector = new goog.cssom.iframe.style.CssSelector_(input);
var result = selector.matchElementAncestry(elementAncestry);
if (expectedResult == null) {
assertEquals('Expected null result', expectedResult, result);
} else {
assertEquals(
'Expected element index for ' + input, expectedResult[0],
result.elementIndex);
assertEquals(
'Expected selector part index for ' + input, expectedResult[1],
result.selectorPartIndex);
}
}
document.body.removeChild(container);
}
function makeIframeDocument(iframe) {
var doc = goog.dom.getFrameContentDocument(iframe);
doc.open();
doc.write('<html><head>');
doc.write('<style>html,body { background-color: transparent; }</style>');
doc.write('</head><body></body></html>');
doc.close();
return doc;
}
function testCopyCss() {
for (var i = 1; i <= 4; i++) {
var sourceElement = document.getElementById('source' + i);
var newFrame = goog.dom.createElement(goog.dom.TagName.IFRAME);
newFrame.allowTransparency = true;
sourceElement.parentNode.insertBefore(newFrame, sourceElement.nextSibling);
var doc = makeIframeDocument(newFrame);
goog.cssom.addCssText(
goog.cssom.iframe.style.getElementContext(sourceElement),
new goog.dom.DomHelper(doc));
doc.body.innerHTML = sourceElement.innerHTML;
var oldProps = recursivelyListCssProperties(sourceElement);
var newProps = recursivelyListCssProperties(doc.body);
assertEquals(oldProps.length, newProps.length);
for (var j = 0; j < oldProps.length; j++) {
for (var k = 0; k < propertiesToTest.length; k++) {
assertEquals(
'testing property ' + propertiesToTest[k],
oldProps[j][0][propertiesToTest[k]],
newProps[j][0][propertiesToTest[k]]);
}
}
}
}
function normalizeCssText(cssText) {
// Normalize cssText for testing purposes.
return cssText.replace(/\s/g, '').toLowerCase();
}
function testAImportantInFF2() {
var testDiv = document.getElementById('source1');
var cssText =
normalizeCssText(goog.cssom.iframe.style.getElementContext(testDiv));
var color = standardizeCSSValue('color', 'red');
var NORMAL_RULE = 'a{color:' + color;
var FF_2_RULE = 'a{color:' + color + '!important';
if (goog.userAgent.GECKO && !goog.userAgent.isVersionOrHigher('1.9a')) {
assertContains(FF_2_RULE, cssText);
} else {
assertContains(NORMAL_RULE, cssText);
assertNotContains(FF_2_RULE, cssText);
}
}
function testCopyBackgroundContext() {
var testDiv = document.getElementById('backgroundTest');
var cssText = goog.cssom.iframe.style.getElementContext(testDiv, null, true);
var iframe = goog.dom.createElement(goog.dom.TagName.IFRAME);
var ancestor = document.getElementById('backgroundTest-ancestor-1');
ancestor.parentNode.insertBefore(iframe, ancestor.nextSibling);
iframe.style.width = '100%';
iframe.style.height = '100px';
iframe.style.borderWidth = '0px';
var doc = makeIframeDocument(iframe);
goog.cssom.addCssText(cssText, new goog.dom.DomHelper(doc));
doc.body.innerHTML = testDiv.innerHTML;
var normalizedCssText = normalizeCssText(cssText);
assertTrue(
'Background color should be copied from parent element',
/body{[^{]*background-color:(?:rgb\(128,0,128\)|#800080)/.test(
normalizedCssText));
assertTrue(
'Background image should be copied from ancestor element',
/body{[^{]*background-image:url\(/.test(normalizedCssText));
// Background-position can't be calculated in FF2, due to this bug:
// http://bugzilla.mozilla.org/show_bug.cgi?id=316981
if (!(goog.userAgent.GECKO && !goog.userAgent.isVersionOrHigher('1.9'))) {
// Expected x position is:
// originalBackgroundPositionX - elementOffsetLeft
// 40px - (1px + 8px) == 31px
// Expected y position is:
// originalBackgroundPositionY - elementOffsetLeft
// 70px - (1px + 10px + 5px) == 54px;
assertTrue(
'Background image position should be adjusted correctly',
/body{[^{]*background-position:31px54px/.test(normalizedCssText));
}
}
function testCopyBackgroundContextFromIframe() {
var testDiv = document.getElementById('backgroundTest');
var iframe = goog.dom.createElement(goog.dom.TagName.IFRAME);
iframe.allowTransparency = true;
iframe.style.position = 'absolute';
iframe.style.top = '5px';
iframe.style.left = '5px';
iframe.style.borderWidth = '2px';
iframe.style.borderStyle = 'solid';
testDiv.appendChild(iframe);
var doc = makeIframeDocument(iframe);
doc.body.backgroundColor = 'transparent';
doc.body.style.margin = '0';
doc.body.style.padding = '0';
doc.body.innerHTML = '<p style="margin: 0">I am transparent!</p>';
var normalizedCssText = normalizeCssText(
goog.cssom.iframe.style.getElementContext(
doc.body.firstChild, null, true));
// Background properties should get copied through from the parent
// document since the iframe is transparent
assertTrue(
'Background color should be copied from parent element',
/body{[^{]*background-color:(?:rgb\(128,0,128\)|#800080)/.test(
normalizedCssText));
assertTrue(
'Background image should be copied from ancestor element',
/body{[^{]*background-image:url\(/.test(normalizedCssText));
// Background-position can't be calculated in FF2, due to this bug:
// http://bugzilla.mozilla.org/show_bug.cgi?id=316981
if (!(goog.userAgent.GECKO && !goog.userAgent.isVersionOrHigher('1.9'))) {
// Image offset should have been calculated to be the same as the
// above example, but adding iframe offset and borderWidth.
// Expected x position is:
// originalBackgroundPositionX - elementOffsetLeft
// 40px - (1px + 8px + 5px + 2px) == 24px
// Expected y position is:
// originalBackgroundPositionY - elementOffsetLeft
// 70px - (1px + 10px + 5px + 5px + 2px) == 47px;
assertTrue(
'Background image position should be adjusted correctly',
!!/body{[^{]*background-position:24px47px/.exec(normalizedCssText));
}
iframe.parentNode.removeChild(iframe);
}
function testCopyFontFaceRules() {
var isFontFaceCssomSupported = goog.userAgent.WEBKIT ||
goog.userAgent.OPERA ||
(goog.userAgent.GECKO && goog.userAgent.isVersionOrHigher('1.9.1'));
// We cannot use goog.testing.ExpectedFailures since it dynamically
// brings in CSS which causes the background context tests to fail
// in IE6.
if (isFontFaceCssomSupported) {
var cssText = goog.cssom.iframe.style.getElementContext(
document.getElementById('cavalier'));
assertTrue(
'The font face rule should have been copied correctly',
/@font-face/.test(cssText));
}
}
|