packages/turndown/src/collapse-whitespace.js
/**
 * collapseWhitespace(options) removes extraneous whitespace from an the given element.
 *
 * @param {Object} options
 */
collapseWhitespace(options) removes extraneous whitespace from an the given element.
@param {Object} options
collapseWhitespace ( options ) removes extraneous whitespace from an the given element . @param { Object } options
function collapseWhitespace (options) {
  var element = options.element
  var isBlock = options.isBlock
  var isVoid = options.isVoid
  var isPre = options.isPre || function (node) {
    return node.nodeName === 'PRE'
  }
  if (!element.firstChild || isPre(element)) return
  var prevText = null
  var prevVoid = false
  var prev = null
  var node = next(prev, element, isPre)
  var prevTextIsOnlySpaces = false;
  while (node !== element) {
    if (node.nodeType === 3 || node.nodeType === 4) { 
      var text = node.data.replace(/[ \r\n\t]+/g, ' ')
      if ((!prevText || / $/.test(prevText.data)) &&
          !prevVoid && text[0] === ' ') {
        text = text.substr(1)
      }
      var textIsOnlySpaces = containsOnlySpaces(text);
      if (!text || (textIsOnlySpaces && prevTextIsOnlySpaces)) {
        node = remove(node)
        continue
      }
      prevTextIsOnlySpaces = textIsOnlySpaces;
      node.data = text
      prevText = node
    } else if (node.nodeType === 1) { 
      if (isBlock(node) || node.nodeName === 'BR') {
        if (prevText) {
          prevText.data = prevText.data.replace(/ $/, '')
        }
        prevText = null
        prevVoid = false
      } else if (isVoid(node)) {
        prevText = null
        prevVoid = true
      }
    } else {
      node = remove(node)
      continue
    }
    var nextNode = next(prev, node, isPre)
    prev = node
    node = nextNode
  }
  if (prevText) {
    prevText.data = prevText.data.replace(/ $/, '')
    if (!prevText.data) {
      remove(prevText)
    }
  }
}
function collapseWhitespace ( options ) { var element = options . element var isBlock = options . isBlock var isVoid = options . isVoid var isPre = options . isPre || function ( node ) { return node . nodeName === 'PRE' } if ( ! element . firstChild || isPre ( element ) ) return var prevText = null var prevVoid = false var prev = null var node = next ( prev , element , isPre ) var prevTextIsOnlySpaces = false ; while ( node !== element ) { if ( node . nodeType === 3 || node . nodeType === 4 ) { var text = node . data . replace ( / [ \r\n\t]+ / g , ' ' ) if ( ( ! prevText || /  $ / . test ( prevText . data ) ) && ! prevVoid && text [ 0 ] === ' ' ) { text = text . substr ( 1 ) } var textIsOnlySpaces = containsOnlySpaces ( text ) ; if ( ! text || ( textIsOnlySpaces && prevTextIsOnlySpaces ) ) { node = remove ( node ) continue } prevTextIsOnlySpaces = textIsOnlySpaces ; node . data = text prevText = node } else if ( node . nodeType === 1 ) { if ( isBlock ( node ) || node . nodeName === 'BR' ) { if ( prevText ) { prevText . data = prevText . data . replace ( /  $ / , '' ) } prevText = null prevVoid = false } else if ( isVoid ( node ) ) { prevText = null prevVoid = true } } else { node = remove ( node ) continue } var nextNode = next ( prev , node , isPre ) prev = node node = nextNode } if ( prevText ) { prevText . data = prevText . data . replace ( /  $ / , '' ) if ( ! prevText . data ) { remove ( prevText ) } } }
collapseWhitespace(options) removes extraneous whitespace from an the given element.
collapseWhitespace ( options ) removes extraneous whitespace from an the given element .
// We keep track of whether the previous was only spaces or not. This prevent the case where multiple empty blocks are // added, which results in multiple spaces. This spaces are then incorrectly interpreted as a code block by renderers. // So by keeping track of this, we make sure that only one space at most is added. // Node.TEXT_NODE or Node.CDATA_SECTION_NODE // `text` might be empty at this point. // Node.ELEMENT_NODE // Avoid trimming space around non-block, non-BR void elements.