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
5c63b28c
Commit
5c63b28c
authored
Apr 10, 2013
by
csillag
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updated d-t-m lib, with optimizations and bugfixes
parent
42e0c56c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
19 deletions
+43
-19
dom_text_mapper.coffee
h/lib/dom_text_mapper.coffee
+43
-19
No files found.
h/lib/dom_text_mapper.coffee
View file @
5c63b28c
...
...
@@ -93,7 +93,7 @@ class window.DomTextMapper
startTime
=
@
timestamp
()
@
saveSelection
()
@
path
=
{}
@
traverseSubTree
@
pathStartNode
@
traverseSubTree
@
pathStartNode
,
@
getDefaultPath
()
t1
=
@
timestamp
()
# console.log "Phase I (Path traversal) took " + (t1 - startTime) + " ms."
...
...
@@ -148,7 +148,7 @@ class window.DomTextMapper
delete
@
path
[
p
]
# console.log "Done. Collecting new path info..."
@
traverseSubTree
node
@
traverseSubTree
node
,
path
# console.log "Done. Updating mappings..."
...
...
@@ -369,28 +369,34 @@ class window.DomTextMapper
when
"#cdata-section"
then
return
"cdata-section()"
else
return
nodeName
getNodePosition
:
(
node
)
->
pos
=
0
tmp
=
node
while
tmp
if
tmp
.
nodeName
is
node
.
nodeName
pos
++
tmp
=
tmp
.
previousSibling
pos
getPathSegment
:
(
node
)
->
name
=
@
getProperNodeName
node
pos
=
@
getNodePosition
node
name
+
(
if
pos
>
1
then
"[
#{
pos
}
]"
else
""
)
getPathTo
:
(
node
)
->
xpath
=
''
;
while
node
!=
@
rootNode
pos
=
0
tempitem2
=
node
while
tempitem2
if
tempitem2
.
nodeName
is
node
.
nodeName
pos
++
tempitem2
=
tempitem2
.
previousSibling
xpath
=
(
@
getProperNodeName
node
)
+
(
if
pos
>
1
then
"["
+
pos
+
']'
else
""
)
+
'/'
+
xpath
xpath
=
(
@
getPathSegment
node
)
+
'/'
+
xpath
node
=
node
.
parentNode
xpath
=
(
if
@
rootNode
.
ownerDocument
?
then
'./'
else
'/'
)
+
xpath
xpath
=
xpath
.
replace
/\/$/
,
''
xpath
# This method is called recursively, to traverse a given sub-tree of the DOM.
traverseSubTree
:
(
node
,
invisible
=
false
,
verbose
=
false
)
->
traverseSubTree
:
(
node
,
path
,
invisible
=
false
,
verbose
=
false
)
->
# Step one: get rendered node content, and store path info,
# if there is valuable content
path
=
@
getPathTo
node
@
underTraverse
=
path
cont
=
@
getNodeContent
node
,
false
@
path
[
path
]
=
path
:
path
...
...
@@ -414,7 +420,8 @@ class window.DomTextMapper
# A: I seem to remember that the answer is yes, but I don't remember why.
if
node
.
hasChildNodes
()
for
child
in
node
.
childNodes
@
traverseSubTree
child
,
invisible
,
verbose
subpath
=
path
+
'/'
+
(
@
getPathSegment
child
)
@
traverseSubTree
child
,
subpath
,
invisible
,
verbose
null
getBody
:
->
(
@
rootWin
.
document
.
getElementsByTagName
"body"
)[
0
]
...
...
@@ -455,7 +462,9 @@ class window.DomTextMapper
# Select the given node (for visual identification),
# and optionally scroll to it
selectNode
:
(
node
,
scroll
=
false
)
->
selectNode
:
(
node
,
scroll
=
false
)
->
unless
node
?
throw
new
Error
"Called selectNode with null node!"
sel
=
@
rootWin
.
getSelection
()
# clear the selection
...
...
@@ -506,12 +515,18 @@ class window.DomTextMapper
# If this is the case, then it's OK.
unless
USE_EMPTY_TEXT_WORKAROUND
and
@
isWhitespace
node
# No, this is not the case. Then this is an error.
throw
exception
console
.
log
"Warning: failed to scan element @ "
+
@
underTraverse
console
.
log
"Content is: "
+
node
.
innerHTML
console
.
log
"We won't be able to properly anchor to any text inside this element."
# throw exception
if
scroll
sn
=
node
while
not
sn
.
scrollIntoViewIfNeeded
?
while
sn
?
and
not
sn
.
scrollIntoViewIfNeeded
?
sn
=
sn
.
parentNode
sn
.
scrollIntoViewIfNeeded
()
if
sn
?
sn
.
scrollIntoViewIfNeeded
()
else
console
.
log
"Failed to scroll to element. (Browser does not support scrollIntoViewIfNeeded?)"
sel
# Read and convert the text of the current selection.
...
...
@@ -661,4 +676,13 @@ class window.DomTextMapper
# Decides whether a given node is a text node that only contains whitespace
isWhitespace
:
(
node
)
->
node
.
nodeType
is
Node
.
TEXT_NODE
and
WHITESPACE
.
test
node
.
data
result
=
switch
node
.
nodeType
when
Node
.
TEXT_NODE
WHITESPACE
.
test
node
.
data
when
Node
.
ELEMENT_NODE
mightBeEmpty
=
true
for
child
in
node
.
childNodes
mightBeEmpty
=
mightBeEmpty
and
@
isWhitespace
child
mightBeEmpty
else
false
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