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.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 @JsxClass
39 public class StyleSheet extends HtmlUnitScriptable {
40
41 private static final Log LOG = LogFactory.getLog(StyleSheet.class);
42
43 /** The HTML element which owns this stylesheet. */
44 private final HTMLElement ownerNode_;
45
46 /**
47 * Default constructor.
48 */
49 public StyleSheet() {
50 super();
51 ownerNode_ = null;
52 }
53
54 /**
55 * JavaScript constructor.
56 */
57 @JsxConstructor
58 public void jsConstructor() {
59 // nothing to do
60 }
61
62 /**
63 * Ctor.
64 * @param ownerNode the owner node
65 */
66 public StyleSheet(final HTMLElement ownerNode) {
67 super();
68 ownerNode_ = ownerNode;
69 }
70
71 /**
72 * Returns the owner node.
73 * @return the owner node
74 */
75 @JsxGetter
76 public HTMLElement getOwnerNode() {
77 return ownerNode_;
78 }
79
80 /**
81 * Returns the URL of the stylesheet.
82 * @return the URL of the stylesheet
83 */
84 @JsxGetter
85 public String getHref() {
86 if (ownerNode_ != null) {
87 final DomNode node = ownerNode_.getDomNodeOrDie();
88 if (node instanceof HtmlStyle) {
89 return null;
90 }
91 if (node instanceof HtmlLink) {
92 // <link rel="stylesheet" type="text/css" href="..." />
93 final HtmlLink link = (HtmlLink) node;
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 }