|
| 1 | +const rule = require('../../../lib/rules/template-valid-input-attributes'); |
| 2 | +const RuleTester = require('eslint').RuleTester; |
| 3 | + |
| 4 | +const err = (attr, type) => `Attribute \`${attr}\` is not allowed on \`<input type="${type}">\``; |
| 5 | + |
| 6 | +const validHbs = [ |
| 7 | + // Attribute matches type. |
| 8 | + '<input type="text" pattern="\\d+" />', |
| 9 | + '<input type="file" accept="image/*" />', |
| 10 | + '<input type="number" min="0" max="100" step="1" />', |
| 11 | + '<input type="image" alt="submit" src="/x.png" />', |
| 12 | + '<input type="email" placeholder="[email protected]" />', |
| 13 | + '<input type="checkbox" checked required />', |
| 14 | + '<input type="radio" checked required name="r" />', |
| 15 | + '<input type="text" size="20" maxlength="100" readonly />', |
| 16 | + '<input type="email" multiple />', |
| 17 | + '<input type="file" multiple capture="user" />', |
| 18 | + // Dynamic type — skip. |
| 19 | + '<input type={{this.t}} pattern="\\d+" />', |
| 20 | + // Mustache string literal — statically resolvable, attribute is compatible. |
| 21 | + '<input type={{"text"}} pattern="\\d+" />', |
| 22 | + // Missing / valueless / empty / unknown type fall back to the Text state |
| 23 | + // per HTML §4.10.5. Attributes valid for text still pass. |
| 24 | + '<input pattern="\\d+" />', |
| 25 | + '<input type />', |
| 26 | + '<input maxlength="100" size="20" readonly />', |
| 27 | + // Not an input — rule doesn't apply. |
| 28 | + '<textarea maxlength="10"></textarea>', |
| 29 | + // Empty/whitespace/unknown type values fall back to the Text state per HTML |
| 30 | + // spec, so `pattern` (allowed on text) is valid. |
| 31 | + '<input type="" pattern="x" />', |
| 32 | + '<input type=" TEXT " pattern="x" />', |
| 33 | + '<input type="UNKNOWN" pattern="x" />', |
| 34 | +]; |
| 35 | + |
| 36 | +const invalidHbs = [ |
| 37 | + { |
| 38 | + code: '<input type="number" pattern="\\d+" />', |
| 39 | + errors: [{ message: err('pattern', 'number') }], |
| 40 | + }, |
| 41 | + // Mustache string literal — statically resolvable, attribute is incompatible. |
| 42 | + { |
| 43 | + code: '<input type={{"number"}} pattern="\\d+" />', |
| 44 | + errors: [{ message: err('pattern', 'number') }], |
| 45 | + }, |
| 46 | + { |
| 47 | + code: '<input type="text" accept="image/*" />', |
| 48 | + errors: [{ message: err('accept', 'text') }], |
| 49 | + }, |
| 50 | + { |
| 51 | + code: '<input type="radio" maxlength="10" />', |
| 52 | + errors: [{ message: err('maxlength', 'radio') }], |
| 53 | + }, |
| 54 | + { |
| 55 | + code: '<input type="checkbox" placeholder="x" />', |
| 56 | + errors: [{ message: err('placeholder', 'checkbox') }], |
| 57 | + }, |
| 58 | + { |
| 59 | + code: '<input type="submit" pattern="x" size="5" />', |
| 60 | + errors: [{ message: err('pattern', 'submit') }, { message: err('size', 'submit') }], |
| 61 | + }, |
| 62 | + { |
| 63 | + code: '<input type="TEXT" accept="image/*" />', |
| 64 | + errors: [{ message: err('accept', 'text') }], |
| 65 | + }, |
| 66 | + // Text-state fallback — <input> with missing/valueless/empty/unknown type |
| 67 | + // is the Text state per HTML spec. Attributes incompatible with text are |
| 68 | + // flagged as `type="text"` in the error message. |
| 69 | + { |
| 70 | + code: '<input name="x" multiple />', |
| 71 | + errors: [{ message: err('multiple', 'text') }], |
| 72 | + }, |
| 73 | + { |
| 74 | + code: '<input alt="foo" />', |
| 75 | + errors: [{ message: err('alt', 'text') }], |
| 76 | + }, |
| 77 | + { |
| 78 | + code: '<input type accept="image/*" />', |
| 79 | + errors: [{ message: err('accept', 'text') }], |
| 80 | + }, |
| 81 | + { |
| 82 | + code: '<input type="unknown" src="/x.png" />', |
| 83 | + errors: [{ message: err('src', 'text') }], |
| 84 | + }, |
| 85 | +]; |
| 86 | + |
| 87 | +const gjsValid = validHbs.map((code) => `<template>${code}</template>`); |
| 88 | +const gjsInvalid = invalidHbs.map(({ code, errors }) => ({ |
| 89 | + code: `<template>${code}</template>`, |
| 90 | + errors, |
| 91 | +})); |
| 92 | + |
| 93 | +const gjsRuleTester = new RuleTester({ |
| 94 | + parser: require.resolve('ember-eslint-parser'), |
| 95 | + parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, |
| 96 | +}); |
| 97 | + |
| 98 | +gjsRuleTester.run('template-valid-input-attributes', rule, { |
| 99 | + valid: gjsValid, |
| 100 | + invalid: gjsInvalid, |
| 101 | +}); |
| 102 | + |
| 103 | +const hbsRuleTester = new RuleTester({ |
| 104 | + parser: require.resolve('ember-eslint-parser/hbs'), |
| 105 | + parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, |
| 106 | +}); |
| 107 | + |
| 108 | +hbsRuleTester.run('template-valid-input-attributes', rule, { |
| 109 | + valid: validHbs, |
| 110 | + invalid: invalidHbs, |
| 111 | +}); |
0 commit comments