<!DOCTYPE html>
<html>
<head>
<title>HTML Templates: 'In body' insertion mode: when template end tag is met, implied end tags should be generated</title>
<meta name="author" title="Sergey G. Grekhov" href="mailto:sgrekhov@unipro.ru">
<meta name="author" title="Aleksei Yu. Semenov" href="a.semenov@unipro.ru">
<meta name="assert" content="'In body' insertion mode: when template end tag is met, implied end tags should be generated">
<link rel="help" href="http://www.w3.org/TR/2013/WD-html-templates-20130214/#in-body-addition">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/html/resources/common.js"></script>
</head>
<body>
<div id="log"></div>
<script type="text/javascript">


test(function () {
    var doc = newHTMLDocument();

    //No end </td></tr></table> tags. Should be added implicitly
    doc.body.innerHTML = '<template id="tpl">'
        + '<table id="tbl"><tr id="tr"><td id="td"></template>';

    var template = doc.querySelector('#tpl');

    assert_not_equals(template, null, 'Template element must be parsed');

    assert_equals(doc.querySelector('#tbl'), null, 'Table element should not be available');
    assert_equals(doc.querySelector('#tr'), null, 'TR element should not be available');
    assert_equals(doc.querySelector('#td'), null, 'TD element should not be available');

    assert_not_equals(template.content.querySelector('#tbl'), null,
            'Template should contain table element');
    assert_not_equals(template.content.querySelector('#tr'), null,
            'Template should contain TR element');
    assert_not_equals(template.content.querySelector('#td'), null,
            'Template should contain TD element');

}, 'Generating of implied end tags. Test table elements');



test(function () {
    var doc = newHTMLDocument();

    //No end </div> tag. Should be added implicitly
    doc.body.innerHTML = '<template id="tpl"><div id="dv">Div content</template>';

    var template = doc.querySelector('#tpl');

    assert_not_equals(template, null, 'Template element must be parsed');

    assert_equals(doc.querySelector('#dv'), null, 'DIV element should not be available');

    assert_not_equals(template.content.querySelector('#dv'), null,
            'Template should contain DIV element');

}, 'Generating of implied end tags. Test div element');


test(function () {
    var doc = newHTMLDocument();

    //No end </div> tag. Should be added implicitly after text content
    doc.body.innerHTML = '<template id="tpl">Template text<div id="dv">Div content</template>';

    var template = doc.querySelector('#tpl');

    assert_not_equals(template, null, 'Template element must be parsed');

    assert_equals(doc.querySelector('#dv'), null, 'DIV element should not be available');

    var div = template.content.querySelector('#dv');

    assert_not_equals( div, null, 'Template should contain DIV element');
    assert_equals(div.textContent, 'Div content', 'Wrong template content inner text');

}, 'Generating of implied end tags. Test some text and DIV element');


test(function () {
    var doc = newHTMLDocument();

    // Wrong end tag. Correct end tag must be added implicitly, wrong one ignored
    doc.body.innerHTML = '<template id="tpl"><div id="dv">Div content</span></template>';

    var template = doc.querySelector('#tpl');

    assert_not_equals(template, null, 'Template element must be parsed');

    assert_equals(template.content.childNodes.length, 1,
            'Wrong number of template\'s children');

    assert_equals(doc.querySelector('#dv'), null, 'DIV element should not be available');

    assert_not_equals(template.content.querySelector('#dv'), null,
            'Template should contain DIV element');
    assert_equals(template.content.querySelector('#dv').textContent,
            'Div content', 'Wrong template content inner text');

}, 'Generating of implied end tags. Test wrong end tag');


testInIFrame('/html/semantics/scripting-1/the-template-element/resources/template-contents-table-no-end-tag.html', function(context) {
    var doc = context.iframes[0].contentDocument;

    var template = doc.body.querySelector('template');

    assert_not_equals(template, null, 'Template element must be parsed');

    assert_not_equals(template.content.querySelector('table'), null,
            'Template should contain table element');
    assert_not_equals(template.content.querySelector('tr'), null,
            'Template should contain TR element');
    assert_not_equals(template.content.querySelector('td'), null,
            'Template should contain TD element');

}, 'Generating of implied end tags. Test table elements. Loading of HTML document from a file');


testInIFrame('/html/semantics/scripting-1/the-template-element/resources/template-contents-div-no-end-tag.html', function(context) {
    var doc = context.iframes[0].contentDocument;

    var template = doc.body.querySelector('template');

    assert_not_equals(template, null, 'Template element must be parsed');

    var div = template.content.querySelector('div');
    assert_not_equals(div, null, 'Template should contain div element');
    assert_equals(div.textContent, 'Hello, template\n    ', 'Invalid div contents');

}, 'Generating of implied end tags. Test div element. Loading of HTML document from a file');
</script>
</body>
</html>
