Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inline CSS Highlight Patch #4646

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
79 changes: 62 additions & 17 deletions mode/htmlmixed/htmlmixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
multilineTagIndentFactor: parserConfig.multilineTagIndentFactor,
multilineTagIndentPastTag: parserConfig.multilineTagIndentPastTag
});

var highlightCodeInStyleAttr = typeof config.inlineCssMode !== 'undefined' ? config.inlineCssMode : false;
var tags = {};
var configTags = parserConfig && parserConfig.tags, configScript = parserConfig && parserConfig.scriptTypes;
addTags(defaultTags, tags);
Expand All @@ -86,26 +86,71 @@

function html(stream, state) {
var style = htmlMode.token(stream, state.htmlState), tag = /\btag\b/.test(style), tagName

if (tag && !/[<>\s\/]/.test(stream.current()) &&
(tagName = state.htmlState.tagName && state.htmlState.tagName.toLowerCase()) &&
tags.hasOwnProperty(tagName)) {
state.inTag = tagName + " "
} else if (state.inTag && tag && />$/.test(stream.current())) {
var inTag = /^([\S]+) (.*)/.exec(state.inTag)
state.inTag = null
var modeSpec = stream.current() == ">" && findMatchingMode(tags[inTag[1]], inTag[2])
var mode = CodeMirror.getMode(config, modeSpec)
var endTagA = getTagRegexp(inTag[1], true), endTag = getTagRegexp(inTag[1], false);
state.token = function (stream, state) {
if (stream.match(endTagA, false)) {
state.token = html;
state.localState = state.localMode = null;
return null;
state.inTag = tagName + " ";
state.isDefault = true;
}
else if(highlightCodeInStyleAttr && tag && !/[<>\s\/]/.test(stream.current())) {
state.inTag = true;
state.isDefault = false;
}
else if(highlightCodeInStyleAttr && !state.isDefault && state.inTag && tag && />$/.test(stream.current())) {
state.isDefault = null;
state.inTag = null;
}
else if(highlightCodeInStyleAttr && !state.isDefault && state.inTag) {
var isStyleAttribute = stream.match(/style=/, true);

if(isStyleAttribute) {
var startingQuote = stream.match(/["']/, true);
if(startingQuote) {
var quoteStack = new Array();
quoteStack.push(startingQuote[0]);
var mode = CodeMirror.getMode(config, {
name: "css",
inline: true
});
state.token = function (stream, state) {
var quote = stream.match(/["']/, false);
if(quote) {
if((quote[0] == '"' && quoteStack[quoteStack.length - 1] == '"') || (quote[0] == "'" && quoteStack[quoteStack.length - 1] == "'"))
quoteStack.pop();

if(quoteStack.length == 0) {
stream.next();
state.token = html;
state.localState = state.localMode = null;
return 'text';
}
}
return state.localMode.token(stream, state.localState);
};
state.localMode = mode;
state.localState = CodeMirror.startState(mode, htmlMode.indent(state.htmlState, ""));
}
return maybeBackup(stream, endTag, state.localMode.token(stream, state.localState));
};
state.localMode = mode;
state.localState = CodeMirror.startState(mode, htmlMode.indent(state.htmlState, ""));
}
}
else if (state.isDefault && state.inTag && tag && />$/.test(stream.current())) {
var inTag = /^([\S]+) (.*)/.exec(state.inTag);
state.inTag = null;
state.isDefault = null;
var modeSpec = stream.current() == ">" && findMatchingMode(tags[inTag[1]], inTag[2])
var mode = CodeMirror.getMode(config, modeSpec)
var endTagA = getTagRegexp(inTag[1], true), endTag = getTagRegexp(inTag[1], false);

state.token = function (stream, state) {
if (stream.match(endTagA, false)) {
state.token = html;
state.localState = state.localMode = null;
return null;
}
return maybeBackup(stream, endTag, state.localMode.token(stream, state.localState));
};
state.localMode = mode;
state.localState = CodeMirror.startState(mode, htmlMode.indent(state.htmlState, ""));
} else if (state.inTag) {
state.inTag += stream.current()
if (stream.eol()) state.inTag += " "
Expand Down