// Generated by CoffeeScript 1.4.0
(function() {
  var balUtilFlow, balUtilHTML,
    __slice = [].slice;

  balUtilFlow = (typeof require === "function" ? require(__dirname + '/flow') : void 0) || this.balUtilFlow;

  balUtilHTML = {
    getAttribute: function(attributes, attribute) {
      var match, regex, value;
      regex = new RegExp("(" + attribute + ")\\s*=\\s*('[^']+'|\\\"[^\\\"]+\\\"|[^'\\\"\\s]\\S*)", 'ig');
      value = null;
      while (match = regex.exec(attributes)) {
        value = match[2].trim().replace(/(^['"]\s*|\s*['"]$)/g, '');
      }
      return value;
    },
    detectIndentation: function(source) {
      var indentation, result;
      result = /\n([ \t]*)\S/m.exec(source);
      indentation = (result != null ? result[1] : void 0) || '';
      return indentation;
    },
    removeIndentation: function(source) {
      var indentation, regex, regexString, result;
      indentation = balUtilHTML.detectIndentation(source);
      regexString = indentation.replace(/\t/g, '\\t');
      regex = new RegExp("^" + regexString, 'gm');
      result = source.replace(regex, '').trim();
      return result;
    },
    replaceElement: function() {
      var args, element, html, regex, removeIndentation, replace, result, _ref;
      args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
      if (args.length === 1) {
        _ref = args[0], html = _ref.html, element = _ref.element, removeIndentation = _ref.removeIndentation, replace = _ref.replace;
      } else {
        html = args[0], element = args[1], replace = args[2];
      }
      regex = new RegExp("<(" + element + "(?:\\:[-:_a-z0-9]+)?)(\\s+[^>]+)?>([\\s\\S]+?)<\\/\\1>", 'ig');
      result = html.replace(regex, function(outerHTML, element, attributes, innerHTML) {
        if (removeIndentation !== false) {
          innerHTML = balUtilHTML.removeIndentation(innerHTML);
        }
        return replace(outerHTML, element, attributes, innerHTML);
      });
      return result;
    },
    replaceElementAsync: function() {
      var args, element, html, next, removeIndentation, replace, result, tasks, _ref;
      args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
      if (args.length === 1) {
        _ref = args[0], html = _ref.html, element = _ref.element, removeIndentation = _ref.removeIndentation, replace = _ref.replace, next = _ref.next;
      } else {
        html = args[0], element = args[1], replace = args[2], next = args[3];
      }
      tasks = new balUtilFlow.Group(function(err) {
        if (err) {
          return next(err);
        }
        return next(null, result);
      });
      result = balUtilHTML.replaceElement({
        html: html,
        element: element,
        removeIndentation: removeIndentation,
        replace: function(outerHTML, element, attributes, innerHTML) {
          var random;
          random = Math.random();
          tasks.push(function(complete) {
            return replace(outerHTML, element, attributes, innerHTML, function(err, replaceElementResult) {
              if (err) {
                return complete(err);
              }
              result = result.replace(random, replaceElementResult);
              return complete();
            });
          });
          return random;
        }
      });
      tasks.sync();
      return this;
    }
  };

  if (typeof module !== "undefined" && module !== null) {
    module.exports = balUtilHTML;
  } else {
    this.balUtilHTML = balUtilHTML;
  }

}).call(this);