From 0523b8ca81e530ac0e7f6b5c76502f05c9c12e3a Mon Sep 17 00:00:00 2001 From: jiuqingsong Date: Mon, 9 Jan 2023 11:57:37 -0800 Subject: [PATCH] Fix A tag without href --- .../lib/domToModel/utils/getDefaultStyle.ts | 9 ++++- .../test/domToModel/utils/parseFormatTest.ts | 33 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/packages/roosterjs-content-model/lib/domToModel/utils/getDefaultStyle.ts b/packages/roosterjs-content-model/lib/domToModel/utils/getDefaultStyle.ts index 6303569c004..bef61a8e267 100644 --- a/packages/roosterjs-content-model/lib/domToModel/utils/getDefaultStyle.ts +++ b/packages/roosterjs-content-model/lib/domToModel/utils/getDefaultStyle.ts @@ -12,5 +12,12 @@ export function getDefaultStyle( element: HTMLElement, context: DomToModelContext ): Partial { - return context.defaultStyles[element.tagName.toLowerCase() as keyof DefaultStyleMap] || {}; + let tag = element.tagName.toLowerCase() as keyof DefaultStyleMap; + + if (tag == 'a' && !element.hasAttribute('href')) { + // For A tag without Href, treat it as SPAN since it will not be rendered as a link + tag = 'span'; + } + + return context.defaultStyles[tag] || {}; } diff --git a/packages/roosterjs-content-model/test/domToModel/utils/parseFormatTest.ts b/packages/roosterjs-content-model/test/domToModel/utils/parseFormatTest.ts index 6254333d442..e75f1caac90 100644 --- a/packages/roosterjs-content-model/test/domToModel/utils/parseFormatTest.ts +++ b/packages/roosterjs-content-model/test/domToModel/utils/parseFormatTest.ts @@ -75,4 +75,37 @@ describe('Default styles', () => { it('Default style for U', () => { runTest('u', { textDecoration: 'underline' }); }); + + it('Default style for A with href', () => { + const element = document.createElement('a'); + const defaultContext = createDomToModelContext(); + const segmentFormat = {}; + const linkFormat = {}; + + element.href = 'http://test.com'; + + parseFormat(element, defaultContext.formatParsers.segment, segmentFormat, defaultContext); + parseFormat(element, defaultContext.formatParsers.link, linkFormat, defaultContext); + + expect(segmentFormat).toEqual({ + underline: true, + textColor: '__hyperLinkColor', + }); + expect(linkFormat).toEqual({ + href: 'http://test.com', + }); + }); + + it('Default style for A without href', () => { + const element = document.createElement('a'); + const defaultContext = createDomToModelContext(); + const segmentFormat = {}; + const linkFormat = {}; + + parseFormat(element, defaultContext.formatParsers.segment, segmentFormat, defaultContext); + parseFormat(element, defaultContext.formatParsers.link, linkFormat, defaultContext); + + expect(segmentFormat).toEqual({}); + expect(linkFormat).toEqual({}); + }); });