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

Remove span from image #2716

Merged
merged 2 commits into from
Jun 19, 2024
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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { addRangeToSelection } from './addRangeToSelection';
import { areSameSelections } from '../../corePlugin/cache/areSameSelections';
import { ensureImageHasSpanParent } from './ensureImageHasSpanParent';
import { ensureUniqueId } from '../setEditorStyle/ensureUniqueId';
import { findLastedCoInMergedCell } from './findLastedCoInMergedCell';
import { findTableCellElement } from './findTableCellElement';
Expand All @@ -20,6 +19,7 @@ const TABLE_ID = 'table';
const CARET_CSS_RULE = 'caret-color: transparent';
const TRANSPARENT_SELECTION_CSS_RULE = 'background-color: transparent !important;';
const SELECTION_SELECTOR = '*::selection';
const DEFAULT_SELECTION_BORDER_COLOR = '#DB626C';

/**
* @internal
Expand All @@ -45,23 +45,21 @@ export const setDOMSelection: SetDOMSelection = (core, selection, skipSelectionC
try {
switch (selection?.type) {
case 'image':
const image = ensureImageHasSpanParent(selection.image);
const image = selection.image;

core.selection.selection = selection;

core.selection.selection = {
type: 'image',
image,
};
const imageSelectionColor = isDarkMode
? core.selection.imageSelectionBorderColorDark
: core.selection.imageSelectionBorderColor;

core.api.setEditorStyle(
core,
DOM_SELECTION_CSS_KEY,
`outline-style:solid!important; outline-color:${imageSelectionColor}!important;display: ${
core.environment.isSafari ? '-webkit-inline-flex' : 'inline-flex'
};`,
[`span:has(>img#${ensureUniqueId(image, IMAGE_ID)})`]
`outline-style:auto!important; outline-color:${
imageSelectionColor || DEFAULT_SELECTION_BORDER_COLOR
}!important;`,
[`#${ensureUniqueId(image, IMAGE_ID)}`]
);
core.api.setEditorStyle(
core,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as addRangeToSelection from '../../../lib/coreApi/setDOMSelection/addRangeToSelection';
import * as ensureImageHasSpanParent from '../../../lib/coreApi/setDOMSelection/ensureImageHasSpanParent';
import { DOMSelection, EditorCore } from 'roosterjs-content-model-types';
import { setDOMSelection } from '../../../lib/coreApi/setDOMSelection/setDOMSelection';

import {
DEFAULT_SELECTION_BORDER_COLOR,
DEFAULT_TABLE_CELL_SELECTION_BACKGROUND_COLOR,
Expand Down Expand Up @@ -314,8 +314,8 @@ describe('setDOMSelection', () => {
expect(setEditorStyleSpy).toHaveBeenCalledWith(
core,
'_DOMSelection',
'outline-style:solid!important; outline-color:#DB626C!important;display: inline-flex;',
['span:has(>img#image_0)']
'outline-style:auto!important; outline-color:#DB626C!important;',
['#image_0']
);
expect(setEditorStyleSpy).toHaveBeenCalledWith(
core,
Expand Down Expand Up @@ -374,8 +374,8 @@ describe('setDOMSelection', () => {
expect(setEditorStyleSpy).toHaveBeenCalledWith(
core,
'_DOMSelection',
'outline-style:solid!important; outline-color:red!important;display: inline-flex;',
['span:has(>img#image_0)']
'outline-style:auto!important; outline-color:red!important;',
['#image_0']
);
expect(setEditorStyleSpy).toHaveBeenCalledWith(
core,
Expand Down Expand Up @@ -441,8 +441,8 @@ describe('setDOMSelection', () => {
expect(setEditorStyleSpy).toHaveBeenCalledWith(
coreValue,
'_DOMSelection',
'outline-style:solid!important; outline-color:DarkColorMock-red!important;display: inline-flex;',
['span:has(>img#image_0)']
'outline-style:auto!important; outline-color:DarkColorMock-red!important;',
['#image_0']
);
expect(setEditorStyleSpy).toHaveBeenCalledWith(
coreValue,
Expand Down Expand Up @@ -502,8 +502,8 @@ describe('setDOMSelection', () => {
expect(setEditorStyleSpy).toHaveBeenCalledWith(
core,
'_DOMSelection',
'outline-style:solid!important; outline-color:#DB626C!important;display: inline-flex;',
['span:has(>img#image_0)']
'outline-style:auto!important; outline-color:#DB626C!important;',
['#image_0']
);
expect(setEditorStyleSpy).toHaveBeenCalledWith(
core,
Expand Down Expand Up @@ -563,8 +563,8 @@ describe('setDOMSelection', () => {
expect(setEditorStyleSpy).toHaveBeenCalledWith(
core,
'_DOMSelection',
'outline-style:solid!important; outline-color:#DB626C!important;display: inline-flex;',
['span:has(>img#image_0_0)']
'outline-style:auto!important; outline-color:#DB626C!important;',
['#image_0_0']
);
expect(setEditorStyleSpy).toHaveBeenCalledWith(
core,
Expand Down Expand Up @@ -927,9 +927,6 @@ describe('setDOMSelection', () => {
describe('Same selection', () => {
beforeEach(() => {
querySelectorAllSpy.and.returnValue([]);
spyOn(ensureImageHasSpanParent, 'ensureImageHasSpanParent').and.callFake(
image => image
);
});

function runTest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ export function removeUnnecessarySpan(root: Node) {
if (
isNodeOfType(child, 'ELEMENT_NODE') &&
child.tagName == 'SPAN' &&
child.attributes.length == 0 &&
!isImageSpan(child)
child.attributes.length == 0
) {
const node = child;
let refNode = child.nextSibling;
Expand All @@ -27,11 +26,3 @@ export function removeUnnecessarySpan(root: Node) {
}
}
}

const isImageSpan = (child: HTMLElement) => {
return (
isNodeOfType(child.firstChild, 'ELEMENT_NODE') &&
child.firstChild.tagName == 'IMG' &&
child.firstChild == child.lastChild
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ describe('removeUnnecessarySpan', () => {
expect(div.innerHTML).toBe('test1<span id="a">test2</span><b>test3</b>');
});

it('Do not remove image span', () => {
it('Remove image span', () => {
const div = document.createElement('div');
div.innerHTML = '<span><img src="test" /></span>';
div.innerHTML = '<span><img src="test"></span>';

removeUnnecessarySpan(div);

expect(div.innerHTML).toBe('<span><img src="test"></span>');
expect(div.innerHTML).toBe('<img src="test">');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,6 @@ export class ImageEditPlugin implements ImageEditor, EditorPlugin {
image: HTMLImageElement,
apiOperation?: ImageEditOperation
) {
const imageSpan = image.parentElement;
if (!imageSpan || (imageSpan && !isElementOfType(imageSpan, 'span'))) {
return;
}
this.imageEditInfo = getSelectedImageMetadata(editor, image);
this.lastSrc = image.getAttribute('src');
this.imageHTMLOptions = getHTMLImageOptions(editor, this.options, this.imageEditInfo);
Expand All @@ -142,7 +138,6 @@ export class ImageEditPlugin implements ImageEditor, EditorPlugin {
} = createImageWrapper(
editor,
image,
imageSpan,
this.options,
this.imageEditInfo,
this.imageHTMLOptions,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createImageCropper } from '../Cropper/createImageCropper';
import { createImageResizer } from '../Resizer/createImageResizer';
import { createImageRotator } from '../Rotator/createImageRotator';
import { wrap } from 'roosterjs-content-model-dom';

import type {
IEditor,
Expand Down Expand Up @@ -28,7 +29,6 @@ export interface WrapperElements {
export function createImageWrapper(
editor: IEditor,
image: HTMLImageElement,
imageSpan: HTMLSpanElement,
options: ImageEditOptions,
editInfo: ImageMetadataFormat,
htmlOptions: ImageHtmlOptions,
Expand Down Expand Up @@ -60,6 +60,7 @@ export function createImageWrapper(
rotators,
croppers
);
const imageSpan = wrap(doc, image, 'span');
const shadowSpan = createShadowSpan(wrapper, imageSpan);
return { wrapper, shadowSpan, imageClone, resizers, rotators, croppers };
}
Expand Down Expand Up @@ -97,7 +98,9 @@ const createWrapper = (
editInfo.angleRad ?? 0
}rad); text-align: left;`
);
wrapper.style.display = editor.getEnvironment().isSafari ? 'inline-block' : 'inline-flex';
wrapper.style.display = editor.getEnvironment().isSafari
? '-webkit-inline-flex'
: 'inline-flex';

const border = createBorder(editor, options.borderColor);
wrapper.appendChild(imageBox);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,7 @@ xdescribe('updateRotateHandlePosition', () => {
bottomPercent: 0,
angleRad: 0,
};
const { wrapper } = createImageWrapper(
editor,
image,
imageSpan,
{},
imageInfo,
options,
'rotate'
);
const { wrapper } = createImageWrapper(editor, image, {}, imageInfo, options, 'rotate');
const rotateCenter = wrapper.querySelector('.r_rotateC')! as HTMLElement;
const rotateHandle = wrapper.querySelector('.r_rotateH')! as HTMLElement;
spyOn(rotateHandle, 'getBoundingClientRect').and.returnValues(rotatePosition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('createImageWrapper', () => {
const result = createImageWrapper(
editor,
image,
imageSpan,

options,
editInfo,
htmlOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,12 @@ describe('updateWrapper', () => {
isSmallImage: false,
};
const image = document.createElement('img');
const imageSpan = document.createElement('span');
imageSpan.appendChild(image);
document.body.appendChild(imageSpan);
document.body.appendChild(image);

it('should update size', () => {
const { wrapper, imageClone, resizers } = createImageWrapper(
editor,
image,
imageSpan,
options,
editInfo,
htmlOptions,
Expand Down
Loading