Skip to content

Fix some angular ssr errors #9992

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
merged 2 commits into from
Jun 5, 2025
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 @@ -40,9 +40,11 @@ export class SvgIconComponent implements OnChanges {
return "presentation";
}
ngOnChanges(): void {
const el = this.viewContaierRef.element.nativeElement;
el.innerHTML = "";
el.appendChild(document.createElementNS("http://www.w3.org/2000/svg", "use"));
this.createSvg();
if (typeof document !== "undefined") {
const el = this.viewContaierRef.element.nativeElement;
el.innerHTML = "";
el.appendChild(document.createElementNS("http://www.w3.org/2000/svg", "use"));
this.createSvg();
}
}
}
7 changes: 4 additions & 3 deletions packages/survey-angular-ui/src/svgbundle.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import { Component, ElementRef, OnDestroy, OnInit, ViewChild } from "@angular/co
export class SvgBundleComponent implements OnInit, OnDestroy {
@ViewChild("svgContainer", { static: true }) svgContainer!: ElementRef<SVGElement>;
private onIconsChanged = () => {
if (!!this.svgContainer?.nativeElement) {
this.svgContainer.nativeElement.innerHTML = SvgRegistry.iconsRenderedHtml();
if (typeof document !== "undefined") {
if (!!this.svgContainer?.nativeElement) {
this.svgContainer.nativeElement.innerHTML = SvgRegistry.iconsRenderedHtml();
}
}
};
ngOnInit(): void {
Expand All @@ -19,5 +21,4 @@ export class SvgBundleComponent implements OnInit, OnDestroy {
ngOnDestroy(): void {
SvgRegistry.onIconsChanged.remove(this.onIconsChanged);
}

}
8 changes: 4 additions & 4 deletions packages/survey-core/src/question.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { PopupModel } from "./popup";
import { ConsoleWarnings } from "./console-warnings";
import { ProcessValue } from "./conditionProcessValue";
import { ITheme } from "./themes";
import { DomWindowHelper } from "./global_variables_utils";
import { DomDocumentHelper, DomWindowHelper } from "./global_variables_utils";
import { ITextArea, TextAreaModel } from "./utils/text-area";
import { Action } from "./actions/action";
import { QuestionSingleInputSummary } from "./questionSingleInputSummary";
Expand Down Expand Up @@ -1371,9 +1371,8 @@ export class Question extends SurveyElement<Question>
if (this.supportComment() || this.supportOther()) {
this.commentElements = [];
this.getCommentElementsId().forEach(id => {
const { root } = settings.environment;
let el = root.getElementById(id);
if (el)this.commentElements.push(el);
const commentEl = el?.querySelector(`#${id}`);
if (commentEl)this.commentElements.push(commentEl as HTMLElement);
});
this.updateCommentElements();
}
Expand Down Expand Up @@ -3062,6 +3061,7 @@ export class Question extends SurveyElement<Question>
}
private triggerResponsivenessCallback: (hard: boolean) => void;
private initResponsiveness(el: HTMLElement) {
if (!DomDocumentHelper.isAvailable()) { return; }
this.destroyResizeObserver();
if (!!el && this.isDefaultRendering()) {
const scrollableSelector = this.getObservedElementSelector();
Expand Down
3 changes: 1 addition & 2 deletions packages/survey-core/src/question_comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ export class QuestionCommentModel extends QuestionTextBase {
return "comment";
}
public afterRenderQuestionElement(el: HTMLElement): void {
const { root } = settings.environment;
this.element = root.getElementById(this.inputId) || el;
this.element = el?.querySelector(`#${this.inputId}`) || el;
super.afterRenderQuestionElement(el);
}
public beforeDestroyQuestionElement(el: HTMLElement): void {
Expand Down
10 changes: 7 additions & 3 deletions packages/survey-core/src/question_signaturepad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,13 @@ export class QuestionSignaturePadModel extends QuestionFileModelBase {
return "signaturepad";
}
public afterRenderQuestionElement(el: HTMLElement) {
if (!!el) {
if (!this.isDesignMode)this.initSignaturePad(el);
this.element = el;
if (DomWindowHelper.isAvailable()) {
if (!!el) {
if (!this.isDesignMode) {
this.initSignaturePad(el);
}
this.element = el;
}
}
super.afterRenderQuestionElement(el);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/survey-core/src/scroll.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

import { DomDocumentHelper, DomWindowHelper } from "./global_variables_utils";
export class ScrollViewModel {
private _containerElementValue: HTMLElement;
private _scrollbarElement: HTMLElement;
Expand All @@ -15,7 +16,7 @@ export class ScrollViewModel {
this._scrollbarElement = element?.querySelector(".sv-scroll__scrollbar");
this._scrollbarSizerElement = element?.querySelector(".sv-scroll__scrollbar-sizer");
this._containerBodyElement = element?.querySelector(".sv-scroll__container");
if (!element) return;
if (!element || !DomWindowHelper.isAvailable()) return;
this._containerBodyResizeObserver = new ResizeObserver(entries => {
for (let entry of entries) {
const height = entry.contentBoxSize
Expand Down
1 change: 1 addition & 0 deletions packages/survey-core/src/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5485,6 +5485,7 @@ export class SurveyModel extends SurveyElementCore
private isSmoothScrollEnabled = false;
private resizeObserver: ResizeObserver;
afterRenderSurvey(htmlElement: any) {
if (!DomWindowHelper.isAvailable()) return;
this.destroyResizeObserver();
if (Array.isArray(htmlElement)) {
htmlElement = SurveyElement.GetFirstNonTextElement(htmlElement);
Expand Down
6 changes: 3 additions & 3 deletions packages/survey-core/tests/question_customtests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ QUnit.test("Single: onAfterRender and onAfterRenderContentElement", function (
elements: [{ type: "newquestion", name: "q1" }],
});
var q = <QuestionCustomModel>survey.getAllQuestions()[0];
const el: HTMLElement = <any>({ a: 5 });
const el: HTMLElement = <any>({ a: 5, querySelector: () => {} });
q.afterRender(el);
assert.equal(
afterRenderQuestion.name,
Expand All @@ -1070,7 +1070,7 @@ QUnit.test("Single: onAfterRender and onAfterRenderContentElement", function (
5,
"onAfterRender, htmlElement parameter is correct"
);
q.contentQuestion.afterRender(<any>7);
q.contentQuestion.afterRender({ b: 7, querySelector: () => {} } as any);
assert.equal(
afterRenderContentElementQuestion.name,
"q1",
Expand All @@ -1082,7 +1082,7 @@ QUnit.test("Single: onAfterRender and onAfterRenderContentElement", function (
"afterRenderContentElement, element parameter is correct"
);
assert.equal(
afterRenderContentElementHtml,
(afterRenderContentElementHtml as any).b,
7,
"afterRenderContentElement, htmlElement parameter is correct"
);
Expand Down
7 changes: 5 additions & 2 deletions packages/survey-vue3-ui/src/Comment.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</div>
<SvComponent
:is="'sv-text-area'"
ref="root"
:get-ref="getRef"
v-else-if="!question.isReadOnlyRenderDiv() && !question.getMaxLength()"
:model="question.textAreaModel"
></SvComponent>
Expand All @@ -29,6 +29,9 @@ import { useQuestion } from "./base";
import { ref } from "vue";
defineOptions({ inheritAttrs: false });
const props = defineProps<{ question: QuestionCommentModel }>();
const root = ref(null);
const root = ref<HTMLElement>();
function getRef(el: HTMLElement) {
root.value = el;
}
useQuestion(props, root);
</script>
8 changes: 6 additions & 2 deletions packages/survey-vue3-ui/src/components/TextArea.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<textarea
ref="contentElement"
:ref="(el)=>getRef(el as HTMLTextAreaElement)"
:readonly="model.isReadOnlyAttr"
:disabled="model.isDisabledAttr"
:value="value"
Expand Down Expand Up @@ -49,10 +49,14 @@
import type { TextAreaModel } from "survey-core";
import { computed, onMounted, onUpdated, onUnmounted, ref } from "vue";

const props = defineProps<{ model: TextAreaModel }>();
const props = defineProps<{ model: TextAreaModel, getRef?: (element: HTMLElement) => void }>();
const contentElement = ref<HTMLTextAreaElement>(
null as any as HTMLTextAreaElement
);
const getRef = function (element: HTMLTextAreaElement) {
contentElement.value = element;
if (props.getRef) props.getRef(element);
};
const value = computed(() => props.model.getTextValue() || "");

onMounted(() => {
Expand Down