1 /*
2 * Copyright (c) 2002-2025 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.CSS_RP_DISPLAY_NONE;
18 import static org.htmlunit.BrowserVersionFeatures.CSS_RT_DISPLAY_RUBY_TEXT_ALWAYS;
19
20 import java.util.Map;
21
22 import org.htmlunit.SgmlPage;
23
24 /**
25 * An element that is returned for an HTML tag that is not supported by this framework.
26 *
27 * @author Mike Bowler
28 * @author David K. Taylor
29 * @author Christian Sell
30 * @author Ahmed Ashour
31 * @author Rodney Gitzel
32 * @author Ronald Brill
33 * @author Frank Danek
34 */
35 public class HtmlUnknownElement extends HtmlElement {
36
37 private boolean createdByJavascript_;
38
39 /**
40 * Creates an instance.
41 *
42 * @param page the page that contains this element
43 * @param tagName the HTML tag represented by this object
44 * @param attributes the initial attributes
45 */
46 HtmlUnknownElement(final SgmlPage page, final String tagName, final Map<String, DomAttr> attributes) {
47 super(tagName, page, attributes);
48 }
49
50 /**
51 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
52 *
53 * Marks this frame as created by javascript.
54 */
55 public void markAsCreatedByJavascript() {
56 createdByJavascript_ = true;
57 }
58
59 /**
60 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
61 *
62 * Returns true if this frame was created by javascript.
63 * @return true or false
64 */
65 public boolean wasCreatedByJavascript() {
66 return createdByJavascript_;
67 }
68
69 /**
70 * {@inheritDoc}
71 */
72 @Override
73 public DisplayStyle getDefaultStyleDisplay() {
74 switch (getTagName()) {
75 case HtmlRuby.TAG_NAME:
76 return DisplayStyle.RUBY;
77 case HtmlRb.TAG_NAME:
78 if (!hasFeature(CSS_RT_DISPLAY_RUBY_TEXT_ALWAYS)
79 && wasCreatedByJavascript() && getParentNode() == null) {
80 return DisplayStyle.BLOCK;
81 }
82 return DisplayStyle.RUBY_BASE;
83 case HtmlRp.TAG_NAME:
84 if (hasFeature(CSS_RP_DISPLAY_NONE)) {
85 return DisplayStyle.NONE;
86 }
87 if (wasCreatedByJavascript() && getParentNode() == null) {
88 return DisplayStyle.BLOCK;
89 }
90 break;
91 case HtmlRt.TAG_NAME:
92 if (!hasFeature(CSS_RT_DISPLAY_RUBY_TEXT_ALWAYS)
93 && wasCreatedByJavascript() && getParentNode() == null) {
94 return DisplayStyle.BLOCK;
95 }
96 return DisplayStyle.RUBY_TEXT;
97 case HtmlRtc.TAG_NAME:
98 if (!hasFeature(CSS_RT_DISPLAY_RUBY_TEXT_ALWAYS)
99 && wasCreatedByJavascript() && getParentNode() == null) {
100 return DisplayStyle.BLOCK;
101 }
102 return DisplayStyle.RUBY_TEXT_CONTAINER;
103 case HtmlDialog.TAG_NAME:
104 return DisplayStyle.NONE;
105 case HtmlSlot.TAG_NAME:
106 return DisplayStyle.CONTENTS;
107 default:
108 }
109 return DisplayStyle.INLINE;
110 }
111
112 /**
113 * {@inheritDoc}
114 * @return {@code true} to make generated XML readable as HTML.
115 */
116 @Override
117 protected boolean isEmptyXmlTagExpanded() {
118 return true;
119 }
120 }