<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=555559
https://bugzilla.mozilla.org/show_bug.cgi?id=668817
https://bugzilla.mozilla.org/show_bug.cgi?id=854812
-->
<head>
  <title>Test for &lt;input type='email'&gt; validity</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=555559">Mozilla Bug 555559</a>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=668817">Mozilla Bug 668817</a>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=854812">Mozilla Bug 854812</a>
<p id="display"></p>
<div id="content" style="display: none">
  <form>
    <input type='email' name='email' id='i' oninvalid="invalidEventHandler(event);">
  <form>
</div>
<pre id="test">
<script type="application/javascript">

/** Test for <input type='email'> validity **/

var gInvalid = false;

function invalidEventHandler(e)
{
  is(e.type, "invalid", "Invalid event type should be invalid");
  gInvalid = true;
}

function checkValidEmailAddress(element)
{
  gInvalid = false;
  ok(!element.validity.typeMismatch && !element.validity.badInput,
     "Element should not suffer from type mismatch or bad input (with value='"+element.value+"')");
  ok(element.validity.valid, "Element should be valid");
  ok(element.checkValidity(), "Element should be valid");
  ok(!gInvalid, "The invalid event should not have been thrown");
  is(element.validationMessage, '',
     "Validation message should be the empty string");
  ok(element.matches(":valid"), ":valid pseudo-class should apply");
}

const VALID = 0;
const TYPE_MISMATCH = 1 << 0;
const BAD_INPUT = 1 << 1;

function checkInvalidEmailAddress(element, failedValidityStates)
{
  info("Checking " + element.value);
  gInvalid = false;
  var expectTypeMismatch = !!(failedValidityStates & TYPE_MISMATCH);
  var expectBadInput = !!(failedValidityStates & BAD_INPUT);
  ok(element.validity.typeMismatch == expectTypeMismatch,
     "Element should " + (expectTypeMismatch ? "" : "not ") + "suffer from type mismatch (with value='"+element.value+"')");
  ok(element.validity.badInput == expectBadInput,
     "Element should " + (expectBadInput ? "" : "not ") + "suffer from bad input (with value='"+element.value+"')");
  ok(!element.validity.valid, "Element should not be valid");
  ok(!element.checkValidity(), "Element should not be valid");
  ok(gInvalid, "The invalid event should have been thrown");
  is(element.validationMessage, "Please enter an email address.",
     "Validation message is not valid");
  ok(element.matches(":invalid"), ":invalid pseudo-class should apply");
}

function testEmailAddress(aElement, aValue, aMultiple, aValidityFailures)
{
  aElement.multiple = aMultiple;
  aElement.value = aValue;

  if (!aValidityFailures) {
    checkValidEmailAddress(aElement);
  } else {
    checkInvalidEmailAddress(aElement, aValidityFailures);
  }
}

var email = document.forms[0].elements[0];

