Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: preactjs/preact-render-to-string
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 2d0cd1decb7e24fdf6facc0d169c7f893e1827eb
Choose a base ref
...
head repository: preactjs/preact-render-to-string
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 60075a5a7389d638d535c85f3706739e9ba932bc
Choose a head ref
  • 3 commits
  • 4 files changed
  • 5 contributors

Commits on Sep 10, 2022

  1. add parent and children for useId (#237)

    * Update index.js
    
    * fix tests
    
    * Update young-cougars-protect.md
    
    * remove changeset
    
    * fix
    
    * fix tests
    
    * Use `props.children` as vnode._children instead of creating a new Array.
    
    * unset vnode parent properties after rendering. Also consolidate Fragment and component rendering codepaths.
    
    * Create dull-baboons-kneel.md
    
    Co-authored-by: Jason Miller <developit@users.noreply.github.com>
    Co-authored-by: Jason Miller <jason@developit.ca>
    3 people authored Sep 10, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    dec7a7a View commit details
  2. correctly unmount vnodes (#242)

    * correctly unmount vnodes
    
    * options.unmount after diffed
    
    * remove change as we are merging to a tagged branch
    
    * Create breezy-avocados-call.md
    
    * Update index.js
    JoviDeCroock authored Sep 10, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    bd5e5eb View commit details
  3. Version Packages (#244)

    Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
    github-actions[bot] and github-actions[bot] authored Sep 10, 2022

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    60075a5 View commit details
Showing with 66 additions and 26 deletions.
  1. +8 −0 CHANGELOG.md
  2. +1 −1 package.json
  3. +2 −0 src/constants.js
  4. +55 −25 src/index.js
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# preact-render-to-string

## 5.2.4

### Patch Changes

- [#242](https://github.com/preactjs/preact-render-to-string/pull/242) [`bd5e5eb`](https://github.com/preactjs/preact-render-to-string/commit/bd5e5eb1c97355d81710c17a10208b1cb3b439a0) Thanks [@JoviDeCroock](https://github.com/JoviDeCroock)! - correctly unmount vnodes

* [#237](https://github.com/preactjs/preact-render-to-string/pull/237) [`dec7a7a`](https://github.com/preactjs/preact-render-to-string/commit/dec7a7a575149187942adb92f644c302db4b0599) Thanks [@JoviDeCroock](https://github.com/JoviDeCroock)! - add parent and children for useId

## 5.2.3

### Patch Changes
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "preact-render-to-string",
"amdName": "preactRenderToString",
"version": "5.2.3",
"version": "5.2.4",
"description": "Render JSX to an HTML string, with support for Preact components.",
"main": "dist/index.js",
"umd:main": "dist/index.js",
2 changes: 2 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -7,6 +7,8 @@ export const SKIP_EFFECTS = '__s';

// VNode properties
export const COMPONENT = '__c';
export const CHILDREN = '__k';
export const PARENT = '__';

// Component properties
export const VNODE = '__v';
80 changes: 55 additions & 25 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ import {
XLINK,
VOID_ELEMENTS
} from './util';
import { options, Fragment } from 'preact';
import { options, h, Fragment } from 'preact';
import { _renderToStringPretty } from './pretty';
import {
COMMIT,
@@ -16,9 +16,11 @@ import {
DIFFED,
DIRTY,
NEXT_STATE,
PARENT,
RENDER,
SKIP_EFFECTS,
VNODE
VNODE,
CHILDREN
} from './constants';

/** @typedef {import('preact').VNode} VNode */
@@ -59,6 +61,9 @@ function renderToString(vnode, context, opts) {
const previousSkipEffects = options[SKIP_EFFECTS];
options[SKIP_EFFECTS] = true;

const parent = h(Fragment, null);
parent[CHILDREN] = [vnode];

let res;
if (
opts &&
@@ -72,14 +77,15 @@ function renderToString(vnode, context, opts) {
) {
res = _renderToStringPretty(vnode, context, opts);
} else {
res = _renderToString(vnode, context, false, undefined);
res = _renderToString(vnode, context, false, undefined, parent);
}

// options._commit, we don't schedule any effects in this library right now,
// so we can pass an empty queue to this hook.
if (options[COMMIT]) options[COMMIT](vnode, EMPTY_ARR);
options[SKIP_EFFECTS] = previousSkipEffects;
EMPTY_ARR.length = 0;

return res;
}

@@ -181,7 +187,7 @@ const isArray = Array.isArray;
const assign = Object.assign;

/** The default export is an alias of `render()`. */
function _renderToString(vnode, context, isSvgMode, selectValue) {
function _renderToString(vnode, context, isSvgMode, selectValue, parent) {
// Ignore non-rendered VNodes/values
if (vnode == null || vnode === true || vnode === false || vnode === '') {
return '';
@@ -195,13 +201,16 @@ function _renderToString(vnode, context, isSvgMode, selectValue) {
// Recurse into children / Arrays
if (isArray(vnode)) {
let rendered = '';
parent[CHILDREN] = vnode;
for (let i = 0; i < vnode.length; i++) {
rendered =
rendered + _renderToString(vnode[i], context, isSvgMode, selectValue);
rendered +
_renderToString(vnode[i], context, isSvgMode, selectValue, parent);
}
return rendered;
}

vnode[PARENT] = parent;
if (options[DIFF]) options[DIFF](vnode);

let type = vnode.type,
@@ -210,30 +219,36 @@ function _renderToString(vnode, context, isSvgMode, selectValue) {
// Invoke rendering on Components
const isComponent = typeof type === 'function';
if (isComponent) {
if (type === Fragment) {
return _renderToString(
vnode.props.children,
context,
isSvgMode,
selectValue
);
}

let rendered;
if (type.prototype && typeof type.prototype.render === 'function') {
rendered = renderClassComponent(vnode, context);
if (type === Fragment) {
rendered = props.children;
} else {
rendered = renderFunctionComponent(vnode, context);
}
if (type.prototype && typeof type.prototype.render === 'function') {
rendered = renderClassComponent(vnode, context);
} else {
rendered = renderFunctionComponent(vnode, context);
}

let component = vnode[COMPONENT];
if (component.getChildContext) {
context = assign({}, context, component.getChildContext());
let component = vnode[COMPONENT];
if (component.getChildContext) {
context = assign({}, context, component.getChildContext());
}
}

// Recurse into children before invoking the after-diff hook
const str = _renderToString(rendered, context, isSvgMode, selectValue);
const str = _renderToString(
rendered,
context,
isSvgMode,
selectValue,
vnode
);

if (options[DIFFED]) options[DIFFED](vnode);
vnode[PARENT] = undefined;

if (options.unmount) options.unmount(vnode);

return str;
}

@@ -314,13 +329,19 @@ function _renderToString(vnode, context, isSvgMode, selectValue) {
pieces = pieces + encodeEntities(children);
hasChildren = true;
} else if (isArray(children)) {
vnode[CHILDREN] = children;
for (let i = 0; i < children.length; i++) {
let child = children[i];

if (child != null && child !== false) {
let childSvgMode =
type === 'svg' || (type !== 'foreignObject' && isSvgMode);
let ret = _renderToString(child, context, childSvgMode, selectValue);
let ret = _renderToString(
child,
context,
childSvgMode,
selectValue,
vnode
);

// Skip if we received an empty string
if (ret) {
@@ -330,9 +351,16 @@ function _renderToString(vnode, context, isSvgMode, selectValue) {
}
}
} else if (children != null && children !== false && children !== true) {
vnode[CHILDREN] = [children];
let childSvgMode =
type === 'svg' || (type !== 'foreignObject' && isSvgMode);
let ret = _renderToString(children, context, childSvgMode, selectValue);
let ret = _renderToString(
children,
context,
childSvgMode,
selectValue,
vnode
);

// Skip if we received an empty string
if (ret) {
@@ -342,6 +370,8 @@ function _renderToString(vnode, context, isSvgMode, selectValue) {
}

if (options[DIFFED]) options[DIFFED](vnode);
vnode[PARENT] = undefined;
if (options.unmount) options.unmount(vnode);

if (hasChildren) {
s = s + pieces;