Permalink
Please sign in to comment.
Browse files
Fixes #2193: Implements workaround for https://code.google.com/p/chro…
…mium/issues/detail?id=516550 by adding Polymer.RenderStatus.whenReady and using it to defer `attached`
- Loading branch information...
Showing
with
209 additions
and 60 deletions.
- +8 −2 src/lib/base.html
- +0 −46 src/lib/imports-status.html
- +65 −0 src/lib/render-status.html
- +1 −3 src/lib/template/dom-bind.html
- +1 −0 src/polymer-lib.html
- +1 −0 test/runner.html
- +36 −0 test/smoke/offsetParent-polymer-import.html
- +3 −9 test/smoke/offsetParent.html
- +32 −0 test/unit/attached-style-elements.html
- +62 −0 test/unit/attached-style.html
10
src/lib/base.html
46
src/lib/imports-status.html
| @@ -1,46 +0,0 @@ | ||
| -<!-- | ||
| -@license | ||
| -Copyright (c) 2014 The Polymer Project Authors. All rights reserved. | ||
| -This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | ||
| -The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | ||
| -The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | ||
| -Code distributed by Google as part of the polymer project is also | ||
| -subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | ||
| ---> | ||
| - | ||
| -<script> | ||
| - | ||
| - Polymer.ImportStatus = { | ||
| - | ||
| - _ready: false, | ||
| - | ||
| - _callbacks: [], | ||
| - | ||
| - whenLoaded: function(cb) { | ||
| - if (this._ready) { | ||
| - cb(); | ||
| - } else { | ||
| - this._callbacks.push(cb); | ||
| - } | ||
| - }, | ||
| - | ||
| - _importsLoaded: function() { | ||
| - this._ready = true; | ||
| - this._callbacks.forEach(function(cb) { | ||
| - cb(); | ||
| - }); | ||
| - this._callbacks = []; | ||
| - } | ||
| - }; | ||
| - | ||
| - window.addEventListener('load', function() { | ||
| - Polymer.ImportStatus._importsLoaded(); | ||
| - }); | ||
| - | ||
| - if (window.HTMLImports) { | ||
| - HTMLImports.whenReady(function() { | ||
| - Polymer.ImportStatus._importsLoaded(); | ||
| - }); | ||
| - } | ||
| - | ||
| -</script> |
65
src/lib/render-status.html
| @@ -0,0 +1,65 @@ | ||
| +<!-- | ||
| +@license | ||
| +Copyright (c) 2014 The Polymer Project Authors. All rights reserved. | ||
| +This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | ||
| +The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | ||
| +The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | ||
| +Code distributed by Google as part of the polymer project is also | ||
| +subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | ||
| +--> | ||
| + | ||
| +<script> | ||
| + /* | ||
| + * Helper for determining when first render occurs. | ||
| + * Call `Polymer.RenderStatus.whenReady(callback)` to be notified when | ||
| + * first render occurs or immediately if it has already occured. | ||
| + * Note that since HTML Imports are designed to load before rendering, | ||
| + * this call can also be used to guarantee that imports have loaded. | ||
| + * This behavior is normalized to function correctly with the HTMLImports | ||
| + * polyfill which does not otherwise maintain this rendering guarantee. | ||
| + * Querying style and layout data before first render is currently | ||
| + * problematic on some browsers (Blink/Webkit) so this helper can be used | ||
| + * to prevent doing so until a safe time. | ||
| + */ | ||
| + Polymer.RenderStatus = { | ||
| + | ||
| + _ready: false, | ||
| + | ||
| + _callbacks: [], | ||
| + | ||
| + whenReady: function(cb) { | ||
| + if (this._ready) { | ||
| + cb(); | ||
| + } else { | ||
| + this._callbacks.push(cb); | ||
| + } | ||
| + }, | ||
| + | ||
| + _makeReady: function() { | ||
| + this._ready = true; | ||
| + this._callbacks.forEach(function(cb) { | ||
| + cb(); | ||
| + }); | ||
| + this._callbacks = []; | ||
| + }, | ||
| + | ||
| + _catchFirstRender: function() { | ||
| + requestAnimationFrame(function() { | ||
| + Polymer.RenderStatus._makeReady(); | ||
| + }); | ||
| + } | ||
| + }; | ||
| + | ||
| + if (window.HTMLImports) { | ||
| + HTMLImports.whenReady(function() { | ||
| + Polymer.RenderStatus._catchFirstRender(); | ||
| + }); | ||
| + } else { | ||
| + Polymer.RenderStatus._catchFirstRender(); | ||
| + } | ||
| + | ||
| + // NOTE: for bc. | ||
| + Polymer.ImportStatus = Polymer.RenderStatus; | ||
| + Polymer.ImportStatus.whenLoaded = Polymer.ImportStatus.whenReady; | ||
| + | ||
| +</script> |
4
src/lib/template/dom-bind.html
1
src/polymer-lib.html
1
test/runner.html
36
test/smoke/offsetParent-polymer-import.html
| @@ -0,0 +1,36 @@ | ||
| +<!-- | ||
| +@license | ||
| +Copyright (c) 2014 The Polymer Project Authors. All rights reserved. | ||
| +This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | ||
| +The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | ||
| +The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | ||
| +Code distributed by Google as part of the polymer project is also | ||
| +subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | ||
| +--> | ||
| +<link rel="import" href="../../polymer.html"> | ||
| + | ||
| +<dom-module id="some-thing"> | ||
| + <script> | ||
| + Polymer({ | ||
| + | ||
| + is: 'some-thing', | ||
| + | ||
| + created: function() { | ||
| + console.warn(this.localName, 'created'); | ||
| + }, | ||
| + | ||
| + attached: function() { | ||
| + this.report(); | ||
| + }, | ||
| + | ||
| + report: function() { | ||
| + console.group(this.localName, 'report'); | ||
| + console.log('offsetParent is', this.offsetParent); | ||
| + console.log('parentNode', this.parentNode, 'has position', | ||
| + window.getComputedStyle(this.parentNode).position); | ||
| + console.groupEnd(this.localName); | ||
| + } | ||
| + | ||
| + }); | ||
| +</script> | ||
| +</dom-module> |
12
test/smoke/offsetParent.html
32
test/unit/attached-style-elements.html
| @@ -0,0 +1,32 @@ | ||
| +<!-- | ||
| +@license | ||
| +Copyright (c) 2014 The Polymer Project Authors. All rights reserved. | ||
| +This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | ||
| +The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | ||
| +The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | ||
| +Code distributed by Google as part of the polymer project is also | ||
| +subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | ||
| +--> | ||
| +<dom-module id="attached-style"> | ||
| + <style> | ||
| + :host { | ||
| + display: block; | ||
| + box-sizing: border-box; | ||
| + border: 1px solid black; | ||
| + height: 100px; | ||
| + } | ||
| + </style> | ||
| + <template>Hi</template> | ||
| + <script> | ||
| + Polymer({ | ||
| + | ||
| + is: 'attached-style', | ||
| + | ||
| + attached: function() { | ||
| + this.offsetParentAtAttachedTime = this.offsetParent; | ||
| + this.offsetHeightAtAttachedTime = this.offsetHeight; | ||
| + } | ||
| + | ||
| + }); | ||
| + </script> | ||
| +</dom-module> |
62
test/unit/attached-style.html
| @@ -0,0 +1,62 @@ | ||
| +<!DOCTYPE html> | ||
| +<!-- | ||
| +@license | ||
| +Copyright (c) 2015 The Polymer Project Authors. All rights reserved. | ||
| +This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | ||
| +The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | ||
| +The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | ||
| +Code distributed by Google as part of the polymer project is also | ||
| +subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | ||
| +--> | ||
| +<html> | ||
| +<head> | ||
| + <meta charset="UTF-8"> | ||
| + | ||
| + <script src="../../../webcomponentsjs/webcomponents-lite.js"></script> | ||
| + <script> | ||
| + addEventListener('WebComponentsReady', function() { | ||
| + window.elementsReady = true; | ||
| + // Record layout data at WebComponentsReady time for later testing | ||
| + var el = document.querySelector('attached-style'); | ||
| + el.offsetParentAtWebComponentsReadyTime = el.offsetParent; | ||
| + el.offsetHeightAtWebComponentsReadyTime = el.offsetHeight; | ||
| + }); | ||
| + </script> | ||
| + <script src="../../../web-component-tester/browser.js"></script> | ||
| + | ||
| + <link rel="import" href="../../polymer.html"> | ||
| + <link rel="import" href="attached-style-elements.html"> | ||
| +</head> | ||
| +<body> | ||
| + | ||
| + <attached-style></attached-style> | ||
| + | ||
| + <script> | ||
| + | ||
| + suite('Attached can get computed style data', function() { | ||
| + | ||
| + var el = document.querySelector('attached-style'); | ||
| + | ||
| + test('style data available at attached time', function() { | ||
| + assert.equal(el.offsetParentAtAttachedTime, document.body); | ||
| + assert.equal(el.offsetHeightAtAttachedTime, 100); | ||
| + }); | ||
| + | ||
| + test('style data available at WebComponentsReady time', function(done) { | ||
| + function finish() { | ||
| + assert.equal(el.offsetParentAtWebComponentsReadyTime, document.body); | ||
| + assert.equal(el.offsetHeightAtWebComponentsReadyTime, 100); | ||
| + done(); | ||
| + } | ||
| + if (!window.elementsReady) { | ||
| + addEventListener('WebComponentsReady', finish); | ||
| + } else { | ||
| + finish(); | ||
| + } | ||
| + }) | ||
| + | ||
| + }); | ||
| + </script> | ||
| + | ||
| +</body> | ||
| +</html> |
0 comments on commit
2bffc4c