dist/optimal-select.js
/**
 * # Optimize
 *
 * 1.) Improve efficiency through shorter selectors by removing redundancy
 * 2.) Improve robustness through selector transformation
 */
# Optimize
1.) Improve efficiency through shorter selectors by removing redundancy
2.) Improve robustness through selector transformation
# Optimize 1 . ) Improve efficiency through shorter selectors by removing redundancy 2 . ) Improve robustness through selector transformation
function optimize(selector, elements) {
  var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  if (!Array.isArray(elements)) {
    elements = !elements.length ? [elements] : (0, _utilities.convertNodeList)(elements);
  }
  if (!elements.length || elements.some(function (element) {
    return element.nodeType !== 1;
  })) {
    throw new Error('Invalid input - to compare HTMLElements its necessary to provide a reference of the selected node(s)! (missing "elements")');
  }
  var globalModified = (0, _adapt2.default)(elements[0], options);
  var path = selector.replace(/> /g, '>').split(/\s+(?=(?:(?:[^"]*"){2})*[^"]*$)/);
  if (path.length < 2) {
    return optimizePart('', selector, '', elements);
  }
  var shortened = [path.pop()];
  while (path.length > 1) {
    var current = path.pop();
    var prePart = path.join(' ');
    var postPart = shortened.join(' ');
    var pattern = prePart + ' ' + postPart;
    var matches = document.querySelectorAll(pattern);
    if (matches.length !== elements.length) {
      shortened.unshift(optimizePart(prePart, current, postPart, elements));
    }
  }
  shortened.unshift(path[0]);
  path = shortened;
  path[0] = optimizePart('', path[0], path.slice(1).join(' '), elements);
  path[path.length - 1] = optimizePart(path.slice(0, -1).join(' '), path[path.length - 1], '', elements);
  if (globalModified) {
    delete true;
  }
  return path.join(' ').replace(/>/g, '> ').trim();
}
function optimize ( selector , elements ) { var options = arguments . length > 2 && arguments [ 2 ] !== undefined ? arguments [ 2 ] : { } ; if ( ! Array . isArray ( elements ) ) { elements = ! elements . length ? [ elements ] : ( 0 , _utilities . convertNodeList ) ( elements ) ; } if ( ! elements . length || elements . some ( function ( element ) { return element . nodeType !== 1 ; } ) ) { throw new Error ( 'Invalid input - to compare HTMLElements its necessary to provide a reference of the selected node(s)! (missing "elements")' ) ; } var globalModified = ( 0 , _adapt2 . default ) ( elements [ 0 ] , options ) ; var path = selector . replace ( / >  / g , '>' ) . split ( / \s+(?=(?:(?:[^"]*"){2})*[^"]*$) / ) ; if ( path . length < 2 ) { return optimizePart ( '' , selector , '' , elements ) ; } var shortened = [ path . pop ( ) ] ; while ( path . length > 1 ) { var current = path . pop ( ) ; var prePart = path . join ( ' ' ) ; var postPart = shortened . join ( ' ' ) ; var pattern = prePart + ' ' + postPart ; var matches = document . querySelectorAll ( pattern ) ; if ( matches . length !== elements . length ) { shortened . unshift ( optimizePart ( prePart , current , postPart , elements ) ) ; } } shortened . unshift ( path [ 0 ] ) ; path = shortened ; path [ 0 ] = optimizePart ( '' , path [ 0 ] , path . slice ( 1 ) . join ( ' ' ) , elements ) ; path [ path . length - 1 ] = optimizePart ( path . slice ( 0 , - 1 ) . join ( ' ' ) , path [ path . length - 1 ] , '' , elements ) ; if ( globalModified ) { delete true ; } return path . join ( ' ' ) . replace ( / > / g , '> ' ) . trim ( ) ; }
# Optimize
 1.)
# Optimize 1 . )
// convert single entry and NodeList // chunk parts outside of quotes (http://stackoverflow.com/a/25663729) // optimize start + end