@@ -604,15 +604,31 @@ drawing.makeTester = function() {
604
604
605
605
/*
606
606
* use our offscreen tester to get a clientRect for an element,
607
- * in a reference frame where it isn't translated and its anchor
608
- * point is at (0,0)
607
+ * in a reference frame where it isn't translated (or transformed) and
608
+ * its anchor point is at (0,0)
609
609
* always returns a copy of the bbox, so the caller can modify it safely
610
+ *
611
+ * @param {SVGElement } node: the element to measure. If possible this should be
612
+ * a <text> or MathJax <g> element that's already passed through
613
+ * `convertToTspans` because in that case we can cache the results, but it's
614
+ * possible to pass in any svg element.
615
+ *
616
+ * @param {boolean } inTester: is this element already in `drawing.tester`?
617
+ * If you are measuring a dummy element, rather than one you really intend
618
+ * to use on the plot, making it in `drawing.tester` in the first place
619
+ * allows us to test faster because it cuts out cloning and appending it.
620
+ *
621
+ * @param {string } hash: for internal use only, if we already know the cache key
622
+ * for this element beforehand.
623
+ *
624
+ * @return {object }: a plain object containing the width, height, left, right,
625
+ * top, and bottom of `node`
610
626
*/
611
627
drawing . savedBBoxes = { } ;
612
628
var savedBBoxesCount = 0 ;
613
629
var maxSavedBBoxes = 10000 ;
614
630
615
- drawing . bBox = function ( node , hash ) {
631
+ drawing . bBox = function ( node , inTester , hash ) {
616
632
/*
617
633
* Cache elements we've already measured so we don't have to
618
634
* remeasure the same thing many times
@@ -645,7 +661,7 @@ drawing.bBox = function(node, hash) {
645
661
if ( ! transform ) {
646
662
// in this case, just varying x and y, don't bother caching
647
663
// the final bBox because the alteration is quick.
648
- var innerBB = drawing . bBox ( innerNode , hash ) ;
664
+ var innerBB = drawing . bBox ( innerNode , false , hash ) ;
649
665
if ( x ) {
650
666
innerBB . left += x ;
651
667
innerBB . right += x ;
@@ -672,12 +688,17 @@ drawing.bBox = function(node, hash) {
672
688
if ( out ) return Lib . extendFlat ( { } , out ) ;
673
689
}
674
690
}
691
+ var testNode , tester ;
692
+ if ( inTester ) {
693
+ testNode = node ;
694
+ }
695
+ else {
696
+ tester = drawing . tester . node ( ) ;
675
697
676
- var tester = drawing . tester . node ( ) ;
677
-
678
- // copy the node to test into the tester
679
- var testNode = node . cloneNode ( true ) ;
680
- tester . appendChild ( testNode ) ;
698
+ // copy the node to test into the tester
699
+ testNode = node . cloneNode ( true ) ;
700
+ tester . appendChild ( testNode ) ;
701
+ }
681
702
682
703
// standardize its position (and newline tspans if any)
683
704
d3 . select ( testNode )
@@ -689,7 +710,7 @@ drawing.bBox = function(node, hash) {
689
710
. node ( )
690
711
. getBoundingClientRect ( ) ;
691
712
692
- tester . removeChild ( testNode ) ;
713
+ if ( ! inTester ) tester . removeChild ( testNode ) ;
693
714
694
715
var bb = {
695
716
height : testRect . height ,
0 commit comments