Skip to content

Commit d910c4c

Browse files
authored
Merge pull request #39 from alienzhou/fix_only-root-node
fix(serialize&deSerialize): getting the parent node will occur errors…
2 parents 2da0197 + 920b019 commit d910c4c

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "web-highlighter",
3-
"version": "0.5.0",
3+
"version": "0.5.1",
44
"description": "✨A no-runtime dependency lib for text highlighting & persistence on any website ✨🖍️",
55
"main": "dist/web-highlighter.min.js",
66
"browser": "dist/web-highlighter.min.js",

src/model/range/dom.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* some dom operations about HighlightRange
33
*/
44

5-
import {CAMEL_DATASET_IDENTIFIER} from '@src/util/const';
5+
import {CAMEL_DATASET_IDENTIFIER, ROOT_IDX, UNKNOWN_IDX} from '@src/util/const';
66
import {DomMeta} from '@src/types';
77

88
const countGlobalNodeIndex = ($node: Node, $root: Document | HTMLElement): number => {
@@ -13,7 +13,7 @@ const countGlobalNodeIndex = ($node: Node, $root: Document | HTMLElement): numbe
1313
return i;
1414
}
1515
}
16-
return -1;
16+
return UNKNOWN_IDX;
1717
};
1818

1919
/**
@@ -65,7 +65,9 @@ const getOriginParent = ($node: Text | HTMLElement): HTMLElement => {
6565

6666
export const getDomMeta = ($node: Text | HTMLElement, offset: number, $root: Document | HTMLElement): DomMeta => {
6767
const $originParent = getOriginParent($node);
68-
const index = countGlobalNodeIndex($originParent, $root);
68+
const index = $originParent === $root
69+
? ROOT_IDX
70+
: countGlobalNodeIndex($originParent, $root);
6971
const preNodeOffset = getTextPreOffset($originParent, $node);
7072
const tagName = $originParent.tagName;
7173

src/model/source/dom.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import {DomNode} from '@src//types';
22
import HighlightSource from './index';
3+
import {ROOT_IDX} from '@src/util/const';
34

45
/**
5-
* Because of supporting highligting a same area (range overlapping),
6+
* Because of supporting highlighting a same area (range overlapping),
67
* Highlighter will calculate which text-node and how much offset it actually be,
7-
* baseed on the origin website dom node and the text offset.
8+
* based on the origin website dom node and the text offset.
89
*
910
* @param {Node} $parent element node in the origin website dom tree
1011
* @param {number} offset text offset in the origin website dom tree
@@ -52,8 +53,11 @@ export const queryElementNode = (
5253
hs: HighlightSource,
5354
$root: HTMLElement | Document
5455
): {start: Node, end: Node} => {
55-
return {
56-
start: $root.getElementsByTagName(hs.startMeta.parentTagName)[hs.startMeta.parentIndex],
57-
end: $root.getElementsByTagName(hs.endMeta.parentTagName)[hs.endMeta.parentIndex],
58-
};
56+
const start = hs.startMeta.parentIndex === ROOT_IDX
57+
? $root
58+
: $root.getElementsByTagName(hs.startMeta.parentTagName)[hs.startMeta.parentIndex];
59+
const end = hs.endMeta.parentIndex === ROOT_IDX
60+
? $root
61+
: $root.getElementsByTagName(hs.endMeta.parentTagName)[hs.endMeta.parentIndex];
62+
return { start, end };
5963
};

src/util/const.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ export const STYLESHEET_TEXT = `
3535
background: #ffb;
3636
}
3737
`;
38+
39+
export const ROOT_IDX = -2;
40+
export const UNKNOWN_IDX = -1;

0 commit comments

Comments
 (0)