Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: infinite loop in selection html with base64 images #82

Merged
merged 8 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/thirty-poems-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"prosemirror-dev-toolkit": patch
---

fix: infinite loop in selection html with base64 images
2 changes: 1 addition & 1 deletion packages/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"svelte-check": "^3.7.1",
"svelte-preprocess": "^5.1.4",
"typescript": "5.4.5",
"vite": "^5.2.11",
"vite": "^5.4.2",
"vite-tsconfig-paths": "^4.3.2"
},
"dependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/prosemirror-dev-toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@
"svelte-preprocess": "^5.1.4",
"tslib": "^2.6.2",
"typescript": "^5.4.5",
"vite": "^5.2.11",
"vite": "^5.4.2",
"vite-tsconfig-paths": "^4.3.2",
"vitest": "^1.5.3"
"vitest": "^2.0.5"
},
"dependencies": {
"html": "^1.0.0",
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { EditorView } from 'prosemirror-view'
import { beforeAll, describe, expect, it, vi } from 'vitest'

import { createHistoryEntry } from './createHistoryEntry'
import { setupEditor } from '$test-utils/setupEditor'
import { AllSelection } from 'prosemirror-state'

let view: EditorView

describe('createHistoryEntry', () => {
beforeAll(() => {
const el = document.createElement('div')
document.body.appendChild(el)
el.id = 'pm-editor'
view = setupEditor(el)
vi.useFakeTimers()
vi.setSystemTime(new Date(1704060000000))
const oldMath = Math
vi.stubGlobal('Math', {
floor: oldMath.floor,
max: oldMath.max,
min: oldMath.min,
random: vi.fn(() => 0)
})
})

it('should collapse base64 src attributes', () => {
const logSpy = vi.spyOn(console, 'log')
const infoSpy = vi.spyOn(console, 'info')
const warnSpy = vi.spyOn(console, 'warn')
const errorSpy = vi.spyOn(console, 'error')

const startingState = view.state
// This will cause an infinite loop in 'html' package
const img1 = view.state.schema.nodes.image.create({
src: 'data:image/png;base64,abcdefgh'
})
const img2 = view.state.schema.nodes.image.create({
src: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAfQAAAH0CAIAAABEtEjdAAAACXBIWXMAAA7zAAAO8wEcU5k6AAAAEXRFWHRUaXRsZQBQREYgQ3JlYXRvckFevCgAAAATdEVYdEF1dGhvcgBQREYgVG9vbHMgQUcbz3cwAAAALXpUWHREZXNjcmlwdGlvbgAACJnLKCkpsNLXLy8v1ytISdMtyc/PKdZLzs8FAG6fCPGXryy4AAPW0ElEQVR42uy9Z5djV3Yl+ByAB+9deG8yMzLSMA1tkeWk6pJKKmla06MlafRB3/UX9BP0eTSj1RpperpLKpXKscgii0xDZpLpIyMiM7yPQCCAgLfPzr73AUikYVVR00sqUvcwVxABIICHB2Dfc/fZZx/eNE2OBQsWLFh8uUJgp4AFCxYsGLizYMGCBQsG7ixYsGDBgoE7CxYsWLBg4M6CBQsWLBi4s2DBggUDdxYsWLBgwcCdBQsWLFgwcGfBggULFgzcWbBgwYIFA3cWLFiwYODOggULFiwYuLNgwYIFCwbuLFiwYMGCgTsLFixYsGDgzoIFCxYsGLizYMGCBQN3FixYsGDBwJ0FCxYsWDBwZ8GCBQsWDNxZsGDBggUDdxYsWLBg4M6CBQsWLBi4s2DBggULBu4sWLBgwYKBOwsWLFiwYODOggULFiwYuLNgwYIFA3cWLFiwYMHAnQULFixYMHBnwYIFCxYM3FmwYMGCBQN3FixYsGDgzoIFCxYsGLizYMGCBQsG7ixYsGDBgoE7CxYsWLBg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8sXBcrRY1BeR72Wbn3E7B55MjUd/gQHJkdDAej9qdJU4A0Hs5062V+KOjRi7brNVMrycYCAX9AbckQ3OpC7JJGrzI5BAvPfuC1W2L12uarQau1vtC8Bo7I4W+KbgcetHb3oXpz/gxMKqfgTuLLwu4Gy/+zj8B8KdCb19jWjDCQzwOeCD31SkrDR7burlZM4vFci2rZrPpfCHj9shDw8megSiYA86sgX8w1apuKEhzkeFu7+0uLi5ub29njyhX3gSQ8Xabyy67RcEJTUkgGA8Foz29Q6FgTBQdOrhsUYIM0RU6hqKQ4rhInl8jOhOUPSnYSRSvDdoaCuQzqACR3Bm/GUarJwk/UXaVHALdbLTkkVa9FtySRAU1LR8Z8P68Dg6KpwQUcnxdAwsFkaYNrak4ZkVRRZvYANAjCdegdzQb1drx8XGxkMXCoCrlSu0YBYJKLavrVZvdxH4gGFJARvX1Do6NTfX3jUj2AKcJWtMs5atH6eNcroADDvmDIRqCx8MpKsn6hbaqsu2JYPV64WhMQmc1DbISqHRCYejp9/RpA7XnnXYYuDNwZ/FlAffPHF7xwvzdmlkKAQhvWEJG0VIZilyrb1NXuEIeIpd6Hrz4cb7XNdI/EHYmLX6gyIkFTqwBfY4LqWwunz4qZdKV3d38/n5BU5CMe2VuiBQ2nQ7Q1h6vCz/dXpfsROVTsDsdbg9ELxIMBdCrRPNxCX2qlsEArdyCjhZstOHIklqS9QpaFXJrqwPWaXe2nQBE0hOl63QZIDVVof2KO+VN0gFLlgHDuhvfupIk8A67jOvB4ND2VyxxWNsMwteLNfIrTg9ZArBlQSmCnCKw/LpKKseNRimfz2SyB4X8cb1eVUzQULrTKURjgcH+2OBgvDcZDQZ8AuFtZMKhF7W9neP0QZ7nZK/L3z86jBICLztaPvMdfLfRAoipmeSNMemaDUYHzy0KTy3SAj1LfOsyA3cG7iy+rKF3jZ'
})
const tr = view.state.tr
tr.insert(0, img1)
tr.insert(0, img2)
tr.setSelection(new AllSelection(tr.doc))
view.dispatch(tr)
const entry = createHistoryEntry([tr], view.state, startingState)
// Hack to fix vitest's mocked Date from producing weird getHours (22)
entry.timeStr = '00' + entry.timeStr.slice(2)
expect(entry).toMatchSnapshot()
expect(logSpy).toHaveBeenCalledTimes(0)
expect(infoSpy).toHaveBeenCalledTimes(0)
expect(warnSpy).toHaveBeenCalledTimes(0)
expect(errorSpy).toHaveBeenCalledTimes(0)
expect(document.body).toMatchSnapshot()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ const formatTimestamp = (timestamp: number) => {
].join(':')
}

const regexp = /(<\/?[\w\d\s="']+>)/gim
// Matches any src attribute containing base64 data due to bug in html package and legibility
// https://github.com/TeemuKoivisto/prosemirror-dev-toolkit/issues/81
const srcAttr = /src=[\"|\']data:(.*);base64,(.*)[\"|\']/g

const wrappingCarets = /(<\/?[\w\d\s="']+>)/gim
const highlightHtmlString = (html: string) =>
html
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(regexp, "<span style='color: cadetblue;'>$&</span>")
.replace(wrappingCarets, "<span style='color: cadetblue;'>$&</span>")

export function createHistoryEntry(
trs: readonly Transaction[],
Expand All @@ -57,7 +61,7 @@ export function createHistoryEntry(
if (domFragment) {
let child = domFragment.firstChild as HTMLElement | null
while (child) {
selectedElementsAsHtml.push(child.outerHTML)
selectedElementsAsHtml.push(child.outerHTML.replaceAll(srcAttr, 'src="..."'))
child = child.nextSibling as HTMLElement | null
}
}
Expand Down
31 changes: 30 additions & 1 deletion packages/prosemirror-dev-toolkit/src/test-utils/schema.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
import { Schema } from 'prosemirror-model'
import { NodeSpec, Schema } from 'prosemirror-model'

export const schema = new Schema({
nodes: {
doc: {
content: 'block+'
},
/// An inline image (`<img>`) node. Supports `src`,
/// `alt`, and `href` attributes. The latter two default to the empty
/// string.
image: {
inline: true,
attrs: {
src: { validate: 'string' },
alt: { default: null, validate: 'string|null' },
title: { default: null, validate: 'string|null' }
},
group: 'inline',
draggable: true,
parseDOM: [
{
tag: 'img[src]',
getAttrs(dom: HTMLElement) {
return {
src: dom.getAttribute('src'),
title: dom.getAttribute('title'),
alt: dom.getAttribute('alt')
}
}
}
],
toDOM(node) {
const { src, alt, title } = node.attrs
return ['img', { src, alt, title }]
}
} as NodeSpec,
paragraph: {
content: 'inline*',
group: 'block',
Expand Down
2 changes: 1 addition & 1 deletion packages/site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@types/react-router-dom": "^5.3.3",
"@types/styled-components": "^5.1.34",
"@vitejs/plugin-react-refresh": "^1.3.6",
"vite": "^5.2.11",
"vite": "^5.4.2",
"vite-tsconfig-paths": "^4.3.2"
},
"dependencies": {
Expand Down
Loading
Loading