src/lib/fibre.js/index.js
function() {

      var matches = this.matches
      var node = this.node
      var elementFilter = this.options.filterElements
      var startPortion,
        endPortion,
        innerPortions = [],
        curNode = node,
        match = matches.shift(),
        atIndex = 0, // i.e. nodeAtIndex
        matchIndex = 0,
        portionIndex = 0,
        doAvoidNode,
        nodeStack = [node]
      out: while (true) {

        if (curNode.nodeType === 3) {

          if (!endPortion && curNode.length + atIndex >= match.endIndex) {

            // We've found the ending
            endPortion = {
              node: curNode,
              index: portionIndex++,
              text: curNode.data.substring(match.startIndex - atIndex, match.endIndex - atIndex),
              indexInMatch: atIndex - match.startIndex,
              indexInNode: match.startIndex - atIndex, // always zero for end-portions
              endIndexInNode: match.endIndex - atIndex,
              isEnd: true
            }
          } else if (startPortion) {
            // Intersecting node
            innerPortions.push({
              node: curNode,
              index: portionIndex++,
              text: curNode.data,
              indexInMatch: atIndex - match.startIndex,
              indexInNode: 0 // always zero for inner-portions
            })
          }

          if (!startPortion && curNode.length + atIndex > match.startIndex) {
            // We've found the match start
            startPortion = {
              node: curNode,
              index: portionIndex++,
              indexInMatch: 0,
              indexInNode: match.startIndex - atIndex,
              endIndexInNode: match.endIndex - atIndex,
              text: curNode.data.substring(match.startIndex - atIndex, match.endIndex - atIndex)
            }
          }

          atIndex += curNode.data.length
        }

        doAvoidNode = curNode.nodeType === 1 && elementFilter && !elementFilter(curNode)
        if (startPortion && endPortion) {

          curNode = this.replaceMatch(match, startPortion, innerPortions, endPortion)
          // processMatches has to return the node that replaced the endNode
          // and then we step back so we can continue from the end of the 
          // match:

          atIndex -= (endPortion.node.data.length - endPortion.endIndexInNode)
          startPortion = null
          endPortion = null
          innerPortions = []
          match = matches.shift()
          portionIndex = 0
          matchIndex++
          if (!match) {
            break; // no more matches
          }

        } else if (
          !doAvoidNode &&
          (curNode.firstChild || curNode.nextSibling)
        ) {
          // Move down or forward:
          if (curNode.firstChild) {
            nodeStack.push(curNode)
            curNode = curNode.firstChild
          } else {
            curNode = curNode.nextSibling
          }
          continue
        }

        // Move forward or up:
        while (true) {
          if (curNode.nextSibling) {
            curNode = curNode.nextSibling
            break
          }
          curNode = nodeStack.pop()
          if (curNode === node) {
            break out
          }
        }

      }

    }
