[p5.js 2.0+ Bug Report]: getURLParams() mis-parses valueless parameters and never percent-decodes values
维护者通常 3 天内回复
评估
- 难度
- 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:
- Open any sketch with
?debug&x=1appended to the URL. - Log
getURLParams(). - It returns
{ debug: "x" }— a pair that does not exist in the query string — andx=1is gone entirely. - 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:
- The separator group accepts
&as well as=.(?:[&=])means a parameter with no value matches&debug&xas keydebug, separator&, valuex— it absorbs the next parameter's name as its own value, and consumes the realx=1pair along with it. A valueless parameter does not just get skipped; it corrupts its neighbour. - 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. - A repeated key keeps only the last occurrence, and the returned object is a
{}literal, so it inheritstoString,constructor,hasOwnPropertyand friends. Code that testsif (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?flagwas 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 forgetURLParams().
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
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
processing/p5.js 的其他 Issue
-
Area:WebGL p5.js 2.0+ p5.strands
难度 2/5 1-3 小时 新手友好度 78/100
processing/p5.js#9310 ·
维护者通常 3 天内回复
-
[p5.js 2.0+ Bug Report]: touches[0].x lags one pointer event behind while dragging可能已有人在做 @titanstride 于 3 天前认领。 未关闭Area:Events p5.js 2.0+
难度 2/5 1-3 小时 新手友好度 76/100
processing/p5.js#9305 · 1 条评论 ·
维护者通常 3 天内回复
-
loadModel() options break without fileType and ignore flipU/flipV可能已有人在做 @saniyaa-17 于 3 天前认领。 未关闭Area:WebGL p5.js 2.0+
难度 2/5 1-3 小时 新手友好度 78/100
processing/p5.js#9302 · 2 条评论 ·
维护者通常 3 天内回复
-
[p5.js 2.0+ Bug Report]: key docs needs updating可能已有人在做 @MannXo 于 3 天前认领。 未关闭Help Wanted
难度 1/5 1 小时以内 新手友好度 62/100
processing/p5.js#9296 · 6 条评论 · 1 个 reaction ·
维护者通常 3 天内回复
-
False positive FES warning for loadModel(path, callback)可能已有人在做 @xsupremeyx 于 4 天前认领。 未关闭p5.js 2.0+
难度 2/5 1-3 小时 新手友好度 62/100
processing/p5.js#9295 · 1 条评论 · 已指派 1 人 ·
维护者通常 3 天内回复
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 66/100
维护者通常 1 天内回复
-
status: waiting for maintainer
难度 2/5 1-3 小时 新手友好度 66/100
mui/material-ui#49511 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 68/100
mpetazzoni/sse.js#122 ·
-
[eslint-plugin] valid-styles rejects <progress> pseudo-elements (::-webkit-progress-bar, ::-webkit-progress-value, ::-moz-progress-bar)可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 2 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 78/100
swc-project/swc-next#1022 ·
维护者通常 1 天内回复