Skip to content

Commit

Permalink
Fix ##50744 - Add rules for reformatting html before comparing
Browse files Browse the repository at this point in the history
Fix #50744
The specification doesn't have the definition of a canonical form. When writing html through `.write()` we should be able to carry the information which is visible to users. 

This should level the playing field in between browsers with their different styles of building HTML during read/write operation of the clipboard API.
  • Loading branch information
karlcow authored Feb 17, 2025
1 parent 84b5f2b commit 1023ea6
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions clipboard-apis/async-write-html-read-html.https.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,23 @@
// but when we are comparing for equality the spaces make a difference.
function reformatHtml(html) {
const parser = new DOMParser();
const htmlString =
parser.parseFromString(html, 'text/html').documentElement.innerHTML;
const reformattedString = htmlString.replace(/\>\s*\</g, '> <');
const parsedHtml = parser.parseFromString(html, 'text/html')

// More canonical form
// Remove the head - not visible to users
const headElement = parsedHtml.head;
if (headElement) { headElement.remove(); }
// Remove the eventual style attributes
const elementsWithStyleAttribute = parsedHtml.querySelectorAll('[style]');
elementsWithStyleAttribute.forEach((element) => { element.removeAttribute('style'); });
const htmlString = parsedHtml.documentElement.innerHTML;
// Remove multiple spaces and returns and replacing by one
// trim leading and tailing white spaces
const reformattedString = htmlString.replace(/\>[\s\n]*\</g, '> <').trim();
return reformattedString;
}


async function readWriteTest(textInput) {
await tryGrantReadPermission();
await tryGrantWritePermission();
Expand Down

0 comments on commit 1023ea6

Please sign in to comment.