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
67101824
Commit
67101824
authored
Oct 07, 2020
by
Kyle Keating
Committed by
Robert Knight
Oct 08, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Covert html.coffee to js
parent
c9cdea1e
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
108 additions
and
92 deletions
+108
-92
html.coffee
src/annotator/anchoring/html.coffee
+0
-89
html.js
src/annotator/anchoring/html.js
+107
-0
types.js
src/annotator/anchoring/types.js
+1
-1
guest.js
src/annotator/guest.js
+0
-2
No files found.
src/annotator/anchoring/html.coffee
deleted
100644 → 0
View file @
c9cdea1e
{
RangeAnchor
TextPositionAnchor
TextQuoteAnchor
}
=
require
(
'./types'
)
querySelector
=
(
type
,
root
,
selector
,
options
)
->
doQuery
=
(
resolve
,
reject
)
->
try
anchor
=
type
.
fromSelector
(
root
,
selector
,
options
)
range
=
anchor
.
toRange
(
options
)
resolve
(
range
)
catch
error
reject
(
error
)
return
new
Promise
(
doQuery
)
###*
# Anchor a set of selectors.
#
# This function converts a set of selectors into a document range.
# 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 Array selectors: The selectors to try.
# :param Object options: Options to pass to the anchor implementations.
# :return: A Promise that resolves to a Range on success.
# :rtype: Promise
####
exports
.
anchor
=
(
root
,
selectors
,
options
=
{})
->
# Selectors
fragment
=
null
position
=
null
quote
=
null
range
=
null
# Collect all the selectors
for
selector
in
selectors
?
[]
switch
selector
.
type
when
'TextPositionSelector'
position
=
selector
options
.
hint
=
position
.
start
# TextQuoteAnchor hint
when
'TextQuoteSelector'
quote
=
selector
when
'RangeSelector'
range
=
selector
# Assert the quote matches the stored quote, if applicable
maybeAssertQuote
=
(
range
)
->
if
quote
?
.
exact
?
and
range
.
toString
()
!=
quote
.
exact
throw
new
Error
(
'quote mismatch'
)
else
return
range
# From a default of failure, we build up catch clauses to try selectors in
# order, from simple to complex.
promise
=
Promise
.
reject
(
'unable to anchor'
)
if
range
?
promise
=
promise
.
catch
->
return
querySelector
(
RangeAnchor
,
root
,
range
,
options
)
.
then
(
maybeAssertQuote
)
if
position
?
promise
=
promise
.
catch
->
return
querySelector
(
TextPositionAnchor
,
root
,
position
,
options
)
.
then
(
maybeAssertQuote
)
if
quote
?
promise
=
promise
.
catch
->
# Note: similarity of the quote is implied.
return
querySelector
(
TextQuoteAnchor
,
root
,
quote
,
options
)
return
promise
exports
.
describe
=
(
root
,
range
,
options
=
{})
->
types
=
[
RangeAnchor
,
TextPositionAnchor
,
TextQuoteAnchor
]
selectors
=
for
type
in
types
try
anchor
=
type
.
fromRange
(
root
,
range
,
options
)
selector
=
anchor
.
toSelector
(
options
)
catch
continue
return
selectors
src/annotator/anchoring/html.js
0 → 100644
View file @
67101824
import
{
RangeAnchor
,
TextPositionAnchor
,
TextQuoteAnchor
}
from
'./types'
;
/**
* @typedef {import("./types").AnyRangeType} AnyRangeType
* @typedef {import('../../types/api').Selector} Selector
*/
/**
* @param {RangeAnchor|TextPositionAnchor|TextQuoteAnchor} anchor
* @param {Object} [options]
* @param {number} [options.hint]
*/
async
function
querySelector
(
anchor
,
options
=
{})
{
return
anchor
.
toRange
(
options
);
}
/**
* Anchor a set of selectors.
*
* This function converts a set of selectors into a document range.
* It encapsulates the core anchoring algorithm, using the selectors alone or
* in combination to establish the best anchor within the document.
*
* @param {Node} root - The root element of the anchoring context.
* @param {Selector[]} selectors - The selectors to try.
* @param {Object} [options]
* @param {number} [options.hint]
*/
export
function
anchor
(
root
,
selectors
,
options
=
{})
{
let
position
=
null
;
let
quote
=
null
;
let
range
=
null
;
// Collect all the selectors
for
(
let
selector
of
selectors
)
{
switch
(
selector
.
type
)
{
case
'TextPositionSelector'
:
position
=
selector
;
options
.
hint
=
position
.
start
;
// TextQuoteAnchor hint
break
;
case
'TextQuoteSelector'
:
quote
=
selector
;
break
;
case
'RangeSelector'
:
range
=
selector
;
break
;
}
}
/**
* Assert the quote matches the stored quote, if applicable
* @param {Range} range
*/
const
maybeAssertQuote
=
range
=>
{
if
(
quote
?.
exact
&&
range
.
toString
()
!==
quote
.
exact
)
{
throw
new
Error
(
'quote mismatch'
);
}
else
{
return
range
;
}
};
// 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'
);
if
(
range
)
{
promise
=
promise
.
catch
(()
=>
{
let
anchor
=
RangeAnchor
.
fromSelector
(
root
,
range
);
return
querySelector
(
anchor
,
options
).
then
(
maybeAssertQuote
);
});
}
if
(
position
)
{
promise
=
promise
.
catch
(()
=>
{
let
anchor
=
TextPositionAnchor
.
fromSelector
(
root
,
position
);
return
querySelector
(
anchor
,
options
).
then
(
maybeAssertQuote
);
});
}
if
(
quote
)
{
promise
=
promise
.
catch
(()
=>
{
let
anchor
=
TextQuoteAnchor
.
fromSelector
(
root
,
quote
);
return
querySelector
(
anchor
,
options
);
});
}
return
promise
;
}
/**
* @param {Node} root
* @param {Range} range
*/
export
function
describe
(
root
,
range
)
{
const
types
=
[
RangeAnchor
,
TextPositionAnchor
,
TextQuoteAnchor
];
const
result
=
[];
for
(
let
type
of
types
)
{
try
{
const
anchor
=
type
.
fromRange
(
root
,
range
);
result
.
push
(
anchor
.
toSelector
());
}
catch
(
error
)
{
continue
;
}
}
return
result
;
}
src/annotator/anchoring/types.js
View file @
67101824
...
...
@@ -137,7 +137,7 @@ export class TextPositionAnchor {
}
/**
* Converts between
TextQuoteSelector selectors and Range
objects.
* Converts between
`TextQuoteSelector` selectors and `Range`
objects.
*/
export
class
TextQuoteAnchor
{
/**
...
...
src/annotator/guest.js
View file @
67101824
...
...
@@ -4,9 +4,7 @@ import scrollIntoView from 'scroll-into-view';
import
Delegator
from
'./delegator'
;
import
{
Adder
}
from
'./adder'
;
// @ts-expect-error - Module is CoffeeScript
import
*
as
htmlAnchoring
from
'./anchoring/html'
;
import
{
sniff
}
from
'./anchoring/range'
;
import
{
getHighlightsContainingNode
,
...
...
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