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.

93 lines
3.4 KiB

  1. // Generated by CoffeeScript 1.4.0
  2. (function() {
  3. var balUtilFlow, balUtilHTML,
  4. __slice = [].slice;
  5. balUtilFlow = (typeof require === "function" ? require(__dirname + '/flow') : void 0) || this.balUtilFlow;
  6. balUtilHTML = {
  7. getAttribute: function(attributes, attribute) {
  8. var match, regex, value;
  9. regex = new RegExp("(" + attribute + ")\\s*=\\s*('[^']+'|\\\"[^\\\"]+\\\"|[^'\\\"\\s]\\S*)", 'ig');
  10. value = null;
  11. while (match = regex.exec(attributes)) {
  12. value = match[2].trim().replace(/(^['"]\s*|\s*['"]$)/g, '');
  13. }
  14. return value;
  15. },
  16. detectIndentation: function(source) {
  17. var indentation, result;
  18. result = /\n([ \t]*)\S/m.exec(source);
  19. indentation = (result != null ? result[1] : void 0) || '';
  20. return indentation;
  21. },
  22. removeIndentation: function(source) {
  23. var indentation, regex, regexString, result;
  24. indentation = balUtilHTML.detectIndentation(source);
  25. regexString = indentation.replace(/\t/g, '\\t');
  26. regex = new RegExp("^" + regexString, 'gm');
  27. result = source.replace(regex, '').trim();
  28. return result;
  29. },
  30. replaceElement: function() {
  31. var args, element, html, regex, removeIndentation, replace, result, _ref;
  32. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  33. if (args.length === 1) {
  34. _ref = args[0], html = _ref.html, element = _ref.element, removeIndentation = _ref.removeIndentation, replace = _ref.replace;
  35. } else {
  36. html = args[0], element = args[1], replace = args[2];
  37. }
  38. regex = new RegExp("<(" + element + "(?:\\:[-:_a-z0-9]+)?)(\\s+[^>]+)?>([\\s\\S]+?)<\\/\\1>", 'ig');
  39. result = html.replace(regex, function(outerHTML, element, attributes, innerHTML) {
  40. if (removeIndentation !== false) {
  41. innerHTML = balUtilHTML.removeIndentation(innerHTML);
  42. }
  43. return replace(outerHTML, element, attributes, innerHTML);
  44. });
  45. return result;
  46. },
  47. replaceElementAsync: function() {
  48. var args, element, html, next, removeIndentation, replace, result, tasks, _ref;
  49. args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
  50. if (args.length === 1) {
  51. _ref = args[0], html = _ref.html, element = _ref.element, removeIndentation = _ref.removeIndentation, replace = _ref.replace, next = _ref.next;
  52. } else {
  53. html = args[0], element = args[1], replace = args[2], next = args[3];
  54. }
  55. tasks = new balUtilFlow.Group(function(err) {
  56. if (err) {
  57. return next(err);
  58. }
  59. return next(null, result);
  60. });
  61. result = balUtilHTML.replaceElement({
  62. html: html,
  63. element: element,
  64. removeIndentation: removeIndentation,
  65. replace: function(outerHTML, element, attributes, innerHTML) {
  66. var random;
  67. random = Math.random();
  68. tasks.push(function(complete) {
  69. return replace(outerHTML, element, attributes, innerHTML, function(err, replaceElementResult) {
  70. if (err) {
  71. return complete(err);
  72. }
  73. result = result.replace(random, replaceElementResult);
  74. return complete();
  75. });
  76. });
  77. return random;
  78. }
  79. });
  80. tasks.sync();
  81. return this;
  82. }
  83. };
  84. if (typeof module !== "undefined" && module !== null) {
  85. module.exports = balUtilHTML;
  86. } else {
  87. this.balUtilHTML = balUtilHTML;
  88. }
  89. }).call(this);