1 /*
2 * Copyright (c) 2002-2026 Gargoyle Software Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * https://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 package org.htmlunit.html;
16
17 import java.util.Map;
18
19 import org.htmlunit.HttpHeader;
20 import org.htmlunit.ReferrerPolicy;
21 import org.htmlunit.SgmlPage;
22 import org.htmlunit.html.csp.ContentSecurityPolicy;
23
24 /**
25 * Wrapper for the HTML element "meta".
26 *
27 * @author Mike Bowler
28 * @author Christian Sell
29 * @author Ahmed Ashour
30 * @author Frank Danek
31 * @author Ronald Brill
32 */
33 public class HtmlMeta extends HtmlElement {
34
35 /** The HTML tag represented by this element. */
36 public static final String TAG_NAME = "meta";
37
38 private boolean contentSecurityPolicyProcessed_;
39
40 /**
41 * Creates an instance of HtmlMeta.
42 *
43 * @param qualifiedName the qualified name of the element type to instantiate
44 * @param page the HtmlPage that contains this element
45 * @param attributes the initial attributes
46 */
47 HtmlMeta(final String qualifiedName, final SgmlPage page,
48 final Map<String, DomAttr> attributes) {
49 super(qualifiedName, page, attributes);
50
51 // <meta http-equiv='set-cookie'> is ignored like in the browsers
52 // (the HTML spec says user agents must ignore this pragma)
53 }
54
55 /**
56 * {@inheritDoc}
57 * Applies a Content-Security-Policy defined by this element; like in browsers this
58 * is only done if the element is a child of the head element.
59 */
60 @Override
61 public void onAllChildrenAddedToPage(final boolean postponed) {
62 super.onAllChildrenAddedToPage(postponed);
63
64 if (!contentSecurityPolicyProcessed_
65 && HttpHeader.CONTENT_SECURIRY_POLICY.equalsIgnoreCase(getHttpEquivAttribute().trim())
66 && getParentNode() instanceof HtmlHead
67 && getPage() instanceof HtmlPage htmlPage) {
68 contentSecurityPolicyProcessed_ = true;
69 ContentSecurityPolicy.addMetaPolicy(htmlPage, getContentAttribute());
70 }
71
72 // a meta element with the name 'referrer' sets the referrer policy of the document
73 // (also in the body and if added by script)
74 if ("referrer".equalsIgnoreCase(getNameAttribute().trim())
75 && isAttachedToPage()
76 && getPage() instanceof HtmlPage htmlPage) {
77 final ReferrerPolicy policy = ReferrerPolicy.fromMetaContent(getContentAttribute());
78 if (policy != null) {
79 htmlPage.setReferrerPolicy(policy);
80 }
81 }
82 }
83
84 /**
85 * {@inheritDoc}
86 */
87 @Override
88 public boolean mayBeDisplayed() {
89 return false;
90 }
91
92 /**
93 * Returns the value of the attribute {@code http-equiv}. Refer to the
94 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
95 * documentation for details on the use of this attribute.
96 *
97 * @return the value of the attribute {@code http-equiv}
98 * or an empty string if that attribute isn't defined.
99 */
100 public final String getHttpEquivAttribute() {
101 return getAttribute("http-equiv");
102 }
103
104 /**
105 * Returns the value of the attribute {@code name}. Refer to the
106 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
107 * documentation for details on the use of this attribute.
108 *
109 * @return the value of the attribute {@code name}
110 * or an empty string if that attribute isn't defined.
111 */
112 public final String getNameAttribute() {
113 return getAttributeDirect(NAME_ATTRIBUTE);
114 }
115
116 /**
117 * Returns the value of the attribute {@code content}. Refer to the
118 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
119 * documentation for details on the use of this attribute.
120 *
121 * @return the value of the attribute {@code content}
122 * or an empty string if that attribute isn't defined.
123 */
124 public final String getContentAttribute() {
125 return getAttributeDirect("content");
126 }
127
128 /**
129 * Returns the value of the attribute {@code scheme}. Refer to the
130 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
131 * documentation for details on the use of this attribute.
132 *
133 * @return the value of the attribute {@code scheme}
134 * or an empty string if that attribute isn't defined.
135 */
136 public final String getSchemeAttribute() {
137 return getAttributeDirect("scheme");
138 }
139
140 /**
141 * {@inheritDoc}
142 */
143 @Override
144 public DisplayStyle getDefaultStyleDisplay() {
145 return DisplayStyle.NONE;
146 }
147 }