Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(web): escape prompt editor variable search highlighting (#44625)

Wu Tianwei082aa5bba381
diff --git a/oxlint-suppressions.json b/oxlint-suppressions.json
index df0d9803e20..d3838c5c3d9 100644
--- a/oxlint-suppressions.json
+++ b/oxlint-suppressions.json
@@ -1743,17 +1743,6 @@
"count": 1
}
},
- "web/app/components/base/prompt-editor/plugins/component-picker-block/variable-option.tsx": {
- "better-tailwindcss/enforce-logical-properties": {
- "count": 1
- },
- "jsx-a11y/click-events-have-key-events": {
- "count": 1
- },
- "jsx-a11y/no-static-element-interactions": {
- "count": 1
- }
- },
"web/app/components/base/prompt-editor/plugins/context-block/component.tsx": {
"better-tailwindcss/enforce-logical-properties": {
"count": 7
@@ -7567,4 +7556,4 @@
"count": 2
}
}
-}
+}
\ No newline at end of file
diff --git a/web/app/components/app/configuration/config-prompt/__tests__/simple-prompt-input.spec.tsx b/web/app/components/app/configuration/config-prompt/__tests__/simple-prompt-input.spec.tsx
index 9ca136e322e..1d244eac7db 100644
--- a/web/app/components/app/configuration/config-prompt/__tests__/simple-prompt-input.spec.tsx
+++ b/web/app/components/app/configuration/config-prompt/__tests__/simple-prompt-input.spec.tsx
@@ -146,7 +146,9 @@ it('validates an external tool in the real modal and inserts the saved variable
renderPrompt({ promptTemplate: 'Hello' })
await user.click(screen.getByRole('textbox'))
await replacePromptEditorText(screen.getByRole('textbox'), '/')
- await user.click(await screen.findByText('common.promptEditor.variable.modal.addTool'))
+ await user.click(
+ await screen.findByRole('option', { name: 'common.promptEditor.variable.modal.addTool' }),
+ )
const dialog = await screen.findByRole('dialog')
await user.type(
within(dialog).getByPlaceholderText('appDebug.feature.tools.modal.name.placeholder'),
diff --git a/web/app/components/base/prompt-editor/plugins/component-picker-block/__tests__/index.spec.tsx b/web/app/components/base/prompt-editor/plugins/component-picker-block/__tests__/index.spec.tsx
index 928d8a47185..d050cf61c29 100644
--- a/web/app/components/base/prompt-editor/plugins/component-picker-block/__tests__/index.spec.tsx
+++ b/web/app/components/base/prompt-editor/plugins/component-picker-block/__tests__/index.spec.tsx
@@ -35,6 +35,7 @@ import { EventEmitterContextProvider } from '@/context/event-emitter-provider'
import { INSERT_AGENT_OUTPUT_BLOCK_COMMAND } from '../../agent-output-block/commands'
import { INSERT_CONTEXT_BLOCK_COMMAND } from '../../context-block'
import { INSERT_CURRENT_BLOCK_COMMAND } from '../../current-block'
+import { CustomTextNode } from '../../custom-text/node'
import { INSERT_ERROR_MESSAGE_BLOCK_COMMAND } from '../../error-message-block'
import { INSERT_LAST_RUN_BLOCK_COMMAND } from '../../last-run-block'
import { INSERT_VARIABLE_VALUE_BLOCK_COMMAND } from '../../variable-block'
@@ -174,6 +175,7 @@ const MinimalEditor: React.FC<{
const initialConfig = React.useMemo(
() => ({
namespace: 'component-picker-test',
+ nodes: [CustomTextNode],
onError: (e: Error) => {
throw e
},
@@ -275,6 +277,32 @@ describe('ComponentPicker (component-picker-block/index.tsx)', () => {
})
})
+ it.each(['{[', '[Open manual]({source_url})'])(
+ 'keeps the add-variable action usable after entering "%s"',
+ async (text) => {
+ const user = userEvent.setup()
+ const captures: Captures = { editor: null, eventEmitter: null }
+ render(
+ <MinimalEditor
+ triggerString="{"
+ variableBlock={{ show: true, variables: [] }}
+ captures={captures}
+ />,
+ )
+
+ const editor = await waitForEditor(captures)
+ await setEditorText(editor, text, true)
+
+ const addVariable = await screen.findByRole('option', {
+ name: 'common.promptEditor.variable.modal.add',
+ })
+ expect(readEditorText(editor)).toBe(text)
+ await user.click(addVariable)
+
+ await waitFor(() => expect(readEditorText(editor)).toContain('{{}}'))
+ },
+ )
+
it('renders prompt options in a portal and removes the trigger TextNode when selecting a normal option', async () => {
const user = userEvent.setup()
diff --git a/web/app/components/base/prompt-editor/plugins/component-picker-block/__tests__/variable-option.spec.tsx b/web/app/components/base/prompt-editor/plugins/component-picker-block/__tests__/variable-option.spec.tsx
index f419106806c..c94b9640d5e 100644
--- a/web/app/components/base/prompt-editor/plugins/component-picker-block/__tests__/variable-option.spec.tsx
+++ b/web/app/components/base/prompt-editor/plugins/component-picker-block/__tests__/variable-option.spec.tsx
@@ -2,25 +2,62 @@ import { createEvent, fireEvent, render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { VariableMenuItem } from '../variable-option'
-it('selects the matching variable without losing its full label', async () => {
- const user = userEvent.setup()
- const onClick = vi.fn()
- const onMouseEnter = vi.fn()
+it.each(['System Variable', 'İstanbul System Variable'])(
+ 'selects the case-insensitive match in "%s" without losing its full label',
+ async (title) => {
+ const user = userEvent.setup()
+ const onClick = vi.fn()
+ const onMouseEnter = vi.fn()
+ render(
+ <VariableMenuItem
+ title={title}
+ queryString="TEM"
+ isSelected={false}
+ onClick={onClick}
+ onMouseEnter={onMouseEnter}
+ />,
+ )
+ const option = screen.getByRole('option', { name: title })
+ expect(option).toHaveTextContent(title)
+ expect(screen.getByText('tem')).toBeInTheDocument()
+ await user.hover(option)
+ await user.click(option)
+ expect(onMouseEnter).toHaveBeenCalledOnce()
+ expect(onClick).toHaveBeenCalledOnce()
+ },
+)
+
+it.each(['[', '(', '\\', '.*'])(
+ 'highlights "%s" as literal text without changing the label',
+ (queryString) => {
+ const title = `Before ${queryString} after`
+ render(
+ <VariableMenuItem
+ title={title}
+ queryString={queryString}
+ isSelected={false}
+ onClick={vi.fn()}
+ onMouseEnter={vi.fn()}
+ />,
+ )
+
+ expect(screen.getByRole('option', { name: title })).toHaveTextContent(title)
+ expect(screen.getByText(queryString)).toBeInTheDocument()
+ },
+)
+
+it.each(['', 'missing'])('keeps the full label when the query is "%s"', (queryString) => {
render(
<VariableMenuItem
- title="System Variable"
- queryString="TEM"
+ title="Variable"
+ queryString={queryString}
isSelected={false}
- onClick={onClick}
- onMouseEnter={onMouseEnter}
+ onClick={vi.fn()}
+ onMouseEnter={vi.fn()}
/>,
)
- const option = screen.getByTitle('System Variable')
- expect(option).toHaveTextContent('System Variable')
- await user.hover(option)
- await user.click(option)
- expect(onMouseEnter).toHaveBeenCalledOnce()
- expect(onClick).toHaveBeenCalledOnce()
+
+ expect(screen.getByRole('option', { name: 'Variable' })).toHaveTextContent('Variable')
})
it('consumes mousedown so a variable choice does not disturb the editor selection', () => {
@@ -36,7 +73,7 @@ it('consumes mousedown so a variable choice does not disturb the editor selectio
/>
</div>,
)
- const target = screen.getByText('Variable')
+ const target = screen.getByRole('option', { name: 'Variable' })
const event = createEvent.mouseDown(target)
fireEvent(target, event)
expect(event.defaultPrevented).toBe(true)
diff --git a/web/app/components/base/prompt-editor/plugins/component-picker-block/hooks.tsx b/web/app/components/base/prompt-editor/plugins/component-picker-block/hooks.tsx
index 17d3ad8026b..74406c4c889 100644
--- a/web/app/components/base/prompt-editor/plugins/component-picker-block/hooks.tsx
+++ b/web/app/components/base/prompt-editor/plugins/component-picker-block/hooks.tsx
@@ -252,7 +252,7 @@ export const useExternalToolOptions = (
<VariableMenuItem
title={item.name}
icon={<AppIcon className="h-3.5! w-3.5!" icon={item} />}
- extraElement={<div className="text-xs text-text-tertiary">{item.variableName}</div>}
+ extraElement={<span className="text-xs text-text-tertiary">{item.variableName}</span>}
queryString={queryString}
isSelected={isSelected}
onClick={onSelect}
diff --git a/web/app/components/base/prompt-editor/plugins/component-picker-block/variable-option.tsx b/web/app/components/base/prompt-editor/plugins/component-picker-block/variable-option.tsx
index 422a649ce03..44d78014ef8 100644
--- a/web/app/components/base/prompt-editor/plugins/component-picker-block/variable-option.tsx
+++ b/web/app/components/base/prompt-editor/plugins/component-picker-block/variable-option.tsx
@@ -1,3 +1,4 @@
+import { escapeRegExp } from 'es-toolkit/string'
import { memo } from 'react'
type VariableMenuItemProps = {
@@ -8,7 +9,7 @@ type VariableMenuItemProps = {
queryString: string | null
onClick: () => void
onMouseEnter: () => void
- setRefElement?: (element: HTMLDivElement) => void
+ setRefElement?: (element: HTMLButtonElement) => void
}
export const VariableMenuItem = memo(
({
@@ -26,8 +27,7 @@ export const VariableMenuItem = memo(
let after = ''
if (queryString) {
- const regex = new RegExp(queryString, 'i')
- const match = regex.exec(title)
+ const match = new RegExp(escapeRegExp(queryString), 'i').exec(title)
if (match) {
before = title.substring(0, match.index)
@@ -37,8 +37,11 @@ export const VariableMenuItem = memo(
}
return (
- <div
- className={`flex h-6 cursor-pointer items-center rounded-md px-3 hover:bg-state-base-hover ${isSelected && 'bg-state-base-hover'} `}
+ <button
+ type="button"
+ role="option"
+ aria-selected={isSelected}
+ className={`flex h-6 w-full cursor-pointer items-center rounded-md px-3 text-start hover:bg-state-base-hover focus-visible:ring-2 focus-visible:ring-state-accent-solid focus-visible:outline-hidden ${isSelected ? 'bg-state-base-hover' : ''}`}
tabIndex={-1}
ref={setRefElement}
onMouseEnter={onMouseEnter}
@@ -48,14 +51,17 @@ export const VariableMenuItem = memo(
}}
onClick={onClick}
>
- <div className="mr-2">{icon}</div>
- <div className="grow truncate text-[13px] text-text-secondary" title={title}>
+ <span aria-hidden className="me-2">
+ {icon}
+ </span>
+ <span className="sr-only">{title}</span>
+ <span aria-hidden className="grow truncate text-[13px] text-text-secondary" title={title}>
{before}
<span className="text-text-accent">{middle}</span>
{after}
- </div>
+ </span>
{extraElement}
- </div>
+ </button>
)
},
)