OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 cr.define('print_preview', function() { | |
6 'use strict'; | |
7 | |
8 /** | |
9 * Checks if |text| has a valid margin value format. A valid format is | |
10 * parsable as a number and is greater than zero. | |
11 * Example: "1.00", "1", ".5", "1.1" are valid values. | |
12 * Example: "1.4dsf", "-1" are invalid. | |
13 * Note: The inch symbol (") at the end of |text| is allowed. | |
14 * | |
15 * @param {string} text The text to check. | |
16 * @return {number} The margin value represented by |text| or null if |text| | |
17 * does not represent a valid number. | |
18 */ | |
19 function extractMarginValue(text) { | |
20 // Remove whitespace anywhere in the string. | |
21 text.replace(/\s*/g, ''); | |
22 if (text.length == 0) | |
23 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.
| |
24 // Remove the inch(") symbol at end of string if present. | |
25 if (text.charAt(text.length - 1) == '\"') | |
26 text = text.slice(0, text.length - 1); | |
27 var regex = /^\d*(\.\d+)?$/ | |
28 if (regex.test(text)) | |
29 return parseFloat(text); | |
30 return null; | |
31 } | |
32 | |
33 /** | |
34 * @param {sting} text The text to check (in inches). | |
35 * @param {number} limit The upper bound of the valid margin range (in | |
36 * points). | |
37 * @return {boolean} True of |text| can be parsed and it is within the allowed | |
38 * range. | |
39 */ | |
40 function isMarginTextValid(text, limit) { | |
41 var value = extractMarginValue(text); | |
42 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
| |
43 return false; | |
44 value = convertInchesToPoints(value); | |
45 return value <= limit; | |
46 } | |
47 | |
48 /** | |
49 * Creates a Rect object. This object describes a rectangle in a 2D plane. The | |
50 * units of |x|, |y|, |width|, |height| are chosen by clients of this class. | |
51 * @constructor | |
52 */ | |
53 function Rect(x, y, width, height) { | |
54 // @type {number} Horizontal distance of the upper left corner from origin. | |
55 this.x = x; | |
56 // @type {number} Vertical distance of the upper left corner from origin. | |
57 this.y = y; | |
58 // @type {number} Width of |this| rectangle. | |
59 this.width = width; | |
60 // @type {number} Height of |this| rectangle. | |
61 this.height = height; | |
62 }; | |
63 | |
64 return { | |
65 extractMarginValue: extractMarginValue, | |
66 isMarginTextValid: isMarginTextValid, | |
67 Rect: Rect, | |
68 }; | |
69 }); | |
OLD | NEW |