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

web: Support align embed/object attribute (part of #4258) #19079

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
30 changes: 30 additions & 0 deletions web/packages/core/src/internal/player/inner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,36 @@ export class InnerPlayer {
}
}

const alignAttr = this.element.attributes.getNamedItem("align");
if (alignAttr !== undefined && alignAttr !== null) {
const alignValue = alignAttr.value.toLowerCase();

const alignCSS = (() => {
switch (alignValue) {
case "right":
return "vertical-align: top; float: right;";
case "left":
return "vertical-align: top; float: left;";
case "bottom":
return "vertical-align: baseline;";
case "top":
return "vertical-align: top;";
case "center":
return "vertical-align: middle;";
case "middle":
return "vertical-align: middle; vertical-align: -webkit-baseline-middle; vertical-align: -moz-middle-with-baseline;";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found in testing on Chrome. The closest page I can find to describing this behavior is https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#align. It says right and left are equivalent to float CSS, though doesn't mention the vertical-align, but it seems to be added in testing. It says top is vertical-align: top, true in testing. It says middle is vertical-align: -moz-middle-with-baseline, and in testing on Chrome it is -webkit-baseline-middle. It says bottom is vertical-align: unset, and in testing it seems to be vertical-align: baseline, which seems to be the default. It doesn't mention center, but in testing that seems to be vertical-align: middle.

default:
return "";
}
})();

if (alignCSS) {
this.dynamicStyles.sheet.insertRule(
`:host { ${alignCSS} }`
);
}
}

const widthAttr = this.element.attributes.getNamedItem("width");
if (widthAttr !== undefined && widthAttr !== null) {
const width = InnerPlayer.htmlDimensionToCssDimension(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class RuffleEmbedElement extends RufflePlayerElement {
* @internal
*/
static override get observedAttributes(): string[] {
return ["src", "width", "height"];
return [...RufflePlayerElement.observedAttributes, "src"];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ export class RufflePlayerElement extends HTMLElement implements PlayerElement {
}

static get observedAttributes(): string[] {
return ["width", "height"];
return ["width", "height", "align"];
}

attributeChangedCallback(
name: string,
_oldValue: string | undefined,
_newValue: string | undefined,
): void {
if (name === "width" || name === "height") {
if (RufflePlayerElement.observedAttributes.includes(name)) {
this.#inner.updateStyles();
}
}
Expand Down