Skip to content

Commit

Permalink
[util/closetag] Clean up handling of different modes
Browse files Browse the repository at this point in the history
  • Loading branch information
marijnh committed Sep 7, 2012
1 parent bd9a83b commit 1cea077
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions lib/util/closetag.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@
/** Array of tag names where an end tag is forbidden. */
CodeMirror.defaults['closeTagVoid'] = ['area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr'];

function resolveMode(mode) {
if (typeof mode == "object") return mode.name;
if (mode.indexOf("/") > -1) return resolveMode(CodeMirror.mimeModes[mode]);
else return mode;
}

function innerState(state) {
// htmlmixed uses .htmlState, PHP .html, XML just the top state object
return state.htmlState || state.html || state;
}

/**
* Call during key processing to close tags. Handles the key event if the tag is closed, otherwise throws CodeMirror.Pass.
* - cm: The editor instance.
Expand All @@ -39,9 +50,8 @@
throw CodeMirror.Pass;
}

var mode = cm.getOption('mode');

if (mode == 'text/html' || mode == 'xml' || mode == 'application/x-httpd-php') {
if (/^(xml|php|htmlmixed)$/.test(resolveMode(cm.getOption('mode')))) {

/*
* Relevant structure of token:
Expand Down Expand Up @@ -72,7 +82,7 @@
}

if (ch == '>') {
var type = state.htmlState ? state.htmlState.type : state.html ? state.html.type : state.type; // htmlmixed : xml : php
var type = innerState(state).type;

if (tok.className == 'tag' && type == 'closeTag') {
throw CodeMirror.Pass; // Don't process the '>' at the end of an end-tag.
Expand All @@ -84,10 +94,10 @@

tok = cm.getTokenAt(cm.getCursor());
state = tok.state;
type = state.htmlState ? state.htmlState.type : state.html ? state.html.type : state.type; // htmlmixed : xml : php
var type = innerState(state).type;

if (tok.className == 'tag' && type != 'selfcloseTag') {
var tagName = state.htmlState ? state.htmlState.tagName : state.html ? state.html.tagName : state.tagName; // htmlmixed : xml : php
var tagName = innerState(state).tagName;
if (tagName.length > 0 && shouldClose(cm, vd, tagName)) {
insertEndTag(cm, indent, pos, tagName);
}
Expand All @@ -100,7 +110,7 @@

} else if (ch == '/') {
if (tok.className == 'tag' && tok.string == '<') {
var tagName = state.htmlState ? (state.htmlState.context ? state.htmlState.context.tagName : '') : (state.context ? state.context.tagName : ''); // htmlmixed : xml
var ctx = innerState(state).context, tagName = ctx && ctx.tagName;
if (tagName.length > 0) {
completeEndTag(cm, pos, tagName);
return;
Expand Down

0 comments on commit 1cea077

Please sign in to comment.