Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

refactor(filesystem): strip header markers before escaping PDF text

Saurav Pandaa9d426ab57ce
diff --git a/browser_use/filesystem/file_system.py b/browser_use/filesystem/file_system.py
index 1eb5b1103..8ddbdbeaf 100644
--- a/browser_use/filesystem/file_system.py
+++ b/browser_use/filesystem/file_system.py
@@ -265,17 +265,17 @@ class PdfFile(BaseFile):
for line in content_lines:
if line.strip():
- escaped_line = html.escape(line)
# Handle basic markdown headers
if line.startswith('# '):
- para = Paragraph(escaped_line[2:], styles['Title'])
+ text, style = line[2:], styles['Title']
elif line.startswith('## '):
- para = Paragraph(escaped_line[3:], styles['Heading1'])
+ text, style = line[3:], styles['Heading1']
elif line.startswith('### '):
- para = Paragraph(escaped_line[4:], styles['Heading2'])
+ text, style = line[4:], styles['Heading2']
else:
- para = Paragraph(escaped_line, styles['Normal'])
- story.append(para)
+ text, style = line, styles['Normal']
+ # Paragraph parses its input as ReportLab markup, but our content is plain text
+ story.append(Paragraph(html.escape(text), style))
else:
story.append(Spacer(1, 6))
diff --git a/tests/ci/infrastructure/test_filesystem.py b/tests/ci/infrastructure/test_filesystem.py
index 99aa1223c..c96c35294 100644
--- a/tests/ci/infrastructure/test_filesystem.py
+++ b/tests/ci/infrastructure/test_filesystem.py
@@ -225,14 +225,19 @@ class TestFileSystem:
async def test_write_pdf_preserves_plain_text_angle_brackets(self, empty_filesystem):
"""PDF content should be rendered as plain text, not ReportLab markup."""
- content = 'Comparison: 2 <b y & 5 > 4'
+ lines = ['# Q&A: <b y vs 5', '## Section 2 & 3', '### Notes <i>', 'Comparison: 2 <b y & 5 > 4']
- result = await empty_filesystem.write_file('comparison.pdf', content)
+ result = await empty_filesystem.write_file('comparison.pdf', '\n\n'.join(lines))
assert result == 'Data written to file comparison.pdf successfully.'
pdf_path = empty_filesystem.data_dir / 'comparison.pdf'
extracted_text = '\n'.join(page.extract_text() or '' for page in PdfReader(pdf_path).pages)
- assert content in extracted_text
+ # Header markers are stripped, everything else survives verbatim
+ assert 'Q&A: <b y vs 5' in extracted_text
+ assert 'Section 2 & 3' in extracted_text
+ assert 'Notes <i>' in extracted_text
+ assert 'Comparison: 2 <b y & 5 > 4' in extracted_text
+ assert '#' not in extracted_text
def test_filesystem_initialization(self, temp_filesystem):
"""Test FileSystem initialization with default files."""