Skip to content
Merged
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
18 changes: 15 additions & 3 deletions packages/super-editor/src/core/helpers/getMarksFromSelection.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function getFormattingStateAtPos(state, pos, editor, options = {}) {
const resolvedRunProperties = resolvedFromSelection?.resolvedRunProperties ?? inlineRunProperties;
const styleRunProperties = resolvedFromSelection?.styleRunProperties ?? null;
const resolvedMarksFromProperties = createMarksFromRunProperties(state, resolvedRunProperties, editor);
resolvedMarks.push(...(resolvedMarksFromProperties.length ? resolvedMarksFromProperties : inlineMarks));
resolvedMarks.push(...mergeResolvedMarksWithInlineFallback(resolvedMarksFromProperties, inlineMarks));
if (storedMarks && includeCursorMarksWithStoredMarks) {
resolvedMarks.push(...cursorMarks);
}
Expand Down Expand Up @@ -99,16 +99,28 @@ function aggregateFormattingSegments(state, editor, segments) {
const resolvedRunProperties = intersectRunProperties(segments.map((segment) => segment.resolvedRunProperties));
const inlineRunProperties = intersectRunProperties(segments.map((segment) => segment.inlineRunProperties));
const styleRunProperties = intersectRunProperties(segments.map((segment) => segment.styleRunProperties));
const resolvedMarks = createMarksFromRunProperties(state, resolvedRunProperties, editor);
const inlineMarks = createMarksFromRunProperties(state, inlineRunProperties, editor);

return {
resolvedMarks: createMarksFromRunProperties(state, resolvedRunProperties, editor),
inlineMarks: createMarksFromRunProperties(state, inlineRunProperties, editor),
resolvedMarks: mergeResolvedMarksWithInlineFallback(resolvedMarks, inlineMarks),
inlineMarks,
resolvedRunProperties,
inlineRunProperties,
styleRunProperties,
};
}

function mergeResolvedMarksWithInlineFallback(resolvedMarks, inlineMarks) {
if (!resolvedMarks.length) return inlineMarks;
if (!inlineMarks.length) return resolvedMarks;

const resolvedMarkNames = new Set(resolvedMarks.map((mark) => mark.type.name));
const missingInlineMarks = inlineMarks.filter((mark) => !resolvedMarkNames.has(mark.type.name));

return [...resolvedMarks, ...missingInlineMarks];
}

function intersectRunProperties(runPropertiesList) {
const filtered = runPropertiesList.filter((props) => props && typeof props === 'object');
if (filtered.length === 0) return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { describe, expect, it, vi } from 'vitest';
import { EditorState, TextSelection } from 'prosemirror-state';
import { Schema } from 'prosemirror-model';

const resolveRunProperties = vi.fn(() => ({ bold: true }));

vi.mock('@superdoc/style-engine/ooxml', () => ({
resolveRunProperties,
}));

vi.mock('@extensions/paragraph/resolvedPropertiesCache.js', () => ({
calculateResolvedParagraphProperties: vi.fn(() => ({})),
}));

describe('getSelectionFormattingState resolved mark fallback', () => {
it('preserves inline highlight when resolved marks omit it', async () => {
const { getSelectionFormattingState } = await import('./getMarksFromSelection.js');

const schema = new Schema({
nodes: {
doc: { content: 'paragraph+' },
paragraph: {
content: 'inline*',
group: 'block',
toDOM() {
return ['p', 0];
},
},
run: {
content: 'text*',
group: 'inline',
inline: true,
attrs: { runProperties: { default: null } },
toDOM() {
return ['span', 0];
},
},
text: { group: 'inline' },
},
marks: {
bold: {
attrs: { value: { default: true } },
toDOM() {
return ['strong', 0];
},
},
highlight: {
attrs: { color: { default: null } },
toDOM() {
return ['mark', 0];
},
},
},
});

const doc = schema.node('doc', null, [
schema.node('paragraph', null, [
schema.node('run', { runProperties: { highlight: { 'w:val': '#ECCF35' } } }, [schema.text('Hello')]),
]),
]);
const baseState = EditorState.create({ schema, doc });
const state = baseState.apply(baseState.tr.setSelection(TextSelection.create(doc, 2, 7)));

const result = getSelectionFormattingState(state, { converter: { convertedXml: {} } });

expect(resolveRunProperties).toHaveBeenCalled();
expect(result.inlineMarks).toContainEqual(expect.objectContaining({ attrs: { color: '#ECCF35' } }));
expect(result.resolvedMarks).toContainEqual(expect.objectContaining({ attrs: { value: true } }));
expect(result.resolvedMarks).toContainEqual(expect.objectContaining({ attrs: { color: '#ECCF35' } }));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,52 @@ describe('getMarksFromSelection', () => {
expect(result.inlineRunProperties).toEqual({ styleId: 'Heading1Char' });
});

it('reconstructs highlight marks from hash-prefixed runProperties values', () => {
const runSchema = new Schema({
nodes: {
doc: { content: 'paragraph+' },
paragraph: {
content: 'inline*',
group: 'block',
toDOM() {
return ['p', 0];
},
},
run: {
content: 'text*',
group: 'inline',
inline: true,
attrs: { runProperties: { default: null } },
toDOM() {
return ['span', 0];
},
},
text: { group: 'inline' },
},
marks: {
highlight: {
attrs: { color: { default: null } },
toDOM() {
return ['mark', 0];
},
},
},
});
const testDoc = runSchema.node('doc', null, [
runSchema.node('paragraph', null, [
runSchema.node('run', { runProperties: { highlight: { 'w:val': '#ECCF35' } } }, [runSchema.text('Hello')]),
]),
]);
const state = EditorState.create({ schema: runSchema, doc: testDoc });
const cursorState = state.apply(state.tr.setSelection(TextSelection.create(testDoc, 3)));

const result = getSelectionFormattingState(cursorState);

expect(result.inlineRunProperties).toEqual({ highlight: { 'w:val': '#ECCF35' } });
expect(result.inlineMarks).toContainEqual(expect.objectContaining({ attrs: { color: '#ECCF35' } }));
expect(result.resolvedMarks).toContainEqual(expect.objectContaining({ attrs: { color: '#ECCF35' } }));
});

it('falls back to cursor marks when the surrounding run has no explicit runProperties', () => {
const runSchema = new Schema({
nodes: {
Expand Down
38 changes: 32 additions & 6 deletions packages/super-editor/src/core/super-converter/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import {
twipsToLines,
eighthPointsToPixels,
linesToTwips,
isValidHexColor,
getHexColorFromDocxSystem,
normalizeHexColor,
} from '@converter/helpers.js';
import { isValidHexColor, getHexColorFromDocxSystem } from '@converter/helpers';
import { SuperConverter } from '@converter/SuperConverter.js';
import { getUnderlineCssString } from '@extensions/linked-styles/underline-css.js';
import {
Expand Down Expand Up @@ -670,11 +672,35 @@ function getFontFamilyValue(attributes, docx) {
* @returns {string|null} Hex color string, 'transparent', or null when unsupported.
*/
function getHighLightValue(attributes) {
const fill = attributes['w:fill'];
if (fill && fill !== 'auto') return `#${fill}`;
if (attributes?.['w:val'] === 'none') return 'transparent';
if (isValidHexColor(attributes?.['w:val'])) return `#${attributes['w:val']}`;
return getHexColorFromDocxSystem(attributes?.['w:val']) || null;
const fill = normalizeHighlightHex(attributes?.['w:fill']);
if (fill) return `#${fill}`;

const value = attributes?.['w:val'];
if (value === 'none') return 'transparent';

const normalizedValue = normalizeHighlightHex(value);
if (normalizedValue) return `#${normalizedValue}`;

return getHexColorFromDocxSystem(value) || null;
}

/**
* Normalize a highlight token to a 6-digit hex string without a leading hash.
* Returns null for non-hex values such as DOCX system color keywords.
*
* @param {unknown} rawValue
* @returns {string|null}
*/
function normalizeHighlightHex(rawValue) {
if (typeof rawValue !== 'string') return null;

const trimmedValue = rawValue.trim();
if (!trimmedValue || trimmedValue.toLowerCase() === 'auto') return null;

const normalizedValue = normalizeHexColor(trimmedValue);
if (!normalizedValue || !isValidHexColor(normalizedValue)) return null;

return normalizedValue;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ describe('encodeMarksFromRPr', () => {
});
});

it('should encode highlight from a hash-prefixed w:val', () => {
const rPr = { highlight: { 'w:val': '#ECCF35' } };
const marks = encodeMarksFromRPr(rPr, {});
expect(marks).toContainEqual({
type: 'highlight',
attrs: { color: '#ECCF35' },
});
});

it('should encode highlight from w:shd', () => {
const rPr = { shading: { fill: 'FFA500' } };
const marks = encodeMarksFromRPr(rPr, {});
Expand Down
Loading
Loading