1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 base.exportTo('tracing', function() { 6 7 /** 8 * Uses an embedded iframe to measure provided elements without forcing layout 9 * on the main document. You must call attach() on the stick before using it, 10 * and call detach() on it when you are done using it. 11 * @constructor 12 * @extends {Object} 13 */ 14 function MeasuringStick() { 15 this.iframe_ = undefined; 16 } 17 18 MeasuringStick.prototype = { 19 __proto__: Object.prototype, 20 21 /** 22 * Measures the provided element without forcing layout on the main 23 * document. 24 */ 25 measure: function(element) { 26 this.iframe_.contentDocument.body.appendChild(element); 27 var style = this.iframe_.contentWindow.getComputedStyle(element); 28 var width = parseInt(style.width, 10); 29 var height = parseInt(style.height, 10); 30 this.iframe_.contentDocument.body.removeChild(element); 31 return { width: width, height: height }; 32 }, 33 34 attach: function() { 35 var iframe = document.createElement('iframe'); 36 iframe.style.cssText = 37 'width:100%;height:0;border:0;visibility:hidden'; 38 document.body.appendChild(iframe); 39 this.iframe_ = iframe; 40 this.iframe_.contentDocument.body.style.cssText = 41 'padding:0;margin:0;overflow:hidden'; 42 43 var stylesheets = document.querySelectorAll('link[rel=stylesheet]'); 44 for (var i = 0; i < stylesheets.length; i++) { 45 var stylesheet = stylesheets[i]; 46 var link = this.iframe_.contentDocument.createElement('link'); 47 link.rel = 'stylesheet'; 48 link.href = stylesheet.href; 49 this.iframe_.contentDocument.head.appendChild(link); 50 } 51 }, 52 53 detach: function() { 54 document.body.removeChild(this.iframe_); 55 this.iframe_ = undefined; 56 } 57 }; 58 59 return { 60 MeasuringStick: MeasuringStick 61 }; 62 }); 63