@@ -604,15 +604,31 @@ drawing.makeTester = function() {
604604
605605/*
606606 * 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)
609609 * 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`
610626 */
611627drawing . savedBBoxes = { } ;
612628var savedBBoxesCount = 0 ;
613629var maxSavedBBoxes = 10000 ;
614630
615- drawing . bBox = function ( node , hash ) {
631+ drawing . bBox = function ( node , inTester , hash ) {
616632 /*
617633 * Cache elements we've already measured so we don't have to
618634 * remeasure the same thing many times
@@ -645,7 +661,7 @@ drawing.bBox = function(node, hash) {
645661 if ( ! transform ) {
646662 // in this case, just varying x and y, don't bother caching
647663 // the final bBox because the alteration is quick.
648- var innerBB = drawing . bBox ( innerNode , hash ) ;
664+ var innerBB = drawing . bBox ( innerNode , false , hash ) ;
649665 if ( x ) {
650666 innerBB . left += x ;
651667 innerBB . right += x ;
@@ -672,12 +688,17 @@ drawing.bBox = function(node, hash) {
672688 if ( out ) return Lib . extendFlat ( { } , out ) ;
673689 }
674690 }
691+ var testNode , tester ;
692+ if ( inTester ) {
693+ testNode = node ;
694+ }
695+ else {
696+ tester = drawing . tester . node ( ) ;
675697
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+ }
681702
682703 // standardize its position (and newline tspans if any)
683704 d3 . select ( testNode )
@@ -689,7 +710,7 @@ drawing.bBox = function(node, hash) {
689710 . node ( )
690711 . getBoundingClientRect ( ) ;
691712
692- tester . removeChild ( testNode ) ;
713+ if ( ! inTester ) tester . removeChild ( testNode ) ;
693714
694715 var bb = {
695716 height : testRect . height ,
0 commit comments