No Description

urls.js 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. var EscapeStore = require('./escape-store');
  2. module.exports = function Urls(context) {
  3. var urls = new EscapeStore('URL');
  4. return {
  5. // Strip urls by replacing them by a special
  6. // marker for further restoring. It's done via string scanning
  7. // instead of regexps to speed up the process.
  8. escape: function(data) {
  9. var nextStart = 0;
  10. var nextEnd = 0;
  11. var cursor = 0;
  12. var tempData = [];
  13. for (; nextEnd < data.length;) {
  14. nextStart = data.indexOf('url(', nextEnd);
  15. if (nextStart == -1)
  16. break;
  17. nextEnd = data.indexOf(')', nextStart);
  18. // Following lines are a safety mechanism to ensure
  19. // incorrectly terminated urls are processed correctly.
  20. if (nextEnd == -1) {
  21. nextEnd = data.indexOf('}', nextStart);
  22. if (nextEnd == -1)
  23. nextEnd = data.length;
  24. else
  25. nextEnd--;
  26. context.warnings.push('Broken URL declaration: \'' + data.substring(nextStart, nextEnd + 1) + '\'.');
  27. }
  28. var url = data.substring(nextStart, nextEnd + 1);
  29. var placeholder = urls.store(url);
  30. tempData.push(data.substring(cursor, nextStart));
  31. tempData.push(placeholder);
  32. cursor = nextEnd + 1;
  33. }
  34. return tempData.length > 0 ?
  35. tempData.join('') + data.substring(cursor, data.length) :
  36. data;
  37. },
  38. restore: function(data) {
  39. return data.replace(urls.placeholderRegExp, function(placeholder) {
  40. return urls.restore(placeholder).replace(/\s/g, '');
  41. });
  42. }
  43. };
  44. };