-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1463 from microsoft/u/juliaroldi/sanitizeLinks
Clear local file paths and remove link
- Loading branch information
Showing
3 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
packages/roosterjs-editor-plugins/lib/plugins/Paste/sanitizeLinks/sanitizeLinks.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { chainSanitizerCallback } from 'roosterjs-editor-dom'; | ||
import { HtmlSanitizerOptions } from 'roosterjs-editor-types'; | ||
|
||
const HTTP = 'http:'; | ||
const HTTPS = 'https:'; | ||
|
||
/** | ||
* @internal | ||
* Clear local paths and remove link | ||
* @param sanitizingOption the sanitizingOption of BeforePasteEvent | ||
* */ | ||
export default function sanitizeLinks(sanitizingOption: Required<HtmlSanitizerOptions>) { | ||
chainSanitizerCallback( | ||
sanitizingOption.attributeCallbacks, | ||
'href', | ||
(value: string, element: HTMLElement) => validateLink(value, element) | ||
); | ||
} | ||
|
||
function validateLink(link: string, htmlElement: HTMLElement) { | ||
let url; | ||
try { | ||
url = new URL(link); | ||
} catch { | ||
url = undefined; | ||
} | ||
|
||
if (url && (url.protocol === HTTP || url.protocol === HTTPS)) { | ||
return link; | ||
} | ||
htmlElement.removeAttribute('href'); | ||
return ''; | ||
} |
80 changes: 80 additions & 0 deletions
80
packages/roosterjs-editor-plugins/test/paste/sanitizeLinksTest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import sanitizeLinks from '../../lib/plugins/Paste/sanitizeLinks/sanitizeLinks'; | ||
import { HtmlSanitizer } from 'roosterjs-editor-dom'; | ||
import { | ||
BeforePasteEvent, | ||
SanitizeHtmlOptions, | ||
PluginEventType, | ||
ClipboardData, | ||
} from 'roosterjs-editor-types'; | ||
|
||
describe('sanitizeLinks', () => { | ||
function callSanitizer(fragment: DocumentFragment, sanitizingOption: SanitizeHtmlOptions) { | ||
const sanitizer = new HtmlSanitizer(sanitizingOption); | ||
sanitizer.convertGlobalCssToInlineCss(fragment); | ||
sanitizer.sanitize(fragment); | ||
} | ||
|
||
function runTest(source: string, expected: string) { | ||
const doc = new DOMParser().parseFromString(source, 'text/html'); | ||
const fragment = doc.createDocumentFragment(); | ||
while (doc.body.firstChild) { | ||
fragment.appendChild(doc.body.firstChild); | ||
} | ||
|
||
const event = createBeforePasteEventMock(fragment); | ||
sanitizeLinks(event.sanitizingOption); | ||
callSanitizer(fragment, event.sanitizingOption); | ||
|
||
while (fragment.firstChild) { | ||
doc.body.appendChild(fragment.firstChild); | ||
} | ||
|
||
expect(doc.body.innerHTML).toBe(expected); | ||
} | ||
|
||
it('sanitize anchor', () => { | ||
runTest('<a href="/text.txt"></a>', '<a></a>'); | ||
}); | ||
|
||
it('not sanitize anchor', () => { | ||
runTest( | ||
'<a href="https://microsoft.github.io/"></a>', | ||
'<a href="https://microsoft.github.io/"></a>' | ||
); | ||
}); | ||
|
||
it('sanitize div', () => { | ||
runTest('<div><a href="/text.txt"></a></div>', '<div><a></a></div>'); | ||
}); | ||
|
||
it('not sanitize div', () => { | ||
runTest( | ||
'<div><a href="https://microsoft.github.io/"></a></div>', | ||
'<div><a href="https://microsoft.github.io/"></a></div>' | ||
); | ||
}); | ||
}); | ||
|
||
function createBeforePasteEventMock(fragment: DocumentFragment) { | ||
return ({ | ||
eventType: PluginEventType.BeforePaste, | ||
clipboardData: <ClipboardData>{}, | ||
fragment: fragment, | ||
sanitizingOption: { | ||
elementCallbacks: {}, | ||
attributeCallbacks: {}, | ||
cssStyleCallbacks: {}, | ||
additionalTagReplacements: {}, | ||
additionalAllowedAttributes: [], | ||
additionalAllowedCssClasses: [], | ||
additionalDefaultStyleValues: {}, | ||
additionalGlobalStyleNodes: [], | ||
additionalPredefinedCssForElement: {}, | ||
preserveHtmlComments: false, | ||
unknownTagReplacement: null, | ||
}, | ||
htmlBefore: '', | ||
htmlAfter: '', | ||
htmlAttributes: {}, | ||
} as unknown) as BeforePasteEvent; | ||
} |