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