~automne-team/automne/trunk

« back to all changes in this revision

Viewing changes to automne/codemirror/php.js

  • Committer: sebastien-pauchet
  • Date: 2012-04-17 07:37:49 UTC
  • mfrom: (363.2.153 4.2)
  • Revision ID: seb@automne-cms.org-20120417073749-by8eh7ta6d9aadg6
Merge from stable 4.2.1 branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
  }
14
14
  var phpConfig = {
15
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"),
 
16
    keywords: keywords("abstract and array as break case catch class clone const continue declare default " +
 
17
                       "do else elseif enddeclare endfor endforeach endif endswitch endwhile extends final " +
 
18
                       "for foreach function global goto if implements interface instanceof namespace " +
 
19
                       "new or private protected public static switch throw trait try use var while xor " +
 
20
                       "die echo empty exit eval include include_once isset list require require_once return " +
 
21
                       "print unset __halt_compiler self static parent"),
21
22
    blockKeywords: keywords("catch do else elseif for foreach if switch try while"),
22
23
    atoms: keywords("true false null TRUE FALSE NULL"),
23
24
    multiLineStrings: true,
35
36
        return false;
36
37
      },
37
38
      "#": function(stream, state) {
38
 
        stream.skipToEnd();
 
39
        while (!stream.eol() && !stream.match("?>", false)) stream.next();
39
40
        return "comment";
 
41
      },
 
42
      "/": function(stream, state) {
 
43
        if (stream.eat("/")) {
 
44
          while (!stream.eol() && !stream.match("?>", false)) stream.next();
 
45
          return "comment";
 
46
        }
 
47
        return false;
40
48
      }
41
49
    }
42
50
  };
48
56
    var phpMode = CodeMirror.getMode(config, phpConfig);
49
57
 
50
58
    function dispatch(stream, state) { // TODO open PHP inside text/css
 
59
      var isPHP = state.mode == "php";
 
60
      if (stream.sol() && state.pending != '"') state.pending = null;
51
61
      if (state.curMode == htmlMode) {
52
 
        var style = htmlMode.token(stream, state.curState);
53
 
        if (style == "meta" && /^<\?/.test(stream.current())) {
 
62
        if (stream.match(/^<\?\w*/)) {
54
63
          state.curMode = phpMode;
55
64
          state.curState = state.php;
56
 
          state.curClose = /^\?>/;
57
 
                  state.mode =  'php';
58
 
        }
59
 
        else if (style == "tag" && stream.current() == ">" && state.curState.context) {
 
65
          state.curClose = "?>";
 
66
          state.mode = "php";
 
67
          return "meta";
 
68
        }
 
69
        if (state.pending == '"') {
 
70
          while (!stream.eol() && stream.next() != '"') {}
 
71
          var style = "string";
 
72
        } else if (state.pending && stream.pos < state.pending.end) {
 
73
          stream.pos = state.pending.end;
 
74
          var style = state.pending.style;
 
75
        } else {
 
76
          var style = htmlMode.token(stream, state.curState);
 
77
        }
 
78
        state.pending = null;
 
79
        var cur = stream.current(), openPHP = cur.search(/<\?/);
 
80
        if (openPHP != -1) {
 
81
          if (style == "string" && /\"$/.test(cur) && !/\?>/.test(cur)) state.pending = '"';
 
82
          else state.pending = {end: stream.pos, style: style};
 
83
          stream.backUp(cur.length - openPHP);
 
84
        } else if (style == "tag" && stream.current() == ">" && state.curState.context) {
60
85
          if (/^script$/i.test(state.curState.context.tagName)) {
61
86
            state.curMode = jsMode;
62
87
            state.curState = jsMode.startState(htmlMode.indent(state.curState, ""));
63
88
            state.curClose = /^<\/\s*script\s*>/i;
64
 
                        state.mode =  'javascript';
 
89
            state.mode = "javascript";
65
90
          }
66
91
          else if (/^style$/i.test(state.curState.context.tagName)) {
67
92
            state.curMode = cssMode;
68
93
            state.curState = cssMode.startState(htmlMode.indent(state.curState, ""));
69
 
            state.curClose =  /^<\/\s*style\s*>/i;
70
 
            state.mode =  'css';
 
94
            state.curClose = /^<\/\s*style\s*>/i;
 
95
            state.mode = "css";
71
96
          }
72
97
        }
73
98
        return style;
74
 
      }
75
 
      else if (stream.match(state.curClose, false)) {
 
99
      } else if ((!isPHP || state.php.tokenize == null) &&
 
100
                 stream.match(state.curClose, isPHP)) {
76
101
        state.curMode = htmlMode;
77
102
        state.curState = state.html;
78
103
        state.curClose = null;
79
 
                state.mode =  'html';
80
 
        return dispatch(stream, state);
 
104
        state.mode = "html";
 
105
        if (isPHP) return "meta";
 
106
        else return dispatch(stream, state);
 
107
      } else {
 
108
        return state.curMode.token(stream, state.curState);
81
109
      }
82
 
      else return state.curMode.token(stream, state.curState);
83
110
    }
84
111
 
85
112
    return {
87
114
        var html = htmlMode.startState();
88
115
        return {html: html,
89
116
                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'}
 
117
                curMode: parserConfig.startOpen ? phpMode : htmlMode,
 
118
                curState: parserConfig.startOpen ? phpMode.startState() : html,
 
119
                curClose: parserConfig.startOpen ? /^\?>/ : null,
 
120
                mode: parserConfig.startOpen ? "php" : "html",
 
121
                pending: null}
94
122
      },
95
123
 
96
124
      copyState: function(state) {
99
127
        if (state.curState == html) cur = htmlNew;
100
128
        else if (state.curState == php) cur = phpNew;
101
129
        else cur = CodeMirror.copyState(state.curMode, state.curState);
102
 
        return {html: htmlNew, php: phpNew, curMode: state.curMode, curState: cur, curClose: state.curClose};
 
130
        return {html: htmlNew, php: phpNew, curMode: state.curMode, curState: cur,
 
131
                curClose: state.curClose, mode: state.mode,
 
132
                pending: state.pending};
103
133
      },
104
134
 
105
135
      token: dispatch,