MCPcopy
hub / github.com/angular/angular / addServerStyles

Function addServerStyles

packages/platform-browser/src/dom/shared_styles_host.ts:65–92  ·  view source on GitHub ↗

* Searches a DOM document's head element for style elements with a matching application * identifier attribute (`ng-app-id`) to the provide identifier and adds usage records for each. * @param doc An HTML DOM document instance. * @param appId A string containing an Angular application identifer.

(
  doc: Document,
  appId: string,
  inline: Map<string, UsageRecord<HTMLStyleElement>>,
  external: Map<string, UsageRecord<HTMLLinkElement>>,
)

Source from the content-addressed store, hash-verified

63 * @returns Whether or not styles were added.
64 */
65function addServerStyles(
66 doc: Document,
67 appId: string,
68 inline: Map<string, UsageRecord<HTMLStyleElement>>,
69 external: Map<string, UsageRecord<HTMLLinkElement>>,
70): boolean {
71 const elements = doc.head?.querySelectorAll<HTMLStyleElement | HTMLLinkElement>(
72 `style[${APP_ID_ATTRIBUTE_NAME}="${appId}"],link[${APP_ID_ATTRIBUTE_NAME}="${appId}"]`,
73 );
74
75 if (!elements || elements.length === 0) return false;
76
77 for (const styleElement of elements) {
78 styleElement.removeAttribute(APP_ID_ATTRIBUTE_NAME);
79 if (styleElement instanceof HTMLLinkElement) {
80 // Only use filename from href
81 // The href is build time generated with a unique value to prevent duplicates.
82 external.set(styleElement.href.slice(styleElement.href.lastIndexOf('/') + 1), {
83 usage: 0,
84 elements: [styleElement],
85 });
86 } else if (styleElement.textContent) {
87 inline.set(styleElement.textContent, {usage: 0, elements: [styleElement]});
88 }
89 }
90
91 return true;
92}
93
94/**
95 * Creates a `link` element for the provided external style URL.

Callers 1

constructorMethod · 0.85

Calls 2

removeAttributeMethod · 0.65
setMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…