From 96a62d2d00bf0decd95550c44e69c9c08e635b81 Mon Sep 17 00:00:00 2001 From: Fred Chasen Date: Tue, 28 Aug 2018 16:00:35 -0700 Subject: [PATCH] Account for body offset in iframe view size calculations --- src/contents.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/contents.js b/src/contents.js index 47e0f20..3bbadd0 100644 --- a/src/contents.js +++ b/src/contents.js @@ -144,6 +144,7 @@ class Contents { * @returns {number} width */ textWidth() { + let rect; let width; let range = this.document.createRange(); let content = this.content || this.document.body; @@ -153,7 +154,12 @@ class Contents { range.selectNodeContents(content); // get the width of the text content - width = range.getBoundingClientRect().width; + rect = range.getBoundingClientRect(); + width = rect.width; + + if (this.content === this.document.body && rect.left) { + width += rect.left; + } if (border && border.width) { width += border.width; @@ -167,6 +173,7 @@ class Contents { * @returns {number} height */ textHeight() { + let rect; let height; let range = this.document.createRange(); let content = this.content || this.document.body; @@ -174,7 +181,12 @@ class Contents { range.selectNodeContents(content); - height = range.getBoundingClientRect().height; + rect = range.getBoundingClientRect(); + height = rect.height; + + if (this.content === this.document.body && rect.top) { + height += rect.top; + } if (height && border.height) { height += border.height;