MCPcopy Create free account
hub / github.com/angular/angular / Meta

Class Meta

packages/platform-browser/src/browser/meta.ts:62–178  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

60 */
61@Service()
62export class Meta {
63 private readonly _doc = inject(DOCUMENT);
64 private readonly _dom = getDOM();
65 private _cachedHead: HTMLHeadElement | undefined;
66
67 /**
68 * Retrieves or creates a specific `<meta>` tag element in the current HTML document.
69 * In searching for an existing tag, Angular attempts to match the `name` or `property` attribute
70 * values in the provided tag definition, and verifies that all other attribute values are equal.
71 * If an existing element is found, it is returned and is not modified in any way.
72 * @param tag The definition of a `<meta>` element to match or create.
73 * @param forceCreation True to create a new element without checking whether one already exists.
74 * @returns The existing element with the same attributes and values if found,
75 * the new element if no match is found, or `null` if the tag parameter is not defined.
76 */
77 addTag(tag: MetaDefinition, forceCreation: boolean = false): HTMLMetaElement | null {
78 if (!tag) return null;
79 return this._getOrCreateElement(tag, forceCreation);
80 }
81
82 /**
83 * Retrieves or creates a set of `<meta>` tag elements in the current HTML document.
84 * In searching for an existing tag, Angular attempts to match the `name` or `property` attribute
85 * values in the provided tag definition, and verifies that all other attribute values are equal.
86 * @param tags An array of tag definitions to match or create.
87 * @param forceCreation True to create new elements without checking whether they already exist.
88 * @returns The matching elements if found, or the new elements.
89 */
90 addTags(tags: MetaDefinition[], forceCreation: boolean = false): HTMLMetaElement[] {
91 return tags
92 .filter((tag): tag is MetaDefinition => !!tag)
93 .map((tag) => this._getOrCreateElement(tag, forceCreation));
94 }
95
96 /**
97 * Retrieves a `<meta>` tag element in the current HTML document.
98 * @param attrSelector The tag attribute and value to match against, in the format
99 * `"tag_attribute='value string'"`.
100 * @returns The matching element, if any.
101 */
102 getTag(attrSelector: string): HTMLMetaElement | null {
103 if (!attrSelector) return null;
104 const meta = this._doc.querySelector<HTMLMetaElement>(buildMetaSelector(attrSelector));
105 return isMetaTag(meta) ? meta : null;
106 }
107
108 /**
109 * Retrieves a set of `<meta>` tag elements in the current HTML document.
110 * @param attrSelector The tag attribute and value to match against, in the format
111 * `"tag_attribute='value string'"`.
112 * @returns The matching elements, if any.
113 */
114 getTags(attrSelector: string): HTMLMetaElement[] {
115 if (!attrSelector) return [];
116 const list = this._doc.querySelectorAll<HTMLMetaElement>(buildMetaSelector(attrSelector));
117 return list ? Array.from(list).filter((elem) => isMetaTag(elem)) : [];
118 }
119

Callers

nothing calls this directly

Calls 2

injectFunction · 0.90
getDOMFunction · 0.85

Tested by

no test coverage detected