Commit 5c63b28c authored by csillag's avatar csillag

Updated d-t-m lib, with optimizations and bugfixes

parent 42e0c56c
......@@ -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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment