Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

[p5.js 2.0+ Bug Report]: getURLParams() mis-parses valueless parameters and never percent-decodes values

未关闭 适合新手
#9,241 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 3 天内回复

@mayanksingh-7974 已经在做这个了。

开始于 2026年10月11日。

  • #9274 来自 @mayanksingh-7974 —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
88/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
javascript
领域
frontend

调研方向

从 src/core/environment.js:1058 中的 getURLParams 开始,先运行 issue 里的四个独立用例(内联 regex 片段),在改动任何东西之前查看误解析现象。建议的替换是 Object.fromEntries(new URLSearchParams(location.search)),它能处理无值键、百分号解码以及将 + 视为空格;同时去掉 lastIndex 的 workaround。在 environment 单元测试下添加针对 ?debug&x=1、?a=1&b、?name=Hello%20World 和 ?q=a+b 的回归测试,并检查 getURLParams 参考文档中关于 double-decode 的说明。

由索引模型根据 Issue 内容生成。

描述

Most appropriate sub-area of p5.js?
  • Core/Environment/Rendering
p5.js version

2.3.2 (verified on main @ 94fb07d)

Web browser and version

All — the parser is a hand-rolled regex, not a browser API.

Operating system

All

Steps to reproduce this
Steps:
  1. Open any sketch with ?debug&x=1 appended to the URL.
  2. Log getURLParams().
  3. It returns { debug: "x" } — a pair that does not exist in the query string — and x=1 is gone entirely.
  4. Try ?name=Hello%20World: the value comes back still percent-encoded.
Snippet:
function setup() {
  print(getURLParams());
}

The parser can be run standalone to see all the cases:

const re = /[?&]([^&=]+)(?:[&=])([^&=]+)/gim;
function current(search) {
  let m; const v = {};
  while ((m = re.exec(search)) != null) {
    if (m.index === re.lastIndex) re.lastIndex++;
    v[m[1]] = m[2];
  }
  return v;
}

current('?debug&x=1');           // { debug: "x" }           expected { debug: "", x: "1" }
current('?a=1&b');               // { a: "1" }               expected { a: "1", b: "" }
current('?name=Hello%20World');  // { name: "Hello%20World" } expected { name: "Hello World" }
current('?q=a+b');               // { q: "a+b" }             expected { q: "a b" }

Cause

// src/core/environment.js:1058
fn.getURLParams = function () {
  const re = /[?&]([^&=]+)(?:[&=])([^&=]+)/gim;
  let m;
  const v = {};
  while ((m = re.exec(location.search)) != null) {
    if (m.index === re.lastIndex) {
      re.lastIndex++;
    }
    v[m[1]] = m[2];
  }
  return v;
};

Three problems:

  1. The separator group accepts & as well as =. (?:[&=]) means a parameter with no value matches &debug&x as key debug, separator &, value x — it absorbs the next parameter's name as its own value, and consumes the real x=1 pair along with it. A valueless parameter does not just get skipped; it corrupts its neighbour.
  2. Nothing is decoded. No decodeURIComponent, and + is not translated to a space, so any value containing a space, &, / or a non-ASCII character arrives in its encoded form.
  3. A repeated key keeps only the last occurrence, and the returned object is a {} literal, so it inherits toString, constructor, hasOwnProperty and friends. Code that tests if (params.constructor) gets a surprising truthy value.

Expected behaviour

Query strings should be parsed per the URL standard: values decoded, + treated as a space, and a parameter with no value reported as an empty string rather than absorbing the parameter after it.

Suggested fix

The platform already does all of this correctly, and URLSearchParams has been available in every browser p5 2.x supports for years:

fn.getURLParams = function () {
  return Object.fromEntries(new URLSearchParams(location.search));
};

That also removes the lastIndex workaround (needed only because of the zero-length-match hazard in the hand-rolled loop) and the prototype-inherited keys.

Two notes on compatibility, since this is a behaviour change and not purely a fix:

  • Valueless parameters start appearing as '' instead of being mis-parsed. Any sketch relying on the current output for ?flag was getting wrong data, but it is still a change worth a line in the release notes.
  • Values arrive decoded. A sketch that was calling decodeURIComponent() on the result itself would now double-decode. Worth mentioning in the reference docs for getURLParams().

If you'd prefer to preserve repeated keys as arrays while you're in there, that is easy to add on top — but it is a separate decision, so I've left it out of the suggestion.

I'd be glad to open a PR with the replacement and tests covering all four cases above.

主要语言
JavaScript
星标
24.1k
派生
3.9k
平均合并
2 天 16 小时
30 天内合并 PR
33

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

processing/p5.js 的其他 Issue

查看 processing/p5.js 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。