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

Show specificity in a css selector hover. #129

Merged
merged 7 commits into from
Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"xml2js": "^0.4.19"
},
"dependencies": {
"specificity": "^0.4.1",
"vscode-languageserver-types": "^3.13.0",
"vscode-nls": "^4.0.0"
},
Expand Down
50 changes: 42 additions & 8 deletions src/services/selectorPrinting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import * as nodes from '../parser/cssNodes';
import { MarkedString } from 'vscode-languageserver-types';
import { Scanner } from '../parser/cssScanner';
import { calculate } from 'specificity';

export class Element {

Expand Down Expand Up @@ -311,24 +310,59 @@ function unescape(content: string) {
return content;
}

function specificityMarkedString(node: nodes.Node): MarkedString {
let specificityCalculation = calculate(node.getText())[0];
let specificity = specificityCalculation["specificityArray"].slice(1); //remove first score (inline css)
let value = "Specificity: " + specificity.join(",");
return { language: 'text', value };
function selectorToSpecifityMarkedString(node: nodes.Node): MarkedString {
//https://www.w3.org/TR/selectors-3/#specificity
function calculateScore(node: nodes.Node) {
node.getChildren().forEach(element => {
switch(element.type) {
case nodes.NodeType.IdentifierSelector:
specificity[0] += 1; //a
break;
case nodes.NodeType.ClassSelector:
case nodes.NodeType.AttributeSelector:
specificity[1] += 1; //b
break;
case nodes.NodeType.ElementNameSelector:
//ignore universal selector
if (element.getText() === "*") {
break;
}
specificity[2] += 1; //c
break;
case nodes.NodeType.PseudoSelector:
if (element.getText().match(/^::/)) {
specificity[2] += 1; //c (speudo element)
Ragnoroct marked this conversation as resolved.
Show resolved Hide resolved
} else {
//ignore psuedo class NOT
if (element.getText().match(/^:not/i)) {
break;
}
specificity[1] += 1; //b (speudo class)
Ragnoroct marked this conversation as resolved.
Show resolved Hide resolved
}
break;
}
if (element.getChildren().length > 0) {
calculateScore(element);
}
});
}

let specificity = [0, 0, 0]; //a,b,c
calculateScore(node);
return { language: 'text', value: "Specificity: " + specificity.join(",") };
}

export function selectorToMarkedString(node: nodes.Selector): MarkedString[] {
let root = selectorToElement(node);
let markedStrings = new MarkedStringPrinter('"').print(root);
markedStrings.push(specificityMarkedString(node));
markedStrings.push(selectorToSpecifityMarkedString(node));
return markedStrings;
}

export function simpleSelectorToMarkedString(node: nodes.SimpleSelector): MarkedString[] {
let element = toElement(node);
let markedStrings = new MarkedStringPrinter('"').print(element);
markedStrings.push(specificityMarkedString(node));
markedStrings.push(selectorToSpecifityMarkedString(node));
return markedStrings;
}

Expand Down