function() {

      var matches = this.matches
      var node = this.node
      var elementFilter = this.options.filterElements
      var startPortion,
        endPortion,
        innerPortions = [],
        curNode = node,
        match = matches.shift(),
        atIndex = 0, // i.e. nodeAtIndex
        matchIndex = 0,
        portionIndex = 0,
        doAvoidNode,
        nodeStack = [node]
      out: while (true) {

        if (curNode.nodeType === 3) {

          if (!endPortion && curNode.length + atIndex >= match.endIndex) {

            // We've found the ending
            endPortion = {
              node: curNode,
              index: portionIndex++,
              text: curNode.data.substring(match.startIndex - atIndex, match.endIndex - atIndex),
              indexInMatch: atIndex - match.startIndex,
              indexInNode: match.startIndex - atIndex, // always zero for end-portions
              endIndexInNode: match.endIndex - atIndex,
              isEnd: true
            }
          } else if (startPortion) {
            // Intersecting node
            innerPortions.push({
              node: curNode,
              index: portionIndex++,
              text: curNode.data,
              indexInMatch: atIndex - match.startIndex,
              indexInNode: 0 // always zero for inner-portions
            })
          }

          if (!startPortion && curNode.length + atIndex > match.startIndex) {
            // We've found the match start
            startPortion = {
              node: curNode,
              index: portionIndex++,
              indexInMatch: 0,
              indexInNode: match.startIndex - atIndex,
              endIndexInNode: match.endIndex - atIndex,
              text: curNode.data.substring(match.startIndex - atIndex, match.endIndex - atIndex)
            }
          }

          atIndex += curNode.data.length
        }

        doAvoidNode = curNode.nodeType === 1 && elementFilter && !elementFilter(curNode)
        if (startPortion && endPortion) {

          curNode = this.replaceMatch(match, startPortion, innerPortions, endPortion)
          // processMatches has to return the node that replaced the endNode
          // and then we step back so we can continue from the end of the 
          // match:

          atIndex -= (endPortion.node.data.length - endPortion.endIndexInNode)
          startPortion = null
          endPortion = null
          innerPortions = []
          match = matches.shift()
          portionIndex = 0
          matchIndex++
          if (!match) {
            break; // no more matches
          }

        } else if (
          !doAvoidNode &&
          (curNode.firstChild || curNode.nextSibling)
        ) {
          // Move down or forward:
          if (curNode.firstChild) {
            nodeStack.push(curNode)
            curNode = curNode.firstChild
          } else {
            curNode = curNode.nextSibling
          }
          continue
        }

        // Move forward or up:
        while (true) {
          if (curNode.nextSibling) {
            curNode = curNode.nextSibling
            break
          }
          curNode = nodeStack.pop()
          if (curNode === node) {
            break out
          }
        }

      }

    }
function ( ) { var matches = this . matches var node = this . node var elementFilter = this . options . filterElements var startPortion , endPortion , innerPortions = [ ] , curNode = node , match = matches . shift ( ) , atIndex = 0 , // i.e. nodeAtIndex matchIndex = 0 , portionIndex = 0 , doAvoidNode , nodeStack = [ node ] out : while ( true ) { if ( curNode . nodeType === 3 ) { if ( ! endPortion && curNode . length + atIndex >= match . endIndex ) { // We've found the ending endPortion = { node : curNode , index : portionIndex ++ , text : curNode . data . substring ( match . startIndex - atIndex , match . endIndex - atIndex ) , indexInMatch : atIndex - match . startIndex , indexInNode : match . startIndex - atIndex , // always zero for end-portions endIndexInNode : match . endIndex - atIndex , isEnd : true } } else if ( startPortion ) { // Intersecting node innerPortions . push ( { node : curNode , index : portionIndex ++ , text : curNode . data , indexInMatch : atIndex - match . startIndex , indexInNode : 0 // always zero for inner-portions } ) } if ( ! startPortion && curNode . length + atIndex > match . startIndex ) { // We've found the match start startPortion = { node : curNode , index : portionIndex ++ , indexInMatch : 0 , indexInNode : match . startIndex - atIndex , endIndexInNode : match . endIndex - atIndex , text : curNode . data . substring ( match . startIndex - atIndex , match . endIndex - atIndex ) } } atIndex += curNode . data . length } doAvoidNode = curNode . nodeType === 1 && elementFilter && ! elementFilter ( curNode ) if ( startPortion && endPortion ) { curNode = this . replaceMatch ( match , startPortion , innerPortions , endPortion ) // processMatches has to return the node that replaced the endNode // and then we step back so we can continue from the end of the  // match: atIndex -= ( endPortion . node . data . length - endPortion . endIndexInNode ) startPortion = null endPortion = null innerPortions = [ ] match = matches . shift ( ) portionIndex = 0 matchIndex ++ if ( ! match ) { break ; // no more matches } } else if ( ! doAvoidNode && ( curNode . firstChild || curNode . nextSibling ) ) { // Move down or forward: if ( curNode . firstChild ) { nodeStack . push ( curNode ) curNode = curNode . firstChild } else { curNode = curNode . nextSibling } continue } // Move forward or up: while ( true ) { if ( curNode . nextSibling ) { curNode = curNode . nextSibling break } curNode = nodeStack . pop ( ) if ( curNode === node ) { break out } } } }
Steps through the target node, looking for matches, and
calling replaceFn when a match is found.
Steps through the target node looking for matches and calling replaceFn when a match is found .