valid-lang-evaluate.js 992 B

123456789101112131415161718192021222324252627282930313233343536
  1. import { isValidLang, getBaseLang } from '../../core/utils';
  2. import { sanitize } from '../../commons/text';
  3. function validLangEvaluate(node, options, virtualNode) {
  4. const invalid = [];
  5. options.attributes.forEach(langAttr => {
  6. const langVal = virtualNode.attr(langAttr);
  7. if (typeof langVal !== 'string') {
  8. return;
  9. }
  10. const baselangVal = getBaseLang(langVal);
  11. const invalidLang = options.value
  12. ? !options.value.map(getBaseLang).includes(baselangVal)
  13. : !isValidLang(baselangVal);
  14. // Edge sets lang to an empty string when xml:lang is set
  15. // so we need to ignore empty strings here
  16. if (
  17. (baselangVal !== '' && invalidLang) ||
  18. // whitespace only lang value is invalid
  19. (langVal !== '' && !sanitize(langVal))
  20. ) {
  21. invalid.push(langAttr + '="' + virtualNode.attr(langAttr) + '"');
  22. }
  23. });
  24. if (invalid.length) {
  25. this.data(invalid);
  26. return true;
  27. }
  28. return false;
  29. }
  30. export default validLangEvaluate;