A grunt task to check l10n .po files for unexpected/malformed HTML.
Перейти к файлу
Vlad Filippov 1ad559464b
Merge pull request #13 from mozilla/dependabot/npm_and_yarn/yargs-parser-13.1.2
chore(deps): bump yargs-parser from 13.1.1 to 13.1.2
2020-09-13 15:16:15 -04:00
lib chore(l10n): Method renaming for clarity 2019-11-11 19:08:20 +00:00
tasks chore(deps): Update dependencies 2019-11-11 17:12:43 +00:00
test fix(lint): Fix self closing tags 2019-11-11 17:41:02 +00:00
.eslintrc chore(deps): Update dependencies 2019-11-11 17:12:43 +00:00
.gitignore initial checkin 2015-09-15 19:40:49 +01:00
.travis.yml chore(deps): Update dependencies 2019-11-11 17:12:43 +00:00
CHANGELOG.md chore(release): 0.0.3 2019-11-11 19:20:18 +00:00
CODE_OF_CONDUCT.md Add Mozilla Code of Conduct file 2019-03-29 14:51:17 -07:00
CONTRIBUTING.md feat(docs): to the README, CONTRIBUTING, and jshint. 2015-09-15 21:24:36 +01:00
Gruntfile.js chore(deps): Update dependencies 2019-11-11 17:12:43 +00:00
LICENSE initial checkin 2015-09-15 19:40:49 +01:00
README.md fix(docs): clarify the description of the checking logic 2015-09-16 12:09:49 +01:00
package-lock.json chore(deps): bump yargs-parser from 13.1.1 to 13.1.2 2020-09-11 22:28:21 +00:00
package.json chore(release): 0.0.3 2019-11-11 19:20:18 +00:00

README.md

grunt-l10n-lint

BuildStatus

grunt-l10n-lint is a grunt task to check l10n .po files for unexpected/malformed HTML.

Getting Started

This plugin requires Grunt >0.4.5

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-l10n-lint --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-l10n-lint');

The "l10n-lint" task

The source files used to extract allowed HTML are defined using the untranslated option.

The target files to be checked are in files.

  grunt.initConfig({
    'l10n-lint': {
      test: {
        options: {
          untranslated: ['test/fixtures/templates/**/*.pot']
        },
        files: [{
          src: 'files-to-check/**/*.po'
        }]
      }
    }
  });

The lint process

The grunt task uses one or more .po or .pot files as the source templates from which to extract allowed HTML. The source files are to create a list of elements, attributes, and attribute values.

The grunt task then parses one or more translated .po files for strings that contain HTML that is malformed, contains unexpected tags, attributes, or attribute values.

The translated strings are checked, one by one, for:

  1. Grossly malformed HTML
  • Unnamed tags (e.g. <>, </>)
  • Unclosed tags, mismatched < or > (e.g., <span, span>)
  • Unclosed elements (e.g., <span>This span is not closed)
  • Elements closed in the wrong order.
    • (e.g., <a><span>closed out of order</a></span>)
  1. Unexpected tags. Tag names that are not used in the source .pot files may not be present in the translated .po files. For instance:
  • If the source .pot files contain only a and span tags, the translated files:
    1. Can contain 0 or more a and span tags.
    2. All other tags cause an error.
  1. Unexpected attributes. Attributes that are not used on a type of element in the source .pot files may not be present on instances of that element type in the translated .po files. For instance:
  • If the source .pot files contain <a href="..." target="...">, <button id="..."> and <button class="...">, the translated files:
    1. Can have either, both or neither of the href and target attributes on each a element.
    2. Can have either, both or neither of the id and class attributes on each button element.
    3. All other attributes will cause an error.
  1. Unexpected attribute values. Attribute values that are not used on a type of element in the source .pot files may not be present on instances of that element type in the translated .po files. For instance:
  • If the source .pot files contain <a href="/signin" target="_blank"> and <button id="logout">, the translated files:
    1. Can only have the value /signin for href attributes.
    2. Can only have the value _blank for target attributes.
    3. Can only have the value logout for id attributes.
    4. All other attribute values cause an error.

The target translation checks are very coarse.

For example, if the source .pot file contains a single a element, any translated string can contain an a element.

If the source .pot file contains two a elements, one with id="first-anchor" and another with id="second-anchor", any translated string could contain an a element with either id.

All translated strings are assumed to be independent items, and are checked individually. Quotes that surround attribute values are not checked, as long as the tag correctly closes and the attribute value matches an expected value, the value is accepted.

Running the tests

npm test

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Get involved:

Please see the CONTRIBUTING.md file.

License:

This software is available under version 2.0 of the MPL:

https://www.mozilla.org/MPL/