Support highlighting of separate parts of the selector

This commit is contained in:
MrOtherGuy 2020-09-13 22:01:57 +03:00
parent 043ae65c7c
commit f500bcf685
2 changed files with 72 additions and 25 deletions

View file

@ -84,8 +84,12 @@ pre{
.comment{ color: rgb(50,180,20) } .comment{ color: rgb(50,180,20) }
.selector{ color: violet } .selector{ color: violet }
.pseudo{ color: silver }
.id{ color: lightcoral }
.class{ color: lightgreen }
.attribute{ color: cyan }
.atrule{ color: royalblue } .atrule{ color: royalblue }
.atvalue{ color: greenyellow } .atvalue{ color: lightblue }
.property{ color: orange } .property{ color: orange }
.value{ color: skyblue } .value{ color: skyblue }
.curly{ color: orangered } .curly{ color: orangered }

View file

@ -248,21 +248,62 @@ const Highlighter = new(function(){
this.previous = ()=>previous; this.previous = ()=>previous;
this.set = function(a){ previous = current; current = a; return} this.set = function(a){ previous = current; current = a; return}
})(); })();
let pointer = 0; let pointer = 0;
let token = ""; let token = "";
function createToken(type,c){ const selectorToClassMap = new Map([
[":","pseudo"],
["#","id"],
[".","class"],
["[","attribute"]]);
this.parse = function(targetNode,text){
clearCodeBlock();
let node = document.createElement("div");
function createElementFromToken(type,c){
if(token.length === 0 && !c){
return
}
let n = document.createElement("span"); let n = document.createElement("span");
n.textContent = c || token;
n.className = (`token ${type}`);
token = ""; if(type==="selector"){
return n
let parts = token.split(/([\.#:\[]\w[\w-_"'=\]]*|\s\w[\w-_"'=\]]*)/);
for(let part of parts){
if(part.length === 0){
continue
}
let c = part[0];
switch (c){
case ":":
case "#":
case "[":
case ".":
let p = n.appendChild(document.createElement("span"));
p.className = selectorToClassMap.get(c);
p.textContent = part;
break;
default:
n.append(part);
}
} }
this.parse = function(node,text){
for(let e of Array.from(node.childNodes)){ }else{
node.removeChild(e) n.textContent = c || token;
} }
n.className = (`token ${type}`);
token = "";
node.appendChild(n);
return
}
let c; let c;
let curly = false; let curly = false;
while(pointer < text.length){ while(pointer < text.length){
@ -281,17 +322,17 @@ const Highlighter = new(function(){
state.set(2); state.set(2);
if(token.length > 1){ if(token.length > 1){
token = token.slice(0,-1); token = token.slice(0,-1);
node.appendChild(createToken("selector")); createElementFromToken("selector");
token += "/" token += "/"
} }
} }
break; break;
case "{": case "{":
state.set(3); state.set(3);
node.appendChild(createToken("selector")); createElementFromToken("selector");
break; break;
case "}": case "}":
node.appendChild(createToken("text")); createElementFromToken("text");
break; break;
case "@": case "@":
state.set(5); state.set(5);
@ -306,7 +347,7 @@ const Highlighter = new(function(){
token += "/"; token += "/";
pointer++; pointer++;
state.set(state.previous()); state.set(state.previous());
node.appendChild(createToken("comment")); createElementFromToken("comment");
} }
} }
break; break;
@ -319,29 +360,29 @@ const Highlighter = new(function(){
} }
break; break;
case ":": case ":":
node.appendChild(createToken("property")); createElementFromToken("property");
state.set(4); state.set(4);
break; break;
case "}": case "}":
node.appendChild(createToken("text")); createElementFromToken("text");
state.set(0); state.set(0);
} }
break; break;
case 4: case 4:
switch(c){ switch(c){
case ";": case ";":
node.appendChild(createToken("value")); createElementFromToken("value");
state.set(3); state.set(3);
break; break;
case "}": case "}":
node.appendChild(createToken("value")); createElementFromToken("value");
state.set(0); state.set(0);
} }
break; break;
case 5: case 5:
switch(c){ switch(c){
case " ": case " ":
node.appendChild(createToken("atrule")); createElementFromToken("atrule");
state.set(6); state.set(6);
} }
break; break;
@ -349,7 +390,7 @@ const Highlighter = new(function(){
switch(c){ switch(c){
case ";": case ";":
case "{": case "{":
node.appendChild(createToken("atvalue")); createElementFromToken("atvalue");
state.set(0); state.set(0);
} }
break break
@ -357,16 +398,18 @@ const Highlighter = new(function(){
false false
} }
curly && node.appendChild(createToken("curly",c)); curly && createElementFromToken("curly",c);
pointer++ pointer++
} }
node.appendChild(createToken("text")); createElementFromToken("text");
token = ""; token = "";
state.set(0); state.set(0);
pointer = 0; pointer = 0;
targetNode.appendChild(node);
return return
} }
return this return this