-
Notifications
You must be signed in to change notification settings - Fork 402
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
fix: simplify style logic in template compiler #314
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0b27792
fix: simplify style logic in template compiler
pmdartus 33236d3
fix: remove unused API to fix linting error
pmdartus 48f9cf4
fix: use camelCase instead of kebab-case for css properties
pmdartus edf8b41
Merge branch 'master' into pmdartus/fix-invalid-style-parsing
pmdartus 5d37713
docs: add comments to clarify the style changes
pmdartus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,20 +2,17 @@ import assert from "../assert"; | |
import { | ||
isString, | ||
isUndefined, | ||
StringCharCodeAt, | ||
} from '../language'; | ||
import { EmptyObject } from '../utils'; | ||
import { VNode, Module } from "../../3rdparty/snabbdom/types"; | ||
import { VNode, Module, VNodeStyle } from "../../3rdparty/snabbdom/types"; | ||
import { removeAttribute } from '../dom'; | ||
|
||
const DashCharCode = 45; | ||
|
||
function updateStyle(oldVnode: VNode, vnode: VNode) { | ||
const { data: { style: newStyle } } = vnode; | ||
const { style: newStyle } = vnode.data; | ||
if (isUndefined(newStyle)) { | ||
return; | ||
} | ||
let { data: { style: oldStyle } } = oldVnode; | ||
let { style: oldStyle } = oldVnode.data; | ||
if (oldStyle === newStyle) { | ||
return; | ||
} | ||
|
@@ -27,13 +24,13 @@ function updateStyle(oldVnode: VNode, vnode: VNode) { | |
let name: string; | ||
const elm = (vnode.elm as HTMLElement); | ||
const { style } = elm; | ||
if (isUndefined(newStyle) || newStyle as any === '') { | ||
if (isUndefined(newStyle) || newStyle === '') { | ||
removeAttribute.call(elm, 'style'); | ||
} else if (isString(newStyle)) { | ||
style.cssText = newStyle; | ||
} else { | ||
if (!isUndefined(oldStyle)) { | ||
for (name in oldStyle) { | ||
for (name in oldStyle as VNodeStyle) { | ||
if (!(name in newStyle)) { | ||
style.removeProperty(name); | ||
} | ||
|
@@ -44,14 +41,8 @@ function updateStyle(oldVnode: VNode, vnode: VNode) { | |
|
||
for (name in newStyle) { | ||
const cur = newStyle[name]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add comment here. |
||
if (cur !== (oldStyle as any)[name]) { | ||
if (StringCharCodeAt.call(name, 0) === DashCharCode && StringCharCodeAt.call(name, 1) === DashCharCode) { | ||
// if the name is prefixed with --, it will be considered a variable, and setProperty() is needed | ||
style.setProperty(name, cur); | ||
} else { | ||
// @ts-ignore | ||
style[name] = cur; | ||
} | ||
if (cur !== (oldStyle as VNodeStyle)[name]) { | ||
style[name] = cur; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
packages/lwc-template-compiler/src/parser/__tests__/style.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { parseStyleText, parseClassNames } from '../style'; | ||
|
||
describe('parseStyleText', () => { | ||
it('should parse simple style text', () => { | ||
const res = parseStyleText('color: blue'); | ||
expect(res).toEqual({ color: 'blue' }); | ||
}); | ||
|
||
it('should parse simple style text with trailing coma', () => { | ||
const res = parseStyleText('color: blue;'); | ||
expect(res).toEqual({ | ||
color: 'blue' | ||
}); | ||
}); | ||
|
||
it('should parse simple style with multiple values', () => { | ||
const res = parseStyleText('box-shadow: 10px 5px 5px black;'); | ||
expect(res).toEqual({ | ||
boxShadow: '10px 5px 5px black' | ||
}); | ||
}); | ||
|
||
it('should parse multiple declaration', () => { | ||
const res = parseStyleText(`font-size: 12px;background: blue; color:red ;`); | ||
expect(res).toEqual({ | ||
fontSize: '12px', | ||
background: 'blue', | ||
color: 'red' | ||
}); | ||
}); | ||
|
||
it('should parse css functions', () => { | ||
const res = parseStyleText(`background-color:rgba(255,0,0,0.3)`); | ||
expect(res).toEqual({ | ||
backgroundColor: 'rgba(255,0,0,0.3)' | ||
}); | ||
}); | ||
|
||
it('should support base 64 encoded strings', () => { | ||
const image = 'url("data:image/webp;base64,AAAAAAAAAAA")'; | ||
const res = parseStyleText(`background: ${image}`); | ||
expect(res).toEqual({ | ||
background: image | ||
}); | ||
}); | ||
}); | ||
|
||
describe('parseClassNames', () => { | ||
it('should support a single class', () => { | ||
const res = parseClassNames('foo'); | ||
expect(res).toEqual({ foo: true }); | ||
}); | ||
|
||
it('should support simple class list', () => { | ||
const res = parseClassNames('foo bar'); | ||
expect(res).toEqual({ foo: true, bar: true }); | ||
}); | ||
|
||
it('should support simple class list with trailing spaces', () => { | ||
const res = parseClassNames(' foo bar '); | ||
expect(res).toEqual({ foo: true, bar: true }); | ||
}); | ||
|
||
it('should support simple class list multiple spaces', () => { | ||
const res = parseClassNames('foo bar'); | ||
expect(res).toEqual({ foo: true, bar: true }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should mark this as a difference since this file is copied and modified from snabbdom, so we know when copying the new one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... @pmdartus
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
additionally, we can expand this type in LWC only.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will fix this in an upcoming PR when removing the engine registry.