Chromium Code Reviews| 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..d38ab44cfba1033e75b1f529b3917a5cd2e8488e |
| --- /dev/null |
| +++ b/chrome/browser/resources/print_preview/margin_utils.js |
| @@ -0,0 +1,69 @@ |
| +// 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 {number} The margin value represented by |text| or null if |text| |
| + * does not represent a valid number. |
| + */ |
| + function extractMarginValue(text) { |
| + // Remove whitespace anywhere in the string. |
| + text.replace(/\s*/g, ''); |
| + if (text.length == 0) |
| + return null; |
|
Evan Stade
2011/10/07 03:29:58
why didn't you make this -1?
dpapad
2011/10/07 16:49:20
Done.
|
| + // 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 parseFloat(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) |
|
Evan Stade
2011/10/07 03:29:58
I don't think this will work, since 0 == null
dpapad
2011/10/07 16:49:20
(0 == null) returns false when I tried in the cons
|
| + return false; |
| + value = convertInchesToPoints(value); |
| + return value <= limit; |
| + } |
| + |
| + /** |
| + * Creates a Rect object. This object describes a rectangle in a 2D plane. The |
| + * units of |x|, |y|, |width|, |height| are chosen by clients of this class. |
| + * @constructor |
| + */ |
| + function Rect(x, y, width, height) { |
| + // @type {number} Horizontal distance of the upper left corner from origin. |
| + this.x = x; |
| + // @type {number} Vertical distance of the upper left corner from origin. |
| + this.y = y; |
| + // @type {number} Width of |this| rectangle. |
| + this.width = width; |
| + // @type {number} Height of |this| rectangle. |
| + this.height = height; |
| + }; |
| + |
| + return { |
| + extractMarginValue: extractMarginValue, |
| + isMarginTextValid: isMarginTextValid, |
| + Rect: Rect, |
| + }; |
| +}); |