free.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. module.exports = function Free() {
  2. var texts = [];
  3. return {
  4. // Strip content tags by replacing them by the __CSSFREETEXT__
  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 tempData = [];
  9. var nextStart = 0;
  10. var nextEnd = 0;
  11. var cursor = 0;
  12. var matchedParenthesis = null;
  13. var singleParenthesis = "'";
  14. var doubleParenthesis = '"';
  15. var dataLength = data.length;
  16. for (; nextEnd < data.length; ) {
  17. var nextStartSingle = data.indexOf(singleParenthesis, nextEnd + 1);
  18. var nextStartDouble = data.indexOf(doubleParenthesis, nextEnd + 1);
  19. if (nextStartSingle == -1)
  20. nextStartSingle = dataLength;
  21. if (nextStartDouble == -1)
  22. nextStartDouble = dataLength;
  23. if (nextStartSingle < nextStartDouble) {
  24. nextStart = nextStartSingle;
  25. matchedParenthesis = singleParenthesis;
  26. } else {
  27. nextStart = nextStartDouble;
  28. matchedParenthesis = doubleParenthesis;
  29. }
  30. if (nextStart == -1)
  31. break;
  32. nextEnd = data.indexOf(matchedParenthesis, nextStart + 1);
  33. if (nextStart == -1 || nextEnd == -1)
  34. break;
  35. tempData.push(data.substring(cursor, nextStart));
  36. tempData.push('__CSSFREETEXT__');
  37. texts.push(data.substring(nextStart, nextEnd + 1));
  38. cursor = nextEnd + 1;
  39. }
  40. return tempData.length > 0 ?
  41. tempData.join('') + data.substring(cursor, data.length) :
  42. data;
  43. },
  44. restore: function(data) {
  45. return data.replace(/__CSSFREETEXT__/g, function() {
  46. return texts.shift();
  47. });
  48. }
  49. };
  50. };