Skip to content

Commit acf2f96

Browse files
committed
feat: remove maxChar in getOuterHTML
max character limits should be enforced globally
1 parent d525992 commit acf2f96

File tree

3 files changed

+8
-45
lines changed

3 files changed

+8
-45
lines changed

packages/extension/src/htmlUtils.ts

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,15 @@ interface StructureSummary {
66
totalDepth: number;
77
}
88

9-
/**
10-
* Truncates HTML based on depth and line length controls
11-
* @param node - The DOM node to truncate
12-
* @param maxDepth - Maximum nesting depth of tags to include
13-
* @param maxLineLength - Maximum length of each line
14-
* @param maxChars - Maximum number of characters
15-
* @returns Truncated HTML
16-
*/
179
export function truncateHTML(
18-
node: Node,
10+
element: Element,
1911
maxDepth?: number,
2012
maxLineLength?: number,
21-
maxChars?: number,
2213
): string {
23-
let result: Node = node.cloneNode(true);
24-
25-
// Truncate by depth
26-
if (maxDepth !== undefined && maxDepth > 0) {
27-
for (let depth = maxDepth; depth > 0; depth--) {
28-
// has to use original node each time to ensure summary is correct
29-
let truncated = truncateByDepth(node, depth);
30-
let html = (truncated as Element).outerHTML || truncated.textContent;
31-
if (maxChars === undefined || (html && html.length < maxChars)) {
32-
result = truncated;
33-
break;
34-
}
35-
}
36-
}
14+
let pruned = truncateByDepth(element, maxDepth) as Element;
3715

3816
// Get HTML string
39-
let html = (result as Element).outerHTML || result.textContent || "";
17+
let html = pruned.outerHTML;
4018
html = beautify.html(html, {
4119
indent_size: 2,
4220
wrap_line_length: 0, // Don't wrap - let truncateByLineLength handle it

packages/extension/src/offscreen_inspectors.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,25 +94,15 @@ async function processRequest(
9494

9595
case "getOuterHTML": {
9696
// some safe defaults
97-
const {
98-
node: uid,
99-
maxDepth = 3,
100-
maxLineLength = 200,
101-
maxChars = 100000,
102-
} = request;
97+
const { node: uid, maxDepth = 3, maxLineLength = 200 } = request;
10398
const node = nodeManager.getNode(uid, inspector);
10499
if (!node) {
105100
throw new Error(`Node not found for: ${uid}`);
106-
} else if (!node.tracked) {
107-
throw new Error(`Node is no longer existed for: ${uid}`);
101+
} else if (!(node instanceof InspectorElement)) {
102+
throw new Error(`Non-element nodes do not support styles for: ${uid}`);
108103
}
109104
// Apply depth and line length controls if provided
110-
const html = truncateHTML(
111-
node._docNode,
112-
maxDepth,
113-
maxLineLength,
114-
maxChars,
115-
);
105+
const html = truncateHTML(node._docNode, maxDepth, maxLineLength);
116106
console.log("serveRequest - getOuterHTML html:", html);
117107
return { outerHTML: html };
118108
}

packages/mcp/src/server.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,19 +160,14 @@ export class DevToolCSSMCPServer {
160160
.number()
161161
.optional()
162162
.describe("Maximum line length (default: 200)"),
163-
maxChars: z
164-
.number()
165-
.optional()
166-
.describe("Maximum total characters (default: 100000)"),
167163
},
168164
},
169-
async ({ node, maxDepth, maxLineLength, maxChars }) => {
165+
async ({ node, maxDepth, maxLineLength }) => {
170166
const result = await this.wsServer.sendRequest({
171167
tool: "getOuterHTML",
172168
node,
173169
maxDepth,
174170
maxLineLength,
175-
maxChars,
176171
});
177172
return {
178173
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],

0 commit comments

Comments
 (0)