diff options
author | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-12 23:09:25 +0100 |
---|---|---|
committer | Ulrich-Matthias Schäfer <ulima.ums@googlemail.com> | 2018-11-28 13:42:25 +0100 |
commit | 675847a99f1640c87df0a6187eeb34b90d903666 (patch) | |
tree | 78483c739c2b164cd6b098d952658d636e3be745 /src | |
parent | fa0461eeddf65a249e1a674305684ae756a69965 (diff) | |
download | svg.js-675847a99f1640c87df0a6187eeb34b90d903666.tar.gz svg.js-675847a99f1640c87df0a6187eeb34b90d903666.zip |
plumber differences between node and browser so that tests run on both
Diffstat (limited to 'src')
-rw-r--r-- | src/animation/Animator.js | 4 | ||||
-rw-r--r-- | src/types/Box.js | 26 | ||||
-rw-r--r-- | src/utils/window.js | 23 |
3 files changed, 41 insertions, 12 deletions
diff --git a/src/animation/Animator.js b/src/animation/Animator.js index 1392daa..0119cba 100644 --- a/src/animation/Animator.js +++ b/src/animation/Animator.js @@ -5,7 +5,7 @@ const Animator = { nextDraw: null, frames: new Queue(), timeouts: new Queue(), - timer: globals.window.performance || globals.window.Date, + timer: () => globals.window.performance || globals.window.Date, transforms: [], frame (fn) { @@ -29,7 +29,7 @@ const Animator = { delay = delay || 0 // Work out when the event should fire - var time = Animator.timer.now() + delay + var time = Animator.timer().now() + delay // Add the timeout to the end of the queue var node = Animator.timeouts.push({ run: fn, time: time }) diff --git a/src/types/Box.js b/src/types/Box.js index e55f114..a08ad67 100644 --- a/src/types/Box.js +++ b/src/types/Box.js @@ -104,7 +104,7 @@ export default class Box { } } -function getBox (cb) { +function getBox (cb, retry) { let box try { @@ -114,23 +114,29 @@ function getBox (cb) { throw new Error('Element not in the dom') } } catch (e) { - try { - let clone = this.clone().addTo(parser().svg).show() - box = cb(clone.node) - clone.remove() - } catch (e) { - throw new Error('Getting a bounding box of element "' + this.node.nodeName + '" is not possible') - } + box = retry(this) } + return box } export function bbox () { - return new Box(getBox.call(this, (node) => node.getBBox())) + return new Box(getBox.call(this, (node) => node.getBBox(), (el) => { + try { + let clone = el.clone().addTo(parser().svg).show() + let box = clone.node.getBBox() + clone.remove() + return box + } catch (e) { + throw new Error('Getting bbox of element "' + el.node.nodeName + '" is not possible') + } + })) } export function rbox (el) { - let box = new Box(getBox.call(this, (node) => node.getBoundingClientRect())) + let box = new Box(getBox.call(this, (node) => node.getBoundingClientRect(), (el) => { + throw new Error('Getting rbox of element "' + el.node.nodeName + '" is not possible') + })) if (el) return box.transform(el.screenCTM().inverse()) return box.addOffset() } diff --git a/src/utils/window.js b/src/utils/window.js index 9e51339..626fde3 100644 --- a/src/utils/window.js +++ b/src/utils/window.js @@ -7,3 +7,26 @@ export function registerWindow (win = null, doc = null) { globals.window = win globals.document = doc } + +const save = {} + +export function saveWindow () { + save.window = globals.window + save.document = globals.document +} + +export function restoreWindow () { + globals.window = save.window + globals.document = save.document +} + +export function withWindow (win, fn) { + saveWindow() + registerWindow(win, win.document) + fn(win, win.document) + restoreWindow() +} + +export function getWindow () { + return globals.window +} |