Skip to content

Commit cb73c8d

Browse files
feat(studio): apply a style to a run of characters (#3142)
* feat(studio): apply a style to a run of characters Styling text in a composition cannot be done by wrapping a DOM range in a span. That is three lines, and then every interesting case is a special case: recolouring nests spans that shadow each other, removing a style cannot reach the ancestor that set it, and styling across an existing run's boundary has to split it. Each fix is a new branch and the branches interact. So the element is read into a flat list of styled runs, the style is applied to a span of characters in that list, and the element is rebuilt from it. Replacing, removing, splitting and merging stop being cases: the rebuild emits one span per distinct run and cannot nest or duplicate, whatever was there before. Selection offsets count UTF-16 units, so a boundary can land between the halves of an emoji; the applied range widens to whole characters. A colour an ancestor overpaints is mirrored into the fill, because a colour that does not paint reads to the user as a colour that did not save. The toolbar that drives this arrives with the editor in the next change. * fix(studio): harden inline text styling boundaries * fix(studio): align inline styling with persistence * test(studio): pin inline identity delimiters
1 parent 636dc04 commit cb73c8d

3 files changed

Lines changed: 1309 additions & 3 deletions

File tree

‎packages/core/src/utils/richTextSanitize.ts‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ export function isRichTextFormattingTag(tagName: string): boolean {
9898
return FORMATTING_TAGS.has(tagName.toUpperCase());
9999
}
100100

101+
/** Whether an attribute survives the rich-text persistence boundary. */
102+
export function isRichTextFormattingAttribute(name: string, value: string): boolean {
103+
return FORMATTING_ATTRS.has(name.toLowerCase()) && SAFE_ATTR_VALUE.test(value);
104+
}
105+
106+
/** Whether a declaration survives the rich-text persistence boundary. */
107+
export function isRichTextFormattingStyle(property: string, value: string): boolean {
108+
return (
109+
FORMATTING_STYLE_PROPS.has(property.toLowerCase()) &&
110+
value.length > 0 &&
111+
!UNSAFE_VALUE.test(value)
112+
);
113+
}
114+
101115
function isElementNode(node: Node): node is Element {
102116
return node.nodeType === ELEMENT_NODE;
103117
}
@@ -165,7 +179,7 @@ function stripAttributes(element: Element): void {
165179
const style = element.getAttribute("style");
166180
for (const name of Array.from(element.getAttributeNames())) {
167181
const value = element.getAttribute(name) ?? "";
168-
if (FORMATTING_ATTRS.has(name.toLowerCase()) && SAFE_ATTR_VALUE.test(value)) continue;
182+
if (isRichTextFormattingAttribute(name, value)) continue;
169183
element.removeAttribute(name);
170184
}
171185
if (style === null) return;
@@ -182,8 +196,7 @@ function filterStyle(style: string): string {
182196
if (colon === -1) return null;
183197
const property = declaration.slice(0, colon).trim().toLowerCase();
184198
const value = declaration.slice(colon + 1).trim();
185-
if (!FORMATTING_STYLE_PROPS.has(property)) return null;
186-
if (!value || UNSAFE_VALUE.test(value)) return null;
199+
if (!isRichTextFormattingStyle(property, value)) return null;
187200
return `${property}: ${value}`;
188201
})
189202
.filter((declaration): declaration is string => declaration !== null)

0 commit comments

Comments
 (0)