You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

123 lines
2.9 KiB

  1. define(['text'], function(textPlugin) {
  2. var buildText = {};
  3. return {
  4. load: function(name, req, onLoad, config) {
  5. var self = this,
  6. file = name,
  7. segments = file.split('/');
  8. // If the module name does not have an extension, append the default one
  9. if (segments[segments.length - 1].lastIndexOf('.') == -1) {
  10. file += '.html';
  11. }
  12. textPlugin.get(req.toUrl(file), function(html) {
  13. for (var option in config.config.html) {
  14. if (option in self.transform) {
  15. html = self.transform[option](config.config.html[option], html);
  16. }
  17. }
  18. if (config.isBuild) {
  19. buildText[name] = textPlugin.jsEscape(html);
  20. }
  21. onLoad(html);
  22. }, onLoad.error);
  23. },
  24. write: function(pluginName, moduleName, write) {
  25. if (buildText.hasOwnProperty(moduleName)) {
  26. var name = "'" + pluginName + "!" + moduleName + "'",
  27. text = "function () {return '" + buildText[moduleName] + "';}";
  28. write("define(" + name + ", " + text + ");\n");
  29. }
  30. },
  31. transform: {
  32. comments: function(action, html) {
  33. if (action === 'strip') {
  34. return html.replace(/<!--(.|[\n\r])*?-->/gm, '');
  35. } else {
  36. return html;
  37. }
  38. },
  39. whitespaceBetweenTags: function(action, html) {
  40. var pattern = />[\n\r\s]+</gm;
  41. if (action === 'strip') {
  42. return html.replace(pattern, '><');
  43. } else if (action === 'collapse') {
  44. return html.replace(pattern, '> <');
  45. } else {
  46. return html;
  47. }
  48. },
  49. whitespaceBetweenTagsAndText: function(action, html) {
  50. var afterTagPattern = />[\n\r\s]+/gm,
  51. beforeTagPattern = /[\n\r\s]+</gm;
  52. if (action === 'strip') {
  53. return html.replace(afterTagPattern, '>').replace(beforeTagPattern, '<');
  54. } else if (action === 'collapse') {
  55. return html.replace(afterTagPattern, '> ').replace(beforeTagPattern, ' <');
  56. } else {
  57. return html;
  58. }
  59. },
  60. whitespaceWithinTags: function(action, html) {
  61. if (action === 'collapse') {
  62. var tagPattern = /<([^>"']*?|"[^"]*?"|'[^']*?')+>/g,
  63. attrPattern = /([^\0\n\r\s"'>\/=]+)(?:\s*(=)\s*([^\n\r\s"'=><`]+|"[^"]*"|'[^']*'))?/gi,
  64. lastIndex = 0,
  65. result = '',
  66. match,
  67. tag;
  68. while ((match = tagPattern.exec(html)) !== null) {
  69. // Copy text between the beginning of this match and the end of the last one
  70. result += html.substring(lastIndex, match.index);
  71. tag = match[0];
  72. if (/^<[^\/]/.test(tag)) { // It's a start tag
  73. var attrs = tag.match(attrPattern),
  74. start = attrs.shift(),
  75. end = /\/>$/.test(tag) ? '/>' : '>';
  76. result += start + attrs.map(function(attr) {
  77. return attr.replace(attrPattern, ' $1$2$3');
  78. }).join('') + end;
  79. } else { // It's an end tag
  80. result += tag.replace(/[\n\r\s]+/g, '');
  81. }
  82. lastIndex = tagPattern.lastIndex;
  83. }
  84. return result + html.substring(lastIndex);
  85. } else {
  86. return html;
  87. }
  88. }
  89. }
  90. };
  91. });