From 6dae4765e14e4d8b2ec336ae8b36826725dbfb42 Mon Sep 17 00:00:00 2001 From: alienzhou Date: Tue, 12 May 2020 22:47:32 +0800 Subject: [PATCH 1/2] fix(serialize&deSerialize): getting the parent node will occur errors when the root has no children( --- src/model/range/dom.ts | 8 +++++--- src/model/source/dom.ts | 16 ++++++++++------ src/util/const.ts | 3 +++ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/model/range/dom.ts b/src/model/range/dom.ts index 8f85e17..bfd2b32 100644 --- a/src/model/range/dom.ts +++ b/src/model/range/dom.ts @@ -2,7 +2,7 @@ * some dom operations about HighlightRange */ -import {CAMEL_DATASET_IDENTIFIER} from '@src/util/const'; +import {CAMEL_DATASET_IDENTIFIER, ROOT_IDX, UNKNOWN_IDX} from '@src/util/const'; import {DomMeta} from '@src/types'; const countGlobalNodeIndex = ($node: Node, $root: Document | HTMLElement): number => { @@ -13,7 +13,7 @@ const countGlobalNodeIndex = ($node: Node, $root: Document | HTMLElement): numbe return i; } } - return -1; + return UNKNOWN_IDX; }; /** @@ -65,7 +65,9 @@ const getOriginParent = ($node: Text | HTMLElement): HTMLElement => { export const getDomMeta = ($node: Text | HTMLElement, offset: number, $root: Document | HTMLElement): DomMeta => { const $originParent = getOriginParent($node); - const index = countGlobalNodeIndex($originParent, $root); + const index = $originParent === $root + ? ROOT_IDX + : countGlobalNodeIndex($originParent, $root); const preNodeOffset = getTextPreOffset($originParent, $node); const tagName = $originParent.tagName; diff --git a/src/model/source/dom.ts b/src/model/source/dom.ts index 9e631cf..fbf8556 100644 --- a/src/model/source/dom.ts +++ b/src/model/source/dom.ts @@ -1,10 +1,11 @@ import {DomNode} from '@src//types'; import HighlightSource from './index'; +import {ROOT_IDX} from '@src/util/const'; /** - * Because of supporting highligting a same area (range overlapping), + * Because of supporting highlighting a same area (range overlapping), * Highlighter will calculate which text-node and how much offset it actually be, - * baseed on the origin website dom node and the text offset. + * based on the origin website dom node and the text offset. * * @param {Node} $parent element node in the origin website dom tree * @param {number} offset text offset in the origin website dom tree @@ -52,8 +53,11 @@ export const queryElementNode = ( hs: HighlightSource, $root: HTMLElement | Document ): {start: Node, end: Node} => { - return { - start: $root.getElementsByTagName(hs.startMeta.parentTagName)[hs.startMeta.parentIndex], - end: $root.getElementsByTagName(hs.endMeta.parentTagName)[hs.endMeta.parentIndex], - }; + const start = hs.startMeta.parentIndex === ROOT_IDX + ? $root + : $root.getElementsByTagName(hs.startMeta.parentTagName)[hs.startMeta.parentIndex]; + const end = hs.endMeta.parentIndex === ROOT_IDX + ? $root + : $root.getElementsByTagName(hs.endMeta.parentTagName)[hs.endMeta.parentIndex]; + return { start, end }; }; diff --git a/src/util/const.ts b/src/util/const.ts index 4349286..529242e 100644 --- a/src/util/const.ts +++ b/src/util/const.ts @@ -35,3 +35,6 @@ export const STYLESHEET_TEXT = ` background: #ffb; } `; + +export const ROOT_IDX = -2; +export const UNKNOWN_IDX = -1; \ No newline at end of file From 920b019732e08a0e02dfe0fd64fb38b6d9bd27b4 Mon Sep 17 00:00:00 2001 From: alienzhou Date: Sun, 17 May 2020 14:37:37 +0800 Subject: [PATCH 2/2] chore: update version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 331d415..704c8cf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "web-highlighter", - "version": "0.5.0", + "version": "0.5.1", "description": "✨A no-runtime dependency lib for text highlighting & persistence on any website ✨🖍️", "main": "dist/web-highlighter.min.js", "browser": "dist/web-highlighter.min.js",