xml.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. define(function(require, exports, module) {
  31. "use strict";
  32. var oop = require("../../lib/oop");
  33. var Behaviour = require("../behaviour").Behaviour;
  34. var CstyleBehaviour = require("./cstyle").CstyleBehaviour;
  35. var TokenIterator = require("../../token_iterator").TokenIterator;
  36. function hasType(token, type) {
  37. var tokenTypes = token.type.split('.');
  38. return type.split('.').every(function(type){
  39. return (tokenTypes.indexOf(type) !== -1);
  40. });
  41. return hasType;
  42. }
  43. var XmlBehaviour = function () {
  44. this.inherit(CstyleBehaviour, ["string_dquotes"]); // Get string behaviour
  45. this.add("autoclosing", "insertion", function (state, action, editor, session, text) {
  46. if (text == '>') {
  47. var position = editor.getCursorPosition();
  48. var iterator = new TokenIterator(session, position.row, position.column);
  49. var token = iterator.getCurrentToken();
  50. if (token && hasType(token, 'string') && iterator.getCurrentTokenColumn() + token.value.length > position.column)
  51. return;
  52. var atCursor = false;
  53. if (!token || !hasType(token, 'meta.tag') && !(hasType(token, 'text') && token.value.match('/'))){
  54. do {
  55. token = iterator.stepBackward();
  56. } while (token && (hasType(token, 'string') || hasType(token, 'keyword.operator') || hasType(token, 'entity.attribute-name') || hasType(token, 'text')));
  57. } else {
  58. atCursor = true;
  59. }
  60. if (!token || !hasType(token, 'meta.tag.name') || iterator.stepBackward().value.match('/')) {
  61. return;
  62. }
  63. var tag = token.value;
  64. if (atCursor){
  65. var tag = tag.substring(0, position.column - token.start);
  66. }
  67. return {
  68. text: '>' + '</' + tag + '>',
  69. selection: [1, 1]
  70. }
  71. }
  72. });
  73. this.add('autoindent', 'insertion', function (state, action, editor, session, text) {
  74. if (text == "\n") {
  75. var cursor = editor.getCursorPosition();
  76. var line = session.getLine(cursor.row);
  77. var rightChars = line.substring(cursor.column, cursor.column + 2);
  78. if (rightChars == '</') {
  79. var next_indent = this.$getIndent(line);
  80. var indent = next_indent + session.getTabString();
  81. return {
  82. text: '\n' + indent + '\n' + next_indent,
  83. selection: [1, indent.length, 1, indent.length]
  84. }
  85. }
  86. }
  87. });
  88. }
  89. oop.inherits(XmlBehaviour, Behaviour);
  90. exports.XmlBehaviour = XmlBehaviour;
  91. });