fix php highlighting inside html attributes

This commit is contained in:
nightwing 2013-02-10 00:58:30 +04:00
commit c83f48b371
3 changed files with 66 additions and 17 deletions

View file

@ -1058,8 +1058,8 @@ var PhpHighlightRules = function() {
for (var i in this.$rules) { for (var i in this.$rules) {
this.$rules[i].unshift({ this.$rules[i].unshift({
token : "support.php_tag", // php open tag token : "support.php_tag", // php open tag
regex : "<\\?(?:php|\\=)?", regex : "<\\?(?:php|=)?",
next : "php-start" next : {push: "php-start"}
}); });
} }
@ -1068,8 +1068,9 @@ var PhpHighlightRules = function() {
this.$rules["php-start"].unshift({ this.$rules["php-start"].unshift({
token : "support.php_tag", // php close tag token : "support.php_tag", // php close tag
regex : "\\?>", regex : "\\?>",
next : "start" next : "pop"
}); });
this.normalizeRules();
}; };
oop.inherits(PhpHighlightRules, HtmlHighlightRules); oop.inherits(PhpHighlightRules, HtmlHighlightRules);

View file

@ -43,8 +43,7 @@ var TextHighlightRules = function() {
token : "empty_line", token : "empty_line",
regex : '^$' regex : '^$'
}, { }, {
token : "text", defaultToken : "text"
regex : ".+"
}] }]
}; };
}; };
@ -96,35 +95,80 @@ var TextHighlightRules = function() {
return this.$embeds; return this.$embeds;
}; };
var pushState = function(currentState, stack) {
if (currentState != "start")
stack.unshift(this.nextState, currentState);
return this.nextState;
};
var popState = function(currentState, stack) {
if (stack[0] !== currentState)
return "start";
stack.shift();
return stack.shift();
};
this.normalizeRules = function() { this.normalizeRules = function() {
var id = 0; var id = 0;
for (var key in this.$rules) { var rules = this.$rules;
var state = this.$rules[key]; function processState(key) {
var state = rules[key];
state.processed = true;
for (var i = 0; i < state.length; i++) { for (var i = 0; i < state.length; i++) {
var rule = state[i]; var rule = state[i];
if (!rule.regex && rule.start) {
rule.regex = rule.start;
if (!rule.next)
rule.next = [];
rule.next.push({
defaultToken: rule.token
}, {
token: rule.token + ".end",
regex: rule.end || rule.start,
next: key
});
rule.token = rule.token + ".start";
}
if (rule.next && Array.isArray(rule.next)) { if (rule.next && Array.isArray(rule.next)) {
var stateName = rule.stateName || ("state" + id++); var stateName = rule.stateName || (rule.token + id++);
this.$rules[stateName] = rule.next; rules[stateName] = rule.next;
rule.next = stateName; rule.next = stateName;
processState(stateName);
} else if (rule.next && typeof rule.next == "object") {
var next = rule.next;
rule.nextState = next.push;
rule.next = pushState;
} else if (rule.next == "pop") {
rule.next = popState;
} }
if (rule.rules) { if (rule.rules) {
for (var r in rule.rules) { for (var r in rule.rules) {
if (this.$rules[r]) { if (rules[r]) {
if (this.$rules[r].push) if (rules[r].push)
this.$rules[r].push.apply(this.$rules[r], rule.rules[r]); rules[r].push.apply(rules[r], rule.rules[r]);
} else { } else {
this.$rules[r] = rule.rules[r]; rules[r] = rule.rules[r];
} }
} }
} }
if (rule.include || typeof rule == "string") { if (rule.include || typeof rule == "string") {
var args = [i, 1].concat(this.$rules[rule.include || rule]); var includeName = rule.include || rule;
var toInsert = rules[includeName];
} else if (Array.isArray(rule))
toInsert = rule;
if (toInsert) {
var args = [i, 1].concat(toInsert);
if (rule.noEscape) if (rule.noEscape)
args = args.filter(function(x) {return !x.next;}); args = args.filter(function(x) {return !x.next;});
state.splice.apply(state, args); state.splice.apply(state, args);
// skip included rules since they are already processed
//i += args.length - 3;
i--;
toInsert = null
} }
} }
} };
Object.keys(rules).forEach(processState);
}; };
this.createKeywordMapper = function(map, defaultToken, ignoreCase, splitChar) { this.createKeywordMapper = function(map, defaultToken, ignoreCase, splitChar) {

View file

@ -194,7 +194,11 @@ var Tokenizer = function(rules) {
: rule.token; : rule.token;
if (rule.next) { if (rule.next) {
currentState = rule.next; if (typeof rule.next == "string")
currentState = rule.next;
else
currentState = rule.next(currentState, stack);
state = this.states[currentState]; state = this.states[currentState];
if (!state) { if (!state) {
window.console && console.error && console.error(currentState, "doesn't exist"); window.console && console.error && console.error(currentState, "doesn't exist");