Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2002-2024 Gargoyle Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.htmlunit.javascript.host;
import static org.htmlunit.javascript.configuration.SupportedBrowser.CHROME;
import static org.htmlunit.javascript.configuration.SupportedBrowser.EDGE;
import static org.htmlunit.javascript.configuration.SupportedBrowser.FF;
import static org.htmlunit.javascript.configuration.SupportedBrowser.FF_ESR;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.htmlunit.FormEncodingType;
import org.htmlunit.WebRequest;
import org.htmlunit.corejs.javascript.Context;
import org.htmlunit.corejs.javascript.ES6Iterator;
import org.htmlunit.corejs.javascript.EcmaError;
import org.htmlunit.corejs.javascript.Function;
import org.htmlunit.corejs.javascript.IteratorLikeIterable;
import org.htmlunit.corejs.javascript.NativeObject;
import org.htmlunit.corejs.javascript.ScriptRuntime;
import org.htmlunit.corejs.javascript.Scriptable;
import org.htmlunit.corejs.javascript.ScriptableObject;
import org.htmlunit.corejs.javascript.SymbolKey;
import org.htmlunit.javascript.HtmlUnitScriptable;
import org.htmlunit.javascript.JavaScriptEngine;
import org.htmlunit.javascript.configuration.JsxClass;
import org.htmlunit.javascript.configuration.JsxConstructor;
import org.htmlunit.javascript.configuration.JsxFunction;
import org.htmlunit.javascript.configuration.JsxGetter;
import org.htmlunit.javascript.configuration.JsxSymbol;
import org.htmlunit.util.NameValuePair;
import org.htmlunit.util.UrlUtils;
/**
* A JavaScript object for {@code URLSearchParams}.
*
* @author Ahmed Ashour
* @author Ronald Brill
* @author Ween Jiann
* @author cd alexndr
* @author Lai Quang Duong
*/
@JsxClass({CHROME, EDGE, FF, FF_ESR})
public class URLSearchParams extends HtmlUnitScriptable {
private static final Log LOG = LogFactory.getLog(URLSearchParams.class);
/** Constant used to register the prototype in the context. */
public static final String URL_SEARCH_PARMS_TAG = "URLSearchParams";
private URL url_;
public static final class NativeParamsIterator extends ES6Iterator {
enum Type { KEYS, VALUES, BOTH }
private final Type type_;
private final String className_;
private final transient Iterator iterator_;
public static void init(final ScriptableObject scope, final String className) {
ES6Iterator.init(scope, false, new NativeParamsIterator(className), URL_SEARCH_PARMS_TAG);
}
public NativeParamsIterator(final String className) {
iterator_ = Collections.emptyIterator();
type_ = Type.BOTH;
className_ = className;
}
public NativeParamsIterator(final Scriptable scope, final String className, final Type type,
final Iterator iterator) {
super(scope, URL_SEARCH_PARMS_TAG);
iterator_ = iterator;
type_ = type;
className_ = className;
}
@Override
public String getClassName() {
return className_;
}
@Override
protected boolean isDone(final Context cx, final Scriptable scope) {
return !iterator_.hasNext();
}
@Override
protected Object nextValue(final Context cx, final Scriptable scope) {
final NameValuePair e = iterator_.next();
switch (type_) {
case KEYS:
return e.getName();
case VALUES:
return e.getValue();
case BOTH:
return cx.newArray(scope, new Object[] {e.getName(), e.getValue()});
default:
throw new AssertionError();
}
}
}
/**
* Constructs a new instance.
*/
public URLSearchParams() {
}
/**
* Constructs a new instance for the given js url.
* @param url the base url
*/
URLSearchParams(final URL url) {
url_ = url;
}
/**
* Constructs a new instance.
* @param params the params string
*/
@JsxConstructor
public void jsConstructor(final Object params) {
url_ = new URL();
url_.jsConstructor("http://www.htmlunit.org", "");
if (params == null || JavaScriptEngine.isUndefined(params)) {
return;
}
try {
url_.setSearch(resolveParams(params));
}
catch (final EcmaError e) {
throw JavaScriptEngine.typeError("Failed to construct 'URLSearchParams': " + e.getErrorMessage());
}
catch (final MalformedURLException e) {
LOG.error(e.getMessage(), e);
}
}
/*
* Implementation follows https://url.spec.whatwg.org/#urlsearchparams-initialize
*/
private static List resolveParams(final Object params) {
// if params is a sequence
if (params instanceof Scriptable && ScriptableObject.hasProperty((Scriptable) params, SymbolKey.ITERATOR)) {
final Context cx = Context.getCurrentContext();
final Scriptable paramsScriptable = (Scriptable) params;
final List nameValuePairs = new ArrayList<>();
try (IteratorLikeIterable itr = buildIteratorLikeIterable(cx, paramsScriptable)) {
for (final Object nameValue : itr) {
if (!(nameValue instanceof Scriptable)) {
throw JavaScriptEngine.typeError("The provided value cannot be converted to a sequence.");
}
if (!ScriptableObject.hasProperty((Scriptable) nameValue, SymbolKey.ITERATOR)) {
throw JavaScriptEngine.typeError("The object must have a callable @@iterator property.");
}
try (IteratorLikeIterable nameValueItr = buildIteratorLikeIterable(cx, (Scriptable) nameValue)) {
final Iterator