Skip to content

Commit 2166fb8

Browse files
committed
Implement a basic specificity calculator
1 parent 9bb0e6e commit 2166fb8

File tree

2 files changed

+100
-5
lines changed

2 files changed

+100
-5
lines changed

src/App.vue

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
<template>
22
<div id="app">
3-
<img src="./assets/logo.png">
4-
<hello></hello>
3+
<img src="./assets/logo.png" class="logo">
4+
<selector-form></selector-form>
55
</div>
66
</template>
77

88
<script>
9-
import Hello from './components/Hello';
9+
import SelectorForm from './components/SelectorForm';
1010
1111
export default {
1212
name: 'app',
1313
components: {
14-
Hello,
14+
SelectorForm,
1515
},
1616
};
1717
</script>
@@ -23,6 +23,10 @@ export default {
2323
-moz-osx-font-smoothing: grayscale;
2424
text-align: center;
2525
color: #2c3e50;
26-
margin-top: 60px;
26+
margin-top: 40px;
27+
}
28+
29+
.logo {
30+
height: 120px;
2731
}
2832
</style>

src/components/SelectorForm.vue

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<template>
2+
<div class="hello">
3+
<h1>CSSpecify</h1>
4+
<input class="selector-input" type="text" v-model="selectorString"/>
5+
<ul>
6+
<li v-for="token in tokens">{{token}}</li>
7+
</ul>
8+
<h2>specificity: {{ specificity }}</h2>
9+
</div>
10+
</template>
11+
12+
<script>
13+
export default {
14+
name: 'hello',
15+
data() {
16+
return {
17+
selectorString: 'ul#bacon.foo li.bar p #crispy',
18+
idMatcher: /#[A-z0-9-_]+/g,
19+
classMatcher: /\.[A-z0-9-_]+/g,
20+
tagMatcher: /(?:^| )\w+/g,
21+
};
22+
},
23+
methods: {
24+
extractTokens(regex) {
25+
const match = this.selectorString.match(regex);
26+
if (!match) {
27+
return [];
28+
}
29+
return match;
30+
},
31+
},
32+
computed: {
33+
idTokens() {
34+
return this.extractTokens(this.idMatcher);
35+
},
36+
classTokens() {
37+
return this.extractTokens(this.classMatcher);
38+
},
39+
tagTokens() {
40+
return this.extractTokens(this.tagMatcher);
41+
},
42+
tokens() {
43+
return this.idTokens.concat(this.classTokens).concat(this.tagTokens);
44+
},
45+
specificity() {
46+
let result = '';
47+
result += this.idTokens.length.toString();
48+
result += this.classTokens.length.toString();
49+
result += this.tagTokens.length.toString();
50+
return result;
51+
},
52+
},
53+
};
54+
</script>
55+
56+
<!-- Add "scoped" attribute to limit CSS to this component only -->
57+
<style scoped>
58+
h1, h2 {
59+
font-weight: normal;
60+
}
61+
62+
h1 {
63+
font-size: 100px;
64+
margin: 20px 0 60px;
65+
}
66+
67+
.selector-input {
68+
display: block;
69+
margin: 0 auto;
70+
height: 60px;
71+
width: 600px;
72+
font-size: 40px;
73+
font-weight: lighter;
74+
padding: 10px 20px;
75+
text-align: center;
76+
}
77+
78+
a {
79+
color: #42b983;
80+
}
81+
82+
ul {
83+
list-style-type: none;
84+
padding: 0;
85+
}
86+
87+
li {
88+
display: inline-block;
89+
margin: 0 10px;
90+
}
91+
</style>

0 commit comments

Comments
 (0)