-
Notifications
You must be signed in to change notification settings - Fork 31
[Angular CSDK] Basic rendering finalize #461
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
Merged
illiakovalenko
merged 6 commits into
feature/angular-csdk
from
feature/jss-9058-tailwind
May 7, 2026
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6810e92
Apply better styling, refactor sample components, adjust package comp…
art-alexeyenko 7ce661e
Apply better styling, refactor sample components, adjust package comp…
art-alexeyenko 05539b8
fix unit tests
art-alexeyenko 4dc593e
remove JSS mentions
art-alexeyenko c049947
refactor, lint, coverage and more tests
art-alexeyenko c111969
extra form tests
art-alexeyenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
31 changes: 31 additions & 0 deletions
31
packages/angular/src/field-directives/link-field-binding.spec.ts
This file contains hidden or 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,31 @@ | ||
| /* eslint-disable jsdoc/require-jsdoc */ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { buildHrefFromLinkField, resolveLinkFromField } from './link-field-binding'; | ||
| import type { LinkField } from '@sitecore-content-sdk/content/layout'; | ||
|
|
||
| describe('link-field-binding', () => { | ||
| describe('resolveLinkFromField', () => { | ||
| it('returns value from LinkField wrapper', () => { | ||
| const field: LinkField = { value: { href: '/x', text: 'X' } }; | ||
| expect(resolveLinkFromField(field)).toEqual(field.value); | ||
| }); | ||
|
|
||
| it('returns bare LinkFieldValue when href is set at root', () => { | ||
| const field = { href: '/y', text: 'Y' }; | ||
| expect(resolveLinkFromField(field)).toBe(field); | ||
| }); | ||
| }); | ||
|
|
||
| describe('buildHrefFromLinkField', () => { | ||
| it('concatenates query and hash fragment', () => { | ||
| expect( | ||
| buildHrefFromLinkField({ | ||
| href: '/p', | ||
| querystring: 'a=1', | ||
| anchor: 'sec', | ||
| linktype: 'internal', | ||
| }), | ||
| ).toBe('/p?a=1#sec'); | ||
| }); | ||
| }); | ||
| }); |
94 changes: 94 additions & 0 deletions
94
packages/angular/src/field-directives/link-field-binding.ts
|
art-alexeyenko marked this conversation as resolved.
Outdated
|
This file contains hidden or 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,94 @@ | ||
| import { Renderer2 } from '@angular/core'; | ||
| import { isFieldValueEmpty, LinkField, LinkFieldValue } from '@sitecore-content-sdk/content/layout'; | ||
| import { getClassFromField } from './utils'; | ||
|
|
||
| function addClassTokens(renderer: Renderer2, element: HTMLElement, classString: string): void { | ||
| for (const token of classString.trim().split(/\s+/).filter(Boolean)) { | ||
| renderer.addClass(element, token); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Normalizes a Sitecore link field input to a {@link LinkFieldValue}, or `undefined` when empty. | ||
| */ | ||
| export function resolveLinkFromField( | ||
| field: LinkField | LinkFieldValue | undefined | null | ||
| ): LinkFieldValue | undefined { | ||
| if (!field || isFieldValueEmpty(field)) { | ||
| return undefined; | ||
| } | ||
| return (field as LinkFieldValue).href ? (field as LinkFieldValue) : (field as LinkField).value; | ||
| } | ||
|
|
||
| /** | ||
| * Builds the `href` string (path + query + hash fragment) from a link value. | ||
| */ | ||
| export function buildHrefFromLinkField(link: LinkFieldValue): string { | ||
| const anchor = link.linktype !== 'anchor' && link.anchor ? `#${link.anchor}` : ''; | ||
| const querystring = link.querystring ? `?${link.querystring}` : ''; | ||
| return `${link.href || ''}${querystring}${anchor}`; | ||
| } | ||
|
|
||
| export interface ApplyLinkFieldToAnchorOptions { | ||
| preferTextFromField: boolean; | ||
| originalClass?: string; | ||
| originalTitle?: string; | ||
| originalTarget?: string; | ||
| } | ||
|
|
||
| /** | ||
| * Applies Sitecore link attributes and optional text to a host anchor (shared by ScLink / ScRouterLink). | ||
| */ | ||
| export function applyLinkFieldToAnchor( | ||
| renderer: Renderer2, | ||
| element: HTMLAnchorElement, | ||
| link: LinkFieldValue, | ||
| options: ApplyLinkFieldToAnchorOptions | ||
| ): void { | ||
| renderer.setAttribute(element, 'href', buildHrefFromLinkField(link)); | ||
|
|
||
| const classValue = getClassFromField(link); | ||
| if (classValue) { | ||
| addClassTokens(renderer, element, classValue); | ||
| } else { | ||
| renderer.removeAttribute(element, 'class'); | ||
| if (options.originalClass) { | ||
| addClassTokens(renderer, element, options.originalClass); | ||
| } | ||
| } | ||
|
|
||
| if (link.title) { | ||
| renderer.setAttribute(element, 'title', link.title); | ||
| } else { | ||
| renderer.removeAttribute(element, 'title'); | ||
| if (options.originalTitle) { | ||
| renderer.setAttribute(element, 'title', options.originalTitle); | ||
| } | ||
| } | ||
| if (link.target) { | ||
| renderer.setAttribute(element, 'target', link.target); | ||
| if (link.target === '_blank' && !element.getAttribute('rel')) { | ||
| renderer.setAttribute(element, 'rel', 'noopener noreferrer'); | ||
|
art-alexeyenko marked this conversation as resolved.
Outdated
|
||
| } | ||
| } else { | ||
| renderer.removeAttribute(element, 'target'); | ||
| if (options.originalTarget) { | ||
| renderer.setAttribute(element, 'target', options.originalTarget); | ||
| } | ||
| } | ||
|
|
||
| const hasChildren = element.childNodes.length > 0 && element.textContent?.trim(); | ||
| if (!hasChildren) { | ||
| const text = link.text || link.href || ''; | ||
| renderer.setProperty(element, 'textContent', text); | ||
| } else if (options.preferTextFromField && link.text) { | ||
|
art-alexeyenko marked this conversation as resolved.
Outdated
|
||
| renderer.setProperty(element, 'textContent', link.text || ''); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Clears link-driven attributes when the field is empty (matches ScLink behavior: drop `href` only). | ||
| */ | ||
| export function clearLinkHrefOnAnchor(renderer: Renderer2, element: HTMLAnchorElement): void { | ||
| renderer.removeAttribute(element, 'href'); | ||
|
art-alexeyenko marked this conversation as resolved.
Outdated
|
||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.