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 static org.htmlunit.BrowserVersionFeatures.CSP_OBJECT_BLOCKED_FIRES_ERROR;
18
19 import java.util.Map;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.htmlunit.SgmlPage;
24 import org.htmlunit.WebClient;
25 import org.htmlunit.html.csp.ContentSecurityPolicy;
26 import org.htmlunit.javascript.PostponedAction;
27 import org.htmlunit.javascript.host.event.Event;
28 import org.htmlunit.javascript.host.html.HTMLObjectElement;
29 import org.htmlunit.util.StringUtils;
30 import org.htmlunit.xml.XmlPage;
31
32 /**
33 * Wrapper for the HTML element "object".
34 *
35 * @author Mike Bowler
36 * @author David K. Taylor
37 * @author Christian Sell
38 * @author Ahmed Ashour
39 * @author Ronald Brill
40 * @author Frank Danek
41 */
42 public class HtmlObject extends HtmlElement implements ValidatableHtmlElement {
43
44 private static final Log LOG = LogFactory.getLog(HtmlObject.class);
45
46 /** The HTML tag represented by this element. */
47 public static final String TAG_NAME = "object";
48
49 private String customValidity_;
50
51 /**
52 * Creates an instance of HtmlObject.
53 *
54 * @param qualifiedName the qualified name of the element type to instantiate
55 * @param page the HtmlPage that contains this element
56 * @param attributes the initial attributes
57 */
58 HtmlObject(final String qualifiedName, final SgmlPage page,
59 final Map<String, DomAttr> attributes) {
60 super(qualifiedName, page, attributes);
61 }
62
63 /**
64 * Returns the value of the attribute {@code declare}. Refer to the
65 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
66 * documentation for details on the use of this attribute.
67 *
68 * @return the value of the attribute {@code declare}
69 * or an empty string if that attribute isn't defined.
70 */
71 public final String getDeclareAttribute() {
72 return getAttributeDirect("declare");
73 }
74
75 /**
76 * Returns the value of the attribute {@code classid}. Refer to the
77 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
78 * documentation for details on the use of this attribute.
79 *
80 * @return the value of the attribute {@code classid}
81 * or an empty string if that attribute isn't defined.
82 */
83 public final String getClassIdAttribute() {
84 return getAttributeDirect("classid");
85 }
86
87 /**
88 * Returns the value of the attribute "codebase". Refer to the
89 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
90 * documentation for details on the use of this attribute.
91 *
92 * @return the value of the attribute "codebase"
93 * or an empty string if that attribute isn't defined.
94 */
95 public final String getCodebaseAttribute() {
96 return getAttributeDirect("codebase");
97 }
98
99 /**
100 * Returns the value of the attribute {@code data}. Refer to the
101 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
102 * documentation for details on the use of this attribute.
103 *
104 * @return the value of the attribute {@code data}
105 * or an empty string if that attribute isn't defined.
106 */
107 public final String getDataAttribute() {
108 return getAttributeDirect("data");
109 }
110
111 /**
112 * Returns the value of the attribute {@code type}. Refer to the
113 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
114 * documentation for details on the use of this attribute.
115 *
116 * @return the value of the attribute {@code type}
117 * or an empty string if that attribute isn't defined.
118 */
119 public final String getTypeAttribute() {
120 return getAttributeDirect(TYPE_ATTRIBUTE);
121 }
122
123 /**
124 * Returns the value of the attribute "codetype". Refer to the
125 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
126 * documentation for details on the use of this attribute.
127 *
128 * @return the value of the attribute "codetype"
129 * or an empty string if that attribute isn't defined.
130 */
131 public final String getCodeTypeAttribute() {
132 return getAttributeDirect("codetype");
133 }
134
135 /**
136 * Returns the value of the attribute {@code archive}. Refer to the
137 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
138 * documentation for details on the use of this attribute.
139 *
140 * @return the value of the attribute {@code archive}
141 * or an empty string if that attribute isn't defined.
142 */
143 public final String getArchiveAttribute() {
144 return getAttributeDirect("archive");
145 }
146
147 /**
148 * Returns the value of the attribute {@code standby}. Refer to the
149 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
150 * documentation for details on the use of this attribute.
151 *
152 * @return the value of the attribute {@code standby}
153 * or an empty string if that attribute isn't defined.
154 */
155 public final String getStandbyAttribute() {
156 return getAttributeDirect("standby");
157 }
158
159 /**
160 * Returns the value of the attribute {@code height}. Refer to the
161 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
162 * documentation for details on the use of this attribute.
163 *
164 * @return the value of the attribute {@code height}
165 * or an empty string if that attribute isn't defined.
166 */
167 public final String getHeightAttribute() {
168 return getAttributeDirect("height");
169 }
170
171 /**
172 * Returns the value of the attribute {@code width}. Refer to the
173 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
174 * documentation for details on the use of this attribute.
175 *
176 * @return the value of the attribute {@code width}
177 * or an empty string if that attribute isn't defined.
178 */
179 public final String getWidthAttribute() {
180 return getAttributeDirect("width");
181 }
182
183 /**
184 * Returns the value of the attribute {@code usemap}. Refer to the
185 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
186 * documentation for details on the use of this attribute.
187 *
188 * @return the value of the attribute {@code usemap}
189 * or an empty string if that attribute isn't defined.
190 */
191 public final String getUseMapAttribute() {
192 return getAttributeDirect("usemap");
193 }
194
195 /**
196 * Returns the value of the attribute {@code name}. Refer to the
197 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
198 * documentation for details on the use of this attribute.
199 *
200 * @return the value of the attribute {@code name}
201 * or an empty string if that attribute isn't defined.
202 */
203 public final String getNameAttribute() {
204 return getAttributeDirect(NAME_ATTRIBUTE);
205 }
206
207 /**
208 * Returns the value of the attribute {@code tabindex}. Refer to the
209 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
210 * documentation for details on the use of this attribute.
211 *
212 * @return the value of the attribute {@code tabindex}
213 * or an empty string if that attribute isn't defined.
214 */
215 public final String getTabIndexAttribute() {
216 return getAttributeDirect("tabindex");
217 }
218
219 /**
220 * Returns the value of the attribute {@code align}. Refer to the
221 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
222 * documentation for details on the use of this attribute.
223 *
224 * @return the value of the attribute {@code align}
225 * or an empty string if that attribute isn't defined.
226 */
227 public final String getAlignAttribute() {
228 return getAttributeDirect("align");
229 }
230
231 /**
232 * Returns the value of the attribute {@code border}. Refer to the
233 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
234 * documentation for details on the use of this attribute.
235 *
236 * @return the value of the attribute {@code border}
237 * or an empty string if that attribute isn't defined.
238 */
239 public final String getBorderAttribute() {
240 return getAttributeDirect("border");
241 }
242
243 /**
244 * Returns the value of the attribute {@code hspace}. Refer to the
245 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
246 * documentation for details on the use of this attribute.
247 *
248 * @return the value of the attribute {@code hspace}
249 * or an empty string if that attribute isn't defined.
250 */
251 public final String getHspaceAttribute() {
252 return getAttributeDirect("hspace");
253 }
254
255 /**
256 * Returns the value of the attribute {@code vspace}. Refer to the
257 * <a href="http://www.w3.org/TR/html401/">HTML 4.01</a>
258 * documentation for details on the use of this attribute.
259 *
260 * @return the value of the attribute {@code vspace}
261 * or an empty string if that attribute isn't defined.
262 */
263 public final String getVspaceAttribute() {
264 return getAttributeDirect("vspace");
265 }
266
267 /**
268 * {@inheritDoc}
269 * Checks the data url against the Content-Security-Policy (object-src).
270 */
271 @Override
272 protected void onAddedToPage() {
273 super.onAddedToPage();
274 checkContentSecurityPolicy(getAttributeDirect("data"));
275 }
276
277 /**
278 * {@inheritDoc}
279 * Checks the new data url against the Content-Security-Policy (object-src).
280 */
281 @Override
282 protected void setAttributeNS(final String namespaceURI, final String qualifiedName, final String value,
283 final boolean notifyAttributeChangeListeners, final boolean notifyMutationObservers) {
284 final String qualifiedNameLC = StringUtils.toRootLowerCase(qualifiedName);
285 final boolean changed = "data".equals(qualifiedNameLC)
286 && !getAttributeNS(namespaceURI, qualifiedNameLC).equals(value);
287
288 super.setAttributeNS(namespaceURI, qualifiedName, value,
289 notifyAttributeChangeListeners, notifyMutationObservers);
290
291 if (changed && isAttachedToPage()) {
292 checkContentSecurityPolicy(value);
293 }
294 }
295
296 /**
297 * Checks the url against the Content-Security-Policy (object-src); if the url is blocked,
298 * Firefox fires the error event (async).
299 */
300 private void checkContentSecurityPolicy(final String data) {
301 if (ContentSecurityPolicy.allowsObject(this, data)) {
302 return;
303 }
304
305 final HtmlPage htmlPage = getHtmlPageOrNull();
306 if (htmlPage == null || !hasFeature(CSP_OBJECT_BLOCKED_FIRES_ERROR)) {
307 return;
308 }
309
310 final WebClient client = htmlPage.getWebClient();
311 if (!client.isJavaScriptEngineEnabled()) {
312 return;
313 }
314
315 final PostponedAction action = new PostponedAction(htmlPage, "HtmlObject.blocked") {
316 @Override
317 public void execute() {
318 HtmlObject.this.fireEvent(Event.TYPE_ERROR);
319 }
320 };
321 if (READY_STATE_LOADING.equals(htmlPage.getReadyState())) {
322 htmlPage.addAfterLoadAction(action);
323 }
324 else {
325 client.getJavaScriptEngine().addPostponedAction(action);
326 }
327 }
328
329 /**
330 * Initialize the clsid.
331 * {@inheritDoc}
332 */
333 @Override
334 public void onAllChildrenAddedToPage(final boolean postponed) {
335 if (getOwnerDocument() instanceof XmlPage) {
336 return;
337 }
338
339 if (LOG.isDebugEnabled()) {
340 LOG.debug("Object node added: " + asXml());
341 }
342
343 final String clsId = getClassIdAttribute();
344 if (ATTRIBUTE_NOT_DEFINED != clsId && getPage().getWebClient().isJavaScriptEngineEnabled()) {
345 ((HTMLObjectElement) getScriptableObject()).setClassid(clsId);
346 }
347 }
348
349 /**
350 * {@inheritDoc}
351 */
352 @Override
353 public DisplayStyle getDefaultStyleDisplay() {
354 return DisplayStyle.INLINE;
355 }
356
357 /**
358 * {@inheritDoc}
359 */
360 @Override
361 public boolean isValid() {
362 return true;
363 }
364
365 /**
366 * {@inheritDoc}
367 */
368 @Override
369 public boolean willValidate() {
370 return false;
371 }
372
373 /**
374 * {@inheritDoc}
375 */
376 @Override
377 public String getCustomValidity() {
378 return customValidity_;
379 }
380
381 /**
382 * {@inheritDoc}
383 */
384 @Override
385 public void setCustomValidity(final String message) {
386 customValidity_ = message;
387 }
388
389 @Override
390 public boolean isCustomErrorValidityState() {
391 return !StringUtils.isEmptyOrNull(customValidity_);
392 }
393
394 @Override
395 public boolean isValidValidityState() {
396 return !isCustomErrorValidityState();
397 }
398 }