Skip to content

Commit

Permalink
feat(attr-lowercase): ignore camelCase SVG attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
nschonni committed Dec 14, 2021
1 parent 8900d14 commit 0147b76
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 7 deletions.
77 changes: 75 additions & 2 deletions dist/core/rules/attr-lowercase.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 74 additions & 1 deletion dist/htmlhint.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,79 @@
var attrLowercase = {};

Object.defineProperty(attrLowercase, "__esModule", { value: true });
const svgIgnores = [
'allowReorder',
'attributeName',
'attributeType',
'autoReverse',
'baseFrequency',
'baseProfile',
'calcMode',
'clipPath',
'clipPathUnits',
'contentScriptType',
'contentStyleType',
'diffuseConstant',
'edgeMode',
'externalResourcesRequired',
'filterRes',
'filterUnits',
'glyphRef',
'gradientTransform',
'gradientUnits',
'kernelMatrix',
'kernelUnitLength',
'keyPoints',
'keySplines',
'keyTimes',
'lengthAdjust',
'limitingConeAngle',
'markerHeight',
'markerUnits',
'markerWidth',
'maskContentUnits',
'maskUnits',
'numOctaves',
'onBlur',
'onChange',
'onClick',
'onFocus',
'onKeyUp',
'onLoad',
'pathLength',
'patternContentUnits',
'patternTransform',
'patternUnits',
'pointsAtX',
'pointsAtY',
'pointsAtZ',
'preserveAlpha',
'preserveAspectRatio',
'primitiveUnits',
'refX',
'refY',
'repeatCount',
'repeatDur',
'requiredExtensions',
'requiredFeatures',
'specularConstant',
'specularExponent',
'spreadMethod',
'startOffset',
'stdDeviation',
'stitchTiles',
'surfaceScale',
'systemLanguage',
'tableValues',
'targetX',
'targetY',
'textLength',
'viewBox',
'viewTarget',
'xChannelSelector',
'yChannelSelector',
'zoomAndPan',
];
function testAgainstStringOrRegExp(value, comparison) {
if (comparison instanceof RegExp) {
return comparison.test(value)
Expand All @@ -361,7 +434,7 @@
id: 'attr-lowercase',
description: 'All attribute names must be in lowercase.',
init(parser, reporter, options) {
const exceptions = Array.isArray(options) ? options : [];
const exceptions = (Array.isArray(options) ? options : []).concat(svgIgnores);
parser.addListener('tagstart', (event) => {
const attrs = event.attrs;
let attr;
Expand Down
2 changes: 1 addition & 1 deletion dist/htmlhint.min.js

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions docs/user-guide/rules/attr-lowercase.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ Level: `error`

1. true: enable rule
2. false: disable rule
3. ['viewBox', 'Test']: enable rule except for the given attribute names
3. ['fooBar', 'Test']: enable rule except for the given attribute names. All SVG camelCase properties are included, for example `viewBox`

### Example

```json
{
...
"attr-lowercase": ['viewBox']
"attr-lowercase": ['fooBar']
...
}
```
Expand All @@ -28,6 +28,9 @@ The following pattern is **not** considered a rule violation:
<!-- prettier-ignore -->
```html
<img src="test.png" alt="test" />

<!-- known SVG attributes are ignored -->
<svg width="200" height="200" viewBox="0 0 200 200" />
```

The following pattern is considered a rule violation:
Expand Down
78 changes: 77 additions & 1 deletion src/core/rules/attr-lowercase.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,79 @@
import { Rule } from '../types'

const svgIgnores = [
'allowReorder',
'attributeName',
'attributeType',
'autoReverse',
'baseFrequency',
'baseProfile',
'calcMode',
'clipPath',
'clipPathUnits',
'contentScriptType',
'contentStyleType',
'diffuseConstant',
'edgeMode',
'externalResourcesRequired',
'filterRes',
'filterUnits',
'glyphRef',
'gradientTransform',
'gradientUnits',
'kernelMatrix',
'kernelUnitLength',
'keyPoints',
'keySplines',
'keyTimes',
'lengthAdjust',
'limitingConeAngle',
'markerHeight',
'markerUnits',
'markerWidth',
'maskContentUnits',
'maskUnits',
'numOctaves',
'onBlur',
'onChange',
'onClick',
'onFocus',
'onKeyUp',
'onLoad',
'pathLength',
'patternContentUnits',
'patternTransform',
'patternUnits',
'pointsAtX',
'pointsAtY',
'pointsAtZ',
'preserveAlpha',
'preserveAspectRatio',
'primitiveUnits',
'refX',
'refY',
'repeatCount',
'repeatDur',
'requiredExtensions',
'requiredFeatures',
'specularConstant',
'specularExponent',
'spreadMethod',
'startOffset',
'stdDeviation',
'stitchTiles',
'surfaceScale',
'systemLanguage',
'tableValues',
'targetX',
'targetY',
'textLength',
'viewBox',
'viewTarget',
'xChannelSelector',
'yChannelSelector',
'zoomAndPan',
]

/**
* testAgainstStringOrRegExp
*
Expand Down Expand Up @@ -43,7 +117,9 @@ export default {
id: 'attr-lowercase',
description: 'All attribute names must be in lowercase.',
init(parser, reporter, options) {
const exceptions = Array.isArray(options) ? options : []
const exceptions = (Array.isArray(options) ? options : []).concat(
svgIgnores
)

parser.addListener('tagstart', (event) => {
const attrs = event.attrs
Expand Down
20 changes: 20 additions & 0 deletions test/rules/attr-lowercase.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,24 @@ describe(`Rules: ${ruleId}`, () => {
const messages = HTMLHint.verify(code, ruleOptions)
expect(messages.length).to.be(0)
})

it('Known SVG properties should be ignored with no config', () => {
const code = '<svg width="200" height="200" viewBox="0 0 200 200" />'
const messages = HTMLHint.verify(code, ruleOptions)
expect(messages.length).to.be(0)
})

it('Known SVG properties should be ignored with a config override', () => {
const code = '<svg width="200" height="200" viewBox="0 0 200 200" />'
ruleOptions[ruleId] = ['testBox']
const messages = HTMLHint.verify(code, ruleOptions)
expect(messages.length).to.be(0)
})

it('Double ignored SVG properties should not cause issues', () => {
const code = '<svg width="200" height="200" viewBox="0 0 200 200" />'
ruleOptions[ruleId] = ['viewBox']
const messages = HTMLHint.verify(code, ruleOptions)
expect(messages.length).to.be(0)
})
})

0 comments on commit 0147b76

Please sign in to comment.