// Simple values, checking the e-mail syntax validity.
var values = [
  [ '' ], // The empty string shouldn't be considered as invalid.
  [ 'foo@bar.com', VALID ],
  [ ' foo@bar.com', VALID ],
  [ 'foo@bar.com ', VALID ],
  [ '\r\n foo@bar.com', VALID ],
  [ 'foo@bar.com \n\r', VALID ],
  [ '\n\n \r\rfoo@bar.com\n\n   \r\r', VALID ],
  [ '\n\r \n\rfoo@bar.com\n\r   \n\r', VALID ],
  [ 'tulip', TYPE_MISMATCH ],
  // Some checks on the user part of the address.
  [ '@bar.com', TYPE_MISMATCH ],
  [ 'f\noo@bar.com', VALID ],
  [ 'f\roo@bar.com', VALID ],
  [ 'f\r\noo@bar.com', VALID ],
  [ 'fü@foo.com', TYPE_MISMATCH ],
  // Some checks for the domain part.
  [ 'foo@bar', VALID ],
  [ 'foo@b', VALID ],
  [ 'foo@', TYPE_MISMATCH ],
  [ 'foo@bar.', TYPE_MISMATCH ],
  [ 'foo@foo.bar', VALID ],
  [ 'foo@foo..bar', TYPE_MISMATCH ],
  [ 'foo@.bar', TYPE_MISMATCH ],
  [ 'foo@tulip.foo.bar', VALID ],
  [ 'foo@tulip.foo-bar', VALID ],
  [ 'foo@1.2', VALID ],
  [ 'foo@127.0.0.1', VALID ],
  [ 'foo@1.2.3', VALID ],
  [ 'foo@b\nar.com', VALID ],
  [ 'foo@b\rar.com', VALID ],
  [ 'foo@b\r\nar.com', VALID ],
  [ 'foo@.', TYPE_MISMATCH ],
  [ 'foo@fü.com', VALID ],
  [ 'foo@fu.cüm', VALID ],
  [ 'thisUsernameIsLongerThanSixtyThreeCharactersInLengthRightAboutNow@mozilla.tld', VALID ],
  // Long strings with UTF-8 in username.
  [ 'this.is.email.should.be.longer.than.sixty.four.characters.föö@mözillä.tld', TYPE_MISMATCH ],
  [ 'this-is-email-should-be-longer-than-sixty-four-characters-föö@mözillä.tld', TYPE_MISMATCH, true ],
  // Long labels (labels greater than 63 chars long are not allowed).
  [ 'foo@thislabelisexactly63characterssssssssssssssssssssssssssssssssss', VALID ],
  [ 'foo@thislabelisexactly63characterssssssssssssssssssssssssssssssssss.com', VALID ],
  [ 'foo@foo.thislabelisexactly63characterssssssssssssssssssssssssssssssssss.com', VALID ],
  [ 'foo@foo.thislabelisexactly63characterssssssssssssssssssssssssssssssssss', VALID ],
  [ 'foo@thislabelisexactly64charactersssssssssssssssssssssssssssssssssss', TYPE_MISMATCH | BAD_INPUT ],
  [ 'foo@thislabelisexactly64charactersssssssssssssssssssssssssssssssssss.com', TYPE_MISMATCH | BAD_INPUT ],
  [ 'foo@foo.thislabelisexactly64charactersssssssssssssssssssssssssssssssssss.com', TYPE_MISMATCH | BAD_INPUT ],
  [ 'foo@foo.thislabelisexactly64charactersssssssssssssssssssssssssssssssssss', TYPE_MISMATCH | BAD_INPUT ],
  // Long labels with UTF-8 (punycode encoding will increase the label to more than 63 chars).
  [ 'foo@thisläbelisexäctly63charäcterssssssssssssssssssssssssssssssssss', TYPE_MISMATCH | BAD_INPUT ],
  [ 'foo@thisläbelisexäctly63charäcterssssssssssssssssssssssssssssssssss.com', TYPE_MISMATCH | BAD_INPUT ],
  [ 'foo@foo.thisläbelisexäctly63charäcterssssssssssssssssssssssssssssssssss.com', TYPE_MISMATCH | BAD_INPUT ],
  [ 'foo@foo.thisläbelisexäctly63charäcterssssssssssssssssssssssssssssssssss', TYPE_MISMATCH | BAD_INPUT ],
  // The domains labels (sub-domains or tld) can't start or finish with a '-'
  [ 'foo@foo-bar', VALID ],
  [ 'foo@-foo', TYPE_MISMATCH ],
  [ 'foo@foo-.bar', TYPE_MISMATCH ],
  [ 'foo@-.-', TYPE_MISMATCH ],
  [ 'foo@fo-o.bar', VALID ],
  [ 'foo@fo-o.-bar', TYPE_MISMATCH ],
  [ 'foo@fo-o.bar-', TYPE_MISMATCH ],
  [ 'foo@fo-o.-', TYPE_MISMATCH ],
  [ 'foo@fo--o', VALID ],
];

