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.net.URI;
18 import java.net.URISyntaxException;
19 import java.util.Map;
20
21 import org.htmlunit.SgmlPage;
22 import org.htmlunit.util.StringUtils;
23
24 /**
25 * Wrapper for the HTML element "input" where type is "url".
26 *
27 * @author Ahmed Ashour
28 * @author Ronald Brill
29 * @author Frank Danek
30 * @author Anton Demydenko
31 */
32 public class HtmlUrlInput extends HtmlSelectableTextInput implements LabelableElement {
33
34 /**
35 * Creates an instance.
36 *
37 * @param qualifiedName the qualified name of the element type to instantiate
38 * @param page the page that contains this element
39 * @param attributes the initial attributes
40 */
41 HtmlUrlInput(final String qualifiedName, final SgmlPage page,
42 final Map<String, DomAttr> attributes) {
43 super(qualifiedName, page, attributes);
44 setRawValue(getValue().trim());
45 }
46
47 /**
48 * {@inheritDoc}
49 */
50 @Override
51 public String getValue() {
52 final String raw = getRawValue();
53 if (StringUtils.isBlank(raw)) {
54 return "";
55 }
56 return raw;
57 }
58
59 /**
60 * {@inheritDoc}
61 */
62 @Override
63 public void setDefaultChecked(final boolean defaultChecked) {
64 // Empty.
65 }
66
67 /**
68 * {@inheritDoc}
69 */
70 @Override
71 protected boolean isPatternSupported() {
72 return true;
73 }
74
75 /**
76 * {@inheritDoc}
77 */
78 @Override
79 protected boolean isBlankPatternValidated() {
80 return false;
81 }
82
83 /**
84 * {@inheritDoc}
85 */
86 @Override
87 protected boolean isMinMaxLengthSupported() {
88 return true;
89 }
90
91 /**
92 * {@inheritDoc}
93 * Per spec, a non-empty value must be a valid absolute URL. An empty value
94 * is never a type mismatch on its own (that's valueMissing's concern, if
95 * 'required' is set). {@link java.net.URI} is used as a practical
96 * approximation of the WHATWG URL Standard parser this codebase doesn't
97 * implement -- it isn't a perfect match for every edge case a real
98 * browser's parser accepts or rejects, but requiring an absolute URI
99 * (a URI with a scheme) captures the core constraint correctly.
100 */
101 @Override
102 public boolean hasTypeMismatchValidityState() {
103 final String value = getValue();
104 if (StringUtils.isEmptyOrNull(value)) {
105 return false;
106 }
107
108 try {
109 return !new URI(value.trim()).isAbsolute();
110 }
111 catch (final URISyntaxException e) {
112 return true;
113 }
114 }
115
116 /**
117 * {@inheritDoc}
118 */
119 @Override
120 public String getTypeMismatchMessage() {
121 return "Please enter a URL.";
122 }
123 }