From 416deeb8659275e558cd5b608c1567e0744614cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Mon, 20 Dec 2021 16:19:07 +0100 Subject: [PATCH] [REM] component: remove support for css tag --- src/app/template_set.ts | 12 ++ src/component/component.ts | 1 - src/component/component_node.ts | 4 - src/component/style.ts | 88 --------- src/index.ts | 2 +- src/memo.ts | 2 +- src/portal.ts | 2 +- src/tags.ts | 28 --- tests/compiler/translation.test.ts | 3 +- tests/components/app.test.ts | 3 +- tests/components/basics.test.ts | 3 +- tests/components/concurrency.test.ts | 2 +- tests/components/error_handling.test.ts | 2 +- .../components/higher_order_component.test.ts | 3 +- tests/components/lifecycle.test.ts | 3 +- tests/components/props.test.ts | 3 +- tests/components/reactivity.test.ts | 2 +- tests/components/style_tag.test.ts | 175 ------------------ tests/components/t_component.test.ts | 3 +- tests/misc/memo.test.ts | 3 +- tests/misc/portal.test.ts | 3 +- tests/reactivity.test.ts | 11 +- 22 files changed, 36 insertions(+), 322 deletions(-) delete mode 100644 src/component/style.ts delete mode 100644 src/tags.ts delete mode 100644 tests/components/style_tag.test.ts diff --git a/src/app/template_set.ts b/src/app/template_set.ts index 7ff281a1..4a9874a7 100644 --- a/src/app/template_set.ts +++ b/src/app/template_set.ts @@ -100,3 +100,15 @@ export class TemplateSet { }); } } + +// ----------------------------------------------------------------------------- +// xml tag helper +// ----------------------------------------------------------------------------- +export function xml(...args: Parameters) { + const name = `__template__${xml.nextId++}`; + const value = String.raw(...args); + globalTemplates[name] = value; + return name; +} + +xml.nextId = 1; diff --git a/src/component/component.ts b/src/component/component.ts index 551bc776..0caad653 100644 --- a/src/component/component.ts +++ b/src/component/component.ts @@ -7,7 +7,6 @@ import type { ComponentNode } from "./component_node"; export class Component { static template: string = ""; - static style: string = ""; static props?: any; props: any; diff --git a/src/component/component_node.ts b/src/component/component_node.ts index e34f5d2e..bd96adad 100644 --- a/src/component/component_node.ts +++ b/src/component/component_node.ts @@ -12,7 +12,6 @@ import { import { handleError, fibersInError } from "./error_handling"; import { applyDefaultProps } from "./props_validation"; import { STATUS } from "./status"; -import { applyStyles } from "./style"; export function component( name: string | typeof Component, @@ -106,9 +105,6 @@ export class ComponentNode this.childEnv = env; this.component = new C(props, env, this) as any; this.renderFn = app.getTemplate(C.template).bind(this.component, this.component, this); - if (C.style) { - applyStyles(C); - } this.component.setup(); } diff --git a/src/component/style.ts b/src/component/style.ts deleted file mode 100644 index 62f99865..00000000 --- a/src/component/style.ts +++ /dev/null @@ -1,88 +0,0 @@ -import { Component } from "./component"; - -export const globalStylesheets: { [key: string]: HTMLStyleElement } = {}; - -export function registerSheet(id: string, css: string) { - const sheet = document.createElement("style"); - sheet.innerHTML = processSheet(css); - globalStylesheets[id] = sheet; -} - -/** - * Apply the stylesheets defined by the component. Note that we need to make - * sure all inherited stylesheets are applied as well, in a reverse order to - * ensure that `); - const style = getComputedStyle(fixture.querySelector("div")!); - expect(style.color).toBe("red"); - expect(fixture.innerHTML).toBe('
text
'); - }); - - test("inherited components properly apply css", async () => { - class Root extends Component { - static template = xml`
text
`; - static style = css` - .app { - color: red; - } - `; - } - class OtherRoot extends Root { - static style = css` - .app { - font-weight: bold; - } - `; - } - expect(document.head.innerHTML).toBe(""); - await mount(OtherRoot, fixture); - expect(document.head.innerHTML).toBe(``); - const style = getComputedStyle(fixture.querySelector("div")!); - expect(style.color).toBe("red"); - expect(style.fontWeight).toBe("bold"); - expect(fixture.innerHTML).toBe('
text
'); - }); - - test("inherited components properly apply css, part 2", async () => { - class App extends Component { - static template = xml`
`; - static style = css` - .app { - color: tomato; - } - `; - } - class BetterApp extends App {} - class EvenBetterApp extends BetterApp { - static style = css` - .app { - background-color: papayawhip; - } - `; - } - expect(document.head.innerHTML).toBe(""); - await mount(EvenBetterApp, fixture); - expect(document.head.innerHTML).toBe(``); - }); - - test("get a meaningful error message if css helper is missing", async () => { - class App extends Component { - static template = xml`
text
`; - static style = `.app {color: red;}`; - } - let error: Error; - try { - await mount(App, fixture); - } catch (e) { - error = e as Error; - } - expect(error!).toBeDefined(); - expect(error!.message).toBe( - "Invalid css stylesheet for component 'App'. Did you forget to use the 'css' tag helper?" - ); - }); - - test("inline stylesheets are processed", async () => { - class App extends Component { - static template = xml`
text
`; - static style = css` - .app { - color: red; - .some-class { - font-weight: bold; - width: 40px; - } - display: block; - } - `; - } - await mount(App, fixture); - expect(document.head.querySelector("style")!.innerHTML).toBe(`.app { - color: red; -} -.app .some-class { - font-weight: bold; - width: 40px; -} -.app { - display: block; -}`); - }); - - test("properly handle rules with commas", async () => { - class App extends Component { - static template = xml`
`; - static style = css` - .parent-a, - .parent-b { - .child-a, - .child-b { - color: red; - } - } - `; - } - await mount(App, fixture); - expect(document.head.querySelector("style")!.innerHTML) - .toBe(`.parent-a .child-a, .parent-a .child-b, .parent-b .child-a, .parent-b .child-b { - color: red; -}`); - }); - - test("handle & selector", async () => { - class App extends Component { - static template = xml`
`; - static style = css` - .btn { - &.danger { - color: red; - } - } - .some-class { - &.btn { - .other-class ~ & { - color: red; - } - } - } - `; - } - await mount(App, fixture); - expect(document.head.querySelector("style")!.innerHTML).toBe(`.btn.danger { - color: red; -} -.other-class ~ .some-class.btn { - color: red; -}`); - }); -}); diff --git a/tests/components/t_component.test.ts b/tests/components/t_component.test.ts index 65d337a8..8ea37727 100644 --- a/tests/components/t_component.test.ts +++ b/tests/components/t_component.test.ts @@ -1,5 +1,4 @@ -import { Component, mount, useState } from "../../src"; -import { xml } from "../../src/tags"; +import { Component, mount, useState, xml } from "../../src"; import { makeTestFixture, nextTick, snapshotEverything, useLogLifecycle } from "../helpers"; let fixture: HTMLElement; diff --git a/tests/misc/memo.test.ts b/tests/misc/memo.test.ts index 5360dae2..2ae3a13a 100644 --- a/tests/misc/memo.test.ts +++ b/tests/misc/memo.test.ts @@ -1,6 +1,5 @@ -import { Component, mount, useState } from "../../src"; +import { Component, mount, useState, xml } from "../../src"; import { Memo } from "../../src/"; -import { xml } from "../../src/tags"; import { makeTestFixture, nextTick, snapshotEverything } from "../helpers"; let fixture: HTMLElement; diff --git a/tests/misc/portal.test.ts b/tests/misc/portal.test.ts index 66492cf8..ac7aff67 100644 --- a/tests/misc/portal.test.ts +++ b/tests/misc/portal.test.ts @@ -9,8 +9,7 @@ import { onWillUnmount, useState, } from "../../src"; -import { Portal } from "../../src/"; -import { xml } from "../../src/tags"; +import { Portal, xml } from "../../src/"; import { elem, makeTestFixture, nextTick, snapshotEverything } from "../helpers"; let fixture: HTMLElement; diff --git a/tests/reactivity.test.ts b/tests/reactivity.test.ts index 12dcc115..87c1a95b 100644 --- a/tests/reactivity.test.ts +++ b/tests/reactivity.test.ts @@ -1,6 +1,13 @@ -import { Component, mount, onWillRender, onWillStart, onWillUpdateProps, useState } from "../src"; +import { + Component, + mount, + onWillRender, + onWillStart, + onWillUpdateProps, + useState, + xml, +} from "../src"; import { batched, reactive } from "../src/reactivity"; -import { xml } from "../src/tags"; import { makeDeferred, makeTestFixture,