Skip to content

Commit 5c3722b

Browse files
committed
Make the PHP mode depend on the HTML-mixed mode
In order to not duplicate all its logic. Issue #751
1 parent 06f1329 commit 5c3722b

File tree

2 files changed

+14
-33
lines changed

2 files changed

+14
-33
lines changed

mode/php/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<title>CodeMirror: PHP mode</title>
66
<link rel="stylesheet" href="../../lib/codemirror.css">
77
<script src="../../lib/codemirror.js"></script>
8+
<script src="../htmlmixed/htmlmixed.js"></script>
89
<script src="../xml/xml.js"></script>
910
<script src="../javascript/javascript.js"></script>
1011
<script src="../css/css.js"></script>

mode/php/php.js

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,16 @@
5050
};
5151

5252
CodeMirror.defineMode("php", function(config, parserConfig) {
53-
var htmlMode = CodeMirror.getMode(config, {name: "xml", htmlMode: true});
54-
var jsMode = CodeMirror.getMode(config, "javascript");
55-
var cssMode = CodeMirror.getMode(config, "css");
53+
var htmlMode = CodeMirror.getMode(config, "text/html");
5654
var phpMode = CodeMirror.getMode(config, phpConfig);
5755

58-
function dispatch(stream, state) { // TODO open PHP inside text/css
56+
function dispatch(stream, state) {
5957
var isPHP = state.curMode == phpMode;
6058
if (stream.sol() && state.pending != '"') state.pending = null;
61-
if (state.curMode == htmlMode) {
59+
if (!isPHP) {
6260
if (stream.match(/^<\?\w*/)) {
6361
state.curMode = phpMode;
6462
state.curState = state.php;
65-
state.curClose = "?>";
6663
return "meta";
6764
}
6865
if (state.pending == '"') {
@@ -80,51 +77,33 @@
8077
if (style == "string" && /\"$/.test(cur) && !/\?>/.test(cur)) state.pending = '"';
8178
else state.pending = {end: stream.pos, style: style};
8279
stream.backUp(cur.length - openPHP);
83-
} else if (style == "tag" && stream.current() == ">" && state.curState.context) {
84-
if (/^script$/i.test(state.curState.context.tagName)) {
85-
state.curMode = jsMode;
86-
state.curState = jsMode.startState(htmlMode.indent(state.curState, ""));
87-
state.curClose = /^<\/\s*script\s*>/i;
88-
}
89-
else if (/^style$/i.test(state.curState.context.tagName)) {
90-
state.curMode = cssMode;
91-
state.curState = cssMode.startState(htmlMode.indent(state.curState, ""));
92-
state.curClose = /^<\/\s*style\s*>/i;
93-
}
9480
}
9581
return style;
96-
} else if ((!isPHP || state.php.tokenize == null) &&
97-
stream.match(state.curClose, isPHP)) {
82+
} else if (isPHP && state.php.tokenize == null && stream.match("?>")) {
9883
state.curMode = htmlMode;
9984
state.curState = state.html;
100-
state.curClose = null;
101-
if (isPHP) return "meta";
102-
else return dispatch(stream, state);
85+
return "meta";
10386
} else {
104-
return state.curMode.token(stream, state.curState);
87+
return phpMode.token(stream, state.curState);
10588
}
10689
}
10790

10891
return {
10992
startState: function() {
110-
var html = htmlMode.startState();
93+
var html = CodeMirror.startState(htmlMode), php = CodeMirror.startState(phpMode);
11194
return {html: html,
112-
php: phpMode.startState(),
95+
php: php,
11396
curMode: parserConfig.startOpen ? phpMode : htmlMode,
114-
curState: parserConfig.startOpen ? phpMode.startState() : html,
115-
curClose: parserConfig.startOpen ? /^\?>/ : null,
116-
mode: parserConfig.startOpen ? "php" : "html",
97+
curState: parserConfig.startOpen ? php : html,
11798
pending: null};
11899
},
119100

120101
copyState: function(state) {
121102
var html = state.html, htmlNew = CodeMirror.copyState(htmlMode, html),
122103
php = state.php, phpNew = CodeMirror.copyState(phpMode, php), cur;
123-
if (state.curState == html) cur = htmlNew;
124-
else if (state.curState == php) cur = phpNew;
125-
else cur = CodeMirror.copyState(state.curMode, state.curState);
104+
if (state.curMode == htmlMode) cur = htmlNew;
105+
else cur = phpNew;
126106
return {html: htmlNew, php: phpNew, curMode: state.curMode, curState: cur,
127-
curClose: state.curClose, mode: state.mode,
128107
pending: state.pending};
129108
},
130109

@@ -141,7 +120,8 @@
141120

142121
innerMode: function(state) { return {state: state.curState, mode: state.curMode}; }
143122
};
144-
}, "xml", "clike", "javascript", "css");
123+
}, "htmlmixed");
124+
145125
CodeMirror.defineMIME("application/x-httpd-php", "php");
146126
CodeMirror.defineMIME("application/x-httpd-php-open", {name: "php", startOpen: true});
147127
CodeMirror.defineMIME("text/x-php", phpConfig);

0 commit comments

Comments
 (0)