html.js 3.0 KB

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