Url.cjs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. 'use strict';
  2. const url = require('../../utils/url.cjs');
  3. const string = require('../../utils/string.cjs');
  4. const types = require('../../tokenizer/types.cjs');
  5. const name = 'Url';
  6. const structure = {
  7. value: String
  8. };
  9. // <url-token> | <function-token> <string> )
  10. function parse() {
  11. const start = this.tokenStart;
  12. let value;
  13. switch (this.tokenType) {
  14. case types.Url:
  15. value = url.decode(this.consume(types.Url));
  16. break;
  17. case types.Function:
  18. if (!this.cmpStr(this.tokenStart, this.tokenEnd, 'url(')) {
  19. this.error('Function name must be `url`');
  20. }
  21. this.eat(types.Function);
  22. this.skipSC();
  23. value = string.decode(this.consume(types.String));
  24. this.skipSC();
  25. if (!this.eof) {
  26. this.eat(types.RightParenthesis);
  27. }
  28. break;
  29. default:
  30. this.error('Url or Function is expected');
  31. }
  32. return {
  33. type: 'Url',
  34. loc: this.getLocation(start, this.tokenStart),
  35. value
  36. };
  37. }
  38. function generate(node) {
  39. this.token(types.Url, url.encode(node.value));
  40. }
  41. exports.generate = generate;
  42. exports.name = name;
  43. exports.parse = parse;
  44. exports.structure = structure;