Skip to content

Drop in a screenshot and convert it to clean code (HTML/Tailwind/React/Vue)

Python

Disambiguate select-and-edit targets with DOM location context

Abi Rajab45789debef0
diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx
index 20040e26..119701e6 100644
--- a/frontend/src/App.tsx
+++ b/frontend/src/App.tsx
@@ -25,7 +25,10 @@ import {
import { useAppStore } from "./store/app-store";
import { useProjectStore } from "./store/project-store";
import { useDesignSystems } from "./hooks/useDesignSystems";
-import { buildSelectedElementInstruction } from "./components/select-and-edit/utils";
+import {
+ buildSelectedElementInstruction,
+ describeElementContext,
+} from "./components/select-and-edit/utils";
import { useEscapeToExitSelectMode } from "./components/select-and-edit/useEscapeToExitSelectMode";
import Sidebar from "./components/sidebar/Sidebar";
import IconStrip from "./components/sidebar/IconStrip";
@@ -649,7 +652,10 @@ function App() {
selectedElementHtml = elementHtml;
modifiedUpdateInstruction = buildSelectedElementInstruction(
updateInstruction,
- elementHtml
+ elementHtml,
+ selectedElement.isConnected
+ ? describeElementContext(selectedElement)
+ : undefined
);
setSelectedElement(null);
}
diff --git a/frontend/src/components/select-and-edit/utils.test.ts b/frontend/src/components/select-and-edit/utils.test.ts
index 30a99afa..3ba1e79b 100644
--- a/frontend/src/components/select-and-edit/utils.test.ts
+++ b/frontend/src/components/select-and-edit/utils.test.ts
@@ -1,4 +1,75 @@
-import { buildSelectedElementInstruction } from "./utils";
+import {
+ buildSelectedElementInstruction,
+ describeElementContext,
+} from "./utils";
+
+// Minimal stand-in for a DOM element; jest runs in the node environment.
+interface FakeElement {
+ tagName: string;
+ outerHTML: string;
+ parentElement: FakeElement | null;
+ ownerDocument: { getElementsByTagName: (tag: string) => FakeElement[] };
+ getAttribute: (name: string) => string | null;
+}
+
+function fakeElement(tag: string, classAttr: string, outerHTML = ""): FakeElement {
+ return {
+ tagName: tag.toUpperCase(),
+ outerHTML,
+ parentElement: null,
+ ownerDocument: { getElementsByTagName: () => [] },
+ getAttribute: (name) => (name === "class" && classAttr ? classAttr : null),
+ };
+}
+
+function asElement(el: FakeElement): Element {
+ return el as unknown as Element;
+}
+
+describe("describeElementContext", () => {
+ function buildPricingPage() {
+ const body = fakeElement("body", "");
+ const grid = fakeElement("div", "pricing-grid");
+ const card = fakeElement("div", "pricing-card featured");
+ const anchorHtml = '<a href="#" class="btn">Choose plan</a>';
+ const basicBtn = fakeElement("a", "btn", anchorHtml);
+ const proBtn = fakeElement("a", "btn", anchorHtml);
+ const enterpriseBtn = fakeElement("a", "btn", anchorHtml);
+ grid.parentElement = body;
+ card.parentElement = grid;
+ proBtn.parentElement = card;
+ const doc = {
+ getElementsByTagName: () => [basicBtn, proBtn, enterpriseBtn],
+ };
+ [basicBtn, proBtn, enterpriseBtn].forEach((el) => {
+ el.ownerDocument = doc;
+ });
+ return { proBtn };
+ }
+
+ it("builds an ancestor path with classes", () => {
+ const { proBtn } = buildPricingPage();
+ const context = describeElementContext(asElement(proBtn));
+ expect(context).toContain(
+ "Element location: body > div.pricing-grid > div.pricing-card.featured > a.btn"
+ );
+ });
+
+ it("notes the position among identical elements", () => {
+ const { proBtn } = buildPricingPage();
+ const context = describeElementContext(asElement(proBtn));
+ expect(context).toContain("3 elements on the page share this exact markup");
+ expect(context).toContain("number 2 of 3");
+ });
+
+ it("omits the duplicate note when the markup is unique", () => {
+ const heading = fakeElement("h1", "title", "<h1 class=\"title\">Hi</h1>");
+ heading.ownerDocument = { getElementsByTagName: () => [heading] };
+ const context = describeElementContext(asElement(heading));
+ expect(context).toContain("Element location: h1.title");
+ expect(context).not.toContain("share this exact markup");
+ });
+});
describe("buildSelectedElementInstruction", () => {
it("includes the instruction and the element html", () => {
@@ -33,4 +104,21 @@ describe("buildSelectedElementInstruction", () => {
);
expect(result).not.toContain("truncated");
});
+
+ it("includes the element context when provided", () => {
+ const result = buildSelectedElementInstruction(
+ "Make it red",
+ '<a class="btn">Go</a>',
+ "Element location: body > div.card > a.btn"
+ );
+ expect(result).toContain("Element location: body > div.card > a.btn");
+ });
+
+ it("omits the context block when not provided", () => {
+ const result = buildSelectedElementInstruction(
+ "Make it red",
+ '<a class="btn">Go</a>'
+ );
+ expect(result).not.toContain("Element location:");
+ });
});
diff --git a/frontend/src/components/select-and-edit/utils.ts b/frontend/src/components/select-and-edit/utils.ts
index bbc0232d..fd9b3810 100644
--- a/frontend/src/components/select-and-edit/utils.ts
+++ b/frontend/src/components/select-and-edit/utils.ts
@@ -2,9 +2,44 @@
// full current code, so the snippet only needs to identify the element.
const MAX_ELEMENT_HTML_LENGTH = 12000;
+const MAX_PATH_DEPTH = 6;
+
+function describeNode(el: Element): string {
+ const tag = el.tagName.toLowerCase();
+ const classAttr = el.getAttribute("class") || "";
+ const classes = classAttr.split(/\s+/).filter(Boolean).slice(0, 3);
+ return tag + classes.map((c) => `.${c}`).join("");
+}
+
+// The outerHTML alone can't identify the element when siblings share identical
+// markup (e.g. three "Choose plan" buttons styled by a parent class), so also
+// describe where it sits in the DOM.
+export function describeElementContext(el: Element): string {
+ const parts: string[] = [];
+ let current: Element | null = el;
+ while (current && parts.length < MAX_PATH_DEPTH) {
+ if (current.tagName.toLowerCase() === "html") break;
+ parts.unshift(describeNode(current));
+ current = current.parentElement;
+ }
+ const lines = [`Element location: ${parts.join(" > ")}`];
+
+ const identical = Array.from(
+ el.ownerDocument.getElementsByTagName(el.tagName)
+ ).filter((other) => other.outerHTML === el.outerHTML);
+ if (identical.length > 1) {
+ const position = identical.indexOf(el) + 1;
+ lines.push(
+ `${identical.length} elements on the page share this exact markup; the user selected number ${position} of ${identical.length} in document order. Edit only that one and leave the other copies exactly as they are. Because the markup repeats, do not locate the element by its own markup alone — anchor the edit with unique surrounding context (its parent element or a distinguishing ancestor class from the location path above), or scope a style change through that ancestor. Any search/replace whose search text matches more than one place will hit the wrong copy.`
+ );
+ }
+ return lines.join("\n");
+}
+
export function buildSelectedElementInstruction(
instruction: string,
- elementHtml: string
+ elementHtml: string,
+ elementContext?: string
): string {
const truncated =
elementHtml.length > MAX_ELEMENT_HTML_LENGTH
@@ -19,6 +54,6 @@ Apply the change to this specific element that the user selected in the preview:
\`\`\`html
${truncated}
\`\`\`
-
+${elementContext ? `\n${elementContext}\n` : ""}
This snippet is the element's outerHTML captured from the rendered page, so it can differ from the source code (for example JSX uses className, Vue templates use directives and interpolations, and frameworks like Ionic or Bootstrap may inject classes or attributes at runtime). Find the code that produces this element and apply the change there, leaving unrelated code untouched.`;
}