1
0

urls.js 992 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. module.exports = function Urls() {
  2. var urls = [];
  3. return {
  4. // Strip urls by replacing them by the __URL__
  5. // marker for further restoring. It's done via string scanning
  6. // instead of regexps to speed up the process.
  7. escape: function(data) {
  8. var nextStart = 0;
  9. var nextEnd = 0;
  10. var cursor = 0;
  11. var tempData = [];
  12. for (; nextEnd < data.length; ) {
  13. nextStart = data.indexOf('url(', nextEnd);
  14. if (nextStart == -1)
  15. break;
  16. nextEnd = data.indexOf(')', nextStart);
  17. tempData.push(data.substring(cursor, nextStart));
  18. tempData.push('__URL__');
  19. urls.push(data.substring(nextStart, nextEnd + 1));
  20. cursor = nextEnd + 1;
  21. }
  22. return tempData.length > 0 ?
  23. tempData.join('') + data.substring(cursor, data.length) :
  24. data;
  25. },
  26. restore: function(data) {
  27. return data.replace(/__URL__/g, function() {
  28. return urls.shift();
  29. });
  30. }
  31. };
  32. };