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 | var phpKeywords = |
---|
8 | keywords("abstract and array as break case catch cfunction class clone const continue declare " + |
---|
9 | "default do else elseif enddeclare endfor endforeach endif endswitch endwhile extends " + |
---|
10 | "final for foreach function global goto if implements interface instanceof namespace " + |
---|
11 | "new or private protected public static switch throw try use var while xor"); |
---|
12 | |
---|
13 | CodeMirror.defineMode("php", function(config, parserConfig) { |
---|
14 | var htmlMode = CodeMirror.getMode(config, "text/html"); |
---|
15 | var jsMode = CodeMirror.getMode(config, "text/javascript"); |
---|
16 | var cssMode = CodeMirror.getMode(config, "text/css"); |
---|
17 | var phpMode = CodeMirror.getMode(config, {name: "clike", keywords: phpKeywords, multiLineStrings: true, $vars: true}); |
---|
18 | |
---|
19 | function dispatch(stream, state) { // TODO open PHP inside text/css |
---|
20 | if (state.curMode == htmlMode) { |
---|
21 | var style = htmlMode.token(stream, state.curState); |
---|
22 | if (style == "xml-processing" && /^<\?/.test(stream.current())) { |
---|
23 | state.curMode = phpMode; |
---|
24 | state.curState = state.php; |
---|
25 | state.curClose = /^\?>/; |
---|
26 | } |
---|
27 | else if (style == "xml-tag" && stream.current() == ">" && state.curState.context) { |
---|
28 | if (/^script$/i.test(state.curState.context.tagName)) { |
---|
29 | state.curMode = jsMode; |
---|
30 | state.curState = jsMode.startState(htmlMode.indent(state.curState, "")); |
---|
31 | state.curClose = /^<\/\s*script\s*>/i; |
---|
32 | } |
---|
33 | else if (/^style$/i.test(state.curState.context.tagName)) { |
---|
34 | state.curMode = cssMode; |
---|
35 | state.curState = cssMode.startState(htmlMode.indent(state.curState, "")); |
---|
36 | state.curClose = /^<\/\s*style\s*>/i; |
---|
37 | } |
---|
38 | } |
---|
39 | return style; |
---|
40 | } |
---|
41 | else if (stream.match(state.curClose, false)) { |
---|
42 | state.curMode = htmlMode; |
---|
43 | state.curState = state.html; |
---|
44 | state.curClose = null; |
---|
45 | return dispatch(stream, state); |
---|
46 | } |
---|
47 | else return state.curMode.token(stream, state.curState); |
---|
48 | } |
---|
49 | |
---|
50 | return { |
---|
51 | startState: function() { |
---|
52 | var html = htmlMode.startState(); |
---|
53 | return {html: html, |
---|
54 | php: phpMode.startState(), |
---|
55 | curMode: htmlMode, |
---|
56 | curState: html, |
---|
57 | curClose: null} |
---|
58 | }, |
---|
59 | |
---|
60 | copyState: function(state) { |
---|
61 | var html = state.html, htmlNew = CodeMirror.copyState(htmlMode, html), |
---|
62 | php = state.php, phpNew = CodeMirror.copyState(phpMode, php), cur; |
---|
63 | if (state.curState == html) cur = htmlNew; |
---|
64 | else if (state.curState == php) cur = phpNew; |
---|
65 | else cur = CodeMirror.copyState(state.curMode, state.curState); |
---|
66 | return {html: htmlNew, php: phpNew, curMode: state.curMode, curState: cur, curClose: state.curClose}; |
---|
67 | }, |
---|
68 | |
---|
69 | token: dispatch, |
---|
70 | |
---|
71 | indent: function(state, textAfter) { |
---|
72 | if ((state.curMode != phpMode && /^\s*<\//.test(textAfter)) || |
---|
73 | (state.curMode == phpMode && /^\?>/.test(textAfter))) |
---|
74 | return htmlMode.indent(state.html, textAfter); |
---|
75 | return state.curMode.indent(state.curState, textAfter); |
---|
76 | }, |
---|
77 | |
---|
78 | electricChars: "/{}:" |
---|
79 | } |
---|
80 | }); |
---|
81 | })(); |
---|
82 | |
---|
83 | CodeMirror.defineMIME("application/x-httpd-php", "php"); |
---|