aboutsummaryrefslogtreecommitdiff
path: root/imports/codemirror/mode/php/php.js
diff options
context:
space:
mode:
authorValerio Virgillito2012-02-15 17:04:39 -0800
committerValerio Virgillito2012-02-15 17:04:39 -0800
commitfe71cb9aa1700199c0089166ad54fc56eb760644 (patch)
treeb0c7c24b1f45a079e1c09312ca716e3d35aecad4 /imports/codemirror/mode/php/php.js
parent747616980cad14f0b65fbcc7f497ed9680a39d29 (diff)
parentd366c0bd1af6471511217ed574083e15059519b5 (diff)
downloadninja-fe71cb9aa1700199c0089166ad54fc56eb760644.tar.gz
Merge branch 'refs/heads/master' into components
Diffstat (limited to 'imports/codemirror/mode/php/php.js')
-rwxr-xr-ximports/codemirror/mode/php/php.js120
1 files changed, 120 insertions, 0 deletions
diff --git a/imports/codemirror/mode/php/php.js b/imports/codemirror/mode/php/php.js
new file mode 100755
index 00000000..f88b3f84
--- /dev/null
+++ b/imports/codemirror/mode/php/php.js
@@ -0,0 +1,120 @@
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 "die echo empty exit eval include include_once isset list require require_once print unset"),
21 blockKeywords: keywords("catch do else elseif for foreach if switch try while"),
22 atoms: keywords("true false null TRUE FALSE NULL"),
23 multiLineStrings: true,
24 hooks: {
25 "$": function(stream, state) {
26 stream.eatWhile(/[\w\$_]/);
27 return "variable-2";
28 },
29 "<": function(stream, state) {
30 if (stream.match(/<</)) {
31 stream.eatWhile(/[\w\.]/);
32 state.tokenize = heredoc(stream.current().slice(3));
33 return state.tokenize(stream, state);
34 }
35 return false;
36 },
37 "#": function(stream, state) {
38 stream.skipToEnd();
39 return "comment";
40 }
41 }
42 };
43
44 CodeMirror.defineMode("php", function(config, parserConfig) {
45 var htmlMode = CodeMirror.getMode(config, {name: "xml", htmlMode: true});
46 var jsMode = CodeMirror.getMode(config, "javascript");
47 var cssMode = CodeMirror.getMode(config, "css");
48 var phpMode = CodeMirror.getMode(config, phpConfig);
49
50 function dispatch(stream, state) { // TODO open PHP inside text/css
51 if (state.curMode == htmlMode) {
52 var style = htmlMode.token(stream, state.curState);
53 if (style == "meta" && /^<\?/.test(stream.current())) {
54 state.curMode = phpMode;
55 state.curState = state.php;
56 state.curClose = /^\?>/;
57 state.mode = 'php';
58 }
59 else if (style == "tag" && stream.current() == ">" && state.curState.context) {
60 if (/^script$/i.test(state.curState.context.tagName)) {
61 state.curMode = jsMode;
62 state.curState = jsMode.startState(htmlMode.indent(state.curState, ""));
63 state.curClose = /^<\/\s*script\s*>/i;
64 state.mode = 'javascript';
65 }
66 else if (/^style$/i.test(state.curState.context.tagName)) {
67 state.curMode = cssMode;
68 state.curState = cssMode.startState(htmlMode.indent(state.curState, ""));
69 state.curClose = /^<\/\s*style\s*>/i;
70 state.mode = 'css';
71 }
72 }
73 return style;
74 }
75 else if (stream.match(state.curClose, false)) {
76 state.curMode = htmlMode;
77 state.curState = state.html;
78 state.curClose = null;
79 state.mode = 'html';
80 return dispatch(stream, state);
81 }
82 else return state.curMode.token(stream, state.curState);
83 }
84
85 return {
86 startState: function() {
87 var html = htmlMode.startState();
88 return {html: html,
89 php: phpMode.startState(),
90 curMode: parserConfig.startOpen ? phpMode : htmlMode,
91 curState: parserConfig.startOpen ? phpMode.startState() : html,
92 curClose: parserConfig.startOpen ? /^\?>/ : null,
93 mode: parserConfig.startOpen ? 'php' : 'html'}
94 },
95
96 copyState: function(state) {
97 var html = state.html, htmlNew = CodeMirror.copyState(htmlMode, html),
98 php = state.php, phpNew = CodeMirror.copyState(phpMode, php), cur;
99 if (state.curState == html) cur = htmlNew;
100 else if (state.curState == php) cur = phpNew;
101 else cur = CodeMirror.copyState(state.curMode, state.curState);
102 return {html: htmlNew, php: phpNew, curMode: state.curMode, curState: cur, curClose: state.curClose};
103 },
104
105 token: dispatch,
106
107 indent: function(state, textAfter) {
108 if ((state.curMode != phpMode && /^\s*<\//.test(textAfter)) ||
109 (state.curMode == phpMode && /^\?>/.test(textAfter)))
110 return htmlMode.indent(state.html, textAfter);
111 return state.curMode.indent(state.curState, textAfter);
112 },
113
114 electricChars: "/{}:"
115 }
116 });
117 CodeMirror.defineMIME("application/x-httpd-php", "php");
118 CodeMirror.defineMIME("application/x-httpd-php-open", {name: "php", startOpen: true});
119 CodeMirror.defineMIME("text/x-php", phpConfig);
120})();