Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate angular HTML templates

I try to write mostly valid polyglot (X)HTML 5 in my angular HTML templates. They look something like this:

<div class="some-class">
  <input type="checkbox" data-ng-model="variable" />
  <foo-directive data-ng-if="variable"></foo-directive>
</div>

Sometimes I forget closing a tag properly which breaks some browsers. So I would like to include a validator in my toolchain.

The problem is: I do not know of a validator that can handle this case. XML validators typically require a DTD, HTML validators will complain about the angular directives that are used in the code.

Maybe validator is the wrong word and I really want a linter. The only real thing I want it to do is to check that every opening tag has a matching closing tag. Everything else is a bonus.

Do you know of such a validator?

NOTE: I primarily do search for a command line tool that I can integrate with my automated testing. But a webservice might be helpful, too.

like image 457
tobib Avatar asked Jan 26 '15 12:01

tobib


People also ask

How do I validate a template driven form?

To add validation to a template-driven form, you add the same validation attributes as you would with native HTML form validation. Angular uses directives to match these attributes with validator functions in the framework.

How do you check the validity of HTML?

The HTMLSelectElement. checkValidity() method checks whether the element has any constraints and whether it satisfies them. If the element fails its constraints, the browser fires a cancelable invalid event at the element, and then returns false .

Which of the following HTML element is valid in Angular template?

Almost all HTML syntax is valid template syntax. However, because an Angular template is part of an overall webpage, and not the entire page, you don't need to include elements such as <html> , <body> , or <base> , and can focus exclusively on the part of the page you are developing.


1 Answers

Since htmlhint is an option, you can use it as described here from command line (of course you will have to npm install it first and make sure your PATH contains node_modules/.bin):

htmlhint test.html

To test for specific options:

htmlhint -r tag-pair,attr-no-duplication test.html

Or if the options are in a configuration file:

htmlhint -c config-file test.html

I use the following options with Angular:

  • tag-pair: make sure tags are closed properly
  • attr-no-duplication: no duplicate attributes in an element
like image 84
Nikos Paraskevopoulos Avatar answered Oct 25 '22 10:10

Nikos Paraskevopoulos