<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test-pre.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>
description('This test aims to check for typeMismatch flag with type=week input fields');
var i = document.createElement('input');
i.type = 'week';

function check(value, disabled)
{
    i.value = value;
    i.disabled = !!disabled;

    if (i.validity.typeMismatch)
        testFailed('"' + value + '" had typeMismatch. This should not happen with sanitization.');
}

function shouldBeValid(value)
{
    check(value);
    if ((value === '' && i.value === '') || (value !== '' && i.value !== ""))
        testPassed('"' + value + '" is a correct valid week string.');
    else
        testFailed('"' + value + '" is a valid week string that failed to be set on the input.');
}

function shouldBeInvalid(value, disabled)
{
    check(value, disabled);
    if (i.value === '')
        testPassed('"' + value + '" is an invalid week string and was sanitized' + (disabled ? ' while disabled' : '') + '.');
    else
        testFailed('"' + value + '" is an invalid week string and was not sanitized' + (disabled ? ' while disabled' : '') + '.');
}

// Valid values
shouldBeValid('');
shouldBeValid('0001-W01');
shouldBeValid('1583-W01');
shouldBeValid('9999-W52');
shouldBeValid('275760-W37');
shouldBeValid('2009-W01');
shouldBeValid('2004-W53');  // 2004 started on Thursday.
shouldBeValid('2003-W52');  // 2003 started on Wednesday, but wasn't a leap year.
shouldBeValid('1992-W53');  // 1992 started on Wednesday, and was a leap year.

// Invalid values
shouldBeInvalid(' 2009-W01 ');
shouldBeInvalid('2009W01');
shouldBeInvalid('2009-w01');
shouldBeInvalid('2009-W1');
shouldBeInvalid('2009-W001');
shouldBeInvalid('a');
shouldBeInvalid('-1-W09');
shouldBeInvalid('0000-W52');
shouldBeInvalid('2147483648-W01');
shouldBeInvalid('275760-W38');
shouldBeInvalid('2009-W00');
shouldBeInvalid('2009-W-1');
shouldBeInvalid('2004-W54');  // 2004 started on Thursday.
shouldBeInvalid('2003-W53');  // 2003 started on Wednesday, but wasn't a leap year.
shouldBeInvalid('1992-W54');  // 1992 started on Wednesday, and was a leap year.
shouldBeInvalid('2009/09');
shouldBeInvalid('200909');
shouldBeInvalid('2009-Wxx');
shouldBeInvalid('2009');

// Disabled
shouldBeInvalid('invalid', true);
</script>
<script src="../../../resources/js-test-post.js"></script>
</body>
</html>
