1
0

php.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. (function() {
  2. function keywords(str) {
  3. var obj = {}, words = str.split(" ");
  4. for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
  5. return obj;
  6. }
  7. function heredoc(delim) {
  8. return function(stream, state) {
  9. if (stream.match(delim)) state.tokenize = null;
  10. else stream.skipToEnd();
  11. return "string";
  12. }
  13. }
  14. var phpConfig = {
  15. name: "clike",
  16. keywords: keywords("abstract and array as break case catch cfunction class clone const continue declare " +
  17. "default do else elseif enddeclare endfor endforeach endif endswitch endwhile extends " +
  18. "final for foreach function global goto if implements interface instanceof namespace " +
  19. "new or private protected public static switch throw try use var while xor return"),
  20. blockKeywords: keywords("catch do else elseif for foreach if switch try while"),
  21. atoms: keywords("true false null"),
  22. multiLineStrings: true,
  23. hooks: {
  24. "$": function(stream, state) {
  25. stream.eatWhile(/[\w\$_]/);
  26. return "variable-2";
  27. },
  28. "<": function(stream, state) {
  29. if (stream.match(/<</)) {
  30. stream.eatWhile(/[\w\.]/);
  31. state.tokenize = heredoc(stream.current().slice(3));
  32. return state.tokenize(stream, state);
  33. }
  34. return false;
  35. }
  36. }
  37. };
  38. CodeMirror.defineMode("php", function(config, parserConfig) {
  39. var htmlMode = CodeMirror.getMode(config, "text/html");
  40. var jsMode = CodeMirror.getMode(config, "text/javascript");
  41. var cssMode = CodeMirror.getMode(config, "text/css");
  42. var phpMode = CodeMirror.getMode(config, phpConfig);
  43. function dispatch(stream, state) { // TODO open PHP inside text/css
  44. if (state.curMode == htmlMode) {
  45. var style = htmlMode.token(stream, state.curState);
  46. if (style == "meta" && /^<\?/.test(stream.current())) {
  47. state.curMode = phpMode;
  48. state.curState = state.php;
  49. state.curClose = /^\?>/;
  50. }
  51. else if (style == "tag" && stream.current() == ">" && state.curState.context) {
  52. if (/^script$/i.test(state.curState.context.tagName)) {
  53. state.curMode = jsMode;
  54. state.curState = jsMode.startState(htmlMode.indent(state.curState, ""));
  55. state.curClose = /^<\/\s*script\s*>/i;
  56. }
  57. else if (/^style$/i.test(state.curState.context.tagName)) {
  58. state.curMode = cssMode;
  59. state.curState = cssMode.startState(htmlMode.indent(state.curState, ""));
  60. state.curClose = /^<\/\s*style\s*>/i;
  61. }
  62. }
  63. return style;
  64. }
  65. else if (stream.match(state.curClose, false)) {
  66. state.curMode = htmlMode;
  67. state.curState = state.html;
  68. state.curClose = null;
  69. return dispatch(stream, state);
  70. }
  71. else return state.curMode.token(stream, state.curState);
  72. }
  73. return {
  74. startState: function() {
  75. var html = htmlMode.startState();
  76. return {html: html,
  77. php: phpMode.startState(),
  78. curMode: parserConfig.startOpen ? phpMode : htmlMode,
  79. curState: parserConfig.startOpen ? phpMode.startState() : html,
  80. curClose: parserConfig.startOpen ? /^\?>/ : null}
  81. },
  82. copyState: function(state) {
  83. var html = state.html, htmlNew = CodeMirror.copyState(htmlMode, html),
  84. php = state.php, phpNew = CodeMirror.copyState(phpMode, php), cur;
  85. if (state.curState == html) cur = htmlNew;
  86. else if (state.curState == php) cur = phpNew;
  87. else cur = CodeMirror.copyState(state.curMode, state.curState);
  88. return {html: htmlNew, php: phpNew, curMode: state.curMode, curState: cur, curClose: state.curClose};
  89. },
  90. token: dispatch,
  91. indent: function(state, textAfter) {
  92. if ((state.curMode != phpMode && /^\s*<\//.test(textAfter)) ||
  93. (state.curMode == phpMode && /^\?>/.test(textAfter)))
  94. return htmlMode.indent(state.html, textAfter);
  95. return state.curMode.indent(state.curState, textAfter);
  96. },
  97. electricChars: "/{}:"
  98. }
  99. });
  100. CodeMirror.defineMIME("application/x-httpd-php", "php");
  101. CodeMirror.defineMIME("application/x-httpd-php-open", {name: "php", startOpen: true});
  102. CodeMirror.defineMIME("text/x-php", phpConfig);
  103. })();