MCPcopy Create free account
hub / github.com/angular/angular / ɵɵvalidateAttribute

Function ɵɵvalidateAttribute

packages/core/src/sanitization/sanitization.ts:316–390  ·  view source on GitHub ↗
(value: T, tagName: string, attributeName: string)

Source from the content-addressed store, hash-verified

314 * @param attributeName The name of the attribute.
315 */
316export function ɵɵvalidateAttribute<T = any>(value: T, tagName: string, attributeName: string): T {
317 const lowerCaseTagName = tagName.toLowerCase();
318 const lowerCaseAttrName = attributeName.toLowerCase();
319
320 const index = getSelectedIndex();
321 const tNode: TNode | null = index === -1 ? null : getSelectedTNode();
322 if (tNode && tNode.type !== TNodeType.Element) {
323 return value;
324 }
325
326 // Leverage tNode.namespace if active, otherwise check both namespaced and base variants.
327 const fullTagName =
328 lowerCaseTagName[0] !== ':' && tNode?.namespace
329 ? `:${tNode.namespace}:${lowerCaseTagName}`
330 : lowerCaseTagName;
331
332 const validationConfig = SECURITY_SENSITIVE_ELEMENTS[fullTagName]?.[lowerCaseAttrName];
333
334 if (!validationConfig) {
335 return value;
336 }
337
338 const lView = getLView();
339 if (tNode && lowerCaseTagName === 'iframe') {
340 const element = getNativeByTNode(tNode, lView) as RElement;
341 enforceIframeSecurity(element as HTMLIFrameElement);
342 }
343
344 const displayTagName = tagName[0] === ':' ? tagName.split(':').pop()! : tagName;
345
346 if (typeof validationConfig !== 'boolean') {
347 if (!tNode) {
348 const errorMessage =
349 ngDevMode &&
350 `Angular has detected that the \`${attributeName}\` was applied ` +
351 `as a binding to the <${tagName}> element. ` +
352 `For security reasons, the \`${attributeName}\` can be set on the <${tagName}> element ` +
353 `as a static attribute only. \n` +
354 `To fix this, switch the \`${attributeName}\` binding to a static attribute ` +
355 `in a template or in host bindings section.`;
356 throw new RuntimeError(RuntimeErrorCode.UNSAFE_ATTRIBUTE_BINDING, errorMessage);
357 }
358
359 const element = getNativeByTNode(tNode, lView) as SVGAnimateElement;
360 const attributeNameValue = getSecuritySensitiveSVGAnimationAttributeName(
361 element,
362 validationConfig,
363 );
364
365 if (attributeNameValue) {
366 const errorMessage =
367 ngDevMode &&
368 `Angular has detected that the \`${attributeName}\` was applied ` +
369 `as a binding to the <${displayTagName}> element${getTemplateLocationDetails(lView)}. ` +
370 `For security reasons, the \`${attributeName}\` can be set on the <${displayTagName}> element ` +
371 `as a static attribute only when the "attributeName" is set to \'${attributeNameValue}\'. \n` +
372 `To fix this, switch the \`${attributeNameValue}\` binding to a static attribute ` +
373 `in a template or in host bindings section.`;

Callers

nothing calls this directly

Calls 8

getSelectedIndexFunction · 0.90
getSelectedTNodeFunction · 0.90
getLViewFunction · 0.90
getNativeByTNodeFunction · 0.90
enforceIframeSecurityFunction · 0.90
popMethod · 0.80

Tested by

no test coverage detected