src/inject/index.js
function inject(content, opt) {
  var startTag = opt.startTag;
  var endTag = opt.endTag;
  var startMatch;
  var endMatch;

  /**
   * The content consists of:
   *
   * <everything before startMatch>
   * <startMatch>
   * <previousInnerContent>
   * <endMatch>
   * <everything after endMatch>
   */

  while ((startMatch = startTag.exec(content)) !== null) {
    if (typeof opt.onMatch === 'function') {
      opt.onMatch(startMatch);
    }
    if (typeof opt.shouldAbort === 'function' && opt.shouldAbort(startMatch)) {
      continue;
    }
    // Take care of content length change:
    endTag.lastIndex = startTag.lastIndex;
    endMatch = endTag.exec(content);
    if (!endMatch) {
      throw error('Missing end tag for start tag: ' + startMatch[0]);
    }
    var toInject = opt.tagsToInject.slice();

    if (typeof opt.willInject === 'function') {
      opt.willInject(toInject);
    }

    // <everything before startMatch>:
    var newContents = content.slice(0, startMatch.index);

    if (opt.removeTags) {
      if (opt.empty) {
        // Take care of content length change:
        startTag.lastIndex -= startMatch[0].length;
      }
    } else {
      // <startMatch> + <endMatch>
      toInject.unshift(startMatch[0]);
      toInject.push(endMatch[0]);
    }
    var previousInnerContent = content.substring(startTag.lastIndex, endMatch.index);
    var indent = getLeadingWhitespace(previousInnerContent);
    // <new inner content>:
    newContents += toInject.join(indent);
    // <everything after endMatch>:
    newContents += content.slice(endTag.lastIndex);
    // Replace old content with new:
    content = newContents;
  }

  return content;
}
function inject(content, opt) {
  var startTag = opt.startTag;
  var endTag = opt.endTag;
  var startMatch;
  var endMatch;

  /**
   * The content consists of:
   *
   * <everything before startMatch>
   * <startMatch>
   * <previousInnerContent>
   * <endMatch>
   * <everything after endMatch>
   */

  while ((startMatch = startTag.exec(content)) !== null) {
    if (typeof opt.onMatch === 'function') {
      opt.onMatch(startMatch);
    }
    if (typeof opt.shouldAbort === 'function' && opt.shouldAbort(startMatch)) {
      continue;
    }
    // Take care of content length change:
    endTag.lastIndex = startTag.lastIndex;
    endMatch = endTag.exec(content);
    if (!endMatch) {
      throw error('Missing end tag for start tag: ' + startMatch[0]);
    }
    var toInject = opt.tagsToInject.slice();

    if (typeof opt.willInject === 'function') {
      opt.willInject(toInject);
    }

    // <everything before startMatch>:
    var newContents = content.slice(0, startMatch.index);

    if (opt.removeTags) {
      if (opt.empty) {
        // Take care of content length change:
        startTag.lastIndex -= startMatch[0].length;
      }
    } else {
      // <startMatch> + <endMatch>
      toInject.unshift(startMatch[0]);
      toInject.push(endMatch[0]);
    }
    var previousInnerContent = content.substring(startTag.lastIndex, endMatch.index);
    var indent = getLeadingWhitespace(previousInnerContent);
    // <new inner content>:
    newContents += toInject.join(indent);
    // <everything after endMatch>:
    newContents += content.slice(endTag.lastIndex);
    // Replace old content with new:
    content = newContents;
  }

  return content;
}
function inject ( content , opt ) { var startTag = opt . startTag ; var endTag = opt . endTag ; var startMatch ; var endMatch ; /**
   * The content consists of:
   *
   * <everything before startMatch>
   * <startMatch>
   * <previousInnerContent>
   * <endMatch>
   * <everything after endMatch>
   */ while ( ( startMatch = startTag . exec ( content ) ) !== null ) { if ( typeof opt . onMatch === 'function' ) { opt . onMatch ( startMatch ) ; } if ( typeof opt . shouldAbort === 'function' && opt . shouldAbort ( startMatch ) ) { continue ; } // Take care of content length change: endTag . lastIndex = startTag . lastIndex ; endMatch = endTag . exec ( content ) ; if ( ! endMatch ) { throw error ( 'Missing end tag for start tag: ' + startMatch [ 0 ] ) ; } var toInject = opt . tagsToInject . slice ( ) ; if ( typeof opt . willInject === 'function' ) { opt . willInject ( toInject ) ; } // <everything before startMatch>: var newContents = content . slice ( 0 , startMatch . index ) ; if ( opt . removeTags ) { if ( opt . empty ) { // Take care of content length change: startTag . lastIndex -= startMatch [ 0 ] . length ; } } else { // <startMatch> + <endMatch> toInject . unshift ( startMatch [ 0 ] ) ; toInject . push ( endMatch [ 0 ] ) ; } var previousInnerContent = content . substring ( startTag . lastIndex , endMatch . index ) ; var indent = getLeadingWhitespace ( previousInnerContent ) ; // <new inner content>: newContents += toInject . join ( indent ) ; // <everything after endMatch>: newContents += content . slice ( endTag . lastIndex ) ; // Replace old content with new: content = newContents ; } return content ; }
Inject tags into content for given
start and end tags

@param {String} content
@param {Object} opt
@returns {String}
Inject tags into content for given start and end tags