entity-prefix-length.js 543 B

1234567891011121314151617181920212223
  1. 'use strict';
  2. var decode = require('parse-entities');
  3. module.exports = length;
  4. /* Returns the length of HTML entity that is a prefix of
  5. * the given string (excluding the ampersand), 0 if it
  6. * does not start with an entity. */
  7. function length(value) {
  8. var prefix;
  9. /* istanbul ignore if - Currently also tested for at
  10. * implemention, but we keep it here because that’s
  11. * proper. */
  12. if (value.charAt(0) !== '&') {
  13. return 0;
  14. }
  15. prefix = value.split('&', 2).join('&');
  16. return prefix.length - decode(prefix).length;
  17. }