Skip to content

Commit

Permalink
fix: Concatenate text nodes without space (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
eps1lon committed Jun 5, 2020
1 parent b9eb5df commit d668f72
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 14 deletions.
7 changes: 7 additions & 0 deletions .changeset/neat-carrots-sell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"dom-accessibility-api": patch
---

fix: Concatenate text nodes without space

Fixes `<h1>Hello {name}!</h1>` in `react` computing `"Hello name !"` instead of `Hello name!`.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ cloning. See [the test readme](/tests/README.md) for more info about the test se

### browser (Chrome)

136/144 of which 5 are due to missing whitespace.
140/144

### jsdom

<details>
<summary>report 126/159 passing of which 16 are due `::before { content }`, 14 are accessible desc, 7 are pathological </summary>
<summary>report 124/159 passing of which 16 are due `::before { content }`, 15 are accessible desc, 4 are pathological </summary>

```bash
web-platform-tests
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"type": "git",
"url": "https://github.com/eps1lon/dom-accessibility-api.git"
},
"dependencies": {},
"files": [
".browserslistrc",
"dist/"
Expand Down
15 changes: 14 additions & 1 deletion sources/__tests__/accessible-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,19 @@ test.each([
],
])(`test #%#`, testMarkup);

test("text nodes are not concatenated by space", () => {
// how React would create `<h1>Hello {name}!</h1>`
// which transpiles to
// React.createElement('h1', 'Hello ', name, '!')
const heading = document.createElement("h1");
heading.appendChild(document.createTextNode("Hello "));
heading.appendChild(document.createTextNode("Jill"));
heading.appendChild(document.createTextNode("!"));
renderIntoDocument(heading);

expect(heading).toHaveAccessibleName("Hello Jill!");
});

describe("prohibited naming", () => {
test.each([
["caption", "<div data-test role='caption'>table</div>"],
Expand Down Expand Up @@ -401,7 +414,7 @@ describe("options.getComputedStyle", () => {
computeAccessibleName(container.querySelector("button"));

// also mixing in a regression test for the number of calls
expect(window.getComputedStyle).toHaveBeenCalledTimes(4);
expect(window.getComputedStyle).toHaveBeenCalledTimes(3);
});

it("can be mocked with a fake", () => {
Expand Down
14 changes: 10 additions & 4 deletions sources/accessible-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,17 @@ export function computeAccessibleName(
recursion: true,
});
// TODO: Unclear why display affects delimiter
const display =
isElement(node) &&
createGetComputedStyle(node, options)(node).getPropertyValue("display");
// see https://github.com/w3c/accname/issues/3
const display = isElement(child)
? createGetComputedStyle(
child,
options
// eslint-disable-next-line no-mixed-spaces-and-tabs -- prettier bug?
)(child).getPropertyValue("display")
: "inline";
const separator = display !== "inline" ? " " : "";
accumulatedText += `${separator}${result}`;
// trailing separator for wpt tests
accumulatedText += `${separator}${result}${separator}`;
});

if (isElement(node)) {
Expand Down
8 changes: 4 additions & 4 deletions tests/cypress/integration/web-platform-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ context("wpt", () => {
["name_test_case_614-manual", "pass"],
["name_test_case_615-manual", "pass"],
["name_test_case_616-manual", "pass"],
["name_test_case_617-manual", "fail"], // whitespace, check if label children should be concetaned with a space
["name_test_case_618-manual", "fail"], // whitespace, see name_test_case_617-manual
["name_test_case_619-manual", "fail"], // whitespace, see name_test_case_617-manual
["name_test_case_620-manual", "fail"], // whitespace, see name_test_case_617-manual
["name_test_case_617-manual", "pass"],
["name_test_case_618-manual", "pass"],
["name_test_case_619-manual", "pass"],
["name_test_case_620-manual", "pass"],
["name_test_case_621-manual", "pass"],
["name_test_case_659-manual", "fail"], // wrong, ::before + [title] + ::after
["name_test_case_660-manual", "fail"], // wrong, ::before + [title] + ::after
Expand Down
4 changes: 2 additions & 2 deletions tests/wpt-jsdom/to-run.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ DIR: accname

description_*.html: [fail, bridge not implemented]
name_file-label-inline-block-elements-manual.html:
[fail, getComputedStyle display defaults not implemented]
[fail, whitespace issue, likely due to `display`]
name_file-label-inline-block-styles-manual.html:
[fail, getComputedStyle pseudo selector not implemented]
[fail, missing word, unknown]
name_test_case_552-manual.html:
[fail, getComputedStyle pseudo selector not implemented]
name_test_case_553-manual.html:
Expand Down

0 comments on commit d668f72

Please sign in to comment.