-
Notifications
You must be signed in to change notification settings - Fork 11
Normalize HTML when comparing featured item content (unblocks test-functional on all PRs) #485
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
Merged
ChengShi-1
merged 2 commits into
develop
from
fix/normalize-featured-item-html-comparison
Sep 11, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,117 @@ | ||
| import { JSDOM } from 'jsdom' | ||
|
|
||
| const WHITESPACE_SENSITIVE_TAGS = new Set(['pre', 'textarea']) | ||
|
|
||
| const BLOCK_TAGS = new Set([ | ||
| 'address', | ||
| 'article', | ||
| 'aside', | ||
| 'blockquote', | ||
| 'body', | ||
| 'br', | ||
| 'div', | ||
| 'dd', | ||
| 'dl', | ||
| 'dt', | ||
| 'fieldset', | ||
| 'figcaption', | ||
| 'figure', | ||
| 'footer', | ||
| 'form', | ||
| 'h1', | ||
| 'h2', | ||
| 'h3', | ||
| 'h4', | ||
| 'h5', | ||
| 'h6', | ||
| 'head', | ||
| 'header', | ||
| 'hr', | ||
| 'html', | ||
| 'li', | ||
| 'main', | ||
| 'nav', | ||
| 'ol', | ||
| 'p', | ||
| 'pre', | ||
| 'section', | ||
| 'table', | ||
| 'tbody', | ||
| 'td', | ||
| 'tfoot', | ||
| 'th', | ||
| 'thead', | ||
| 'tr', | ||
| 'ul' | ||
| ]) | ||
|
|
||
| const document = new JSDOM('').window.document | ||
|
|
||
| const isElement = (node: Node | null | undefined): node is Element => | ||
| node !== null && node !== undefined && node.nodeType === node.ELEMENT_NODE | ||
|
|
||
| const isText = (node: Node): node is Text => node.nodeType === node.TEXT_NODE | ||
|
|
||
| const tagNameOf = (element: Element): string => element.tagName.toLowerCase() | ||
|
|
||
| const isBlockBoundary = (sibling: Node | undefined, parent: Node): boolean => | ||
| sibling === undefined | ||
| ? !isElement(parent) || BLOCK_TAGS.has(tagNameOf(parent)) | ||
| : isElement(sibling) && BLOCK_TAGS.has(tagNameOf(sibling)) | ||
|
|
||
| const sortAttributes = (element: Element): void => { | ||
| const attributes = Array.from(element.attributes).sort((one, other) => | ||
| one.name.localeCompare(other.name) | ||
| ) | ||
| attributes.forEach((attribute) => element.removeAttribute(attribute.name)) | ||
| attributes.forEach((attribute) => element.setAttribute(attribute.name, attribute.value)) | ||
| } | ||
|
|
||
| const normalizeText = (text: Text, afterBoundary: boolean, beforeBoundary: boolean): void => { | ||
| if (text.data.trim() === '') { | ||
| if (afterBoundary || beforeBoundary) { | ||
| text.remove() | ||
| } else { | ||
| text.data = ' ' | ||
| } | ||
| return | ||
| } | ||
|
|
||
| let collapsed = text.data.replace(/\s+/g, ' ') | ||
| if (afterBoundary) { | ||
| collapsed = collapsed.replace(/^ /, '') | ||
| } | ||
| if (beforeBoundary) { | ||
| collapsed = collapsed.replace(/ $/, '') | ||
| } | ||
| text.data = collapsed | ||
| } | ||
|
|
||
| const normalizeChildren = (parent: Node, whitespaceSensitive: boolean): void => { | ||
| const childrenAsParsed = Array.from(parent.childNodes) | ||
|
|
||
| childrenAsParsed.forEach((child, index) => { | ||
| if (isElement(child)) { | ||
| sortAttributes(child) | ||
| normalizeChildren( | ||
| child, | ||
| whitespaceSensitive || WHITESPACE_SENSITIVE_TAGS.has(tagNameOf(child)) | ||
| ) | ||
| return | ||
| } | ||
| if (isText(child) && !whitespaceSensitive) { | ||
| normalizeText( | ||
| child, | ||
| isBlockBoundary(childrenAsParsed[index - 1], parent), | ||
| isBlockBoundary(childrenAsParsed[index + 1], parent) | ||
| ) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| export const normalizeHtml = (html: string): string => { | ||
| const template = document.createElement('template') | ||
| template.innerHTML = html | ||
| normalizeChildren(template.content, false) | ||
| return template.innerHTML | ||
| } |
This file contains hidden or 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,109 @@ | ||
| import { normalizeHtml } from '../../testHelpers/html/htmlNormalizer' | ||
| import { | ||
| CONTENT_FIELD_WITH_ALL_TAGS, | ||
| SERVER_FORMATTED_CONTENT_FIELD_WITH_ALL_TAGS | ||
| } from '../../testHelpers/collections/collectionHelper' | ||
|
|
||
| describe('normalizeHtml', () => { | ||
| describe('differences the server may introduce', () => { | ||
| test('should ignore the order of attributes', () => { | ||
| expect( | ||
| normalizeHtml('<a target="_blank" rel="nofollow" class="x" href="https://a.b">t</a>') | ||
| ).toEqual( | ||
| normalizeHtml('<a class="x" href="https://a.b" rel="nofollow" target="_blank">t</a>') | ||
| ) | ||
| }) | ||
|
|
||
| test('should ignore indentation introduced between block elements', () => { | ||
| expect(normalizeHtml('<ul><li><p>Item</p></li></ul>')).toEqual( | ||
| normalizeHtml('<ul>\n <li>\n <p>Item</p>\n </li>\n</ul>') | ||
| ) | ||
| }) | ||
|
|
||
| test('should ignore indentation around the content of a block element', () => { | ||
| expect(normalizeHtml('<p>Item</p>')).toEqual(normalizeHtml('<p>\n Item\n</p>')) | ||
| }) | ||
|
|
||
| test('should ignore the case of tag and attribute names', () => { | ||
| expect(normalizeHtml('<P CLASS="x">t</P>')).toEqual(normalizeHtml('<p class="x">t</p>')) | ||
| }) | ||
|
|
||
| test('should ignore the order of attributes whose values contain angle brackets', () => { | ||
| expect(normalizeHtml('<a title="A > B" href="/example">link</a>')).toEqual( | ||
| normalizeHtml('<a href="/example" title="A > B">link</a>') | ||
| ) | ||
| }) | ||
|
|
||
| test('should ignore the order of attributes quoted with single quotes', () => { | ||
| expect(normalizeHtml("<a title='A > B' href='/example'>link</a>")).toEqual( | ||
| normalizeHtml('<a href="/example" title="A > B">link</a>') | ||
| ) | ||
| }) | ||
|
|
||
| test('should ignore equivalent spellings of a void element', () => { | ||
| expect(normalizeHtml('<p>a<br>b</p>')).toEqual(normalizeHtml('<p>a<br/>b</p>')) | ||
| }) | ||
|
|
||
| test('should ignore equivalent spellings of an escaped character', () => { | ||
| expect(normalizeHtml('<p>a & b</p>')).toEqual(normalizeHtml('<p>a & b</p>')) | ||
| }) | ||
|
|
||
| test('should treat the sent and server-returned forms of the featured item fixture as equal', () => { | ||
| expect(normalizeHtml(CONTENT_FIELD_WITH_ALL_TAGS)).toEqual( | ||
| normalizeHtml(SERVER_FORMATTED_CONTENT_FIELD_WITH_ALL_TAGS) | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| describe('differences that must still be detected', () => { | ||
| test('should not ignore differing text content', () => { | ||
| expect(normalizeHtml('<p>Item</p>')).not.toEqual(normalizeHtml('<p>Other</p>')) | ||
| }) | ||
|
|
||
| test('should not ignore differing attribute values', () => { | ||
| expect(normalizeHtml('<a href="https://a.b">t</a>')).not.toEqual( | ||
| normalizeHtml('<a href="https://evil.example">t</a>') | ||
| ) | ||
| }) | ||
|
|
||
| test('should not ignore a dropped attribute', () => { | ||
| expect(normalizeHtml('<a rel="nofollow" href="https://a.b">t</a>')).not.toEqual( | ||
| normalizeHtml('<a href="https://a.b">t</a>') | ||
| ) | ||
| }) | ||
|
|
||
| test('should not ignore a differing attribute value that contains angle brackets', () => { | ||
| expect(normalizeHtml('<a title="A > B" href="/example">link</a>')).not.toEqual( | ||
| normalizeHtml('<a title="A > C" href="/example">link</a>') | ||
| ) | ||
| }) | ||
|
|
||
| test('should not ignore differing structure', () => { | ||
| expect(normalizeHtml('<ul><li>a</li><li>b</li></ul>')).not.toEqual( | ||
| normalizeHtml('<ul><li>a</li></ul>') | ||
| ) | ||
| }) | ||
|
|
||
| test('should not ignore a changed tag', () => { | ||
| expect(normalizeHtml('<strong>t</strong>')).not.toEqual(normalizeHtml('<em>t</em>')) | ||
| }) | ||
|
|
||
| test('should preserve whitespace inside a preformatted block', () => { | ||
| expect(normalizeHtml('<pre><code> indented\n lines</code></pre>')).not.toEqual( | ||
| normalizeHtml('<pre><code>indented lines</code></pre>') | ||
| ) | ||
| }) | ||
|
|
||
| test('should preserve the whitespace that separates inline elements in the featured item fixture', () => { | ||
| expect(normalizeHtml(CONTENT_FIELD_WITH_ALL_TAGS)).not.toEqual( | ||
| normalizeHtml(CONTENT_FIELD_WITH_ALL_TAGS.replace('</strong> <em', '</strong><em')) | ||
| ) | ||
| }) | ||
|
|
||
| test('should preserve whitespace that separates inline elements', () => { | ||
| expect(normalizeHtml('<p><em>a</em> <em>b</em></p>')).not.toEqual( | ||
| normalizeHtml('<p><em>a</em><em>b</em></p>') | ||
| ) | ||
| }) | ||
| }) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
htmlNormalizertokenizes tags with/<[^>]*>/, which breaks valid quoted attributes containing>, such astitle="A > B". Those fragments are no longer parsed or attribute-sorted, so equivalent server-formatted HTML can still fail this functional test. Use an HTML parser/DOM serialization approach, likeconst document = new DOMParser().parseFromString(html, 'text/html'), or make tokenization quote-aware and add a regression test.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I went with the parser, as you suggested. The DOMParser isn't a global in the node jest environment the functional tests run in, so the helper imports jsdom directly (already in the tree via jest-environment-jsdom, now declared). Your regression test is in, plus a negative case. Fixed in 3fdadcc.