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;
16
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import org.htmlunit.corejs.javascript.Scriptable;
21 import org.htmlunit.javascript.HtmlUnitScriptable;
22 import org.htmlunit.javascript.JavaScriptEngine;
23 import org.htmlunit.javascript.configuration.JsxClass;
24 import org.htmlunit.javascript.configuration.JsxConstructor;
25 import org.htmlunit.javascript.configuration.JsxFunction;
26 import org.htmlunit.javascript.configuration.JsxGetter;
27 import org.htmlunit.javascript.configuration.JsxSymbol;
28
29 /**
30 * JavaScript host object for {@code MimeTypeArray}.
31 *
32 * @author Marc Guillemot
33 * @author Ahmed Ashour
34 * @author Ronald Brill
35 *
36 * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/MimeTypeArray">MDN Documentation</a>
37 */
38 @JsxClass
39 public class MimeTypeArray extends HtmlUnitScriptable {
40
41 private final List<MimeType> elements_ = new ArrayList<>();
42
43 /**
44 * Creates an instance of this object.
45 */
46 @JsxConstructor
47 public void jsConstructor() {
48 // nothing to do
49 }
50
51 /**
52 * Returns the MIME type string for the given element.
53 *
54 * @param element a {@link MimeType}
55 * @return the MIME type string
56 */
57 protected String getItemName(final Object element) {
58 return ((MimeType) element).getType();
59 }
60
61 /**
62 * Returns the {@link MimeType} at the given index.
63 *
64 * @param index the index
65 * @return the {@link MimeType} at the given position
66 */
67 @JsxFunction
68 public MimeType item(final int index) {
69 return elements_.get(index);
70 }
71
72 /**
73 * {@inheritDoc}
74 */
75 @Override
76 protected Object getWithPreemption(final String name) {
77 final MimeType response = namedItem(name);
78 if (response != null) {
79 return response;
80 }
81 return NOT_FOUND;
82 }
83
84 /**
85 * {@inheritDoc}
86 */
87 @Override
88 public boolean has(final String name, final Scriptable start) {
89 if (NOT_FOUND != getWithPreemption(name)) {
90 return true;
91 }
92
93 return super.has(name, start);
94 }
95
96 /**
97 * Returns the element at the specified index, or {@code null} if the index is invalid.
98 * {@inheritDoc}
99 */
100 @Override
101 public final MimeType get(final int index, final Scriptable start) {
102 final MimeTypeArray array = (MimeTypeArray) start;
103 final List<MimeType> elements = array.elements_;
104
105 if (index >= 0 && index < elements.size()) {
106 return elements.get(index);
107 }
108 return null;
109 }
110
111 /**
112 * Returns the {@link MimeType} with the given name.
113 *
114 * @param name the MIME type string to look up
115 * @return the matching {@link MimeType}, or {@code null} if not found
116 */
117 @JsxFunction
118 public MimeType namedItem(final String name) {
119 for (final MimeType element : elements_) {
120 if (name.equals(getItemName(element))) {
121 return element;
122 }
123 }
124 return null;
125 }
126
127 /**
128 * Returns the number of MIME types in this array.
129 *
130 * @return the number of elements
131 */
132 @JsxGetter
133 public int getLength() {
134 return elements_.size();
135 }
136
137 /**
138 * Adds a {@link MimeType} to this array.
139 *
140 * @param element the element to add
141 */
142 void add(final MimeType element) {
143 elements_.add(element);
144 }
145
146 /**
147 * Returns an iterator over the values in this array.
148 *
149 * @return the iterator
150 */
151 @JsxSymbol
152 public Scriptable iterator() {
153 return JavaScriptEngine.newArrayIteratorTypeValues(getParentScope(), this);
154 }
155 }