You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

33 lines
981 B

4 years ago
  1. // Can unquote attribute detection
  2. // Adopted implementation of Mathias Bynens
  3. // https://github.com/mathiasbynens/mothereff.in/blob/master/unquoted-attributes/eff.js
  4. var escapesRx = /\\([0-9A-Fa-f]{1,6})(\r\n|[ \t\n\f\r])?|\\./g;
  5. var blockUnquoteRx = /^(-?\d|--)|[\u0000-\u002c\u002e\u002f\u003A-\u0040\u005B-\u005E\u0060\u007B-\u009f]/;
  6. function canUnquote(value) {
  7. if (value === '' || value === '-') {
  8. return;
  9. }
  10. // Escapes are valid, so replace them with a valid non-empty string
  11. value = value.replace(escapesRx, 'a');
  12. return !blockUnquoteRx.test(value);
  13. }
  14. module.exports = function(node) {
  15. var attrValue = node.value;
  16. if (!attrValue || attrValue.type !== 'String') {
  17. return;
  18. }
  19. var unquotedValue = attrValue.value.replace(/^(.)(.*)\1$/, '$2');
  20. if (canUnquote(unquotedValue)) {
  21. node.value = {
  22. type: 'Identifier',
  23. loc: attrValue.loc,
  24. name: unquotedValue
  25. };
  26. }
  27. };