Skip to content

Commit

Permalink
fix: corrects behavior of style removal (#3261)
Browse files Browse the repository at this point in the history
* fix style removal behavior

* adding tests

* limit exports from package root

Co-authored-by: Rob Eisenberg <EisenbergEffect@users.noreply.github.com>
  • Loading branch information
nicholasrice and EisenbergEffect authored Jun 10, 2020
1 parent f772fa0 commit e37b7ab
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
38 changes: 38 additions & 0 deletions packages/web-components/fast-element/src/__test__/styles.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { expect } from "chai";
import { AdoptedStyleSheetsStyles, StyleTarget } from "../styles";

describe("AdoptedStyleSheetsStyles", () => {
context("when removing styles", () => {
it("should remove an associated stylesheet", () => {
const cache = new Map();
const sheet = new AdoptedStyleSheetsStyles([``], cache);
const target: Pick<StyleTarget, "adoptedStyleSheets"> = {
adoptedStyleSheets: [],
};

sheet.addStylesTo(target as StyleTarget);
expect(target.adoptedStyleSheets!.length).to.equal(1);

sheet.removeStylesFrom(target as StyleTarget);
expect(target.adoptedStyleSheets!.length).to.equal(0);
});

it("should not remove unassociated styles", () => {
const cache = new Map();
const sheet = new AdoptedStyleSheetsStyles(["test"], cache);
const style = new CSSStyleSheet();
const target: Pick<StyleTarget, "adoptedStyleSheets"> = {
adoptedStyleSheets: [style],
};
sheet.addStylesTo(target as StyleTarget);

expect(target.adoptedStyleSheets!.length).to.equal(2);
expect(target.adoptedStyleSheets).to.contain(cache.get("test"));

sheet.removeStylesFrom(target as StyleTarget);

expect(target.adoptedStyleSheets!.length).to.equal(1);
expect(target.adoptedStyleSheets).not.to.contain(cache.get("test"));
});
});
});
2 changes: 1 addition & 1 deletion packages/web-components/fast-element/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export * from "./attributes";
export * from "./controller";
export * from "./interfaces";
export * from "./template-compiler";
export * from "./styles";
export { StyleTarget, ElementStyles, css } from "./styles";
export * from "./view";
export * from "./observation/observable";
export * from "./observation/notifier";
Expand Down
12 changes: 8 additions & 4 deletions packages/web-components/fast-element/src/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,13 @@ function reduceBehaviors(
}, null as Behavior[] | null);
}

// https://wicg.github.io/construct-stylesheets/
// https://developers.google.com/web/updates/2019/02/constructable-stylesheets
class AdoptedStyleSheetsStyles extends ElementStyles {
/**
* https://wicg.github.io/construct-stylesheets/
* https://developers.google.com/web/updates/2019/02/constructable-stylesheets
*
* @internal
*/
export class AdoptedStyleSheetsStyles extends ElementStyles {
private readonly styleSheets: CSSStyleSheet[];
public readonly behaviors: ReadonlyArray<Behavior> | null = null;

Expand Down Expand Up @@ -138,7 +142,7 @@ class AdoptedStyleSheetsStyles extends ElementStyles {
public removeStylesFrom(target: StyleTarget): void {
const sourceSheets = this.styleSheets;
target.adoptedStyleSheets = target.adoptedStyleSheets!.filter(
(x: CSSStyleSheet) => sourceSheets.indexOf(x) !== -1
(x: CSSStyleSheet) => sourceSheets.indexOf(x) === -1
);
}
}
Expand Down

0 comments on commit e37b7ab

Please sign in to comment.