@danielkalen/simplybind
Version:
Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.
30 lines (22 loc) • 1.04 kB
text/coffeescript
textContent = 'textContent'
addToNodeStore = (nodeStore, node, targetPlaceholder)->
nodeStore[targetPlaceholder] ?= []
nodeStore[targetPlaceholder].push(node)
return
scanTextNodesPlaceholders = (element, nodeStore)->
childNodes = Array::slice.call(element.childNodes)
for node in childNodes
if node.nodeType isnt 3
scanTextNodesPlaceholders(node, nodeStore)
else if node[textContent].match(pholderRegExSplit)
textPieces = node[textContent].split(pholderRegEx)
if textPieces.length is 3 and textPieces[0]+textPieces[2] is '' # The entire textNode is just the placeholder
addToNodeStore(nodeStore, node, textPieces[1])
else
newFragment = document.createDocumentFragment()
for textPiece,index in textPieces
newNode = newFragment.appendChild document.createTextNode(textPiece)
if index % 2 # is an odd index, indicating that before this text piece should come a placeholder node
addToNodeStore(nodeStore, newNode, textPiece)
node.parentNode.replaceChild(newFragment, node)
return