'use strict';
describe("markups", function() {
  var compile, element, scope;
  beforeEach(function() {
    scope = null;
    element = null;
    compile = function(html) {
      element = jqLite(html);
      scope = angular.compile(element)();
    };
  });
  afterEach(function() {
    dealoc(element);
  });
  it('should translate {{}} in text', function() {
    compile('
hello {{name}}!
');
    expect(sortedHtml(element)).toEqual('hello !
');
    scope.name = 'Misko';
    scope.$digest();
    expect(sortedHtml(element)).toEqual('hello Misko!
');
  });
  it('should translate {{}} in terminal nodes', function() {
    compile('');
    scope.$digest();
    expect(sortedHtml(element).replace(' selected="true"', '')).
      toEqual('');
    scope.name = 'Misko';
    scope.$digest();
    expect(sortedHtml(element).replace(' selected="true"', '')).
      toEqual('');
  });
  it('should translate {{}} in attributes', function() {
    compile('');
    expect(element.attr('ng:bind-attr')).toEqual('{"src":"http://server/{{path}}.png"}');
    scope.path = 'a/b';
    scope.$digest();
    expect(element.attr('src')).toEqual("http://server/a/b.png");
  });
  describe('OPTION value', function() {
    beforeEach(function() {
      this.addMatchers({
        toHaveValue: function(expected){
          this.message = function() {
            return 'Expected "' + this.actual.html() + '" to have value="' + expected + '".';
          };
          var value;
          htmlParser(this.actual.html(), {
            start:function(tag, attrs){
              value = attrs.value;
            },
            end:noop,
            chars:noop
          });
          return trim(value) == trim(expected);
        }
      });
    });
    afterEach(function() {
      if (element) element.remove();
    });
    it('should populate value attribute on OPTION', function() {
      compile('');
      expect(element).toHaveValue('abc');
    });
    it('should ignore value if already exists', function() {
      compile('');
      expect(element).toHaveValue('abc');
    });
    it('should set value even if newlines present', function() {
      compile('');
      expect(element).toHaveValue('\nabc\n');
    });
    it('should set value even if self closing HTML', function() {
      // IE removes the \n from option, which makes this test pointless
      if (msie) return;
      compile('');
      expect(element).toHaveValue('\n');
    });
  });
  it('should bind href', function() {
    compile('');
    expect(sortedHtml(element)).toEqual('');
  });
  it('should bind disabled', function() {
    compile('');
    scope.isDisabled = false;
    scope.$digest();
    expect(element.attr('disabled')).toBeFalsy();
    scope.isDisabled = true;
    scope.$digest();
    expect(element.attr('disabled')).toBeTruthy();
  });
  it('should bind checked', function() {
    compile('');
    scope.isChecked = false;
    scope.$digest();
    expect(element.attr('checked')).toBeFalsy();
    scope.isChecked=true;
    scope.$digest();
    expect(element.attr('checked')).toBeTruthy();
  });
  it('should bind selected', function() {
    compile('');
    jqLite(document.body).append(element)
    scope.isSelected=false;
    scope.$digest();
    expect(element.children()[1].selected).toBeFalsy();
    scope.isSelected=true;
    scope.$digest();
    expect(element.children()[1].selected).toBeTruthy();
  });
  it('should bind readonly', function() {
    compile('');
    scope.isReadonly=false;
    scope.$digest();
    expect(element.attr('readOnly')).toBeFalsy();
    scope.isReadonly=true;
    scope.$digest();
    expect(element.attr('readOnly')).toBeTruthy();
  });
  it('should bind multiple', function() {
    compile('');
    scope.isMultiple=false;
    scope.$digest();
    expect(element.attr('multiple')).toBeFalsy();
    scope.isMultiple='multiple';
    scope.$digest();
    expect(element.attr('multiple')).toBeTruthy();
  });
  it('should bind src', function() {
    compile('');
    scope.url = 'http://localhost/';
    scope.$digest();
    expect(element.attr('src')).toEqual('http://localhost/');
  });
  it('should bind href and merge with other attrs', function() {
    compile('');
    expect(sortedHtml(element)).toEqual('');
  });
  it('should bind Text with no Bindings', function() {
    forEach('src,href,checked,disabled,multiple,readonly,selected'.split(','), function(name) {
      compile('');
      expect(sortedHtml(element)).toEqual('');
      dealoc(element);
    });
  });
  it('should Parse Text With No Bindings', function() {
    var parts = parseBindings("a");
    assertEquals(parts.length, 1);
    assertEquals(parts[0], "a");
    assertTrue(!binding(parts[0]));
  });
  it('should Parse Empty Text', function() {
    var parts = parseBindings("");
    assertEquals(parts.length, 1);
    assertEquals(parts[0], "");
    assertTrue(!binding(parts[0]));
  });
  it('should Parse Inner Binding', function() {
    var parts = parseBindings("a{{b}}C");
    assertEquals(parts.length, 3);
    assertEquals(parts[0], "a");
    assertTrue(!binding(parts[0]));
    assertEquals(parts[1], "{{b}}");
    assertEquals(binding(parts[1]), "b");
    assertEquals(parts[2], "C");
    assertTrue(!binding(parts[2]));
  });
  it('should Parse Ending Binding', function() {
    var parts = parseBindings("a{{b}}");
    assertEquals(parts.length, 2);
    assertEquals(parts[0], "a");
    assertTrue(!binding(parts[0]));
    assertEquals(parts[1], "{{b}}");
    assertEquals(binding(parts[1]), "b");
  });
  it('should Parse Begging Binding', function() {
    var parts = parseBindings("{{b}}c");
    assertEquals(parts.length, 2);
    assertEquals(parts[0], "{{b}}");
    assertEquals(binding(parts[0]), "b");
    assertEquals(parts[1], "c");
    assertTrue(!binding(parts[1]));
  });
  it('should Parse Loan Binding', function() {
    var parts = parseBindings("{{b}}");
    assertEquals(parts.length, 1);
    assertEquals(parts[0], "{{b}}");
    assertEquals(binding(parts[0]), "b");
  });
  it('should Parse Two Bindings', function() {
    var parts = parseBindings("{{b}}{{c}}");
    assertEquals(parts.length, 2);
    assertEquals(parts[0], "{{b}}");
    assertEquals(binding(parts[0]), "b");
    assertEquals(parts[1], "{{c}}");
    assertEquals(binding(parts[1]), "c");
  });
  it('should Parse Two Bindings With Text In Middle', function() {
    var parts = parseBindings("{{b}}x{{c}}");
    assertEquals(parts.length, 3);
    assertEquals(parts[0], "{{b}}");
    assertEquals(binding(parts[0]), "b");
    assertEquals(parts[1], "x");
    assertTrue(!binding(parts[1]));
    assertEquals(parts[2], "{{c}}");
    assertEquals(binding(parts[2]), "c");
  });
  it('should Parse Multiline', function() {
    var parts = parseBindings('"X\nY{{A\nB}}C\nD"');
    assertTrue(!!binding('{{A\nB}}'));
    assertEquals(parts.length, 3);
    assertEquals(parts[0], '"X\nY');
    assertEquals(parts[1], '{{A\nB}}');
    assertEquals(parts[2], 'C\nD"');
  });
  it('should Has Binding', function() {
    assertTrue(hasBindings(parseBindings("{{a}}")));
    assertTrue(!hasBindings(parseBindings("a")));
    assertTrue(hasBindings(parseBindings("{{b}}x{{c}}")));
  });
});