Handle pasted HTML from Office even if there's an image file on the clipboard (#14852)
GitOrigin-RevId: f0413391ec421c845b435e1392140d265a528ee7
This commit is contained in:
@@ -36,17 +36,18 @@ export const pasteHtml = [
|
||||
return false
|
||||
}
|
||||
|
||||
// allow pasting an image to create a figure, if the HTML doesn't contain a table
|
||||
// (because desktop Excel puts both an image and the HTML table on the clipboard)
|
||||
if (clipboardData.files.length > 0 && !html.includes('<table')) {
|
||||
return false
|
||||
}
|
||||
|
||||
// convert the HTML to LaTeX
|
||||
try {
|
||||
const parser = new DOMParser()
|
||||
const { documentElement } = parser.parseFromString(html, 'text/html')
|
||||
|
||||
// fall back to creating a figure when there's an image on the clipoard,
|
||||
// unless the HTML indicates that it came from an Office application
|
||||
// (which also puts an image on the clipboard)
|
||||
if (clipboardData.files.length > 0 && !hasProgId(documentElement)) {
|
||||
return false
|
||||
}
|
||||
|
||||
// if the only content is in a code block, use the plain text version
|
||||
if (onlyCode(documentElement)) {
|
||||
return false
|
||||
@@ -55,7 +56,7 @@ export const pasteHtml = [
|
||||
const latex = htmlToLaTeX(documentElement)
|
||||
|
||||
// if there's no formatting, use the plain text version
|
||||
if (latex === text) {
|
||||
if (latex === text && clipboardData.files.length === 0) {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -112,6 +113,13 @@ const onlyCode = (documentElement: HTMLElement) => {
|
||||
)
|
||||
}
|
||||
|
||||
const hasProgId = (documentElement: HTMLElement) => {
|
||||
const meta = documentElement.querySelector<HTMLMetaElement>(
|
||||
'meta[name="ProgId"]'
|
||||
)
|
||||
return meta && meta.content.trim().length > 0
|
||||
}
|
||||
|
||||
const htmlToLaTeX = (documentElement: HTMLElement) => {
|
||||
// remove style elements
|
||||
removeUnwantedElements(documentElement, 'style')
|
||||
|
||||
Reference in New Issue
Block a user