Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
chore: Reuse isAbsoluteUrl (#5587)
Oleg Isonen0f6383d17671
diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-image.tsx b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-image.tsxindex 87121c420b..cad3ee2ce6 100644--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-image.tsx+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-image.tsx@@ -6,14 +6,7 @@ import { PropertyInlineLabel } from "../../property-label";import { propertyDescriptions } from "@webstudio-is/css-data";import { BackgroundCodeEditor } from "./background-code-editor";import { $assets } from "~/shared/sync/data-stores";--const isAbsoluteURL = (value: string) => {- try {- return Boolean(new URL(value));- } catch {- return false;- }-};+import { isAbsoluteUrl } from "@webstudio-is/sdk";export const BackgroundImage = ({ index }: { index: number }) => {const elementRef = useRef<HTMLDivElement>(null);@@ -36,7 +29,7 @@ export const BackgroundImage = ({ index }: { index: number }) => {const url = layer.value.url;// If it's an absolute URL, no validation needed- if (isAbsoluteURL(url)) {+ if (isAbsoluteUrl(url)) {return undefined;}diff --git a/apps/builder/app/canvas/interceptor.ts b/apps/builder/app/canvas/interceptor.tsindex 2f62062a6c..0125df5b92 100644--- a/apps/builder/app/canvas/interceptor.ts+++ b/apps/builder/app/canvas/interceptor.ts@@ -1,4 +1,4 @@-import { getPagePath } from "@webstudio-is/sdk";+import { getPagePath, isAbsoluteUrl } from "@webstudio-is/sdk";import {compilePathnamePattern,matchPathnamePattern,@@ -13,15 +13,6 @@ import {import { $currentSystem, updateCurrentSystem } from "~/shared/system";import { comparePatterns } from "./shared/routing-priority";-const isAbsoluteUrl = (href: string) => {- try {- new URL(href);- return true;- } catch {- return false;- }-};-const getSelectedPagePathname = () => {const pages = $pages.get();const page = $selectedPage.get();diff --git a/packages/image/package.json b/packages/image/package.jsonindex 44e774d3bd..0ee8c8b291 100644--- a/packages/image/package.json+++ b/packages/image/package.json@@ -12,6 +12,7 @@"dts": "tsc --project tsconfig.dts.json"},"dependencies": {+ "@webstudio-is/sdk": "workspace:*","react": "18.3.0-canary-14898b6a9-20240318","warn-once": "^0.1.1"},diff --git a/packages/sdk/src/url-pattern.test.ts b/packages/sdk/src/url-pattern.test.tsindex 06089fb161..baeb60cbf8 100644--- a/packages/sdk/src/url-pattern.test.ts+++ b/packages/sdk/src/url-pattern.test.ts@@ -1,5 +1,5 @@-import { expect, test } from "vitest";-import { isPathnamePattern } from "./url-pattern";+import { expect, test, describe } from "vitest";+import { isPathnamePattern, isAbsoluteUrl } from "./url-pattern";test("check pathname is pattern", () => {expect(isPathnamePattern("/:name")).toEqual(true);@@ -12,3 +12,33 @@ test("check pathname is pattern", () => {expect(isPathnamePattern("/blog")).toEqual(false);expect(isPathnamePattern("/blog/post-name")).toEqual(false);});++describe("isAbsoluteUrl", () => {+ test("returns true for absolute URLs", () => {+ expect(isAbsoluteUrl("https://example.com")).toBe(true);+ expect(isAbsoluteUrl("http://example.com")).toBe(true);+ expect(isAbsoluteUrl("https://example.com/path")).toBe(true);+ expect(isAbsoluteUrl("https://example.com/path?query=1")).toBe(true);+ expect(isAbsoluteUrl("https://example.com:8080/path")).toBe(true);+ expect(isAbsoluteUrl("ftp://files.example.com")).toBe(true);+ expect(isAbsoluteUrl("mailto:[email protected]")).toBe(true);+ expect(isAbsoluteUrl("data:text/html,<h1>Hello</h1>")).toBe(true);+ });++ test("returns false for relative URLs", () => {+ expect(isAbsoluteUrl("/path")).toBe(false);+ expect(isAbsoluteUrl("/path/to/file")).toBe(false);+ expect(isAbsoluteUrl("path/to/file")).toBe(false);+ expect(isAbsoluteUrl("./path")).toBe(false);+ expect(isAbsoluteUrl("../path")).toBe(false);+ expect(isAbsoluteUrl("")).toBe(false);+ expect(isAbsoluteUrl("?query=1")).toBe(false);+ expect(isAbsoluteUrl("#hash")).toBe(false);+ });++ test("returns false for invalid URLs", () => {+ expect(isAbsoluteUrl("not a url")).toBe(false);+ expect(isAbsoluteUrl("https://")).toBe(false);+ expect(isAbsoluteUrl("://missing-protocol")).toBe(false);+ });+});diff --git a/packages/sdk/src/url-pattern.ts b/packages/sdk/src/url-pattern.tsindex 7ecdb3e67d..0ed69f46b9 100644--- a/packages/sdk/src/url-pattern.ts+++ b/packages/sdk/src/url-pattern.ts@@ -13,3 +13,15 @@ const tokenRegexGlobal = new RegExp(tokenRegex.source, "g");export const matchPathnameParams = (pathname: string) => {return pathname.matchAll(tokenRegexGlobal);};++/**+ * Check if a string is an absolute URL (has a valid protocol)+ */+export const isAbsoluteUrl = (href: string) => {+ try {+ new URL(href);+ return true;+ } catch {+ return false;+ }+};diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 356b65eae8..9367158f95 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -1631,6 +1631,9 @@ importers:packages/image:dependencies:+ '@webstudio-is/sdk':+ specifier: workspace:*+ version: link:../sdkreact:specifier: 18.3.0-canary-14898b6a9-20240318version: 18.3.0-canary-14898b6a9-20240318