Skip to content

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

Merged
merged 9 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/beige-hounds-enjoy.md
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
5 changes: 5 additions & 0 deletions .changeset/quiet-toes-carry.md
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
49 changes: 34 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@
"microbundle": "^0.15.1",
"mocha": "^8.2.1",
"baseline-rts": "npm:preact-render-to-string@latest",
"preact": "^10.11.1",
"preact": "^10.13.0",
"prettier": "^2.2.1",
"sinon": "^9.2.2",
"sinon-chai": "^3.5.0",
"typescript": "^4.1.3"
"typescript": "^5.0.0"
},
"dependencies": {
"pretty-format": "^3.8.0"
Expand Down
11 changes: 2 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,6 @@ function _renderToString(vnode, context, isSvgMode, selectValue, parent) {
rendered =
rendered +
_renderToString(child, context, isSvgMode, selectValue, parent);

if (
Copy link
Member Author

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

Copy link
Member

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?

Copy link
Member Author

@JoviDeCroock JoviDeCroock Mar 20, 2023

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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh right!

typeof child === 'string' ||
typeof child === 'number' ||
typeof child === 'bigint'
) {
// @ts-ignore manually constructing a Text vnode
vnode[i] = h(null, null, child);
}
}
return rendered;
}
Expand Down Expand Up @@ -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 '<!-- -->';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could give us an additional 5% boost

Copy link
Member

Choose a reason for hiding this comment

The 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}>`);
}

Expand Down
55 changes: 44 additions & 11 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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([
'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 = '';
Expand All @@ -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('--') &&
Copy link
Member

Choose a reason for hiding this comment

The 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;
Expand Down
2 changes: 1 addition & 1 deletion test/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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>');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use-id handling seems to be different in Preact 10.13

});
});
});