-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
769fef6
commit b36f17d
Showing
6 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
packages/roosterjs-content-model/lib/formatHandlers/common/widthFormatHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { FormatHandler } from '../FormatHandler'; | ||
import { WidthFormat } from '../../publicTypes/format/formatParts/WidthFormat'; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const widthFormatHandler: FormatHandler<WidthFormat> = { | ||
parse: (format, element, _, defaultStyle) => { | ||
const width = element.style.width || defaultStyle.width; | ||
|
||
if (width) { | ||
format.width = width; | ||
} | ||
}, | ||
apply: (format, element) => { | ||
if (format.width) { | ||
element.style.width = format.width; | ||
} | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
packages/roosterjs-content-model/lib/publicTypes/format/formatParts/WidthFormat.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Format of width | ||
*/ | ||
export type WidthFormat = { | ||
/** | ||
* Width CSS value | ||
*/ | ||
width?: string; | ||
}; |
52 changes: 52 additions & 0 deletions
52
packages/roosterjs-content-model/test/formatHandlers/common/widthFormatHandlerTest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { createDomToModelContext } from '../../../lib/domToModel/context/createDomToModelContext'; | ||
import { createModelToDomContext } from '../../../lib/modelToDom/context/createModelToDomContext'; | ||
import { DomToModelContext } from '../../../lib/publicTypes/context/DomToModelContext'; | ||
import { ModelToDomContext } from '../../../lib/publicTypes/context/ModelToDomContext'; | ||
import { WidthFormat } from '../../../lib/publicTypes/format/formatParts/WidthFormat'; | ||
import { widthFormatHandler } from '../../../lib/formatHandlers/common/widthFormatHandler'; | ||
|
||
describe('widthFormatHandler.parse', () => { | ||
let div: HTMLElement; | ||
let format: WidthFormat; | ||
let context: DomToModelContext; | ||
|
||
beforeEach(() => { | ||
div = document.createElement('div'); | ||
format = {}; | ||
context = createDomToModelContext(); | ||
}); | ||
|
||
it('No word break', () => { | ||
widthFormatHandler.parse(format, div, context, {}); | ||
expect(format).toEqual({}); | ||
}); | ||
|
||
it('Has word break', () => { | ||
div.style.width = '100pt'; | ||
widthFormatHandler.parse(format, div, context, {}); | ||
expect(format).toEqual({ width: '100pt' }); | ||
}); | ||
}); | ||
|
||
describe('wordBreakFormatHandler.apply', () => { | ||
let div: HTMLElement; | ||
let format: WidthFormat; | ||
let context: ModelToDomContext; | ||
|
||
beforeEach(() => { | ||
div = document.createElement('div'); | ||
format = {}; | ||
context = createModelToDomContext(); | ||
}); | ||
|
||
it('No word break', () => { | ||
widthFormatHandler.apply(format, div, context); | ||
expect(div.outerHTML).toBe('<div></div>'); | ||
}); | ||
|
||
it('Has word-break', () => { | ||
format.width = '100pt'; | ||
widthFormatHandler.apply(format, div, context); | ||
expect(div.outerHTML).toBe('<div style="width: 100pt;"></div>'); | ||
}); | ||
}); |