Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 968e540

Browse files
musically-utpkozlowski-opensource
authored andcommitted
fix(position): correct positioning for SVG elements
Bootstrap tooltips calculate the `width` and `height` of elements using first getBoundingClientRect and fall back to `offsetWidth` and `offsetHeight` if it is not available: https://github.com/twbs/bootstrap/blob/master/js/tooltip.js#L297 This fixes a problem with tooltips/popovers on SVG elements in Firefox: twbs/bootstrap#5956 This change mimics that behavior. Closes #1225
1 parent 5a01a69 commit 968e540

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/position/position.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ angular.module('ui.bootstrap.position', [])
5454
offsetParentBCR.left += offsetParentEl.clientLeft - offsetParentEl.scrollLeft;
5555
}
5656

57+
var boundingClientRect = element[0].getBoundingClientRect();
5758
return {
58-
width: element.prop('offsetWidth'),
59-
height: element.prop('offsetHeight'),
59+
width: boundingClientRect.width || element.prop('offsetWidth'),
60+
height: boundingClientRect.height || element.prop('offsetHeight'),
6061
top: elBCR.top - offsetParentBCR.top,
6162
left: elBCR.left - offsetParentBCR.left
6263
};
@@ -69,8 +70,8 @@ angular.module('ui.bootstrap.position', [])
6970
offset: function (element) {
7071
var boundingClientRect = element[0].getBoundingClientRect();
7172
return {
72-
width: element.prop('offsetWidth'),
73-
height: element.prop('offsetHeight'),
73+
width: boundingClientRect.width || element.prop('offsetWidth'),
74+
height: boundingClientRect.height || element.prop('offsetHeight'),
7475
top: boundingClientRect.top + ($window.pageYOffset || $document[0].body.scrollTop || $document[0].documentElement.scrollTop),
7576
left: boundingClientRect.left + ($window.pageXOffset || $document[0].body.scrollLeft || $document[0].documentElement.scrollLeft)
7677
};

src/position/test/test.html

+17-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@
3636
elPosition.left += elPosition.width;
3737

3838
positionedEl.css({left: elPosition.left + 'px', top: elPosition.top + 'px'});
39-
element.after($compile(positionedEl)(scope));
39+
40+
if (attrs['position'] === 'body') {
41+
angular.element(document.getElementsByTagName('body')[0]).after($compile(positionedEl)(scope));
42+
} else {
43+
element.after($compile(positionedEl)(scope));
44+
}
4045
}
4146
};
4247
});
@@ -110,6 +115,15 @@ <h3>Within a table that is inside a relative-positioned DIV</h3>
110115
</table>
111116
</div>
112117

118+
<h3>Inside svg</h3>
119+
120+
<svg height="300px" width="300px">
121+
<rect x="0" y="0" height="300" width="300" fill="aliceblue"></rect>
122+
<rect x="50" y="50" height="200" width="200" position="body" fill="white" stroke="red">
123+
</rect>
124+
</svg>
125+
126+
113127
<h3>Inside looong text</h3>
114128
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur non velit nulla. Suspendisse sit amet tempus diam. Sed at ultricies neque. Suspendisse id felis a sem placerat ornare. Donec auctor, purus at molestie tempor, arcu enim molestie lacus, ac imperdiet massa urna eu massa. Praesent velit tellus, scelerisque a fermentum ut, ornare in diam. Phasellus egestas molestie feugiat. Vivamus sit amet viverra metus.</p>
115129
<p>Etiam ultricies odio commodo erat ullamcorper sodales. Nullam ac dui ac libero dictum mollis. Quisque convallis adipiscing facilisis. In nec nisi velit, id auctor lectus. Cras interdum urna non felis lacinia vulputate. Integer dignissim, mi aliquam gravida auctor, massa odio cursus lorem, eu ultrices eros nisl tempus diam. Maecenas tristique pellentesque nisi sed adipiscing. Aenean hendrerit sapien quis arcu lobortis vitae pulvinar ante volutpat. Morbi consectetur erat eu lacus facilisis eu ullamcorper orci euismod. Quisque diam dui, interdum in suscipit et, fringilla non justo. Pellentesque non nibh odio. Proin sit amet massa sem.</p>
@@ -119,10 +133,12 @@ <h3>Inside looong text</h3>
119133
<div class="content" position>HERE</div>
120134
<p>Maecenas laoreet nisi pretium elit bibendum eget tempor nunc aliquet. Vivamus interdum nisi sit amet tortor fermentum congue. Suspendisse at posuere erat. Aliquam hendrerit ultricies nunc non adipiscing. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Duis molestie viverra nulla a aliquet. Nullam non eros vel sem vehicula suscipit. Ut sit amet arcu ac tortor dignissim viverra in a ligula.</p>
121135

136+
122137
<div style="position: fixed; bottom: 0" class="container">
123138
<h3>Within fixed div</h3>
124139
<div class="content" position>Content</div>
125140
</div>
126141

142+
127143
</body>
128144
</html>

0 commit comments

Comments
 (0)