Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
d883b0b0
Commit
d883b0b0
authored
Jan 26, 2023
by
Alejandro Celaya
Committed by
Alejandro Celaya
Jan 26, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Migrate html module to TS
parent
5744d55a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
35 deletions
+33
-35
html.ts
src/annotator/anchoring/html.ts
+33
-35
No files found.
src/annotator/anchoring/html.
j
s
→
src/annotator/anchoring/html.
t
s
View file @
d883b0b0
import
type
{
RangeSelector
,
Selector
,
TextPositionSelector
,
TextQuoteSelector
,
}
from
'../../types/api'
;
import
{
RangeAnchor
,
TextPositionAnchor
,
TextQuoteAnchor
}
from
'./types'
;
/**
* @typedef {import('../../types/api').RangeSelector} RangeSelector
* @typedef {import('../../types/api').Selector} Selector
* @typedef {import('../../types/api').TextPositionSelector} TextPositionSelector
* @typedef {import('../../types/api').TextQuoteSelector} TextQuoteSelector
*/
type
Options
=
{
hint
?:
number
;
};
/**
* @param {RangeAnchor|TextPositionAnchor|TextQuoteAnchor} anchor
* @param {object} [options]
* @param {number} [options.hint]
*/
async
function
querySelector
(
anchor
,
options
=
{})
{
async
function
querySelector
(
anchor
:
RangeAnchor
|
TextPositionAnchor
|
TextQuoteAnchor
,
options
:
Options
)
{
return
anchor
.
toRange
(
options
);
}
...
...
@@ -23,18 +25,20 @@ async function querySelector(anchor, options = {}) {
* It encapsulates the core anchoring algorithm, using the selectors alone or
* in combination to establish the best anchor within the document.
*
* @param {Element} root - The root element of the anchoring context.
* @param {Selector[]} selectors - The selectors to try.
* @param {object} [options]
* @param {number} [options.hint]
* @param root - The root element of the anchoring context
* @param selectors - The selectors to try
*/
export
function
anchor
(
root
,
selectors
,
options
=
{})
{
let
position
=
/** @type {TextPositionSelector|null} */
(
null
);
let
quote
=
/** @type {TextQuoteSelector|null} */
(
null
);
let
range
=
/** @type {RangeSelector|null} */
(
null
);
export
function
anchor
(
root
:
Element
,
selectors
:
Selector
[],
options
:
Options
=
{}
)
{
let
position
:
TextPositionSelector
|
null
=
null
;
let
quote
:
TextQuoteSelector
|
null
=
null
;
let
range
:
RangeSelector
|
null
=
null
;
// Collect all the selectors
for
(
le
t
selector
of
selectors
)
{
for
(
cons
t
selector
of
selectors
)
{
switch
(
selector
.
type
)
{
case
'TextPositionSelector'
:
position
=
selector
;
...
...
@@ -51,9 +55,8 @@ export function anchor(root, selectors, options = {}) {
/**
* Assert the quote matches the stored quote, if applicable
* @param {Range} range
*/
const
maybeAssertQuote
=
range
=>
{
const
maybeAssertQuote
=
(
range
:
Range
)
=>
{
if
(
quote
?.
exact
&&
range
.
toString
()
!==
quote
.
exact
)
{
throw
new
Error
(
'quote mismatch'
);
}
else
{
...
...
@@ -63,14 +66,13 @@ export function anchor(root, selectors, options = {}) {
// From a default of failure, we build up catch clauses to try selectors in
// order, from simple to complex.
/** @type {Promise<Range>} */
let
promise
=
Promise
.
reject
(
'unable to anchor'
);
let
promise
:
Promise
<
Range
>
=
Promise
.
reject
(
'unable to anchor'
);
if
(
range
)
{
// Const binding assures TS that it won't be re-assigned when callback runs.
const
range_
=
range
;
promise
=
promise
.
catch
(()
=>
{
le
t
anchor
=
RangeAnchor
.
fromSelector
(
root
,
range_
);
cons
t
anchor
=
RangeAnchor
.
fromSelector
(
root
,
range_
);
return
querySelector
(
anchor
,
options
).
then
(
maybeAssertQuote
);
});
}
...
...
@@ -78,7 +80,7 @@ export function anchor(root, selectors, options = {}) {
if
(
position
)
{
const
position_
=
position
;
promise
=
promise
.
catch
(()
=>
{
le
t
anchor
=
TextPositionAnchor
.
fromSelector
(
root
,
position_
);
cons
t
anchor
=
TextPositionAnchor
.
fromSelector
(
root
,
position_
);
return
querySelector
(
anchor
,
options
).
then
(
maybeAssertQuote
);
});
}
...
...
@@ -86,7 +88,7 @@ export function anchor(root, selectors, options = {}) {
if
(
quote
)
{
const
quote_
=
quote
;
promise
=
promise
.
catch
(()
=>
{
le
t
anchor
=
TextQuoteAnchor
.
fromSelector
(
root
,
quote_
);
cons
t
anchor
=
TextQuoteAnchor
.
fromSelector
(
root
,
quote_
);
return
querySelector
(
anchor
,
options
);
});
}
...
...
@@ -94,19 +96,15 @@ export function anchor(root, selectors, options = {}) {
return
promise
;
}
/**
* @param {Element} root
* @param {Range} range
*/
export
function
describe
(
root
,
range
)
{
export
function
describe
(
root
:
Element
,
range
:
Range
)
{
const
types
=
[
RangeAnchor
,
TextPositionAnchor
,
TextQuoteAnchor
];
const
result
=
[];
for
(
le
t
type
of
types
)
{
for
(
cons
t
type
of
types
)
{
try
{
const
anchor
=
type
.
fromRange
(
root
,
range
);
result
.
push
(
anchor
.
toSelector
());
}
catch
(
error
)
{
continue
;
// If resolving some anchor fails, we just want to skip it silently
}
}
return
result
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment