Url.js 1.2 KB

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