Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5932)

Unified Diff: chrome/browser/resources/file_manager/js/text_measure.js

Issue 23532018: Files.app: Extract the TextMeasure class to a new file. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed the comment. Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/resources/file_manager/js/text_measure.js
diff --git a/chrome/browser/resources/file_manager/js/text_measure.js b/chrome/browser/resources/file_manager/js/text_measure.js
new file mode 100644
index 0000000000000000000000000000000000000000..45cf842eccf09fbfff699ffb6a972bed87d9ceb1
--- /dev/null
+++ b/chrome/browser/resources/file_manager/js/text_measure.js
@@ -0,0 +1,50 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+'use strict';
+
+/**
+ * TextMeasure constructor.
+ *
+ * TextMeasure is a measure for text that returns the width of text. This
+ * class has a dummy span element. When measuring the width of text, it sets
+ * the text to the element and obtains the element's size by
+ * getBoundingClientRect.
+ *
+ * @constructor
+ * @param {HTMLElement} element Element that has styles of measured text. The
+ * width of text is mesures like as it is rendered in this element.
+ */
+var TextMeasure = function(element) {
+ var doc = element.ownerDocument;
+ this.dummySpan_ = doc.createElement('span');
+ this.dummySpan_ = doc.getElementsByTagName('body')[0].
+ appendChild(this.dummySpan_);
+ this.dummySpan_.style.position = 'absolute';
+ this.dummySpan_.style.visibility = 'hidden';
+ var styles = window.getComputedStyle(element, '');
+ var stylesToBeCopied = [
+ 'fontSize',
+ 'fontStyle',
+ 'fontWeight',
+ 'fontFamily',
+ 'letterSpacing'
+ ];
+ for (var i = 0; i < stylesToBeCopied.length; i++) {
+ this.dummySpan_.style[stylesToBeCopied[i]] = styles[stylesToBeCopied[i]];
+ }
+ Object.seal(this);
+};
+
+/**
+ * Measures the widht of text.
+ *
+ * @param {string} text Text that is measured the width.
+ * @return {number} Width of the specified text.
+ */
+TextMeasure.prototype.getWidth = function(text) {
+ this.dummySpan_.innerText = text;
+ var rect = this.dummySpan_.getBoundingClientRect();
+ return rect ? rect.width : 0;
+};
« no previous file with comments | « chrome/browser/resources/file_manager/js/main_scripts.js ('k') | chrome/browser/resources/file_manager/main.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698