-
-
Notifications
You must be signed in to change notification settings - Fork 95
check whether style needs a suffix with a set and remove debug casting #286
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
Changes from all commits
b476795
3a84c62
0a945cc
8643ee5
169c147
0ab665b
fb3a5e9
e04c07d
3f21e88
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'preact-render-to-string': patch | ||
--- | ||
|
||
Change style calculation to use a Set rather than Regex |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'preact-render-to-string': major | ||
--- | ||
|
||
Remove the castin to VNode for `preact/debug`, this is fixed in Preact >= 10.13.0 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,15 +145,6 @@ function _renderToString(vnode, context, isSvgMode, selectValue, parent) { | |
rendered = | ||
rendered + | ||
_renderToString(child, context, isSvgMode, selectValue, parent); | ||
|
||
if ( | ||
typeof child === 'string' || | ||
typeof child === 'number' || | ||
typeof child === 'bigint' | ||
) { | ||
// @ts-ignore manually constructing a Text vnode | ||
vnode[i] = h(null, null, child); | ||
} | ||
} | ||
return rendered; | ||
} | ||
|
@@ -338,6 +329,8 @@ function _renderToString(vnode, context, isSvgMode, selectValue, parent) { | |
} | ||
|
||
if (UNSAFE_NAME.test(type)) { | ||
// this seems to performs a lot better than throwing | ||
// return '<!-- -->'; | ||
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. This could give us an additional 5% boost 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. I really like that idea! |
||
throw new Error(`${type} is not a valid HTML tag name in ${s}>`); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,6 @@ export const UNSAFE_NAME = /[\s\n\\/='"\0<>]/; | |
export const XLINK = /^xlink:?./; | ||
|
||
// DOM properties that should NOT have "px" added when numeric | ||
const IS_NON_DIMENSIONAL = /acit|ex(?:s|g|n|p|$)|rph|grid|ows|mnc|ntw|ine[ch]|zoo|^ord|^--/; | ||
const ENCODED_ENTITIES = /["&<]/; | ||
|
||
/** @param {string} str */ | ||
|
@@ -50,9 +49,45 @@ export let isLargeString = (s, length, ignoreLines) => | |
String(s).indexOf('<') !== -1; | ||
|
||
const JS_TO_CSS = {}; | ||
const SUFFIX_CACHE = {}; | ||
|
||
const CSS_REGEX = /([A-Z])/g; | ||
const IS_NON_DIMENSIONAL = new Set([ | ||
marvinhagemeister marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'animation-iteration-count', | ||
'border-image-outset', | ||
'border-image-slice', | ||
'border-image-width', | ||
'box-flex', | ||
'box-flex-group', | ||
'box-ordinal-group', | ||
'column-count', | ||
'fill-opacity', | ||
'flex', | ||
'flex-grow', | ||
'flex-negative', | ||
'flex-order', | ||
'flex-positive', | ||
'flex-shrink', | ||
'flood-opacity', | ||
'font-weight', | ||
'grid-column', | ||
'grid-row', | ||
'line-clamp', | ||
'line-height', | ||
'opacity', | ||
'order', | ||
'orphans', | ||
'stop-opacity', | ||
'stroke-dasharray', | ||
'stroke-dashoffset', | ||
'stroke-miterlimit', | ||
'stroke-opacity', | ||
'stroke-width', | ||
'tab-size', | ||
'widows', | ||
'z-index', | ||
'zoom' | ||
]); | ||
|
||
const CSS_REGEX = /[A-Z]/g; | ||
// Convert an Object style to a CSSText string | ||
export function styleObjToCss(s) { | ||
let str = ''; | ||
|
@@ -63,17 +98,15 @@ export function styleObjToCss(s) { | |
prop[0] == '-' | ||
? prop | ||
: JS_TO_CSS[prop] || | ||
(JS_TO_CSS[prop] = prop.replace(CSS_REGEX, '-$1').toLowerCase()); | ||
(JS_TO_CSS[prop] = prop.replace(CSS_REGEX, '-$&').toLowerCase()); | ||
|
||
let suffix = ';'; | ||
let isNumber = typeof val === 'number'; | ||
if (isNumber && SUFFIX_CACHE[name]) { | ||
suffix = 'px;'; | ||
} else if ( | ||
isNumber && | ||
IS_NON_DIMENSIONAL.test(prop.toLowerCase()) === false | ||
if ( | ||
typeof val === 'number' && | ||
// Exclude custom-attributes | ||
!name.startsWith('--') && | ||
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. Is this check doing the same thing as line 98, i.e. are they both trying to check for custom properties? Do we get any perf benefit by consolidating them? |
||
!IS_NON_DIMENSIONAL.has(name) | ||
) { | ||
SUFFIX_CACHE[name] = true; | ||
suffix = 'px;'; | ||
} | ||
str = str + name + ':' + val + suffix; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1313,7 +1313,7 @@ describe('render', () => { | |
); | ||
} | ||
|
||
expect(render(<App />)).to.equal('<div><p>P481</p><p>P476951</p></div>'); | ||
expect(render(<App />)).to.equal('<div><p>P0-0</p><p>P0-1</p></div>'); | ||
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. use-id handling seems to be different in Preact 10.13 |
||
}); | ||
}); | ||
}); |
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 fixed this in 10.13.0, I honestly think we shouldn't put a preact/debug fix in our hot path
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.
I'm a bit out of the loop. Which issue in
preact/debug
did this section work around?Uh oh!
There was an error while loading. Please reload this page.
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.
This one #257 this was fixed in Preact itself with preactjs/preact#3801
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.
Ohh right!