css-helpers.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. * multiple ocurrences of:
  3. * - alphabetical letter, underscore or dash, or
  4. * - non-ascii character, or
  5. * - escaped character
  6. * @type {RegExp}
  7. */
  8. var rIdentifier =
  9. /(?:[A-Za-z0-9_-]|[^\0-\237]|\\(?:[^A-Fa-f0-9]|[A-Fa-f0-9]{1,6} ?))+/;
  10. /**
  11. * backslash followed by a non-hexadecimal letter or
  12. * a 1 to 6 digit hexadecimal number followed by an optional white space
  13. * @type {RegExp}
  14. */
  15. var rEscapedCharacter = /\\([^A-Fa-f0-9]|[A-Fa-f0-9]{1,6} ?)/g;
  16. /**
  17. * Unescapes a single character
  18. * @param {string} escapedCharacter escaped character starting with a backslash
  19. * @return {string} unescaped character
  20. */
  21. function unescapeCharacter(escapedCharacter) {
  22. var escapeValue = escapedCharacter.substr(1);
  23. var numberValue = parseInt(escapeValue, 16);
  24. if (isNaN(numberValue)) {
  25. return escapeValue;
  26. }
  27. return String.fromCharCode(numberValue);
  28. }
  29. /**
  30. * Unescapes all escaped characters in the given identifier
  31. * @param {string} identifier identifier with possible escaped characters
  32. * @return {string} unescaped identifier
  33. */
  34. function unescapeIdentifier(identifier) {
  35. return identifier.replace(rEscapedCharacter, unescapeCharacter);
  36. }
  37. module.exports = {
  38. rIdentifier: rIdentifier,
  39. rEscapedCharacter: rEscapedCharacter,
  40. unescapeIdentifier: unescapeIdentifier,
  41. unescapeCharacter: unescapeCharacter
  42. };