aboutsummaryrefslogtreecommitdiff
path: root/js/codemirror/mode/php
diff options
context:
space:
mode:
Diffstat (limited to 'js/codemirror/mode/php')
-rw-r--r--js/codemirror/mode/php/index.html49
-rw-r--r--js/codemirror/mode/php/php.js110
2 files changed, 159 insertions, 0 deletions
diff --git a/js/codemirror/mode/php/index.html b/js/codemirror/mode/php/index.html
new file mode 100644
index 00000000..9e545c9b
--- /dev/null
+++ b/js/codemirror/mode/php/index.html
@@ -0,0 +1,49 @@
1<!doctype html>
2<html>
3 <head>
4 <title>CodeMirror 2: PHP mode</title>
5 <link rel="stylesheet" href="../../lib/codemirror.css">
6 <script src="../../lib/codemirror.js"></script>
7 <script src="../xml/xml.js"></script>
8 <script src="../javascript/javascript.js"></script>
9 <script src="../css/css.js"></script>
10 <script src="../clike/clike.js"></script>
11 <script src="php.js"></script>
12 <link rel="stylesheet" href="../../theme/default.css">
13 <style type="text/css">.CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}</style>
14 <link rel="stylesheet" href="../../css/docs.css">
15 </head>
16 <body>
17 <h1>CodeMirror 2: PHP mode</h1>
18
19<form><textarea id="code" name="code">
20<?php
21function hello($who) {
22 return "Hello " . $who;
23}
24?>
25<p>The program says <?= hello("World") ?>.</p>
26<script>
27 alert("And here is some JS code"); // also colored
28</script>
29</textarea></form>
30
31 <script>
32 var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
33 lineNumbers: true,
34 matchBrackets: true,
35 mode: "application/x-httpd-php",
36 indentUnit: 8,
37 indentWithTabs: true,
38 enterMode: "keep",
39 tabMode: "shift"
40 });
41 </script>
42
43 <p>Simple HTML/PHP mode based on
44 the <a href="../clike/">C-like</a> mode. Depends on XML,
45 JavaScript, CSS, and C-like modes.</p>
46
47 <p><strong>MIME types defined:</strong> <code>application/x-httpd-php</code> (HTML with PHP code), <code>text/x-php</code> (plain, non-wrapped PHP code).</p>
48 </body>
49</html>
diff --git a/js/codemirror/mode/php/php.js b/js/codemirror/mode/php/php.js
new file mode 100644
index 00000000..495e4415
--- /dev/null
+++ b/js/codemirror/mode/php/php.js
@@ -0,0 +1,110 @@
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
39 CodeMirror.defineMode("php", function(config, parserConfig) {
40 var htmlMode = CodeMirror.getMode(config, "text/html");
41 var jsMode = CodeMirror.getMode(config, "text/javascript");
42 var cssMode = CodeMirror.getMode(config, "text/css");
43 var phpMode = CodeMirror.getMode(config, phpConfig);
44
45 function dispatch(stream, state) { // TODO open PHP inside text/css
46 if (state.curMode == htmlMode) {
47 var style = htmlMode.token(stream, state.curState);
48 if (style == "meta" && /^<\?/.test(stream.current())) {
49 state.curMode = phpMode;
50 state.curState = state.php;
51 state.curClose = /^\?>/;
52 }
53 else if (style == "tag" && stream.current() == ">" && state.curState.context) {
54 if (/^script$/i.test(state.curState.context.tagName)) {
55 state.curMode = jsMode;
56 state.curState = jsMode.startState(htmlMode.indent(state.curState, ""));
57 state.curClose = /^<\/\s*script\s*>/i;
58 }
59 else if (/^style$/i.test(state.curState.context.tagName)) {
60 state.curMode = cssMode;
61 state.curState = cssMode.startState(htmlMode.indent(state.curState, ""));
62 state.curClose = /^<\/\s*style\s*>/i;
63 }
64 }
65 return style;
66 }
67 else if (stream.match(state.curClose, false)) {
68 state.curMode = htmlMode;
69 state.curState = state.html;
70 state.curClose = null;
71 return dispatch(stream, state);
72 }
73 else return state.curMode.token(stream, state.curState);
74 }
75
76 return {
77 startState: function() {
78 var html = htmlMode.startState();
79 return {html: html,
80 php: phpMode.startState(),
81 curMode: parserConfig.startOpen ? phpMode : htmlMode,
82 curState: parserConfig.startOpen ? phpMode.startState() : html,
83 curClose: parserConfig.startOpen ? /^\?>/ : null}
84 },
85
86 copyState: function(state) {
87 var html = state.html, htmlNew = CodeMirror.copyState(htmlMode, html),
88 php = state.php, phpNew = CodeMirror.copyState(phpMode, php), cur;
89 if (state.curState == html) cur = htmlNew;
90 else if (state.curState == php) cur = phpNew;
91 else cur = CodeMirror.copyState(state.curMode, state.curState);
92 return {html: htmlNew, php: phpNew, curMode: state.curMode, curState: cur, curClose: state.curClose};
93 },
94
95 token: dispatch,
96
97 indent: function(state, textAfter) {
98 if ((state.curMode != phpMode && /^\s*<\//.test(textAfter)) ||
99 (state.curMode == phpMode && /^\?>/.test(textAfter)))
100 return htmlMode.indent(state.html, textAfter);
101 return state.curMode.indent(state.curState, textAfter);
102 },
103
104 electricChars: "/{}:"
105 }
106 });
107 CodeMirror.defineMIME("application/x-httpd-php", "php");
108 CodeMirror.defineMIME("application/x-httpd-php-open", {name: "php", startOpen: true});
109 CodeMirror.defineMIME("text/x-php", phpConfig);
110})();