Repository navigation
Decide whether an element is drawn without checkVisibility #248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bdfd140
Decide whether an element is drawn without checkVisibility
rostislavrotaru 5a885de
Refuse checkVisibility in every form and treat skipped content as not…
rostislavrotaru a4911c2
Test that isDrawn reads past a shadow root to its host
rostislavrotaru 285ec5f
Use Object.isNull in the ancestor walk and name the check test's result
rostislavrotaru File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
src/noldova.ui.components.angular/scripts/checks/browser-api-check.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /** | ||
| * @license | ||
| * Copyright (c) Noldova. | ||
| * | ||
| * This source code is licensed under the license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| import { readdir, readFile } from "node:fs/promises"; | ||
| import path from "node:path"; | ||
| import type { Writable } from "node:stream"; | ||
|
|
||
| import type { ICheck } from "./interfaces/i-check.ts"; | ||
|
|
||
| export class BrowserApiCheck implements ICheck { | ||
| private static readonly FOLDERS: readonly string[] = ["src/noldova.ui.components.angular/src", "src/noldova.ui.components.angular.docs/src"]; | ||
| private static readonly SOURCE: RegExp = /\.ts$/; | ||
| private static readonly DECLARATIONS: RegExp = /\.d\.ts$/; | ||
| private static readonly CALLS: readonly (readonly [RegExp, string])[] = [ | ||
| [/\.checkVisibility\s*\(/, "Element.checkVisibility(), which Safari before 17.4 lacks; call ElementVisibility.isDrawn instead"] | ||
| ]; | ||
| private static readonly ENCODING: BufferEncoding = "utf8"; | ||
|
|
||
| private readonly root: string; | ||
|
|
||
| public readonly title: string = "browser APIs"; | ||
|
|
||
| public constructor(root: string) { | ||
| this.root = root; | ||
| } | ||
|
|
||
| public async runAsync(output: Writable): Promise<boolean> { | ||
| const findings: string[] = []; | ||
| let count = 0; | ||
| for (const folder of BrowserApiCheck.FOLDERS) { | ||
| const files = (await readdir(path.join(this.root, folder), { recursive: true })) | ||
| .filter(t => BrowserApiCheck.SOURCE.test(t) && !BrowserApiCheck.DECLARATIONS.test(t)).sort(); | ||
| count += files.length; | ||
| for (const file of files) { | ||
| const lines = (await readFile(path.join(this.root, folder, file), BrowserApiCheck.ENCODING)).split("\n"); | ||
| lines.forEach((line, index) => { | ||
| for (const [call, advice] of BrowserApiCheck.CALLS) | ||
| if (call.test(line)) | ||
| findings.push(`${path.posix.join(folder, file.split(path.sep).join(path.posix.sep))}:${index + 1} calls ${advice}.`); | ||
| }); | ||
| } | ||
| } | ||
| for (const finding of findings) | ||
| output.write(`${finding}\n`); | ||
| output.write(`Read ${count} files under ${BrowserApiCheck.FOLDERS.join(" and ")} for a browser API a supported browser lacks.\n`); | ||
| return findings.length === 0; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/noldova.ui.components.angular/src/app/models/element-visibility.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /** | ||
| * @license | ||
| * Copyright (c) Noldova. | ||
| * | ||
| * This source code is licensed under the license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| export class ElementVisibility { | ||
| public static isDrawn(element: Element, isVisibilityAsked: boolean = false): boolean { | ||
| return element.getClientRects().length > 0 && (!isVisibilityAsked || getComputedStyle(element).visibility === "visible"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/noldova.ui.components.angular/tests/app/models/element-visibility.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /** | ||
| * @license | ||
| * Copyright (c) Noldova. | ||
| * | ||
| * This source code is licensed under the license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| import { ElementVisibility } from "../../../src/app/models/element-visibility"; | ||
|
|
||
| describe("ElementVisibility", () => { | ||
| let host: HTMLElement; | ||
|
|
||
| beforeEach(() => { | ||
| host = document.createElement("div"); | ||
| host.innerHTML = "<p class=\"shown\">Shown</p><p class=\"none\" style=\"display: none\">None</p>" | ||
| + "<div style=\"display: none\"><p class=\"inside\">Inside</p></div><p class=\"hidden\" style=\"visibility: hidden\">Hidden</p>" | ||
| + "<p class=\"collapsed\" style=\"visibility: collapse\">Collapsed</p>"; | ||
| document.body.append(host); | ||
| }); | ||
|
|
||
| afterEach(() => host.remove()); | ||
|
|
||
| const read = (selector: string, isVisibilityAsked: boolean): boolean => { | ||
| const element = host.querySelector(selector); | ||
| if (element === null) | ||
| throw new Error(`${selector} isn't in the host.`); | ||
| return ElementVisibility.isDrawn(element, isVisibilityAsked); | ||
| }; | ||
|
|
||
| it("answers as checkVisibility does, with and without the visibility property", () => { | ||
| const selectors = [".shown", ".none", ".inside", ".hidden", ".collapsed"]; | ||
|
|
||
| expect(selectors.map(t => [read(t, false), read(t, true)])).toEqual(selectors.map(t => { | ||
| const element = host.querySelector(t); | ||
| return [element?.checkVisibility(), element?.checkVisibility({ visibilityProperty: true })]; | ||
| })); | ||
| expect(selectors.map(t => [read(t, false), read(t, true)])).toEqual([[true, true], [false, false], [false, false], [true, false], [true, false]]); | ||
| }); | ||
|
|
||
| it("calls no checkVisibility, which Safari before 17.4 lacks", () => { | ||
| const check = vi.spyOn(Element.prototype, "checkVisibility").mockImplementation(() => { | ||
| throw new TypeError("checkVisibility is not a function"); | ||
| }); | ||
| try { | ||
| expect([read(".shown", true), read(".hidden", true), read(".none", false)]).toEqual([true, false, false]); | ||
| expect(check).not.toHaveBeenCalled(); | ||
| } finally { | ||
| check.mockRestore(); | ||
| } | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.