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

Content Model: Fix A tag without href #1495

Merged
merged 2 commits into from
Jan 10, 2023
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
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({});
});
});