-
Notifications
You must be signed in to change notification settings - Fork 47k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
renderToString is a legacy server API which used a trick to avoid hav…
…ing the DOCTYPE included when rendering full documents by setting the root formatcontext to HTML_MODE rather than ROOT_HTML_MODE. Previously this was of little consequence but with Float the Root mode started to be used for things like determining if we could flush hoistable elements yet. In issue #27177 we see that hoisted elements can appear before the <html> tag when using a legacy API `renderToString`. This change makes the FormatContext responsible for providing the DOCTYPE chunks rather than the insertionMode. Alongside this the legacy mode now uses the ROOT_HTML_MODE insertionMode so that it follows standard logic for hoisting. The reason I went with this approach is it allows us to avoid any runtime checks for whether we should or should not include a doctype. The only runtime perf consequence is that for legacy APIs when you render <html> there is an extra empty chunk to write which is of tivial consequence
- Loading branch information
Showing
3 changed files
with
64 additions
and
8 deletions.
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
41 changes: 41 additions & 0 deletions
41
packages/react-dom/src/__tests__/ReactDOMLegacyFloat-test.js
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,41 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @emails react-core | ||
* @jest-environment ./scripts/jest/ReactDOMServerIntegrationEnvironment | ||
*/ | ||
|
||
'use strict'; | ||
|
||
let React; | ||
let ReactDOMFizzServer; | ||
|
||
describe('ReactDOMFloat', () => { | ||
beforeEach(() => { | ||
jest.resetModules(); | ||
|
||
React = require('react'); | ||
ReactDOMFizzServer = require('react-dom/server'); | ||
}); | ||
|
||
// fixes #27177 | ||
// @gate enableFloat | ||
it('does not hoist above the <html> tag', async () => { | ||
const result = ReactDOMFizzServer.renderToString( | ||
<html> | ||
<head> | ||
<script src="foo" /> | ||
<meta charSet="utf-8" /> | ||
<title>title</title> | ||
</head> | ||
</html>, | ||
); | ||
|
||
expect(result).toEqual( | ||
'<html><head><meta charSet="utf-8"/><title>title</title><script src="foo"></script></head></html>', | ||
); | ||
}); | ||
}); |