Skip to content

Commit

Permalink
use base36 for style classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Feb 25, 2018
1 parent cbd1a11 commit 1ad28e6
Show file tree
Hide file tree
Showing 91 changed files with 115 additions and 115 deletions.
4 changes: 2 additions & 2 deletions src/css/Stylesheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ export default class Stylesheet {
children: (Rule|Atrule)[];
keyframes: Map<string, string>;

constructor(source: string, parsed: Parsed, filename: string, cascade: boolean, dev: boolean) {
constructor(source: string, parsed: Parsed, name: string, filename: string, cascade: boolean, dev: boolean) {
this.source = source;
this.parsed = parsed;
this.cascade = cascade;
Expand All @@ -277,7 +277,7 @@ export default class Stylesheet {
this.keyframes = new Map();

if (parsed.css && parsed.css.children.length) {
this.id = `svelte-${hash(parsed.css.content.styles)}`;
this.id = `${name ? name.toLowerCase() : 'svelte'}-${hash(parsed.css.content.styles)}`;

this.hasStyles = true;

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export function compile(source: string, _options: CompileOptions) {
return;
}

const stylesheet = new Stylesheet(source, parsed, options.filename, options.cascade !== false, options.dev);
const stylesheet = new Stylesheet(source, parsed, options.name, options.filename, options.cascade !== false, options.dev);

validate(parsed, source, stylesheet, options);

Expand Down
4 changes: 2 additions & 2 deletions src/utils/hash.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// https://github.com/darkskyapp/string-hash/blob/master/index.js
export default function hash(str: string): number {
export default function hash(str: string): string {
let hash = 5381;
let i = str.length;

while (i--) hash = ((hash << 5) - hash) ^ str.charCodeAt(i);
return hash >>> 0;
return (hash >>> 0).toString(36);
}
6 changes: 3 additions & 3 deletions test/css/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ describe('css', () => {
css: read(`test/css/samples/${dir}/expected.css`)
};

assert.equal(dom.css.replace(/svelte-\d+/g, 'svelte-xyz'), expected.css);
assert.equal(dom.css.replace(/sveltecomponent-[a-z0-9]+/g, 'sveltecomponent-xyz'), expected.css);

// verify that the right elements have scoping selectors
if (expected.html !== null) {
Expand All @@ -114,7 +114,7 @@ describe('css', () => {
fs.writeFileSync(`test/css/samples/${dir}/_actual.html`, html);

assert.equal(
normalizeHtml(window, html.replace(/svelte-\d+/g, 'svelte-xyz')),
normalizeHtml(window, html.replace(/sveltecomponent-[a-z0-9]+/g, 'sveltecomponent-xyz')),
normalizeHtml(window, expected.html)
);

Expand All @@ -133,7 +133,7 @@ describe('css', () => {
assert.equal(
normalizeHtml(
window,
component.render(config.data).html.replace(/svelte-\d+/g, 'svelte-xyz')
component.render(config.data).html.replace(/sveltecomponent-[a-z0-9]+/g, 'sveltecomponent-xyz')
),
normalizeHtml(window, expected.html)
);
Expand Down
2 changes: 1 addition & 1 deletion test/css/samples/attribute-selector-only-name/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[foo].svelte-xyz{color:red}[baz].svelte-xyz{color:blue}
[foo].sveltecomponent-xyz{color:red}[baz].sveltecomponent-xyz{color:blue}
2 changes: 1 addition & 1 deletion test/css/samples/attribute-selector-unquoted/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[foo=bar].svelte-xyz{color:red}
[foo=bar].sveltecomponent-xyz{color:red}
2 changes: 1 addition & 1 deletion test/css/samples/basic/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
div.svelte-xyz,.svelte-xyz div{color:red}
div.sveltecomponent-xyz,.sveltecomponent-xyz div{color:red}
2 changes: 1 addition & 1 deletion test/css/samples/cascade-false-empty-rule-dev/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.foo.svelte-xyz{}
.foo.sveltecomponent-xyz{}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/css/samples/cascade-false-keyframes/expected.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/css/samples/cascade-false-pseudo-element/expected.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
.svelte-xyz{color:red}
.sveltecomponent-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<div class="svelte-xyz"></div>
<div class="sveltecomponent-xyz"></div>
2 changes: 1 addition & 1 deletion test/css/samples/cascade-false/expected.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/css/samples/combinator-child/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.test.svelte-xyz>div.svelte-xyz{color:#0af}
.test.sveltecomponent-xyz>div.sveltecomponent-xyz{color:#0af}
2 changes: 1 addition & 1 deletion test/css/samples/combinator-child/expected.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<div class="test svelte-xyz"><div class="svelte-xyz">Testing...</div></div>
<div class="test sveltecomponent-xyz"><div class="sveltecomponent-xyz">Testing...</div></div>
2 changes: 1 addition & 1 deletion test/css/samples/css-vars/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
div.svelte-xyz,.svelte-xyz div{--test:10}
div.sveltecomponent-xyz,.sveltecomponent-xyz div{--test:10}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
p.svelte-xyz span.svelte-xyz{color:red}
p.sveltecomponent-xyz span.sveltecomponent-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<div><p class="svelte-xyz"><span class="svelte-xyz">styled</span></p></div>
<div><p class="sveltecomponent-xyz"><span class="sveltecomponent-xyz">styled</span></p></div>
2 changes: 1 addition & 1 deletion test/css/samples/keyframes/expected.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/css/samples/media-query-word/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
@media only screen and (min-width: 400px){div.svelte-xyz,.svelte-xyz div{color:red}}
@media only screen and (min-width: 400px){div.sveltecomponent-xyz,.sveltecomponent-xyz div{color:red}}
2 changes: 1 addition & 1 deletion test/css/samples/media-query/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
@media(min-width: 400px){.svelte-xyz.large-screen,.svelte-xyz .large-screen{display:block}}
@media(min-width: 400px){.sveltecomponent-xyz.large-screen,.sveltecomponent-xyz .large-screen{display:block}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[data-foo*='bar'].svelte-xyz{color:red}
[data-foo*='bar'].sveltecomponent-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<div><p class="svelte-xyz" data-foo="foobarbaz">this is styled</p>
<div><p class="sveltecomponent-xyz" data-foo="foobarbaz">this is styled</p>
<p data-foo="fooBARbaz">this is unstyled</p></div>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[data-foo='bar' i].svelte-xyz{color:red}
[data-foo='bar' i].sveltecomponent-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<div><p class="svelte-xyz" data-foo="BAR">this is styled</p>
<div><p class="sveltecomponent-xyz" data-foo="BAR">this is styled</p>
<p data-foo="BAZ">this is unstyled</p></div>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[data-foo='bar'].svelte-xyz{color:red}
[data-foo='bar'].sveltecomponent-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<div><p class="svelte-xyz" data-foo="whatever">this is styled</p>
<div><p class="sveltecomponent-xyz" data-foo="whatever">this is styled</p>
<p data-foo="baz">this is unstyled</p></div>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[data-foo='bar'].svelte-xyz{color:red}
[data-foo='bar'].sveltecomponent-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<div><p class="svelte-xyz" data-foo="bar">this is styled</p>
<div><p class="sveltecomponent-xyz" data-foo="bar">this is styled</p>
<p data-foo="baz">this is unstyled</p></div>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[data-foo|='bar'].svelte-xyz{color:red}
[data-foo|='bar'].sveltecomponent-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<div><p class="svelte-xyz" data-foo="bar">this is styled</p>
<p class="svelte-xyz" data-foo="bar-baz">this is styled</p>
<div><p class="sveltecomponent-xyz" data-foo="bar">this is styled</p>
<p class="sveltecomponent-xyz" data-foo="bar-baz">this is styled</p>
<p data-foo="baz-bar">this is unstyled</p></div>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[data-foo^='bar'].svelte-xyz{color:red}
[data-foo^='bar'].sveltecomponent-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<div><p class="svelte-xyz" data-foo="barbaz">this is styled</p>
<div><p class="sveltecomponent-xyz" data-foo="barbaz">this is styled</p>
<p data-foo="bazbar">this is unstyled</p></div>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[data-foo$='bar'].svelte-xyz{color:red}
[data-foo$='bar'].sveltecomponent-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<div><p data-foo="barbaz">this is unstyled</p>
<p class="svelte-xyz" data-foo="bazbar">this is styled</p></div>
<p class="sveltecomponent-xyz" data-foo="bazbar">this is styled</p></div>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[data-foo~='bar'].svelte-xyz{color:red}
[data-foo~='bar'].sveltecomponent-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<div><p class="svelte-xyz" data-foo="qux bar">this is styled</p>
<div><p class="sveltecomponent-xyz" data-foo="qux bar">this is styled</p>
<p data-foo="qux baz">this is unstyled</p></div>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[autoplay].svelte-xyz{color:red}
[autoplay].sveltecomponent-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<div><video class="svelte-xyz" autoplay></video>
<div><video class="sveltecomponent-xyz" autoplay></video>
<video></video></div>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.foo.svelte-xyz{color:red}
.foo.sveltecomponent-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<p class="whatever svelte-xyz">this is styled</p>
<p class="whatever sveltecomponent-xyz">this is styled</p>
<p class="bar">this is unstyled</p>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.foo.svelte-xyz{color:red}
.foo.sveltecomponent-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<p class="foo svelte-xyz">this is styled</p>
<p class="foo sveltecomponent-xyz">this is styled</p>
<p class="bar">this is unstyled</p>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.foo.svelte-xyz .bar{color:red}
.foo.sveltecomponent-xyz .bar{color:red}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<div class="foo svelte-xyz"></div>
<div class="foo sveltecomponent-xyz"></div>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
div.svelte-xyz>p>em{color:red}
div.sveltecomponent-xyz>p>em{color:red}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<div class="svelte-xyz"></div>
<div class="sveltecomponent-xyz"></div>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
div.svelte-xyz>p{color:red}
div.sveltecomponent-xyz>p{color:red}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<div class="svelte-xyz"></div>
<div class="sveltecomponent-xyz"></div>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
div>section>p.svelte-xyz{color:red}
div>section>p.sveltecomponent-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<p class="svelte-xyz">this may or may not be styled</p>
<p class="sveltecomponent-xyz">this may or may not be styled</p>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
div>p.svelte-xyz{color:red}
div>p.sveltecomponent-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<p class="svelte-xyz">this may or may not be styled</p>
<p class="sveltecomponent-xyz">this may or may not be styled</p>
2 changes: 1 addition & 1 deletion test/css/samples/omit-scoping-attribute-id/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#foo.svelte-xyz{color:red}
#foo.sveltecomponent-xyz{color:red}
2 changes: 1 addition & 1 deletion test/css/samples/omit-scoping-attribute-id/expected.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<div class="svelte-xyz" id="foo"></div>
<div class="sveltecomponent-xyz" id="foo"></div>
<div id="bar"></div>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
div.svelte-xyz section p.svelte-xyz{color:red}
div.sveltecomponent-xyz section p.sveltecomponent-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<div class="svelte-xyz"><section><p class="svelte-xyz">this is styled</p></section></div>
<div class="sveltecomponent-xyz"><section><p class="sveltecomponent-xyz">this is styled</p></section></div>
Original file line number Diff line number Diff line change
@@ -1 +1 @@
div.svelte-xyz p.svelte-xyz{color:red}
div.sveltecomponent-xyz p.sveltecomponent-xyz{color:red}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<div class="svelte-xyz"><section><p class="svelte-xyz">this is styled</p></section></div>
<div class="sveltecomponent-xyz"><section><p class="sveltecomponent-xyz">this is styled</p></section></div>
2 changes: 1 addition & 1 deletion test/css/samples/omit-scoping-attribute/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
p.svelte-xyz{color:red}
p.sveltecomponent-xyz{color:red}
2 changes: 1 addition & 1 deletion test/css/samples/omit-scoping-attribute/expected.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<div><p class="svelte-xyz">this is styled</p></div>
<div><p class="sveltecomponent-xyz">this is styled</p></div>
2 changes: 1 addition & 1 deletion test/css/samples/refs-qualified/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[svelte-ref-button].active.svelte-xyz{color:red}
[svelte-ref-button].active.sveltecomponent-xyz{color:red}
2 changes: 1 addition & 1 deletion test/css/samples/refs-qualified/expected.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<button svelte-ref-button="" class="active svelte-xyz">deactivate</button>
<button svelte-ref-button="" class="active sveltecomponent-xyz">deactivate</button>
2 changes: 1 addition & 1 deletion test/css/samples/refs/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
[svelte-ref-a].svelte-xyz{color:red}[svelte-ref-b].svelte-xyz{color:green}
[svelte-ref-a].sveltecomponent-xyz{color:red}[svelte-ref-b].sveltecomponent-xyz{color:green}
4 changes: 2 additions & 2 deletions test/css/samples/refs/expected.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<div class="svelte-xyz" svelte-ref-a=''></div>
<div class="svelte-xyz" svelte-ref-b=''></div>
<div class="sveltecomponent-xyz" svelte-ref-a=''></div>
<div class="sveltecomponent-xyz" svelte-ref-b=''></div>
<div></div>
2 changes: 1 addition & 1 deletion test/css/samples/universal-selector/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.svelte-xyz,.svelte-xyz *{color:red}
.sveltecomponent-xyz,.sveltecomponent-xyz *{color:red}
2 changes: 1 addition & 1 deletion test/css/samples/unknown-at-rule/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
div.svelte-xyz,.svelte-xyz div{@apply --funky-div;}
div.sveltecomponent-xyz,.sveltecomponent-xyz div{@apply --funky-div;}
2 changes: 1 addition & 1 deletion test/css/samples/unused-selector-leading/expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.bar.svelte-xyz{color:red}
.bar.sveltecomponent-xyz{color:red}
Loading

0 comments on commit 1ad28e6

Please sign in to comment.