Linkify: Attempt to handle un-surroundable contents.
This commit is contained in:
parent
27c1a77b86
commit
b378db78f5
@ -16,9 +16,9 @@ Linkify =
|
|||||||
range = d.createRange()
|
range = d.createRange()
|
||||||
for link in links
|
for link in links
|
||||||
boundaries = Linkify.find link, walker
|
boundaries = Linkify.find link, walker
|
||||||
# continue unless boundaries
|
# break unless boundaries
|
||||||
anchor = Linkify.createLink link
|
anchor = Linkify.createLink link
|
||||||
if Linkify.surround anchor, link, range, boundaries
|
if Linkify.surround anchor, range, boundaries
|
||||||
Linkify.cleanLink anchor if Conf['Clean Links']
|
Linkify.cleanLink anchor if Conf['Clean Links']
|
||||||
walker.currentNode = anchor.lastChild
|
walker.currentNode = anchor.lastChild
|
||||||
else
|
else
|
||||||
@ -29,14 +29,13 @@ Linkify =
|
|||||||
# Walk through the nodes until we find the entire link.
|
# Walk through the nodes until we find the entire link.
|
||||||
text = ''
|
text = ''
|
||||||
while node = walker.nextNode()
|
while node = walker.nextNode()
|
||||||
{data} = node
|
|
||||||
text += node.data
|
text += node.data
|
||||||
if text.indexOf(link) > -1
|
break if text.indexOf(link) > -1
|
||||||
startNode = endNode = node
|
# return unless node
|
||||||
break
|
startNode = endNode = node
|
||||||
|
|
||||||
# Walk backwards to find the startNode.
|
# Walk backwards to find the startNode.
|
||||||
text = data
|
text = node.data
|
||||||
until (index = text.indexOf link) > -1
|
until (index = text.indexOf link) > -1
|
||||||
startNode = walker.previousNode()
|
startNode = walker.previousNode()
|
||||||
text = "#{startNode.data}#{text}"
|
text = "#{startNode.data}#{text}"
|
||||||
@ -55,20 +54,40 @@ Linkify =
|
|||||||
target: '_blank'
|
target: '_blank'
|
||||||
a
|
a
|
||||||
|
|
||||||
surround: (anchor, link, range, {startOffset, endOffset, startNode, endNode}) ->
|
surround: (anchor, range, boundaries) ->
|
||||||
# parent = startNode.parentNode
|
{startOffset, endOffset, startNode, endNode} = boundaries
|
||||||
# if parent?.nodeName is 'S' and parent.textContent.length < link.length
|
|
||||||
# parentClone = parent.cloneNode true
|
|
||||||
# $.replace parent, startNode
|
|
||||||
range.setStart startNode, startOffset
|
range.setStart startNode, startOffset
|
||||||
range.setEnd endNode, endOffset
|
range.setEnd endNode, endOffset
|
||||||
try
|
try
|
||||||
range.surroundContents anchor
|
range.surroundContents anchor
|
||||||
# if !Conf['Clean Links'] and parentClone and anchor.firstChild
|
|
||||||
# $.replace anchor.firstChild, parentClone
|
|
||||||
true
|
true
|
||||||
catch
|
catch
|
||||||
false
|
# Attempt to handle cases such as:
|
||||||
|
# [spoiler]www.[/spoiler]example.com #
|
||||||
|
# www.example[spoiler].com[/spoiler] #
|
||||||
|
return false if boundaries.areRelocated
|
||||||
|
Linkify.relocate boundaries
|
||||||
|
Linkify.surround anchor, range, boundaries
|
||||||
|
|
||||||
|
relocate: (boundaries) ->
|
||||||
|
# What do you mean, "silly"?
|
||||||
|
boundaries.areRelocated = true
|
||||||
|
|
||||||
|
if boundaries.startOffset is 0
|
||||||
|
parentNode = boundaries.startNode
|
||||||
|
until parentNode.previousSibling
|
||||||
|
{parentNode} = parentNode
|
||||||
|
parent = parentNode.parentNode
|
||||||
|
boundaries.startNode = parent
|
||||||
|
boundaries.startOffset = [parent.childNodes...].indexOf parentNode
|
||||||
|
|
||||||
|
if boundaries.endOffset is boundaries.endNode.length
|
||||||
|
parentNode = boundaries.endNode
|
||||||
|
until parentNode.nextSibling
|
||||||
|
{parentNode} = parentNode
|
||||||
|
parent = parentNode.parentNode
|
||||||
|
boundaries.endNode = parent
|
||||||
|
boundaries.endOffset = [parent.childNodes...].indexOf parentNode
|
||||||
|
|
||||||
cleanLink: (anchor) ->
|
cleanLink: (anchor) ->
|
||||||
# TODO
|
# TODO
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user