|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | + |
| 3 | +import { StyleScope } from './style-scope'; |
| 4 | +import { StackLayout } from '../layouts/stack-layout'; |
| 5 | +import { Label } from '../label'; |
| 6 | + |
| 7 | +function loaded(css: string): { view: any; root: any } { |
| 8 | + const scope = new StyleScope(); |
| 9 | + scope.css = css; |
| 10 | + |
| 11 | + const root: any = new StackLayout(); |
| 12 | + root._styleScope = scope; |
| 13 | + |
| 14 | + const view: any = new Label(); |
| 15 | + root.addChild(view); |
| 16 | + view._styleScope = scope; |
| 17 | + |
| 18 | + root._cssState.onLoaded(); |
| 19 | + view._cssState.onLoaded(); |
| 20 | + |
| 21 | + return { view, root }; |
| 22 | +} |
| 23 | + |
| 24 | +function changeValues(target: any, eventName: string): unknown[] { |
| 25 | + const values: unknown[] = []; |
| 26 | + target.on(eventName, (args: any) => values.push(args.value)); |
| 27 | + |
| 28 | + return values; |
| 29 | +} |
| 30 | + |
| 31 | +const defaults: any = new Label(); |
| 32 | + |
| 33 | +describe('inline style assignment', () => { |
| 34 | + it('unsets a longhand the new string drops', () => { |
| 35 | + const { view } = loaded(''); |
| 36 | + |
| 37 | + view.style = 'color: red; margin-top: 4'; |
| 38 | + view.style = 'color: blue'; |
| 39 | + |
| 40 | + expect(view.style.color.toString()).toBe('#0000FF'); |
| 41 | + expect(view.style.marginTop).toEqual(defaults.style.marginTop); |
| 42 | + }); |
| 43 | + |
| 44 | + it('unsets every longhand of a shorthand the new string drops', () => { |
| 45 | + const { view } = loaded(''); |
| 46 | + |
| 47 | + view.style = 'margin: 4'; |
| 48 | + view.style = 'color: red'; |
| 49 | + |
| 50 | + expect(view.style.marginTop).toEqual(defaults.style.marginTop); |
| 51 | + expect(view.style.marginRight).toEqual(defaults.style.marginRight); |
| 52 | + expect(view.style.marginBottom).toEqual(defaults.style.marginBottom); |
| 53 | + expect(view.style.marginLeft).toEqual(defaults.style.marginLeft); |
| 54 | + }); |
| 55 | + |
| 56 | + it('keeps the longhands a narrower string still sets', () => { |
| 57 | + const { view } = loaded(''); |
| 58 | + view.style = 'margin: 4'; |
| 59 | + |
| 60 | + const marginTopChanges = changeValues(view.style, 'marginTopChange'); |
| 61 | + view.style = 'margin-top: 4'; |
| 62 | + |
| 63 | + expect(view.style.marginTop).toBe(4); |
| 64 | + expect(view.style.marginLeft).toEqual(defaults.style.marginLeft); |
| 65 | + expect(marginTopChanges).toEqual([]); |
| 66 | + }); |
| 67 | + |
| 68 | + it('unsets the longhands of a dropped shorthand holding a variable', () => { |
| 69 | + const { view } = loaded('label { --m: 6; }'); |
| 70 | + |
| 71 | + view.style = 'margin: var(--m)'; |
| 72 | + expect(view.style.marginTop).toBe(6); |
| 73 | + |
| 74 | + view.style = 'color: red'; |
| 75 | + |
| 76 | + expect(view.style.marginTop).toEqual(defaults.style.marginTop); |
| 77 | + expect(view.style.marginLeft).toEqual(defaults.style.marginLeft); |
| 78 | + }); |
| 79 | + |
| 80 | + it('falls back to the stylesheet value of a dropped property', () => { |
| 81 | + const { view } = loaded('label { margin-top: 8; color: green; }'); |
| 82 | + |
| 83 | + view.style = 'margin-top: 20; color: red'; |
| 84 | + view.style = 'font-size: 12'; |
| 85 | + |
| 86 | + expect(view.style.marginTop).toBe(8); |
| 87 | + expect(view.style.color.toString()).toBe('#008000'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('falls back to the stylesheet value shadowed before css was applied', () => { |
| 91 | + const scope = new StyleScope(); |
| 92 | + scope.css = 'label { margin-top: 8; }'; |
| 93 | + const root: any = new StackLayout(); |
| 94 | + root._styleScope = scope; |
| 95 | + const view: any = new Label(); |
| 96 | + root.addChild(view); |
| 97 | + view._styleScope = scope; |
| 98 | + |
| 99 | + view.style = 'margin-top: 20'; |
| 100 | + view._cssState.onLoaded(); |
| 101 | + expect(view.style.marginTop).toBe(20); |
| 102 | + |
| 103 | + view.style = ''; |
| 104 | + |
| 105 | + expect(view.style.marginTop).toBe(8); |
| 106 | + }); |
| 107 | + |
| 108 | + it('falls back to the inherited value of a dropped inherited property', () => { |
| 109 | + const { view } = loaded('stacklayout { color: red; }'); |
| 110 | + |
| 111 | + view.style = 'color: blue'; |
| 112 | + view.style = ''; |
| 113 | + |
| 114 | + expect(view.style.color.toString()).toBe('#FF0000'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('clears every inline property when assigned an empty string', () => { |
| 118 | + const { view } = loaded(''); |
| 119 | + |
| 120 | + view.style = 'color: red; margin: 4; font-size: 20'; |
| 121 | + view.style = ''; |
| 122 | + |
| 123 | + expect(view.style.color).toBeUndefined(); |
| 124 | + expect(view.style.marginTop).toEqual(defaults.style.marginTop); |
| 125 | + expect(view.style.fontSize).toEqual(defaults.style.fontSize); |
| 126 | + }); |
| 127 | + |
| 128 | + it('does not unset and re-apply a property the new string keeps', () => { |
| 129 | + const { view } = loaded(''); |
| 130 | + view.style = 'color: red; margin: 4'; |
| 131 | + |
| 132 | + const colorChanges = changeValues(view.style, 'colorChange'); |
| 133 | + const marginTopChanges = changeValues(view.style, 'marginTopChange'); |
| 134 | + view.style = 'color: blue; margin-top: 4'; |
| 135 | + |
| 136 | + expect(colorChanges.map(String)).toEqual(['#0000FF']); |
| 137 | + expect(marginTopChanges).toEqual([]); |
| 138 | + }); |
| 139 | + |
| 140 | + it('unsets a dropped property applied through the view', () => { |
| 141 | + const { view } = loaded(''); |
| 142 | + |
| 143 | + view.style = 'text: hello; color: red'; |
| 144 | + expect(view.text).toBe('hello'); |
| 145 | + |
| 146 | + view.style = 'color: red'; |
| 147 | + |
| 148 | + expect(view.text).toEqual(defaults.text); |
| 149 | + }); |
| 150 | + |
| 151 | + it('removes the css variables the new string drops', () => { |
| 152 | + const { view } = loaded(''); |
| 153 | + |
| 154 | + view.style = '--c: red; color: var(--c)'; |
| 155 | + expect(view.style.color.toString()).toBe('#FF0000'); |
| 156 | + |
| 157 | + view.style = 'margin-top: 4'; |
| 158 | + |
| 159 | + expect(view.style.getCssVariable('--c')).toBeNull(); |
| 160 | + expect(view.style.color).toBeUndefined(); |
| 161 | + }); |
| 162 | + |
| 163 | + it('unsets a kept property whose new value is invalid', () => { |
| 164 | + const { view } = loaded(''); |
| 165 | + |
| 166 | + view.style = 'color: red'; |
| 167 | + view.style = 'color: not-a-color'; |
| 168 | + |
| 169 | + expect(view.style.color).toBeUndefined(); |
| 170 | + }); |
| 171 | + |
| 172 | + it('keeps an earlier valid declaration when a later one is invalid', () => { |
| 173 | + const { view } = loaded(''); |
| 174 | + |
| 175 | + view.style = 'color: blue'; |
| 176 | + view.style = 'color: red; color: not-a-color'; |
| 177 | + |
| 178 | + expect(view.style.color.toString()).toBe('#FF0000'); |
| 179 | + }); |
| 180 | + |
| 181 | + it('does not track a property whose only declaration is invalid', () => { |
| 182 | + const { view } = loaded(''); |
| 183 | + view.style.color = 'green'; |
| 184 | + |
| 185 | + view.style = 'color: not-a-color'; |
| 186 | + view.style = ''; |
| 187 | + |
| 188 | + expect(view.style.color.toString()).toBe('#008000'); |
| 189 | + }); |
| 190 | + |
| 191 | + it('falls back to the stylesheet value of a dropped animatable property', () => { |
| 192 | + const { view } = loaded('label { background-color: green; opacity: 0.5; }'); |
| 193 | + |
| 194 | + view.style = 'background-color: red; opacity: 1'; |
| 195 | + expect(view.style.backgroundColor.toString()).toBe('#FF0000'); |
| 196 | + |
| 197 | + view.style = ''; |
| 198 | + |
| 199 | + expect(view.style.backgroundColor.toString()).toBe('#008000'); |
| 200 | + expect(view.style.opacity).toBe(0.5); |
| 201 | + }); |
| 202 | + |
| 203 | + it('resets a dropped animatable property without a stylesheet value', () => { |
| 204 | + const { view } = loaded(''); |
| 205 | + |
| 206 | + view.style = 'background-color: red; opacity: 0.5'; |
| 207 | + view.style = ''; |
| 208 | + |
| 209 | + expect(view.style.backgroundColor).toEqual(defaults.style.backgroundColor); |
| 210 | + expect(view.style.opacity).toEqual(defaults.style.opacity); |
| 211 | + }); |
| 212 | + |
| 213 | + it('leaves values set from code alone when inline strings never set them', () => { |
| 214 | + const { view } = loaded(''); |
| 215 | + view.style.marginTop = 10; |
| 216 | + |
| 217 | + view.style = 'color: red'; |
| 218 | + view.style = ''; |
| 219 | + |
| 220 | + expect(view.style.marginTop).toBe(10); |
| 221 | + }); |
| 222 | +}); |
0 commit comments