PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/closure-library/closure/goog/cssom/iframe/style_test.html

https://github.com/illandril/box2dweb-closure
HTML | 391 lines | 354 code | 31 blank | 6 comment | 0 complexity | 9a401c8e58453031f31e4bb40a15bdde MD5 | raw file
  1. <html>
  2. <!--
  3. Copyright 2010 The Closure Library Authors. All Rights Reserved.
  4. Use of this source code is governed by the Apache License, Version 2.0.
  5. See the COPYING file for details.
  6. -->
  7. <head>
  8. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  9. <script src="../../base.js"></script>
  10. <script>
  11. goog.require('goog.cssom');
  12. goog.require('goog.cssom.iframe.style');
  13. goog.require('goog.testing.jsunit');
  14. </script>
  15. <script>
  16. // unit tests
  17. var propertiesToTest = [
  18. 'color',
  19. 'font-family',
  20. 'font-style',
  21. 'font-size',
  22. 'font-variant',
  23. 'border-top-style',
  24. 'border-top-width',
  25. 'border-top-color',
  26. 'background-color',
  27. 'margin-bottom'
  28. ];
  29. function crawlDom(startNode, func) {
  30. if (startNode.nodeType != 1) { return; }
  31. func(startNode);
  32. for (var i = 0; i < startNode.childNodes.length; i++) {
  33. crawlDom(startNode.childNodes[i], func);
  34. }
  35. }
  36. function getCurrentCssProperties(node, propList) {
  37. var props = {};
  38. if (node.nodeType != 1) { return; }
  39. for (var i = 0; i < propList.length; i++) {
  40. var prop = propList[i];
  41. if (node.currentStyle) { // IE
  42. var propCamelCase = '';
  43. var propParts = prop.split('-');
  44. for (var j = 0; j < propParts.length; j++) {
  45. propCamelCase += propParts[j].charAt(0).toUpperCase() +
  46. propParts[j].substring(1, propParts[j].length);
  47. }
  48. props[prop] = node.currentStyle[propCamelCase];
  49. } else { // standards-compliant browsers
  50. props[prop] = node.ownerDocument.defaultView.getComputedStyle(
  51. node, '').getPropertyValue(prop);
  52. }
  53. }
  54. return props;
  55. }
  56. function CssPropertyCollector(){
  57. var propsList = [];
  58. this.propsList = propsList
  59. this.collectProps = function(node) {
  60. var nodeProps = getCurrentCssProperties(node, propertiesToTest);
  61. if (nodeProps) { propsList.push([nodeProps, node]); }
  62. }
  63. }
  64. function recursivelyListCssProperties(el) {
  65. var collector = new CssPropertyCollector();
  66. crawlDom(el, collector.collectProps);
  67. return collector.propsList;
  68. }
  69. function testMatchCssSelector() {
  70. var container = document.createElement('div');
  71. container.className = 'container';
  72. var el = document.createElement('div');
  73. x = el;
  74. el.id = 'mydiv';
  75. el.className = 'colorful foo';
  76. // set some arbirtrary content
  77. el.innerHTML = '<div><ul><li>One</li><li>Two</li></ul></div>';
  78. container.appendChild(el);
  79. document.body.appendChild(container);
  80. var elementAncestry = new goog.cssom.iframe.style.NodeAncestry_(el);
  81. assertEquals(5, elementAncestry.nodes.length);
  82. // list of input/output results. Output is the index of the selector
  83. // that we expect to match - for example, in 'body div div.colorful',
  84. // 'div.colorful' has an index of 2.
  85. var expectedResults = [
  86. ['body div', [4, 1]],
  87. ['h1', null],
  88. ['body div h1', [4, 1]],
  89. ['body div.colorful h1', [4, 1]],
  90. ['body div div', [4, 2]],
  91. ['body div div div', [4, 2]],
  92. ['body div div.somethingelse div', [4, 1]],
  93. ['body div.somethingelse div', [2, 0]],
  94. ['div.container', [3, 0]],
  95. ['div.container div', [4, 1]],
  96. ['#mydiv', [4, 0]],
  97. ['div#mydiv', [4, 0]],
  98. ['div.colorful', [4, 0]],
  99. ['div#mydiv .colorful', [4, 0]],
  100. ['.colorful', [4, 0]],
  101. ['body * div', [4,2]],
  102. ['body * *', [4,2]]
  103. ];
  104. for (var i = 0; i < expectedResults.length; i++) {
  105. var input = expectedResults[i][0];
  106. var expectedResult = expectedResults[i][1];
  107. var selector = new goog.cssom.iframe.style.CssSelector_(input);
  108. var result = selector.matchElementAncestry(elementAncestry);
  109. if (expectedResult == null) {
  110. assertEquals('Expected null result', expectedResult, result);
  111. } else {
  112. assertEquals('Expected element index for ' + input,
  113. expectedResult[0],
  114. result.elementIndex);
  115. assertEquals('Expected selector part index for ' + input,
  116. expectedResult[1],
  117. result.selectorPartIndex);
  118. }
  119. }
  120. document.body.removeChild(container);
  121. }
  122. function makeIframeDocument(iframe) {
  123. var doc = goog.dom.getFrameContentDocument(iframe);
  124. doc.open();
  125. doc.write('<html><head>');
  126. doc.write('<style>html,body { background-color: transparent; }</style>');
  127. doc.write('</head><body></body></html>');
  128. doc.close();
  129. return doc;
  130. }
  131. function testCopyCss() {
  132. for (var i = 1; i <= 4; i++) {
  133. var sourceElement = document.getElementById('source'+i);
  134. var newFrame = document.createElement('iframe');
  135. newFrame.allowTransparency = true;
  136. sourceElement.parentNode.insertBefore(newFrame,
  137. sourceElement.nextSibling);
  138. var doc = makeIframeDocument(newFrame);
  139. goog.cssom.addCssText(
  140. goog.cssom.iframe.style.getElementContext(sourceElement),
  141. new goog.dom.DomHelper(doc));
  142. doc.body.innerHTML = sourceElement.innerHTML;
  143. var oldProps = recursivelyListCssProperties(sourceElement);
  144. var newProps = recursivelyListCssProperties(doc.body);
  145. assertEquals(oldProps.length, newProps.length);
  146. for (var j=0; j<oldProps.length; j++) {
  147. for (var k=0; k<propertiesToTest.length; k++) {
  148. assertEquals('testing property ' + propertiesToTest[k],
  149. oldProps[j][0][propertiesToTest[k]],
  150. newProps[j][0][propertiesToTest[k]]);
  151. }
  152. }
  153. }
  154. }
  155. function normalizeCssText(cssText) {
  156. // Normalize cssText for testing purposes.
  157. return cssText.replace(/\s/g, '').toLowerCase();
  158. }
  159. function testAImportantInFF2() {
  160. var testDiv = document.getElementById('source1');
  161. var cssText = normalizeCssText(
  162. goog.cssom.iframe.style.getElementContext(testDiv));
  163. var color = standardizeCSSValue('color', 'red');
  164. var NORMAL_RULE = 'a{color:' + color;
  165. var FF_2_RULE = 'a{color:' + color + '!important';
  166. if (goog.userAgent.GECKO && !goog.userAgent.isVersion('1.9a')) {
  167. assertContains(FF_2_RULE, cssText);
  168. } else {
  169. assertContains(NORMAL_RULE, cssText);
  170. assertNotContains(FF_2_RULE, cssText);
  171. }
  172. }
  173. function testCopyBackgroundContext() {
  174. var testDiv = document.getElementById('backgroundTest');
  175. var cssText = goog.cssom.iframe.style.getElementContext(testDiv,
  176. null,
  177. true);
  178. var iframe = document.createElement('iframe');
  179. var ancestor = document.getElementById('backgroundTest-ancestor-1');
  180. ancestor.parentNode.insertBefore(iframe, ancestor.nextSibling);
  181. iframe.style.width = '100%';
  182. iframe.style.height = '100px';
  183. iframe.style.borderWidth = '0px';
  184. var doc = makeIframeDocument(iframe);
  185. goog.cssom.addCssText(cssText, new goog.dom.DomHelper(doc));
  186. doc.body.innerHTML = testDiv.innerHTML;
  187. var normalizedCssText = normalizeCssText(cssText);
  188. assertTrue(
  189. 'Background color should be copied from parent element',
  190. /body{[^{]*background-color:(?:rgb\(128,0,128\)|#800080)/.test(
  191. normalizedCssText));
  192. assertTrue(
  193. 'Background image should be copied from ancestor element',
  194. /body{[^{]*background-image:url\(/.test(normalizedCssText));
  195. // Background-position can't be calculated in FF2, due to this bug:
  196. // http://bugzilla.mozilla.org/show_bug.cgi?id=316981
  197. if (!(goog.userAgent.GECKO && !goog.userAgent.isVersion('1.9'))) {
  198. // Expected x position is:
  199. // originalBackgroundPositionX - elementOffsetLeft
  200. // 40px - (1px + 8px) == 31px
  201. // Expected y position is:
  202. // originalBackgroundPositionY - elementOffsetLeft
  203. // 70px - (1px + 10px + 5px) == 54px;
  204. assertTrue('Background image position should be adjusted correctly',
  205. /body{[^{]*background-position:31px54px/.test(normalizedCssText));
  206. }
  207. }
  208. function testCopyBackgroundContextFromIframe() {
  209. var testDiv = document.getElementById('backgroundTest');
  210. var iframe = document.createElement('iframe');
  211. iframe.allowTransparency = true;
  212. iframe.style.position = 'absolute';
  213. iframe.style.top = '5px';
  214. iframe.style.left = '5px';
  215. iframe.style.borderWidth = '2px';
  216. iframe.style.borderStyle = 'solid';
  217. testDiv.appendChild(iframe);
  218. var doc = makeIframeDocument(iframe);
  219. doc.body.backgroundColor = 'transparent';
  220. doc.body.style.margin = '0';
  221. doc.body.style.padding = '0';
  222. doc.body.innerHTML = '<p style="margin: 0">I am transparent!</p>';
  223. var normalizedCssText = normalizeCssText(
  224. goog.cssom.iframe.style.getElementContext(
  225. doc.body.firstChild, null, true));
  226. // Background properties should get copied through from the parent
  227. // document since the iframe is transparent
  228. assertTrue(
  229. 'Background color should be copied from parent element',
  230. /body{[^{]*background-color:(?:rgb\(128,0,128\)|#800080)/.test(
  231. normalizedCssText));
  232. assertTrue(
  233. 'Background image should be copied from ancestor element',
  234. /body{[^{]*background-image:url\(/.test(normalizedCssText));
  235. // Background-position can't be calculated in FF2, due to this bug:
  236. // http://bugzilla.mozilla.org/show_bug.cgi?id=316981
  237. if (!(goog.userAgent.GECKO && !goog.userAgent.isVersion('1.9'))) {
  238. // Image offset should have been calculated to be the same as the
  239. // above example, but adding iframe offset and borderWidth.
  240. // Expected x position is:
  241. // originalBackgroundPositionX - elementOffsetLeft
  242. // 40px - (1px + 8px + 5px + 2px) == 24px
  243. // Expected y position is:
  244. // originalBackgroundPositionY - elementOffsetLeft
  245. // 70px - (1px + 10px + 5px + 5px + 2px) == 47px;
  246. assertTrue('Background image position should be adjusted correctly',
  247. !!/body{[^{]*background-position:24px47px/.exec(
  248. normalizedCssText))
  249. }
  250. iframe.parentNode.removeChild(iframe);
  251. }
  252. function testCopyFontFaceRules() {
  253. var isFontFaceCssomSupported =
  254. goog.userAgent.WEBKIT ||
  255. goog.userAgent.OPERA ||
  256. (goog.userAgent.GECKO && goog.userAgent.isVersion('1.9.1'));
  257. // We cannot use goog.testing.ExpectedFailures since it dynamically
  258. // brings in CSS which causes the background context tests to fail
  259. // in IE6.
  260. if (isFontFaceCssomSupported) {
  261. var cssText = goog.cssom.iframe.style.getElementContext(
  262. document.getElementById('cavalier'));
  263. assertTrue('The font face rule should have been copied correctly',
  264. /@font-face/.test(cssText));
  265. }
  266. }
  267. </script>
  268. <style type="text/css">
  269. @import url("style_test_import.css");
  270. body { font-family: Verdana; }
  271. div { background-color: #ffc; margin: 10px 0; }
  272. p { margin: 10px 0; }
  273. .boxy { padding: 5px; background-color: #fa0; }
  274. .special .boxy { padding: 10px; background-color: #abef00; }
  275. div div strong { color: red; }
  276. div { line-height: 1.3; }
  277. .wrapper .inner-wrapper { border: 2px solid pink;}
  278. a { color: red; }
  279. #source4 { background-color: #900; }
  280. #backgroundTest-ancestor-1 {
  281. background-color: rgb(128,0,128);
  282. }
  283. #backgroundTest-ancestor-0 {
  284. background-image: url("../../images/blank.gif");
  285. background-position: 40px 70px;
  286. background-repeat: repeat;
  287. background-color: transparent;
  288. padding: 5px 8px;
  289. }
  290. div#backgroundTest-parent {
  291. border: 1px solid black;
  292. background-color: transparent;
  293. }
  294. div#backgroundTest {
  295. position: relative;
  296. background-color: transparent;
  297. height: 100px;
  298. margin-top: 0;
  299. }
  300. </style>
  301. <style type="text/css">
  302. .goog-presently-theme-monochrome {
  303. background-color: black;
  304. color: #CCC;
  305. }
  306. .goog-presently-theme-monochrome a {
  307. color: #FFF;
  308. }
  309. .goog-presently-theme-monochrome p#source4 {
  310. font-variant: small-caps;
  311. }
  312. .italic {
  313. font-style: italic;
  314. }
  315. </style>
  316. <style type="text/css">
  317. @font-face {
  318. font-family: Cavalier;
  319. }
  320. #cavalier {
  321. font-family: Cavalier;
  322. }
  323. </style>
  324. </head>
  325. <body>
  326. <div class="wrapper">
  327. <div class="inner-wrapper">
  328. <div id="source1" class="italic">
  329. hello world
  330. </div>
  331. <div id="source2">
  332. <div>Some <strong>strong</strong> text</div>
  333. <div class="boxy exciting">A box</div>
  334. <div class="inner-wrapper">A wrapper</div>
  335. </div>
  336. <div id="source3" class="special">
  337. Some <strong>strong</strong> text
  338. <div class="boxy">A box</div>
  339. </div>
  340. <div id="ancestor" class="goog-presently-theme-monochrome">
  341. <p id="source4" style="background-color: #900">
  342. Here's a link: <a href="#">my link</a>
  343. </p>
  344. </div>
  345. <div id="backgroundTest-ancestor-1">
  346. <div id="backgroundTest-ancestor-0">
  347. <div id="backgroundTest-parent">
  348. <div id="backgroundTest">
  349. hello
  350. </div>
  351. </div>
  352. </div>
  353. </div>
  354. <div id="cavalier"></div>
  355. </div>
  356. </div>
  357. <br />
  358. </body>
  359. </html>