rewrite-url.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. var path = require('path');
  2. var url = require('url');
  3. var DOUBLE_QUOTE = '"';
  4. var SINGLE_QUOTE = '\'';
  5. var URL_PREFIX = 'url(';
  6. var URL_SUFFIX = ')';
  7. var QUOTE_PREFIX_PATTERN = /^["']/;
  8. var QUOTE_SUFFIX_PATTERN = /["']$/;
  9. var ROUND_BRACKETS_PATTERN = /[\(\)]/;
  10. var URL_PREFIX_PATTERN = /^url\(/i;
  11. var URL_SUFFIX_PATTERN = /\)$/;
  12. var WHITESPACE_PATTERN = /\s/;
  13. var isWindows = process.platform == 'win32';
  14. function rebase(uri, rebaseConfig) {
  15. if (!rebaseConfig) {
  16. return uri;
  17. }
  18. if (isAbsolute(uri) && !isRemote(rebaseConfig.toBase)) {
  19. return uri;
  20. }
  21. if (isRemote(uri) || isSVGMarker(uri) || isInternal(uri)) {
  22. return uri;
  23. }
  24. if (isData(uri)) {
  25. return '\'' + uri + '\'';
  26. }
  27. if (isRemote(rebaseConfig.toBase)) {
  28. return url.resolve(rebaseConfig.toBase, uri);
  29. }
  30. return rebaseConfig.absolute ?
  31. normalize(absolute(uri, rebaseConfig)) :
  32. normalize(relative(uri, rebaseConfig));
  33. }
  34. function isAbsolute(uri) {
  35. return path.isAbsolute(uri);
  36. }
  37. function isSVGMarker(uri) {
  38. return uri[0] == '#';
  39. }
  40. function isInternal(uri) {
  41. return /^\w+:\w+/.test(uri);
  42. }
  43. function isRemote(uri) {
  44. return /^[^:]+?:\/\//.test(uri) || uri.indexOf('//') === 0;
  45. }
  46. function isData(uri) {
  47. return uri.indexOf('data:') === 0;
  48. }
  49. function absolute(uri, rebaseConfig) {
  50. return path
  51. .resolve(path.join(rebaseConfig.fromBase || '', uri))
  52. .replace(rebaseConfig.toBase, '');
  53. }
  54. function relative(uri, rebaseConfig) {
  55. return path.relative(rebaseConfig.toBase, path.join(rebaseConfig.fromBase || '', uri));
  56. }
  57. function normalize(uri) {
  58. return isWindows ? uri.replace(/\\/g, '/') : uri;
  59. }
  60. function quoteFor(unquotedUrl) {
  61. if (unquotedUrl.indexOf(SINGLE_QUOTE) > -1) {
  62. return DOUBLE_QUOTE;
  63. } else if (unquotedUrl.indexOf(DOUBLE_QUOTE) > -1) {
  64. return SINGLE_QUOTE;
  65. } else if (hasWhitespace(unquotedUrl) || hasRoundBrackets(unquotedUrl)) {
  66. return SINGLE_QUOTE;
  67. } else {
  68. return '';
  69. }
  70. }
  71. function hasWhitespace(url) {
  72. return WHITESPACE_PATTERN.test(url);
  73. }
  74. function hasRoundBrackets(url) {
  75. return ROUND_BRACKETS_PATTERN.test(url);
  76. }
  77. function rewriteUrl(originalUrl, rebaseConfig, pathOnly) {
  78. var strippedUrl = originalUrl
  79. .replace(URL_PREFIX_PATTERN, '')
  80. .replace(URL_SUFFIX_PATTERN, '')
  81. .trim();
  82. var unquotedUrl = strippedUrl
  83. .replace(QUOTE_PREFIX_PATTERN, '')
  84. .replace(QUOTE_SUFFIX_PATTERN, '')
  85. .trim();
  86. var quote = strippedUrl[0] == SINGLE_QUOTE || strippedUrl[0] == DOUBLE_QUOTE ?
  87. strippedUrl[0] :
  88. quoteFor(unquotedUrl);
  89. return pathOnly ?
  90. rebase(unquotedUrl, rebaseConfig) :
  91. URL_PREFIX + quote + rebase(unquotedUrl, rebaseConfig) + quote + URL_SUFFIX;
  92. }
  93. module.exports = rewriteUrl;