fix(ivy): ComponentFactory.create should clear host element content (#33487)
Prior to this change, ComponentFactory.create function invocation in Ivy retained the content of the host element (in case host element reference or CSS seelctor is provided as an argument). This behavior is different in View Engine, where the content of the host element was cleared, except for the case when ShadowDom encapsulation is used (to make sure native slot projection works). This commit aligns Ivy and View Engine and makes sure the host element is cleared before component content insertion. PR Close #33487
This commit is contained in:

committed by
Kara Erickson

parent
f750f187d5
commit
d67a38b7f5
@ -118,7 +118,8 @@ export function renderComponent<T>(
|
||||
|
||||
// The first index of the first selector is the tag name.
|
||||
const componentTag = componentDef.selectors ![0] ![0] as string;
|
||||
const hostRNode = locateHostElement(rendererFactory, opts.host || componentTag);
|
||||
const hostRNode =
|
||||
locateHostElement(rendererFactory, opts.host || componentTag, componentDef.encapsulation);
|
||||
const rootFlags = componentDef.onPush ? LViewFlags.Dirty | LViewFlags.IsRoot :
|
||||
LViewFlags.CheckAlways | LViewFlags.IsRoot;
|
||||
const rootContext = createRootContext(opts.scheduler, opts.playerHandler);
|
||||
|
@ -134,7 +134,7 @@ export class ComponentFactory<T> extends viewEngine_ComponentFactory<T> {
|
||||
const sanitizer = rootViewInjector.get(Sanitizer, null);
|
||||
|
||||
const hostRNode = rootSelectorOrNode ?
|
||||
locateHostElement(rendererFactory, rootSelectorOrNode) :
|
||||
locateHostElement(rendererFactory, rootSelectorOrNode, this.componentDef.encapsulation) :
|
||||
elementCreate(this.selector, rendererFactory.createRenderer(null, this.componentDef), null);
|
||||
|
||||
const rootFlags = this.componentDef.onPush ? LViewFlags.Dirty | LViewFlags.IsRoot :
|
||||
|
@ -8,6 +8,7 @@
|
||||
import {Injector} from '../../di';
|
||||
import {ErrorHandler} from '../../error_handler';
|
||||
import {CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA, SchemaMetadata} from '../../metadata/schema';
|
||||
import {ViewEncapsulation} from '../../metadata/view';
|
||||
import {validateAgainstEventAttributes, validateAgainstEventProperties} from '../../sanitization/sanitization';
|
||||
import {Sanitizer} from '../../sanitization/sanitizer';
|
||||
import {assertDataInRange, assertDefined, assertDomNode, assertEqual, assertGreaterThan, assertNotEqual, assertNotSame} from '../../util/assert';
|
||||
@ -678,32 +679,50 @@ function createViewBlueprint(bindingStartIndex: number, initialViewLength: numbe
|
||||
return blueprint as LView;
|
||||
}
|
||||
|
||||
export function createError(text: string, token: any) {
|
||||
function createError(text: string, token: any) {
|
||||
return new Error(`Renderer: ${text} [${stringifyForError(token)}]`);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Locates the host native element, used for bootstrapping existing nodes into rendering pipeline.
|
||||
*
|
||||
* @param elementOrSelector Render element or CSS selector to locate the element.
|
||||
*/
|
||||
export function locateHostElement(
|
||||
factory: RendererFactory3, elementOrSelector: RElement | string): RElement|null {
|
||||
const defaultRenderer = factory.createRenderer(null, null);
|
||||
const rNode = typeof elementOrSelector === 'string' ?
|
||||
(isProceduralRenderer(defaultRenderer) ?
|
||||
defaultRenderer.selectRootElement(elementOrSelector) :
|
||||
defaultRenderer.querySelector(elementOrSelector)) :
|
||||
elementOrSelector;
|
||||
if (ngDevMode && !rNode) {
|
||||
function assertHostNodeExists(rElement: RElement, elementOrSelector: RElement | string) {
|
||||
if (!rElement) {
|
||||
if (typeof elementOrSelector === 'string') {
|
||||
throw createError('Host node with selector not found:', elementOrSelector);
|
||||
} else {
|
||||
throw createError('Host node is required:', elementOrSelector);
|
||||
}
|
||||
}
|
||||
return rNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Locates the host native element, used for bootstrapping existing nodes into rendering pipeline.
|
||||
*
|
||||
* @param rendererFactory Factory function to create renderer instance.
|
||||
* @param elementOrSelector Render element or CSS selector to locate the element.
|
||||
* @param encapsulation View Encapsulation defined for component that requests host element.
|
||||
*/
|
||||
export function locateHostElement(
|
||||
rendererFactory: RendererFactory3, elementOrSelector: RElement | string,
|
||||
encapsulation: ViewEncapsulation): RElement {
|
||||
const renderer = rendererFactory.createRenderer(null, null);
|
||||
|
||||
if (isProceduralRenderer(renderer)) {
|
||||
// When using native Shadow DOM, do not clear host element to allow native slot projection
|
||||
const preserveContent = encapsulation === ViewEncapsulation.ShadowDom;
|
||||
return renderer.selectRootElement(elementOrSelector, preserveContent);
|
||||
}
|
||||
|
||||
let rElement = typeof elementOrSelector === 'string' ?
|
||||
renderer.querySelector(elementOrSelector) ! :
|
||||
elementOrSelector;
|
||||
ngDevMode && assertHostNodeExists(rElement, elementOrSelector);
|
||||
|
||||
// Always clear host element's content when Renderer3 is in use. For procedural renderer case we
|
||||
// make it depend on whether ShadowDom encapsulation is used (in which case the content should be
|
||||
// preserved to allow native slot projection). ShadowDom encapsulation requires procedural
|
||||
// renderer, and procedural renderer case is handled above.
|
||||
rElement.textContent = '';
|
||||
|
||||
return rElement;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -75,7 +75,7 @@ export interface ProceduralRenderer3 {
|
||||
appendChild(parent: RElement, newChild: RNode): void;
|
||||
insertBefore(parent: RNode, newChild: RNode, refChild: RNode|null): void;
|
||||
removeChild(parent: RElement, oldChild: RNode, isHostElement?: boolean): void;
|
||||
selectRootElement(selectorOrNode: string|any): RElement;
|
||||
selectRootElement(selectorOrNode: string|any, preserveContent?: boolean): RElement;
|
||||
|
||||
parentNode(node: RNode): RElement|null;
|
||||
nextSibling(node: RNode): RNode|null;
|
||||
@ -155,6 +155,7 @@ export interface RElement extends RNode {
|
||||
style: RCssStyleDeclaration;
|
||||
classList: RDomTokenList;
|
||||
className: string;
|
||||
textContent: string|null;
|
||||
setAttribute(name: string, value: string): void;
|
||||
removeAttribute(name: string): void;
|
||||
setAttributeNS(namespaceURI: string, qualifiedName: string, value: string): void;
|
||||
|
Reference in New Issue
Block a user