Html HL rules now is extended xml rules

This commit is contained in:
DanyaPostfactum 2013-06-24 20:56:33 +10:00
commit 7c6f40e41e
2 changed files with 59 additions and 80 deletions

View file

@ -38,14 +38,9 @@ var TokenIterator = require("../../token_iterator").TokenIterator;
var voidElements = ['area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr']; var voidElements = ['area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr'];
function hasType(token, type) { function hasType(token, type) {
var hasType = true; var tokenTypes = token.type.split('.');
var typeList = token.type.split('.'); return type.split('.').every(function(type){
var needleList = type.split('.'); return (tokenTypes.indexOf(type) !== -1);
needleList.forEach(function(needle){
if (typeList.indexOf(needle) == -1) {
hasType = false;
return false;
}
}); });
return hasType; return hasType;
} }
@ -60,7 +55,7 @@ var HtmlBehaviour = function () {
var iterator = new TokenIterator(session, position.row, position.column); var iterator = new TokenIterator(session, position.row, position.column);
var token = iterator.getCurrentToken(); var token = iterator.getCurrentToken();
if (hasType(token, 'string') && iterator.getCurrentTokenColumn() + token.value.length > position.column) if (token && hasType(token, 'string') && iterator.getCurrentTokenColumn() + token.value.length > position.column)
return; return;
var atCursor = false; var atCursor = false;
if (!token || !hasType(token, 'meta.tag') && !(hasType(token, 'text') && token.value.match('/'))){ if (!token || !hasType(token, 'meta.tag') && !(hasType(token, 'text') && token.value.match('/'))){
@ -70,8 +65,8 @@ var HtmlBehaviour = function () {
} else { } else {
atCursor = true; atCursor = true;
} }
if (!token || !hasType(token, 'meta.tag-name') || iterator.stepBackward().value.match('/')) { if (!token || !hasType(token, 'meta.tag.name') || iterator.stepBackward().value.match('/')) {
return return;
} }
var element = token.value; var element = token.value;
if (atCursor){ if (atCursor){

View file

@ -35,8 +35,7 @@ var oop = require("../lib/oop");
var lang = require("../lib/lang"); var lang = require("../lib/lang");
var CssHighlightRules = require("./css_highlight_rules").CssHighlightRules; var CssHighlightRules = require("./css_highlight_rules").CssHighlightRules;
var JavaScriptHighlightRules = require("./javascript_highlight_rules").JavaScriptHighlightRules; var JavaScriptHighlightRules = require("./javascript_highlight_rules").JavaScriptHighlightRules;
var xmlUtil = require("./xml_util"); var XmlHighlightRules = require("./xml_highlight_rules").XmlHighlightRules;
var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;
var tagMap = lang.createMap({ var tagMap = lang.createMap({
a : 'anchor', a : 'anchor',
@ -58,81 +57,66 @@ var tagMap = lang.createMap({
}); });
var HtmlHighlightRules = function() { var HtmlHighlightRules = function() {
XmlHighlightRules.call(this);
// regexp must not have capturing parentheses this.addRules({
// regexps are ordered -> the first match is used attributes: [{
this.$rules = { include : "space"
start : [{
token : "text",
regex : "<\\!\\[CDATA\\[",
next : "cdata"
}, { }, {
token : "xml-pe", token : "entity.other.attribute-name",
regex : "<\\?.*?\\?>" regex : "[-_a-zA-Z0-9:]+"
}, { }, {
token : "comment", token : "keyword.operator.separator",
regex : "<\\!--", regex : "=",
next : "comment" push : [{
include: "space",
}, {
token : "string",
regex : "[^<>='\"`\\s]+",
next : "pop"
}, {
token : "empty",
regex : "",
next : "pop"
}]
}, { }, {
token : "xml-pe", include : "string"
regex : "<\\!.*?>"
}, {
token : "meta.tag",
regex : "<(?=script\\b)",
next : "script"
}, {
token : "meta.tag",
regex : "<(?=style\\b)",
next : "style"
}, {
token : "meta.tag", // opening tag
regex : "<\\/?(?=\\S)",
next : "tag"
}, {
token : "text",
regex : "\\s+"
}, {
token : "constant.character.entity",
regex : "(?:&#[0-9]+;)|(?:&#x[0-9a-fA-F]+;)|(?:&[a-zA-Z0-9_:\\.-]+;)"
}], }],
tag: [{
cdata : [ { token : function(start, tag) {
token : "text", var group = tagMap[tag];
regex : "\\]\\]>", return ["punctuation.meta.tag.begin",
next : "start" "meta.tag.name" + (group ? "." + group : "")];
} ], },
regex : "(<)([-_a-zA-Z0-9:]+)",
comment : [ { next: "start_tag_stuff"
token : "comment",
regex : ".*?-->",
next : "start"
}, { }, {
defaultToken : "comment" token : function(start, tag) {
} ] var group = tagMap[tag];
}; return ["punctuation.meta.tag.begin",
"meta.tag.name" + (group ? "." + group : "")];
xmlUtil.tag(this.$rules, "tag", "start", tagMap); },
xmlUtil.tag(this.$rules, "style", "css-start", tagMap); regex : "(</)([-_a-zA-Z0-9:]+)",
xmlUtil.tag(this.$rules, "script", "js-start", tagMap); next: "end_tag_stuff"
}],
this.embedRules(JavaScriptHighlightRules, "js-", [{ start_tag_stuff: [
token: "comment", {include : "attributes"},
regex: "\\/\\/.*(?=<\\/script>)", {token : "punctuation.meta.tag.end", regex : "/?>", next : "start"}
next: "tag" ],
}, { end_tag_stuff: [
token: "meta.tag", {include : "space"},
regex: "<\\/(?=script)", {token : "punctuation.meta.tag.end", regex : ">", next : "start"}
next: "tag" ]
}]); });
this.embedRules(CssHighlightRules, "css-", [{ this.embedTagRules(CssHighlightRules, "css-", "style");
token: "meta.tag", this.embedTagRules(JavaScriptHighlightRules, "js-", "script");
regex: "<\\/(?=style)",
next: "tag" if (this.constructor === HtmlHighlightRules)
}]); this.normalizeRules();
}; };
oop.inherits(HtmlHighlightRules, TextHighlightRules); oop.inherits(HtmlHighlightRules, XmlHighlightRules);
exports.HtmlHighlightRules = HtmlHighlightRules; exports.HtmlHighlightRules = HtmlHighlightRules;
}); });