Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom Rules #47

Closed
taylor1791 opened this issue Dec 16, 2014 · 3 comments
Closed

Custom Rules #47

taylor1791 opened this issue Dec 16, 2014 · 3 comments

Comments

@taylor1791
Copy link

Is there any plan to support "custom rules" or allowing users to create rules in addition to the built in ones? For example, adding a rule that detects inline styles and warns on inline styles.

@yaniswang
Copy link
Contributor

You can use it like this:

HTMLHint.addRule({
    id: 'alt-require',
    description: 'Alt of img must be present and alt of area[href] and input[type=image] must be set value.',
    init: function(parser, reporter){
        var self = this;
        parser.addListener('tagstart', function(event){
            var tagName = event.tagName.toLowerCase(),
                mapAttrs = parser.getMapAttrs(event.attrs),
                col = event.col + tagName.length + 1,
                selector;
            if(tagName === 'img' && !('alt' in mapAttrs)){
                reporter.warn('Alt of img tag must be present.', event.line, col, self, event.raw);
            }
            else if((tagName === 'area' && 'href' in mapAttrs) ||
                (tagName === 'input' && mapAttrs['type'] === 'image')){
                if(!('alt' in mapAttrs) || mapAttrs['alt'] === ''){
                    selector = tagName === 'area' ? 'area[href]' : 'input[type=image]';
                    reporter.warn('Alt of ' + selector + ' must be set value.', event.line, col, self, event.raw);
                }
            }
        });
    }
});

@ortonomy
Copy link

Hey, where do I use this HTML.addrule function? - in my Gruntfile?

@spinnytea
Copy link

@ortonomy there's a blurb about it on the gulp-htmlhint page
https://www.npmjs.com/package/gulp-htmlhint#customrules

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants