Url.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. var isWhiteSpace = require('../../tokenizer').isWhiteSpace;
  2. var cmpStr = require('../../tokenizer').cmpStr;
  3. var TYPE = require('../../tokenizer').TYPE;
  4. var FUNCTION = TYPE.Function;
  5. var URL = TYPE.Url;
  6. var RIGHTPARENTHESIS = TYPE.RightParenthesis;
  7. // <url-token> | <function-token> <string> )
  8. module.exports = {
  9. name: 'Url',
  10. structure: {
  11. value: ['String', 'Raw']
  12. },
  13. parse: function() {
  14. var start = this.scanner.tokenStart;
  15. var value;
  16. switch (this.scanner.tokenType) {
  17. case URL:
  18. var rawStart = start + 4;
  19. var rawEnd = this.scanner.tokenEnd - 1;
  20. while (rawStart < rawEnd && isWhiteSpace(this.scanner.source.charCodeAt(rawStart))) {
  21. rawStart++;
  22. }
  23. while (rawStart < rawEnd && isWhiteSpace(this.scanner.source.charCodeAt(rawEnd - 1))) {
  24. rawEnd--;
  25. }
  26. value = {
  27. type: 'Raw',
  28. loc: this.getLocation(rawStart, rawEnd),
  29. value: this.scanner.source.substring(rawStart, rawEnd)
  30. };
  31. this.eat(URL);
  32. break;
  33. case FUNCTION:
  34. if (!cmpStr(this.scanner.source, this.scanner.tokenStart, this.scanner.tokenEnd, 'url(')) {
  35. this.error('Function name must be `url`');
  36. }
  37. this.eat(FUNCTION);
  38. this.scanner.skipSC();
  39. value = this.String();
  40. this.scanner.skipSC();
  41. this.eat(RIGHTPARENTHESIS);
  42. break;
  43. default:
  44. this.error('Url or Function is expected');
  45. }
  46. return {
  47. type: 'Url',
  48. loc: this.getLocation(start, this.scanner.tokenStart),
  49. value: value
  50. };
  51. },
  52. generate: function(node) {
  53. this.chunk('url');
  54. this.chunk('(');
  55. this.node(node.value);
  56. this.chunk(')');
  57. }
  58. };