1
0

xml-hint.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. (function() {
  2. CodeMirror.xmlHints = [];
  3. CodeMirror.xmlHint = function(cm) {
  4. var cursor = cm.getCursor();
  5. if (cursor.ch > 0) {
  6. var text = cm.getRange(CodeMirror.Pos(0, 0), cursor);
  7. var typed = '';
  8. var simbol = '';
  9. for(var i = text.length - 1; i >= 0; i--) {
  10. if(text[i] == ' ' || text[i] == '<') {
  11. simbol = text[i];
  12. break;
  13. }
  14. else {
  15. typed = text[i] + typed;
  16. }
  17. }
  18. text = text.slice(0, text.length - typed.length);
  19. var path = getActiveElement(text) + simbol;
  20. var hints = CodeMirror.xmlHints[path];
  21. if(typeof hints === 'undefined')
  22. hints = [''];
  23. else {
  24. hints = hints.slice(0);
  25. for (var i = hints.length - 1; i >= 0; i--) {
  26. if(hints[i].indexOf(typed) != 0)
  27. hints.splice(i, 1);
  28. }
  29. }
  30. return {
  31. list: hints,
  32. from: CodeMirror.Pos(cursor.line, cursor.ch - typed.length),
  33. to: cursor
  34. };
  35. }
  36. };
  37. var getActiveElement = function(text) {
  38. var element = '';
  39. if(text.length >= 0) {
  40. var regex = new RegExp('<([^!?][^\\s/>]*)[\\s\\S]*?>', 'g');
  41. var matches = [];
  42. var match;
  43. while ((match = regex.exec(text)) != null) {
  44. matches.push({
  45. tag: match[1],
  46. selfclose: (match[0].slice(match[0].length - 2) === '/>')
  47. });
  48. }
  49. for (var i = matches.length - 1, skip = 0; i >= 0; i--) {
  50. var item = matches[i];
  51. if (item.tag[0] == '/')
  52. {
  53. skip++;
  54. }
  55. else if (item.selfclose == false)
  56. {
  57. if (skip > 0)
  58. {
  59. skip--;
  60. }
  61. else
  62. {
  63. element = '<' + item.tag + '>' + element;
  64. }
  65. }
  66. }
  67. element += getOpenTag(text);
  68. }
  69. return element;
  70. };
  71. var getOpenTag = function(text) {
  72. var open = text.lastIndexOf('<');
  73. var close = text.lastIndexOf('>');
  74. if (close < open)
  75. {
  76. text = text.slice(open);
  77. if(text != '<') {
  78. var space = text.indexOf(' ');
  79. if(space < 0)
  80. space = text.indexOf('\t');
  81. if(space < 0)
  82. space = text.indexOf('\n');
  83. if (space < 0)
  84. space = text.length;
  85. return text.slice(0, space);
  86. }
  87. }
  88. return '';
  89. };
  90. })();