From c4ea9321dcffbda9004461a7b0027cf5c893f3c0 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 2 May 2020 13:00:46 -0400 Subject: Validate document nodes before use (#493) * Validate document.body before use in loadScripts This also fixes an issue where reject wasn't being passed to loadScriptSentinel. * Validate document nodes before use in _getSiteColor * Validate document.body before use in _getViewport * Validate document.body before use in setContentScale * Validate document.body before use in docImposterCreate --- ext/fg/js/document.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'ext/fg/js/document.js') diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js index 3b4cc28f..6103c7c5 100644 --- a/ext/fg/js/document.js +++ b/ext/fg/js/document.js @@ -28,6 +28,9 @@ function docSetImposterStyle(style, propertyName, value) { } function docImposterCreate(element, isTextarea) { + const body = document.body; + if (body === null) { return [null, null]; } + const elementStyle = window.getComputedStyle(element); const elementRect = element.getBoundingClientRect(); const documentRect = document.documentElement.getBoundingClientRect(); @@ -78,7 +81,7 @@ function docImposterCreate(element, isTextarea) { } container.appendChild(imposter); - document.body.appendChild(container); + body.appendChild(container); // Adjust size const imposterRect = imposter.getBoundingClientRect(); -- cgit v1.2.3 From d6ae3229614fb11fee799387d17347ea97509c59 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Wed, 6 May 2020 19:34:32 -0400 Subject: Text scanning update (#507) * Fix unity test missing a parameter * Update docSentenceExtract to not rescan content --- ext/fg/js/document.js | 2 +- ext/fg/js/source.js | 10 +++++++--- test/data/html/test-document1.html | 1 + 3 files changed, 9 insertions(+), 4 deletions(-) (limited to 'ext/fg/js/document.js') diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js index 6103c7c5..d639bc86 100644 --- a/ext/fg/js/document.js +++ b/ext/fg/js/document.js @@ -159,7 +159,7 @@ function docSentenceExtract(source, extent) { const sourceLocal = source.clone(); const position = sourceLocal.setStartOffset(extent); - sourceLocal.setEndOffset(position + extent); + sourceLocal.setEndOffset(extent * 2 - position, true); const content = sourceLocal.text(); let quoteStack = []; diff --git a/ext/fg/js/source.js b/ext/fg/js/source.js index b3119d40..c9d70215 100644 --- a/ext/fg/js/source.js +++ b/ext/fg/js/source.js @@ -46,10 +46,14 @@ class TextSourceRange { return this.content; } - setEndOffset(length) { - const state = TextSourceRange.seekForward(this.range.startContainer, this.range.startOffset, length); + setEndOffset(length, fromEnd=false) { + const state = ( + fromEnd ? + TextSourceRange.seekForward(this.range.endContainer, this.range.endOffset, length) : + TextSourceRange.seekForward(this.range.startContainer, this.range.startOffset, length) + ); this.range.setEnd(state.node, state.offset); - this.content = state.content; + this.content = (fromEnd ? this.content + state.content : state.content); return length - state.remainder; } diff --git a/test/data/html/test-document1.html b/test/data/html/test-document1.html index 0754a314..98a6fb44 100644 --- a/test/data/html/test-document1.html +++ b/test/data/html/test-document1.html @@ -103,6 +103,7 @@ data-end-node-selector="img" data-end-offset="0" data-result-type="TextSourceElement" + data-sentence-extent="100" data-sentence="よみちゃん" > よみちゃん -- cgit v1.2.3