Skip to content

Commit

Permalink
fix(tab-header): unsafe querySelector (#1608)
Browse files Browse the repository at this point in the history
  • Loading branch information
Arturo Castillo Delgado authored Feb 24, 2023
1 parent ceac390 commit 82d0f14
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/components/src/components/button/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export class Button {
*/
setChildrenIconSize() {
if (this.size != null && buttonIconSizeMap[this.size] != null) {
const icons: ScaleIcon[] = Array.from(this.hostElement.children).filter(
const icons: ScaleIcon[] = Array.from(this.hostElement.childNodes).filter(
isScaleIcon
);
icons.forEach((icon) => {
Expand Down
31 changes: 10 additions & 21 deletions packages/components/src/components/tab-header/tab-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import classNames from 'classnames';
import { ScaleIcon, isScaleIcon } from '../../utils/utils';
import statusNote from '../../utils/status-note';

const DEFAULT_ICON_SIZE = 24;
const PER_SPEC_ICON_SIZE = 16;

let i = 0;
Expand All @@ -26,7 +25,6 @@ let i = 0;
})
export class TabHeader {
generatedId: number = i++;
container: HTMLElement;

@Element() hostElement: HTMLElement;

Expand All @@ -45,6 +43,9 @@ export class TabHeader {

@Watch('selected')
selectedChanged(newValue: boolean) {
if (!this.hostElement.isConnected) {
return;
}
if (!this.disabled) {
if (newValue === true) {
// Having focus on the host element, and not on inner elements,
Expand Down Expand Up @@ -77,30 +78,23 @@ export class TabHeader {
* Find slotted icons, and if any, add the `selected` attribute accordingly.
*/
updateSlottedIcon() {
const slot = this.container.querySelector('slot') as HTMLSlotElement;
if (slot === null) {
return;
}
const children = Array.from(slot.assignedNodes())
.filter((node) => node.nodeType === 1)
.filter((node) => node.nodeName.toUpperCase().indexOf('ICON') > -1);
if (children.length === 0) {
return;
}
const icons: ScaleIcon[] = Array.from(this.hostElement.childNodes).filter(
isScaleIcon
);
const action = this.selected ? 'setAttribute' : 'removeAttribute';
children.forEach((child) => child[action]('selected', ''));
icons.forEach((child) => child[action]('selected', ''));
}

/**
* Set any children icon's size according the button size.
*/
setChildrenIconSize() {
const icons: ScaleIcon[] = Array.from(this.hostElement.children).filter(
const icons: ScaleIcon[] = Array.from(this.hostElement.childNodes).filter(
isScaleIcon
);
icons.forEach((icon) => {
// This is meh people might actually want 24
if (icon.size === DEFAULT_ICON_SIZE) {
if (icon.size !== PER_SPEC_ICON_SIZE) {
icon.size = PER_SPEC_ICON_SIZE;
}
});
Expand All @@ -117,12 +111,7 @@ export class TabHeader {
onBlur={() => (this.hasFocus = false)}
>
{this.styles && <style>{this.styles}</style>}

<span
part={this.getBasePartMap()}
ref={(el) => (this.container = el)}
class={this.getCssClassMap()}
>
<span part={this.getBasePartMap()} class={this.getCssClassMap()}>
<slot />
</span>
</Host>
Expand Down
4 changes: 2 additions & 2 deletions packages/components/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ export function isClickOutside(event: MouseEvent, host: HTMLElement) {
return true;
}

export interface ScaleIcon extends Element {
export interface ScaleIcon extends Node {
size?: number;
}

export const isScaleIcon = (el: Node) => {
if (el == null) {
if (el == null || el.nodeType !== 1) {
return false;
}
return el.nodeName.toUpperCase().substring(0, 10) === 'SCALE-ICON';
Expand Down

0 comments on commit 82d0f14

Please sign in to comment.