getElementsByClassName returns nothing for multiple or space-padded class names

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

维护者通常 5 天内回复

还没有人认领这个 Issue。

评估

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

调研方向

issue 指向 esm/mixin/parent-node.js,其中 getElementsByClassName 将未拆分的参数传给 classList.has。先阅读该方法及相关测试,然后将其行为与 DOM 规范关于 ASCII 空白和必须包含所有类的要求进行比较。补充覆盖多个类名、顺序相反和前后空格的测试;四个示例都各返回一个匹配项时即为完成。

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

描述

getElementsByClassName matches the whole argument as a single class, so "a b", "b a" or " a " find nothing.

const { parseHTML } = require("linkedom");
const { document } = parseHTML('<!doctype html><html><body><p class="a b">x</p></body></html>');

console.log(
  document.getElementsByClassName("a").length,
  document.getElementsByClassName("a b").length,
  document.getElementsByClassName("b a").length,
  document.getElementsByClassName("  a ").length
);

node t.js

Expected: 1 1 1 1

Actual: 1 0 0 0

Tested with linkedom 0.18.15 from npm and main at 6e2ae0c, Node.js 26.13.0.

The DOM spec splits the argument on ASCII whitespace and requires all classes (https://dom.spec.whatwg.org/#concept-getelementsbyclassname). getElementsByClassName in esm/mixin/parent-node.js calls classList.has(className) with the unsplit string.

Credit: https://ucsbplse.github.io/

主要语言
HTML
星标
2.1k
派生
117
平均合并
4 天 13 小时
30 天内合并 PR
2

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

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

WebReflection/linkedom 的其他 Issue

查看 WebReflection/linkedom 的全部 Issue

相似的 Issue

更多 Web Dev Issue

把新 issue 发到你的邮箱

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