
import _extend from 'nitro-tools/extend';

function _ready (_callback, delay) {
  var callback = delay ? function () { setTimeout(_callback, delay); } : _callback;

  if (/loaded|complete/.test(document.readyState)) {
    callback();
  } else {
    window.addEventListener('load', callback);
  }
}

function findBubbleClose (str) {
  var level = 0;

  for( var i = 0, len = str.length; i < len ; i++ ) {
    if( str[i] === '(' ) {
      level++;
    } else if( str[i] === ')' ) {
      if( level === 0 ) {
        return i;
      } else {
        level--;
      }
    }
  }

  return -1;
}

var _arraySlice = Array.prototype.slice;
var _arrayPush = Array.prototype.push;

function hasSelector (selector, rootElement) {
  var split_has = selector.split(':has(');

  return split_has.reduce(function (matches, partial) {

    var close_pos = findBubbleClose(partial),
        has_filter = partial.substr(0, close_pos),
        partial_query = partial.substr(close_pos + 1).trim();

    if( close_pos === -1 ) {
      throw new Error('malformed selector');
    }

    matches = matches.filter(function (element) {
      return element.querySelector(has_filter);
    });

    if( partial_query ) {
      var submatches = [];

      matches.forEach(function (element) {
        _arrayPush.apply(submatches, element.querySelectorAll(partial_query) );
      });

      return submatches;
    }

    return matches;

  }, _arraySlice.call( (rootElement || document).querySelectorAll( split_has.shift() ) ) );
}

function querySelector (selector, rootElement) {
  // 'tr:has(> .row) div:has(span) img'.split(':has(');
  if( !selector ) {
    return [];
  }

  selector = selector
    .replace(/:first(?!-)/, ':first-child')
    .replace(/:last(?!-)/, ':last-child')
    .replace(/:(password|checkbox|file|submit|image)(?!-)/, '[type="$1"]')
  ;

  if( !/:has\(/.test(selector) ) {
    return [].slice.call( (rootElement || document).querySelectorAll( selector ) );
  }

  return hasSelector(selector);
}

function cssQuery (_selector, rootElement) {
  var selectors = _selector.split(/\s*,\s*/);

  return selectors.reduce(function (list, selector) {
    return list.concat( querySelector(selector, rootElement) );
  }, []);
}

function clearElement (el) {
  var child = el.firstChild;

  while( child ) {
    el.removeChild(child);
    child = el.firstChild;
  }
}

// var _classActions = {
//   add: document.documentElement.classList ? function (element, className) {
//     element.classList.add(className);
//   } : function (element, className) {
//     var RE_CLEANCLASS = new RegExp('\\b' + (className || '') + '\\b','');
//     _classActions.remove(element, className);
//     element.className += ' ' + className;
//   },
//   remove: document.documentElement.classList ? function (element, className) {
//     element.classList.remove(className);
//   } : function (element, className) {
//     var RE_CLEANCLASS = new RegExp('\\b' + (className || '') + '\\b','');
//     element.className = element.className.replace(RE_CLEANCLASS,'');
//   },
//   action: function (action, tools) {
//     return function (element, className) {
//       if( className.indexOf(' ') >= 0 ) {
//         className.split(' ').forEach(function (cn) {
//           _classActions[action](element, cn);
//         });
//       } else {
//         _classActions[action](element, className);
//       }
//       return tools;
//     };
//   }
// };

function writeIframe (iframe, content) {
  var iframeDoc = iframe.contentWindow.document;
  try { iframeDoc.charset = 'UTF-8'; } catch(err) {}
  iframeDoc.open();
  iframeDoc.write(content);
  iframeDoc.close();
}

var remove_style = / Trident\//.test(navigator.userAgent) ? '' : null;

function getIFrame (iframeStyles) {
  var iframe = document.createElement('iframe');
  _extend.extend(iframe.style, iframeStyles || {});

  iframe.frameBorder = '0';
  // iframe.style.display = 'none';
  iframe.onload = function () {
    iframe.style.display = remove_style;
  };
  return iframe;
}

function matchMedia (query) {
  return (window.matchMedia = window.matchMedia || window.webkitMatchMedia || window.mozMatchMedia || window.msMatchMedia)(query);
}

var isMobile = matchMedia('( max-width: 600px )');

var _ = {
  isMobile: function () {
    return isMobile.matches;
  },
  ready: _ready,
  getIFrame: getIFrame,
  writeIframe: writeIframe,
  cssQuery: cssQuery,
  clearElement: clearElement,
  elementData: document.createElement('div').dataset ? function (el, key, value) {
    if( value !== undefined ) {
      el.dataset[key] = value;
    }
    return el.dataset[key];
  } : function (el, key, value) {
    if( value !== undefined ) {
      el.setAttribute('data-' + key, value);
    }
    return el.getAttribute('data-' + key);
  },
};

// var addClass = _classActions.action('add', _ ),
//     removeClass = _classActions.action('remove', _ );

// _.addClass = addClass;
// _.removeClass = removeClass;

_.addClass = function (element, className) {
  element.classList.add(className);
};

_.removeClass = function (element, className) {
  element.classList.remove(className);
};

export default _;

@@ -1647,196 +1647,8 @@
  %7D%0A%0A
-  selector = selector%0A    .replace(/:first(?!-)/, ':first-child')%0A    .replace(/:last(?!-)/, ':last-child')%0A    .replace(/:(password%7Ccheckbox%7Cfile%7Csubmit%7Cimage)(?!-)/, '%5Btype=%22$1%22%5D')%0A  ;%0A%0A
   if

Update browser-tools.js