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..907712545b1f58524fa62afbf49091bf02d9c7fa |
| --- /dev/null |
| +++ b/chrome/browser/resources/print_preview/margin_utils.js |
| @@ -0,0 +1,110 @@ |
| +// 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. |
| + 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); |
| + // TODO(dpapad): convert |value| to number before comparing. |
|
Evan Stade
2011/10/05 03:05:23
I don't understand this comment, if value is not a
dpapad
2011/10/05 16:39:49
It is already of type number. The comment is obsol
|
| + return value <= limit; |
| + } |
| + |
| + /** |
| + * Creates a Point object. This object describes a point in a 2D plane. |
| + * @constructor |
| + */ |
| + function Point(x, y) { |
|
Evan Stade
2011/10/05 03:05:23
what is the point of point? why not just do var po
dpapad
2011/10/05 16:39:49
Having a class instead of creating an object on th
|
| + this.x_ = x; |
| + this.y_ = y; |
| + }; |
| + |
| + Point.prototype = { |
| + get x() { |
| + return this.x_; |
| + }, |
| + |
| + get y() { |
| + return this.y_; |
| + }, |
| + |
| + set x(x) { |
| + this.x_ = x;; |
| + }, |
| + |
| + set y(y) { |
| + return this.y_ = y; |
| + } |
| + }; |
| + |
| + /** |
| + * Creates a Rect object. This object describes a rectangle in a 2D plane. |
| + * @constructor |
| + */ |
| + function Rect(x, y, width, height) { |
| + this.origin_ = new Point(x, y); |
| + this.width_ = width; |
| + this.height_ = height; |
| + }; |
| + |
| + Rect.prototype = { |
| + get x() { |
| + return this.origin_.x; |
| + }, |
| + get y() { |
| + return this.origin_.y; |
| + }, |
| + get origin() { |
| + return this.origin_; |
| + }, |
| + get width() { |
| + return this.width_; |
| + }, |
| + get height() { |
| + return this.height_; |
| + }, |
| + }; |
| + |
| + return { |
| + extractMarginValue: extractMarginValue, |
| + isMarginTextValid: isMarginTextValid, |
| + Point: Point, |
| + Rect: Rect, |
| + }; |
| +}); |