// Multiple values, we don't check e-mail validity, only multiple stuff.
var multipleValues = [
  [ 'foo@bar.com, foo@bar.com', VALID ],
  [ 'foo@bar.com,foo@bar.com', VALID ],
  [ 'foo@bar.com,foo@bar.com,foo@bar.com', VALID ],
  [ '     foo@bar.com     ,     foo@bar.com    ', VALID ],
  [ '\tfoo@bar.com\t,\tfoo@bar.com\t', VALID ],
  [ '\rfoo@bar.com\r,\rfoo@bar.com\r', VALID ],
  [ '\nfoo@bar.com\n,\nfoo@bar.com\n', VALID ],
  [ '\ffoo@bar.com\f,\ffoo@bar.com\f', VALID ],
  [ '\t foo@bar.com\r,\nfoo@bar.com\f', VALID ],
  [ 'foo@b,ar.com,foo@bar.com', TYPE_MISMATCH ],
  [ 'foo@bar.com,foo@bar.com,', TYPE_MISMATCH ],
  [ '   foo@bar.com   ,   foo@bar.com   ,   ', TYPE_MISMATCH ],
  [ ',foo@bar.com,foo@bar.com', TYPE_MISMATCH ],
  [ ',foo@bar.com,foo@bar.com', TYPE_MISMATCH ],
  [ 'foo@bar.com,,,foo@bar.com', TYPE_MISMATCH ],
  [ 'foo@bar.com;foo@bar.com', TYPE_MISMATCH ],
  [ '<foo@bar.com>, <foo@bar.com>', TYPE_MISMATCH ],
  [ 'foo@bar, foo@bar.com', VALID ],
  [ 'foo@bar.com, foo', TYPE_MISMATCH ],
  [ 'foo, foo@bar.com', TYPE_MISMATCH ],
];

/* Additional username checks. */

var legalCharacters = "abcdefghijklmnopqrstuvwxyz";
legalCharacters += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
legalCharacters += "0123456789";
legalCharacters += "!#$%&'*+-/=?^_`{|}~.";

// Add all username legal characters individually to the list.
for (c of legalCharacters) {
  values.push([c + "@bar.com", VALID]);
}
// Add the concatenation of all legal characters too.
values.push([legalCharacters + "@bar.com", VALID]);

// Add username illegal characters, the same way.
var illegalCharacters = "()<>[]:;@\\, \t";
for (c of illegalCharacters) {
  values.push([illegalCharacters + "@bar.com", TYPE_MISMATCH]);
}

/* Additional domain checks. */

legalCharacters = "abcdefghijklmnopqrstuvwxyz";
legalCharacters += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
legalCharacters += "0123456789";

// Add domain legal characters (except '.' and '-' because they are special).
for (c of legalCharacters) {
  values.push(["foo@foo.bar" + c, VALID]);
}
// Add the concatenation of all legal characters too.
values.push(["foo@bar." + legalCharacters, VALID]);

// Add domain illegal characters.
illegalCharacters = "()<>[]:;@\\,!#$%&'*+/=?^_`{|}~ \t";
for (c of illegalCharacters) {
  values.push(['foo@foo.ba' + c + 'r', TYPE_MISMATCH]);
}

values.forEach(function([value, valid, todo]) {
  if (todo === true) {
    email.value = value;
    todo_is(email.validity.valid, true, "value should be valid");
  } else {
    testEmailAddress(email, value, false, valid);
  }
});

multipleValues.forEach(function([value, valid]) {
  testEmailAddress(email, value, true, valid);
});

// Make sure setting multiple changes the value.
email.multiple = false;
email.value = "foo@bar.com, foo@bar.com";
checkInvalidEmailAddress(email, TYPE_MISMATCH);
email.multiple = true;
checkValidEmailAddress(email);

</script>
</pre>
</body>
</html>
