Index: chrome/browser/resources/print_preview/margin_utils.js |
diff --git a/chrome/browser/resources/print_preview/margin_utils.js b/chrome/browser/resources/print_preview/margin_utils.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..90c9736a2b79951067b191e63466fbeeb9f6f527 |
--- /dev/null |
+++ b/chrome/browser/resources/print_preview/margin_utils.js |
@@ -0,0 +1,82 @@ |
+// Copyright (c) 2011 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. |
+ |
+cr.define('print_preview', function() { |
+ 'use strict'; |
+ |
+ /** |
+ * Checks if |text| has a valid margin value format. A valid format is |
+ * parsable as a number and is greater than zero. |
+ * Example: "1.00", "1", ".5", "1.1" are valid values. |
+ * Example: "1.4dsf", "-1" are invalid. |
+ * Note: The inch symbol (") at the end of |text| is allowed. |
+ * |
+ * @param {string} text The text to check. |
+ * @return {boolean} true if the |text| represents a valid margin value |
+ */ |
+ function extractMarginValue(text) { |
+ // Remove whitespace anywhere in the string. |
+ text.replace(/\s*/g, ''); |
+ // TODO(dpapad): Return -1 instead of null to indicate an invalid value. |
Evan Stade
2011/10/05 22:42:14
is this todo still valid? what's wrong with what i
dpapad
2011/10/06 00:05:39
This TODO is not necessary, i just though it is be
|
+ if (text.length == 0) |
+ return null; |
+ // Remove the inch(") symbol at end of string if present. |
+ if (text.charAt(text.length - 1) == '\"') |
+ text = text.slice(0, text.length - 1); |
+ var regex = /^\d*(\.\d+)?$/ |
+ if (regex.test(text)) |
+ return text; |
+ return null; |
+ } |
+ |
+ /** |
+ * @param {sting} text The text to check (in inches). |
+ * @param {number} limit The upper bound of the valid margin range (in |
+ * points). |
+ * @return {boolean} True of |text| can be parsed and it is within the allowed |
+ * range. |
+ */ |
+ function isMarginTextValid(text, limit) { |
+ var value = extractMarginValue(text); |
+ if (value == null) |
+ return false; |
+ value = print_preview.MarginSettings.convertInchesToPoints(value); |
+ return value <= limit; |
+ } |
+ |
+ /** |
+ * Creates a Rect object. This object describes a rectangle in a 2D plane. |
Evan Stade
2011/10/05 22:42:14
here is where you would mention that the units can
dpapad
2011/10/06 00:05:39
Done.
|
+ * @constructor |
+ */ |
+ function Rect(x, y, width, height) { |
+ this.x_ = x; |
+ this.y_ = y; |
+ this.width_ = width; |
+ this.height_ = height; |
+ }; |
+ |
+ Rect.prototype = { |
+ get x() { |
Evan Stade
2011/10/05 22:42:14
remove the getters you don't need (all but origin)
dpapad
2011/10/06 00:05:39
Done. Removed origin too since it is not used in t
|
+ return this.x_; |
+ }, |
+ get y() { |
+ return this.y_; |
+ }, |
+ get origin() { |
+ return this.origin_; |
+ }, |
+ get width() { |
+ return this.width_; |
+ }, |
+ get height() { |
+ return this.height_; |
+ }, |
+ }; |
+ |
+ return { |
+ extractMarginValue: extractMarginValue, |
+ isMarginTextValid: isMarginTextValid, |
+ Rect: Rect, |
+ }; |
+}); |