Skip to content
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

Handling fixed position and refactoring validation #61

Merged
merged 15 commits into from
Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ function getPositionFallbackRules(node: csstree.CssNode) {
return {};
}

function getCSSPropertyValue(el: HTMLElement, prop: string) {
export function getCSSPropertyValue(el: HTMLElement, prop: string) {
return getComputedStyle(el).getPropertyValue(prop).trim();
}

Expand Down
36 changes: 19 additions & 17 deletions src/validate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { platform } from '@floating-ui/dom';

import { getCSSPropertyValue } from './parse.js';

function hasStyle(element: HTMLElement, cssProperty: string, value: string) {
return (
element.style.getPropertyValue(cssProperty) === value ||
sanajaved7 marked this conversation as resolved.
Show resolved Hide resolved
getCSSPropertyValue(element, cssProperty) === value
);
}

// Given a target element and CSS selector(s) for potential anchor element(s),
// returns the first element that passes validation,
// or `null` if no valid anchor element is found
Expand All @@ -25,25 +34,19 @@ export async function validatedForPositioning(
}

export function isFixedPositioned(el: HTMLElement) {
return Boolean(
el.style.position === 'fixed' || getComputedStyle(el).position === 'fixed',
);
return hasStyle(el, 'position', 'fixed');
}

export function isAbsolutelyPositioned(el?: HTMLElement | null) {
return Boolean(
el &&
(el.style.position === 'absolute' ||
getComputedStyle(el).position === 'absolute' ||
isFixedPositioned(el)),
(hasStyle(el, 'position', 'fixed') ||
sanajaved7 marked this conversation as resolved.
Show resolved Hide resolved
hasStyle(el, 'position', 'absolute')),
);
}

// Determines whether the containing block (CB) of the element
// is the initial containing block (ICB):
// - `offsetParent` returns `null` when the CB is the ICB,
// except in Firefox where `offsetParent` returns the `body` element
// - Excludes elements when they or their parents have `display: none`
// is the initial containing block (ICB)
export function isContainingBlockICB(
jgerigmeyer marked this conversation as resolved.
Show resolved Hide resolved
targetContainingBlock: Element | Window | undefined,
) {
Expand All @@ -60,13 +63,9 @@ export async function isValidAnchorElement(

// If el has the same containing block as the querying element,
// el must not be absolutely positioned.
// A separate check for fixed positioning is added here
// because its offsetParent will always resolve to null:
// https://w3c.github.io/csswg-drafts/cssom-view/#extensions-to-the-htmlelement-interface
if (
(isAbsolutelyPositioned(anchor) &&
anchorContainingBlock === targetContainingBlock) ||
isFixedPositioned(anchor)
isAbsolutelyPositioned(anchor) &&
anchorContainingBlock === targetContainingBlock
) {
return false;
}
Expand Down Expand Up @@ -95,14 +94,17 @@ export async function isValidAnchorElement(
}
}

// Either el is a descendant of query el’s containing block,
// or query el’s containing block is the initial containing block
// https://drafts4.csswg.org/css-anchor-1/#determining
let isDescendant;
if (targetContainingBlock && targetContainingBlock === window) {
isDescendant = (targetContainingBlock as Window).document.contains(anchor);
jgerigmeyer marked this conversation as resolved.
Show resolved Hide resolved
} else if (targetContainingBlock) {
isDescendant = (targetContainingBlock as HTMLElement).contains(anchor);
}

const targetCBIsInitialCB = await isContainingBlockICB(targetContainingBlock);
const targetCBIsInitialCB = isContainingBlockICB(targetContainingBlock);

if (isDescendant || targetCBIsInitialCB) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe('parseCSS', () => {
},
};

expect(expected).toEqual(result);
expect(result).toEqual(expected);
});

it('parses `anchor()` (implicit name via `anchor` attr)', async () => {
Expand Down