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

Side by Side Diff: tools/dom/src/CssRectangle.dart

Issue 15507007: Provide cross-browser Rects for box model dimensions for Elements. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 5 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 part of html;
6
7 class _ContentCssRect extends CssRect {
Jennifer Messerly 2013/07/16 22:03:16 might be worth adding a short class comment here a
Emily Fortuna 2013/07/18 22:45:02 Done.
8
9 _ContentCssRect(element) : super(element);
10 num get height => _element.offsetHeight +
11 _addOrSubtractToBoxModel(CssRect._HEIGHT, 'content');
12
13 num get width => _element.offsetWidth +
14 _addOrSubtractToBoxModel(CssRect._WIDTH, 'content');
15
16 /**
17 * Set the height to `newHeight`.
18 *
19 * Values of newHeight that are less than zero are converted to effectively
20 * setting the height to 0. This is equivalent to the `height`
21 * function in jQuery and the calculated `height` css value, converted to a
Jennifer Messerly 2013/07/16 22:03:16 uppercase CSS?
Emily Fortuna 2013/07/18 22:45:02 Done.
22 * num in pixels.
23 */
24 void set height(Dimension newHeight) {
Jennifer Messerly 2013/07/16 22:03:16 hmmm. I'm not sure about having the setter take a
Emily Fortuna 2013/07/18 22:45:02 Done.
25 if (newHeight.value < 0) newHeight = new Dimension.px(0);
26 _element.style.height = newHeight.toString();
27 }
28
29 /**
30 * Set the current computed width in pixels of this element.
31 *
32 * This is equivalent to the `width` function in jQuery and the calculated
33 * `width` css value, converted to a dimensionless num in pixels.
34 */
35 void set width(Dimension newWidth) {
36 if (newWidth.value < 0) newWidth = new Dimension.px(0);
37 _element.style.width = newWidth.toString();
38 }
39
40 num get left() => _element.getBoundingClientRect().left -
Jennifer Messerly 2013/07/16 22:03:16 not sure about the parens here. that looks like ol
Emily Fortuna 2013/07/18 22:45:02 Oh man. Old habits die hard. Fixed.
41 _addOrSubtractToBoxModel(['left'], 'content');
42 num get top() => _element.getBoundingClientRect().top -
43 _addOrSubtractToBoxModel(['top'], 'content');
44 }
45
46 class _ContentCssListRect extends _ContentCssRect {
47 List<Element> _elementList;
Jennifer Messerly 2013/07/16 22:03:16 this could be final?
48
49 _ContentCssListRect(elementList) : super(elementList.first) {
50 _elementList = elementList;
51 }
52
53 /**
54 * Set the height to `newHeight`.
55 *
56 * Values of newHeight that are less than zero are converted to effectively
57 * setting the height to 0. This is equivalent to the `height`
58 * function in jQuery and the calculated `height` css value, converted to a
59 * num in pixels.
60 */
61 void set height(Dimension newHeight) {
62 _elementList.forEach((e) => e.contentEdge.height = newHeight);
63 }
64
65 /**
66 * Set the current computed width in pixels of this element.
67 *
68 * This is equivalent to the `width` function in jQuery and the calculated
69 * `width` css value, converted to a dimensionless num in pixels.
70 */
71 void set width(Dimension newWidth) {
72 _elementList.forEach((e) => e.contentEdge.width = newWidth);
73 }
74 }
75
76 class _PaddingCssRect extends CssRect {
77 _PaddingCssRect(element) : super(element);
78 num get height => _element.offsetHeight +
79 _addOrSubtractToBoxModel(CssRect._HEIGHT, 'padding');
80 num get width => _element.offsetWidth +
81 _addOrSubtractToBoxModel(CssRect._WIDTH, 'padding');
82
83 num get left() => _element.getBoundingClientRect().left -
84 _addOrSubtractToBoxModel(['left'], 'padding');
85 num get top() => _element.getBoundingClientRect().top -
86 _addOrSubtractToBoxModel(['top'], 'padding');
87 }
88
89 class _BorderCssRect extends CssRect {
90 _BorderCssRect(element) : super(element);
91 num get height() => _element.offsetHeight;
92 num get width() => _element.offsetWidth;
93
94 num get left() => _element.getBoundingClientRect().left;
95 num get top() => _element.getBoundingClientRect().top;
96 }
97
98 class _MarginCssRect extends CssRect {
99 _MarginCssRect(element) : super(element);
100 num get height() => _element.offsetHeight +
101 _addOrSubtractToBoxModel(CssRect._HEIGHT, 'margin');
102 num get width() =>
103 _element.offsetWidth + _addOrSubtractToBoxModel(CssRect._WIDTH, 'margin');
104
105 num get left() => _element.getBoundingClientRect().left -
106 _addOrSubtractToBoxModel(['left'], 'margin');
107 num get top() => _element.getBoundingClientRect().top -
108 _addOrSubtractToBoxModel(['top'], 'margin');
109 }
110
111 /**
112 * A class for representing CSS dimensions.
113 *
114 * In contrast to the more general purpose [Rect] class, this class's values are
115 * mutable, so one can change the height of an element programmatically.
116 *
117 * SPECIAL NOTE: Do _not_ use these methods of setting or getting dimensions
Jennifer Messerly 2013/07/16 22:03:16 trivial nit: I think this one is "SPECIAL NOTE" an
Emily Fortuna 2013/07/18 22:45:02 Done.
118 * for animations, as they will trigger a redraw of the layout.
119 */
120 abstract class CssRect extends RectBase implements Rect {
121 Element _element;
122
123 CssRect(this._element);
124
125 num get left();
Jennifer Messerly 2013/07/16 22:03:16 same issue here with parens
Emily Fortuna 2013/07/18 22:45:02 Done.
126
127 num get top();
128
129 /**
130 * The height of this rectangle.
131 *
132 * This is equivalent to the `height` function in jQuery and the calculated
133 * `height` css value, converted to a dimensionless num in pixels. Unlike
134 * [getBoundingClientRect], `height` will return the same numerical width if
135 * the element is hidden or not.
136 */
137 num get height();
138
139 /**
140 * The width of this rectangle.
141 *
142 * This is equivalent to the `width` function in jQuery and the calculated
143 * `width` css value, converted to a dimensionless num in pixels. Unlike
144 * [getBoundingClientRect], `width` will return the same numerical width if
145 * the element is hidden or not.
146 */
147 num get width();
148
149 /**
150 * Set the height to `newHeight`.
151 *
152 * Values of newHeight that are less than zero are converted to effectively
153 * setting the height to 0. Note that only the content height can be set via
154 * this method.
155 */
156 void set height(Dimension newHeight) {
157 throw new UnsupportedError("Can only set height for content rect.");
158 }
159
160 /**
161 * Set the current computed width in pixels of this element.
162 *
163 * Note that only the content width can be set via this method.
164 */
165 void set width(Dimension newWidth) {
166 throw new UnsupportedError("Can only set width for content rect.");
167 }
168
169 /**
170 * Return a value that is used to modify the initial height or width
171 * measurement of an element. Depending on the value (ideally an enum) passed
172 * to augmentingMeasurement, we may need to add or subtract margin, padding,
173 * or border values, depending on the measurement we're trying to obtain.
174 */
175 num _addOrSubtractToBoxModel(List<String> dimensions,
176 String augmentingMeasurement) {
177 // getComputedStyle always returns pixel values (hence, computed), so we're
178 // always dealing with pixels in this method.
179 var styles = _element.getComputedStyle();
180
181 var val = 0;
182
183 for (String measurement in dimensions) {
184 // The border-box and default box model both exclude margin in the regular
185 // height/width calculation, so add it if we want it for this measurement.
186 if (augmentingMeasurement == 'margin') {
187 val += new Dimension._(styles.getPropertyValue(
188 '$augmentingMeasurement-$measurement')).value;
189 }
190
191 // The border-box includes padding and border, so remove it if we want
192 // just the content itself.
193 if (augmentingMeasurement == 'content') {
194 val -= new Dimension._(
195 styles.getPropertyValue('padding-$measurement')).value;
196 }
197
198 // At this point, we don't wan't to augment with border or margin,
199 // so remove border.
200 if (augmentingMeasurement != 'margin') {
201 val -= new Dimension._(styles.getPropertyValue(
202 'border-${measurement}-width')).value;
203 }
204 }
205 return val;
206 }
207
208 final static _HEIGHT = ['top', 'bottom'];
209 final static _WIDTH = ['right', 'left'];
210 }
211
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698