var Issue = require('../issue');

module.exports = {
    name: 'input-req-label',
    on: ['tag'],
    filter: ['input', 'label'],
    desc: 'If set, a label is required for text and radio input elements.',

    labels: {},
    inputsInfo: []
};

module.exports.end = function () {
    var issues = [];
    this.inputsInfo.forEach(function (input) {
        if (!this.labels[input.id] && !this.labels[input.name]) {
            issues.push(new Issue('E033', input.location, {
                'idValue': input.id,
                'nameValue': input.name
            }));
        }
    }.bind(this));

    // wipe previous table
    this.labels = {};
    this.inputsInfo = [];

    return issues;
};

module.exports.lint = function (element, opts) {
    var attrs = element.attribs;
    function getAttrVal(name) {
        return attrs.hasOwnProperty(name) && attrs[name]
            ? attrs[name].value
            : null;
    }

    // if it's a label with a 'for', store that value
    if (element.name === 'label') {
        var f = getAttrVal('for');
        if (f) { this.labels[f] = element; }
        return [];
    }

    // if it's not a text-type input, ignore it
    var type = getAttrVal('type');
    if (type !== 'text' && type !== 'radio') {
        return [];
    }

    // check if the input has a label as a parent.
    for (var e = element; e = e.parent; ) {
        if (e.name === 'label') {
            return [];
        }
    }

    // check if the input has a named label, by storing the values to
    // check at the end.
    var id = getAttrVal('id');
    var name = type === 'text' ? getAttrVal('name') : null;
    if (id || name) {
        this.inputsInfo.push({
            'id': id,
            'name': name,
            'location': element.openLineCol
        });
    } else {
        return new Issue('E033', element.openLineCol, {
            'idValue': 'null',
            'nameValue': 'null'
        });
    }

    return [];
};

@@ -134,16 +134,18 @@
   desc: 
+%5B%0A
 'If set,
@@ -149,63 +149,292 @@
 et, 
-a label is required for text and radio input elements.'
+each text or radio input element must have an associated %60label%60',%0A'element. The label may be a parent of the input element, or may identify',%0A'the element it labels using its %60for%60 attribute, which must match the',%0A'input%5C's %60id%60 (or %60name%60, for text inputs) attribute.'%0A%5D.join('%5Cn')
 ,%0A%0A 

Use more complete description for input-req-label