Upstream version 11.39.266.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / third_party / tvcm / src / tvcm / measuring_stick.html
1 <!DOCTYPE html>
2 <!--
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.
6 -->
7 <link rel="import" href="/tvcm.html">
8 <script>
9 'use strict';
10
11 tvcm.exportTo('tvcm', function() {
12
13   /**
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.
17    * @constructor
18    * @extends {Object}
19    */
20   function MeasuringStick() {
21     this.iframe_ = undefined;
22   }
23
24   MeasuringStick.prototype = {
25     __proto__: Object.prototype,
26
27     /**
28      * Measures the provided element without forcing layout on the main
29      * document.
30      */
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 };
38     },
39
40     attach: function() {
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';
48
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);
56       }
57     },
58
59     detach: function() {
60       document.body.removeChild(this.iframe_);
61       this.iframe_ = undefined;
62     }
63   };
64
65   return {
66     MeasuringStick: MeasuringStick
67   };
68 });
69 </script>