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

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, 7 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 {
Jacob 2013/05/22 02:09:51 These should just be in their own pub package unle
Jennifer Messerly 2013/07/10 21:05:56 Isn't "hard to get right" an argument for trying t
Emily Fortuna 2013/07/10 21:40:16 They are being exposed on Element.
8
9 _ContentCssRect(element) : super(element);
10 int get height => _element.offsetHeight +
11 _addOrSubtractToBoxModel(CssRect._HEIGHT, 'content');
Jacob 2013/05/22 02:09:51 make 'content' a string constant at least
12
13 int 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 an
22 * int in pixels.
23 */
24 void set height(Dimension newHeight) {
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 int 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 int get left() => _element.getBoundingClientRect().left -
41 _addOrSubtractToBoxModel(['left'], 'content');
42 int get top() => _element.getBoundingClientRect().top -
43 _addOrSubtractToBoxModel(['top'], 'content');
44 }
45
46 class _ContentCssListRect extends _ContentCssRect {
47 List<Element> _elementList;
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 an
59 * int 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 int 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 int get height => _element.offsetHeight +
79 _addOrSubtractToBoxModel(CssRect._HEIGHT, 'padding');
80 int get width => _element.offsetWidth +
81 _addOrSubtractToBoxModel(CssRect._WIDTH, 'padding');
82
83 int get left() => _element.getBoundingClientRect().left -
84 _addOrSubtractToBoxModel(['left'], 'padding');
85 int get top() => _element.getBoundingClientRect().top -
86 _addOrSubtractToBoxModel(['top'], 'padding');
87 }
88
89 class _BorderCssRect extends CssRect {
90 _BorderCssRect(element) : super(element);
91 int get height() => _element.offsetHeight;
Jacob 2013/05/22 02:09:51 Should we put this in the platform? For example, t
92 int get width() => _element.offsetWidth;
93
94 int get left() => _element.getBoundingClientRect().left;
95 int get top() => _element.getBoundingClientRect().top;
96 }
97
98 class _MarginCssRect extends CssRect {
99 _MarginCssRect(element) : super(element);
100 int get height() => _element.offsetHeight +
101 _addOrSubtractToBoxModel(CssRect._HEIGHT, 'margin');
102 int get width() =>
103 _element.offsetWidth + _addOrSubtractToBoxModel(CssRect._WIDTH, 'margin');
104
105 int get left() => _element.getBoundingClientRect().left -
106 _addOrSubtractToBoxModel(['left'], 'margin');
107 int 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 height for animations, as
118 * they will trigger a redraw of the layout. For deferred layout options, see
119 * the LayoutManager.
120 */
121 abstract class CssRect extends RectBase implements Rect {
122 Element _element;
123
124 CssRect(this._element);
125
126 int get left();
127
128 int get top();
129
130 /**
131 * The height of this rectangle.
132 *
133 * This is equivalent to the `height` function in jQuery and the calculated
134 * `height` css value, converted to a dimensionless int in pixels. Unlike
135 * [getBoundingClientRect], `height` will return the same numerical width if
136 * the element is hidden or not.
137 */
138 int get height();
Jacob 2013/05/22 02:09:51 these should be num not int. it is possible to hav
Emily Fortuna 2013/07/10 21:40:16 Done.
139
140 /**
141 * The width of this rectangle.
142 *
143 * This is equivalent to the `width` function in jQuery and the calculated
144 * `width` css value, converted to a dimensionless int in pixels. Unlike
145 * [getBoundingClientRect], `width` will return the same numerical width if
146 * the element is hidden or not.
147 */
148 int get width();
149
150 /**
151 * Set the height to `newHeight`.
152 *
153 * Values of newHeight that are less than zero are converted to effectively
154 * setting the height to 0. Note that only the content height can be set via
155 * this method.
156 */
157 void set height(Dimension newHeight) {
158 throw new UnsupportedError("Can only set height for content rect.");
159 }
160
161 /**
162 * Set the current computed width in pixels of this element.
163 *
164 * Note that only the content width can be set via this method.
165 */
166 void set width(Dimension newWidth) {
167 throw new UnsupportedError("Can only set width for content rect.");
168 }
169
170 /**
171 * Return a value that is used to modify the initial height or width
172 * measurement of an element. Depending on the value (ideally an enum) passed
173 * to augmentingMeasurement, we may need to add or subtract margin, padding,
174 * or border values, depending on the measurement we're trying to obtain.
175 */
176 int _addOrSubtractToBoxModel(List<String> dimensions,
177 String augmentingMeasurement) {
178 // getComputedStyle always returns pixel values (hence, computed), so we're
179 // always dealing with pixels in this method.
180 var styles = _element.getComputedStyle();
181
182 var val = 0;
183
184 for (String measurement in dimensions) {
185 // The border-box and default box model both exclude margin in the regular
186 // height/width calculation, so add it if we want it for this measurement.
187 if (augmentingMeasurement == "margin") {
188 val += new Dimension.css(styles.getPropertyValue(
189 '$augmentingMeasurement-$measurement')).value;
190 }
191
192 // The border-box includes padding and border, so remove it if we want
193 // just the content itself.
194 if (augmentingMeasurement == 'content') {
195 val -= new Dimension.css(
196 styles.getPropertyValue('padding-$measurement')).value;
197 }
198
199 // At this point, we don't wan't to augment with border or margin,
200 // so remove border.
201 if (augmentingMeasurement != 'margin') {
202 val -= new Dimension.css(styles.getPropertyValue(
203 'border-${measurement}-width')).value;
204 }
205 }
206 return val;
207 }
208
209 final static _HEIGHT = ['top', 'bottom'];
210 final static _WIDTH = ['right', 'left'];
211 }
212
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698