/custom-element-name/vendor/validate.js

http://github.com/mathiasbynens/mothereffingcssescapes · JavaScript · 96 lines · 74 code · 20 blank · 2 comment · 18 complexity · 3e2599d3b78ddc7b46bbfe0381b64913 MD5 · raw file

  1. 'use strict';
  2. var isPotentialCustomElementName = require('is-potential-custom-element-name');
  3. // https://html.spec.whatwg.org/multipage/scripting.html#valid-custom-element-name
  4. var reservedNames = [
  5. 'annotation-xml',
  6. 'color-profile',
  7. 'font-face',
  8. 'font-face-src',
  9. 'font-face-uri',
  10. 'font-face-format',
  11. 'font-face-name',
  12. 'missing-glyph'
  13. ];
  14. function hasError(name) {
  15. if (!name) {
  16. return 'Missing element name.';
  17. }
  18. if (/[A-Z]/.test(name)) {
  19. return 'Custom element names must not contain uppercase ASCII characters.';
  20. }
  21. if (name.indexOf('-') === -1) {
  22. return 'Custom element names must contain a hyphen. Example: unicorn-cake';
  23. }
  24. if (/^\d/i.test(name)) {
  25. return 'Custom element names must not start with a digit.';
  26. }
  27. if (/^-/i.test(name)) {
  28. return 'Custom element names must not start with a hyphen.';
  29. }
  30. // https://html.spec.whatwg.org/multipage/scripting.html#prod-potentialcustomelementname
  31. if (!isPotentialCustomElementName(name)) {
  32. return 'Invalid element name.';
  33. }
  34. if (reservedNames.indexOf(name) !== -1) {
  35. return 'The supplied element name is reserved and can\'t be used.\nSee: https://html.spec.whatwg.org/multipage/scripting.html#valid-custom-element-name';
  36. }
  37. }
  38. function hasWarning(name) {
  39. if (/^polymer-/i.test(name)) {
  40. return 'Custom element names should not start with `polymer-`.\nSee: http://webcomponents.github.io/articles/how-should-i-name-my-element';
  41. }
  42. if (/^x-/i.test(name)) {
  43. return 'Custom element names should not start with `x-`.\nSee: http://webcomponents.github.io/articles/how-should-i-name-my-element/';
  44. }
  45. if (/^ng-/i.test(name)) {
  46. return 'Custom element names should not start with `ng-`.\nSee: http://docs.angularjs.org/guide/directive#creating-directives';
  47. }
  48. if (/^xml/i.test(name)) {
  49. return 'Custom element names should not start with `xml`.';
  50. }
  51. if (/^[^a-z]/i.test(name)) {
  52. return 'This element name is only valid in XHTML, not in HTML. First character should be in the range a-z.';
  53. }
  54. if (/[^a-z0-9]$/i.test(name)) {
  55. return 'Custom element names should not end with a non-alpha character.';
  56. }
  57. if (/[\.]/.test(name)) {
  58. return 'Custom element names should not contain a dot character as it would need to be escaped in a CSS selector.';
  59. }
  60. if (/[^\x20-\x7E]/.test(name)) {
  61. return 'Custom element names should not contain non-ASCII characters.';
  62. }
  63. if (/--/.test(name)) {
  64. return 'Custom element names should not contain consecutive hyphens.';
  65. }
  66. if (/[^a-z0-9]{2}/i.test(name)) {
  67. return 'Custom element names should not contain consecutive non-alpha characters.';
  68. }
  69. }
  70. module.exports = function (name) {
  71. var errMsg = hasError(name);
  72. return {
  73. isValid: !errMsg,
  74. message: errMsg || hasWarning(name)
  75. };
  76. };