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.javascript.host.css;
16
17 import java.net.MalformedURLException;
18 import java.net.URL;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22 import org.htmlunit.html.DomNode;
23 import org.htmlunit.html.HtmlLink;
24 import org.htmlunit.html.HtmlPage;
25 import org.htmlunit.html.HtmlStyle;
26 import org.htmlunit.javascript.HtmlUnitScriptable;
27 import org.htmlunit.javascript.configuration.JsxClass;
28 import org.htmlunit.javascript.configuration.JsxConstructor;
29 import org.htmlunit.javascript.configuration.JsxGetter;
30 import org.htmlunit.javascript.host.html.HTMLElement;
31
32 /**
33 * A JavaScript object for {@code StyleSheet}.
34 *
35 * @author Ahmed Ashour
36 * @author Ronald Brill
37 *
38 * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/StyleSheet">MDN Documentation</a>
39 */
40 @JsxClass
41 public class StyleSheet extends HtmlUnitScriptable {
42
43 private static final Log LOG = LogFactory.getLog(StyleSheet.class);
44
45 /** The HTML element which owns this stylesheet. */
46 private final HTMLElement ownerNode_;
47
48 /**
49 * Default constructor.
50 */
51 public StyleSheet() {
52 super();
53 ownerNode_ = null;
54 }
55
56 /**
57 * JavaScript constructor.
58 */
59 @JsxConstructor
60 public void jsConstructor() {
61 // nothing to do
62 }
63
64 /**
65 * Ctor.
66 * @param ownerNode the owner node
67 */
68 public StyleSheet(final HTMLElement ownerNode) {
69 super();
70 ownerNode_ = ownerNode;
71 }
72
73 /**
74 * Returns the owner node.
75 * @return the owner node
76 */
77 @JsxGetter
78 public HTMLElement getOwnerNode() {
79 return ownerNode_;
80 }
81
82 /**
83 * Returns the URL of the stylesheet.
84 * @return the URL of the stylesheet
85 */
86 @JsxGetter
87 public String getHref() {
88 if (ownerNode_ != null) {
89 final DomNode node = ownerNode_.getDomNodeOrDie();
90 if (node instanceof HtmlStyle) {
91 return null;
92 }
93 if (node instanceof HtmlLink link) {
94 final String href = link.getHrefAttribute();
95 // Expand relative URLs.
96 try {
97 final HtmlPage page = (HtmlPage) link.getPage();
98 final URL url = page.getFullyQualifiedUrl(href);
99 return url.toExternalForm();
100 }
101 catch (final MalformedURLException e) {
102 // Log the error and fall through to the return values below.
103 LOG.warn(e.getMessage(), e);
104 }
105 }
106 }
107
108 return getUri();
109 }
110
111 /**
112 * Returns this stylesheet's URI (used to resolved contained @import rules).
113 * For inline styles this is the page uri.
114 * @return this stylesheet's URI (used to resolved contained @import rules)
115 */
116 public String getUri() {
117 return null;
118 }
119 }