Skip to content

Commit

Permalink
Fix A tag without href (#1495)
Browse files Browse the repository at this point in the history
  • Loading branch information
JiuqingSong authored Jan 10, 2023
1 parent e618daa commit b06c77f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,12 @@ export function getDefaultStyle(
element: HTMLElement,
context: DomToModelContext
): Partial<CSSStyleDeclaration> {
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] || {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -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({});
});
});

0 comments on commit b06c77f

Please sign in to comment.