3 Copyright (c) 2014 The Chromium Authors. All rights reserved.
4 Use of this source code is governed by a BSD-style license that can be
5 found in the LICENSE file.
7 <link rel="import" href="/tvcm.html">
11 tvcm.exportTo('tvcm', function() {
14 * Uses an embedded iframe to measure provided elements without forcing layout
15 * on the main document. You must call attach() on the stick before using it,
16 * and call detach() on it when you are done using it.
20 function MeasuringStick() {
21 this.iframe_ = undefined;
24 MeasuringStick.prototype = {
25 __proto__: Object.prototype,
28 * Measures the provided element without forcing layout on the main
31 measure: function(element) {
32 this.iframe_.contentDocument.body.appendChild(element);
33 var style = this.iframe_.contentWindow.getComputedStyle(element);
34 var width = parseInt(style.width, 10);
35 var height = parseInt(style.height, 10);
36 this.iframe_.contentDocument.body.removeChild(element);
37 return { width: width, height: height };
41 var iframe = document.createElement('iframe');
42 iframe.style.cssText =
43 'position:absolute;width:100%;height:0;border:0;visibility:hidden';
44 document.body.appendChild(iframe);
45 this.iframe_ = iframe;
46 this.iframe_.contentDocument.body.style.cssText =
47 'padding:0;margin:0;overflow:hidden';
49 var stylesheets = document.querySelectorAll('link[rel=stylesheet]');
50 for (var i = 0; i < stylesheets.length; i++) {
51 var stylesheet = stylesheets[i];
52 var link = this.iframe_.contentDocument.createElement('link');
53 link.rel = 'stylesheet';
54 link.href = stylesheet.href;
55 this.iframe_.contentDocument.head.appendChild(link);
60 document.body.removeChild(this.iframe_);
61 this.iframe_ = undefined;
66 MeasuringStick: MeasuringStick