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

Base stylesheet.id on CSS contents #1119

Merged
merged 4 commits into from
Jan 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions src/css/Stylesheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { walk } from 'estree-walker';
import { getLocator } from 'locate-character';
import Selector from './Selector';
import getCodeFrame from '../utils/getCodeFrame';
import hash from '../utils/hash';
import Element from '../generators/nodes/Element';
import { Validator } from '../validate/index';
import { Node, Parsed, Warning } from '../interfaces';
Expand Down Expand Up @@ -269,12 +270,12 @@ export default class Stylesheet {
this.cascade = cascade;
this.filename = filename;

this.id = `svelte-${parsed.hash}`;

this.children = [];
this.keyframes = new Map();

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

this.hasStyles = true;

const stack: (Rule | Atrule)[] = [];
Expand Down
3 changes: 1 addition & 2 deletions src/parse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { trimStart, trimEnd } from '../utils/trim';
import getCodeFrame from '../utils/getCodeFrame';
import reservedNames from '../utils/reservedNames';
import fullCharCodeAt from '../utils/fullCharCodeAt';
import hash from './utils/hash';
import hash from '../utils/hash';
import { Node, Parsed } from '../interfaces';
import CompileError from '../utils/CompileError';

Expand Down Expand Up @@ -207,7 +207,6 @@ export default function parse(
options: ParserOptions = {}
): Parsed {
const parser = new Parser(template, options);

return {
hash: hash(parser.template),
html: parser.html,
Expand Down
File renamed without changes.
2 changes: 2 additions & 0 deletions test/css/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ describe('css', () => {
normalizeHtml(window, html.replace(/svelte-\d+/g, 'svelte-xyz')),
normalizeHtml(window, expected.html)
);

window.document.head.innerHTML = ''; // remove added styles
} catch (err) {
console.log(dom.code);
throw err;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,13 @@ function data() {
}

function encapsulateStyles(node) {
setAttribute(node, "svelte-3590263702", "");
setAttribute(node, "svelte-2794052100", "");
}

function add_css() {
var style = createElement("style");
style.id = 'svelte-3590263702-style';
style.textContent = "p[svelte-3590263702],[svelte-3590263702] p{color:red}";
style.id = 'svelte-2794052100-style';
style.textContent = "p[svelte-2794052100],[svelte-2794052100] p{color:red}";
appendNode(style, document.head);
}

Expand Down Expand Up @@ -245,7 +245,7 @@ function SvelteComponent(options) {
init(this, options);
this._state = assign(data(), options.data);

if (!document.getElementById("svelte-3590263702-style")) add_css();
if (!document.getElementById("svelte-2794052100-style")) add_css();

this._fragment = create_main_fragment(this._state, this);

Expand Down
10 changes: 5 additions & 5 deletions test/js/samples/collapses-text-around-comments/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ function data() {
};

function encapsulateStyles(node) {
setAttribute(node, "svelte-3590263702", "");
setAttribute(node, "svelte-2794052100", "");
}

function add_css() {
var style = createElement("style");
style.id = 'svelte-3590263702-style';
style.textContent = "p[svelte-3590263702],[svelte-3590263702] p{color:red}";
style.id = 'svelte-2794052100-style';
style.textContent = "p[svelte-2794052100],[svelte-2794052100] p{color:red}";
appendNode(style, document.head);
}

Expand Down Expand Up @@ -53,7 +53,7 @@ function SvelteComponent(options) {
init(this, options);
this._state = assign(data(), options.data);

if (!document.getElementById("svelte-3590263702-style")) add_css();
if (!document.getElementById("svelte-2794052100-style")) add_css();

this._fragment = create_main_fragment(this._state, this);

Expand All @@ -64,4 +64,4 @@ function SvelteComponent(options) {
}

assign(SvelteComponent.prototype, proto);
export default SvelteComponent;
export default SvelteComponent;
8 changes: 4 additions & 4 deletions test/js/samples/css-media-query/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,13 @@ var proto = {

/* generated by Svelte vX.Y.Z */
function encapsulateStyles(node) {
setAttribute(node, "svelte-2363328337", "");
setAttribute(node, "svelte-3905933315", "");
}

function add_css() {
var style = createElement("style");
style.id = 'svelte-2363328337-style';
style.textContent = "@media(min-width: 1px){div[svelte-2363328337],[svelte-2363328337] div{color:red}}";
style.id = 'svelte-3905933315-style';
style.textContent = "@media(min-width: 1px){div[svelte-3905933315],[svelte-3905933315] div{color:red}}";
appendNode(style, document.head);
}

Expand Down Expand Up @@ -231,7 +231,7 @@ function SvelteComponent(options) {
init(this, options);
this._state = assign({}, options.data);

if (!document.getElementById("svelte-2363328337-style")) add_css();
if (!document.getElementById("svelte-3905933315-style")) add_css();

this._fragment = create_main_fragment(this._state, this);

Expand Down
10 changes: 5 additions & 5 deletions test/js/samples/css-media-query/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
import { appendNode, assign, createElement, detachNode, init, insertNode, noop, proto, setAttribute } from "svelte/shared.js";

function encapsulateStyles(node) {
setAttribute(node, "svelte-2363328337", "");
setAttribute(node, "svelte-3905933315", "");
}

function add_css() {
var style = createElement("style");
style.id = 'svelte-2363328337-style';
style.textContent = "@media(min-width: 1px){div[svelte-2363328337],[svelte-2363328337] div{color:red}}";
style.id = 'svelte-3905933315-style';
style.textContent = "@media(min-width: 1px){div[svelte-3905933315],[svelte-3905933315] div{color:red}}";
appendNode(style, document.head);
}

Expand Down Expand Up @@ -43,7 +43,7 @@ function SvelteComponent(options) {
init(this, options);
this._state = assign({}, options.data);

if (!document.getElementById("svelte-2363328337-style")) add_css();
if (!document.getElementById("svelte-3905933315-style")) add_css();

this._fragment = create_main_fragment(this._state, this);

Expand All @@ -54,4 +54,4 @@ function SvelteComponent(options) {
}

assign(SvelteComponent.prototype, proto);
export default SvelteComponent;
export default SvelteComponent;
4 changes: 2 additions & 2 deletions test/server-side-rendering/samples/styles-nested/_actual.css
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
div[svelte-1408461649],[svelte-1408461649] div{color:red}
div[svelte-1580141456],[svelte-1580141456] div{color:green}
div[svelte-724714405],[svelte-724714405] div{color:red}
div[svelte-300476157],[svelte-300476157] div{color:green}
6 changes: 3 additions & 3 deletions test/server-side-rendering/samples/styles-nested/_actual.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<div svelte-1408461649>red</div>
<div svelte-1580141456>green: foo</div>
<div svelte-724714405>red</div>
<div svelte-300476157>green: foo</div>



<div svelte-1580141456>green: bar</div>
<div svelte-300476157>green: bar</div>


Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
div[svelte-1408461649],[svelte-1408461649] div{color:red}
div[svelte-1580141456],[svelte-1580141456] div{color:green}
div[svelte-724714405],[svelte-724714405] div{color:red}
div[svelte-300476157],[svelte-300476157] div{color:green}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<div svelte-1408461649>red</div>
<div svelte-1580141456>green: foo</div>
<div svelte-724714405>red</div>
<div svelte-300476157>green: foo</div>



<div svelte-1580141456>green: bar</div>
<div svelte-300476157>green: bar</div>


2 changes: 1 addition & 1 deletion test/server-side-rendering/samples/styles/_actual.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
div[svelte-2278551596],[svelte-2278551596] div{color:red}
div[svelte-724714405],[svelte-724714405] div{color:red}
2 changes: 1 addition & 1 deletion test/server-side-rendering/samples/styles/_actual.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<div svelte-2278551596>red</div>
<div svelte-724714405>red</div>
2 changes: 1 addition & 1 deletion test/server-side-rendering/samples/styles/_expected.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
div[svelte-2278551596],[svelte-2278551596] div{color:red}
div[svelte-724714405],[svelte-724714405] div{color:red}
2 changes: 1 addition & 1 deletion test/server-side-rendering/samples/styles/_expected.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<div svelte-2278551596>red</div>
<div svelte-724714405>red</div>
2 changes: 1 addition & 1 deletion test/sourcemaps/samples/css-cascade-false/output.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/sourcemaps/samples/css/output.css

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