|
| 1 | + |
| 2 | +var tagsTypings = { |
| 3 | + a: { |
| 4 | + selfclosing: false, |
| 5 | + attrsRequired: ['href', 'title'], |
| 6 | + redundantAttrs: ['alt'] |
| 7 | + }, |
| 8 | + div: { |
| 9 | + selfclosing: false |
| 10 | + }, |
| 11 | + main: { |
| 12 | + selfclosing: false, |
| 13 | + redundantAttrs: ['role'] |
| 14 | + }, |
| 15 | + nav: { |
| 16 | + selfclosing: false, |
| 17 | + redundantAttrs: ['role'] |
| 18 | + }, |
| 19 | + script: { |
| 20 | + attrsOptional: [['async', 'async'], ['defer', 'defer']] |
| 21 | + }, |
| 22 | + img: { |
| 23 | + selfclosing: true, |
| 24 | + attrsRequired: [ |
| 25 | + 'src', 'alt', 'title' |
| 26 | + ] |
| 27 | + } |
| 28 | +}; |
| 29 | + |
| 30 | +var assign = function(target) { |
| 31 | + var _source; |
| 32 | + |
| 33 | + for (var i = 1; i < arguments.length; i++) { |
| 34 | + _source = arguments[i]; |
| 35 | + for (var prop in _source) { |
| 36 | + target[prop] = _source[prop]; |
| 37 | + } |
| 38 | + } |
| 39 | + return target; |
| 40 | +} |
| 41 | + |
| 42 | +export default { |
| 43 | + id: 'tags-check', |
| 44 | + description: 'Checks html tags.', |
| 45 | + init: function (parser, reporter, options) { |
| 46 | + var self = this; |
| 47 | + |
| 48 | + if (typeof options !== 'boolean') { |
| 49 | + assign(tagsTypings, options); |
| 50 | + } |
| 51 | + |
| 52 | + parser.addListener('tagstart', function (event) { |
| 53 | + var attrs = event.attrs; |
| 54 | + var col = event.col + event.tagName.length + 1; |
| 55 | + |
| 56 | + var tagName = event.tagName.toLowerCase(); |
| 57 | + |
| 58 | + if (tagsTypings[tagName]) { |
| 59 | + var currentTagType = tagsTypings[tagName]; |
| 60 | + |
| 61 | + if (currentTagType.selfclosing === true && !event.close) { |
| 62 | + reporter.warn('The <' + tagName + '> tag must be selfclosing.', event.line, event.col, self, event.raw); |
| 63 | + } else if (currentTagType.selfclosing === false && event.close) { |
| 64 | + reporter.warn('The <' + tagName +'> tag must not be selfclosing.', event.line, event.col, self, event.raw); |
| 65 | + } |
| 66 | + |
| 67 | + if (currentTagType.attrsRequired) { |
| 68 | + currentTagType.attrsRequired.forEach(function (id) { |
| 69 | + if (Array.isArray(id)) { |
| 70 | + var copyOfId = id.map(function (a) { return a;}); |
| 71 | + var realID = copyOfId.shift(); |
| 72 | + var values = copyOfId; |
| 73 | + |
| 74 | + if (attrs.some(function (attr) {return attr.name === realID;})) { |
| 75 | + attrs.forEach(function (attr) { |
| 76 | + if (attr.name === realID && values.indexOf(attr.value) === -1) { |
| 77 | + reporter.error('The <' + tagName +'> tag must have attr \'' + realID + '\' with one value of \'' + values.join('\' or \'') + '\'.', event.line, col, self, event.raw); |
| 78 | + } |
| 79 | + }); |
| 80 | + } else { |
| 81 | + reporter.error('The <' + tagName + '> tag must have attr \'' + realID + '\'.', event.line, col, self, event.raw); |
| 82 | + } |
| 83 | + } else if (!attrs.some(function (attr) {return id.split('|').indexOf(attr.name) !== -1;})) { |
| 84 | + reporter.error('The <' + tagName + '> tag must have attr \'' + id + '\'.', event.line, col, self, event.raw); |
| 85 | + } |
| 86 | + }); |
| 87 | + } |
| 88 | + if (currentTagType.attrsOptional) { |
| 89 | + currentTagType.attrsOptional.forEach(function (id) { |
| 90 | + if (Array.isArray(id)) { |
| 91 | + var copyOfId = id.map(function (a) { return a;}); |
| 92 | + var realID = copyOfId.shift(); |
| 93 | + var values = copyOfId; |
| 94 | + |
| 95 | + if (attrs.some(function (attr) {return attr.name === realID;})) { |
| 96 | + attrs.forEach(function (attr) { |
| 97 | + if (attr.name === realID && values.indexOf(attr.value) === -1) { |
| 98 | + reporter.error('The <' + tagName + '> tag must have optional attr \'' + realID + |
| 99 | + '\' with one value of \'' + values.join('\' or \'') + '\'.', event.line, col, self, event.raw); |
| 100 | + } |
| 101 | + }); |
| 102 | + } |
| 103 | + } |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + if (currentTagType.redundantAttrs) { |
| 108 | + currentTagType.redundantAttrs.forEach(function (attrName) { |
| 109 | + if (attrs.some(function (attr) { return attr.name === attrName;})) { |
| 110 | + reporter.error('The attr \'' + attrName + '\' is redundant for <' + tagName + '> and should be ommited.', event.line, col, self, event.raw); |
| 111 | + } |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + } |
| 116 | + }); |
| 117 | + } |
| 118 | +}; |
0 commit comments