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

feature: rewrite markup out of pipeline to ensure all block div's contain appropriate paragraph tags #45

Merged
merged 2 commits into from
Mar 12, 2024

Conversation

shsteimer
Copy link
Contributor

@shsteimer shsteimer commented Feb 21, 2024

Description

This change adds paragraph tags to block divs during decoration to avoid situations where the DOM lacks consistency depending on exactly how the block was authored.

Related Issue

Fix #44

Motivation and Context

Ideally this type of change could be made to HTML pipeline, but we can't do that for backwards compatibility reasons. By doing this here, we can slowly phase out projects that may be backwards incompatible with this type of change, and eventually (maybe?) remove it from here and add it to the pipeline.

This is likely not the only change that will eventually go into this category, which is why I did this in a new pipeline-utils file.

How Has This Been Tested?

Added test coverage. I have used similar code on projects as well

Screenshots (if appropriate):

n/a

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • I have signed the Adobe Open Source CLA.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

dist/aem.js Outdated Show resolved Hide resolved
dist/aem.js Outdated Show resolved Hide resolved
dist/aem.js Outdated Show resolved Hide resolved
dist/aem.js Outdated
// eslint-disable-next-line import/prefer-default-export
function addMissingParagraphs(block) {
block.querySelectorAll(':scope > div > div').forEach((blockColumn) => {
const hasContent = !!blockColumn.firstElementChild || !!block.textContent.trim();
Copy link
Member

@fkakatie fkakatie Feb 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const hasContent = !!blockColumn.firstElementChild || !!block.textContent.trim();
const hasContent = blockColumn.hasChildNodes() && (!blockColumn.firstElementChild || !blockColumn.querySelector('picture'));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the picture check into the hasWrapper check rather than hasContent but otherwise this is (I think) mostly what you suggested here.

<div class="code original">
<div>
<div><code>code blocks also get wrapped</code></div>
<div>text with some <code>inline code block also get wrapped</code></div>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one callout on code elements: In one revision of this, I was excluding code from being wrapped similar to picture but decided against it because of the possibility a text node with a <code> sibling as shown here.

If we wanted to get surgical, we could probably only wrap the code elements when they do have a text sibling, but that would add some complexity to the logic that I'm not sure is necessary.

Copy link

@buuhuu buuhuu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shsteimer please have a look at adobe-rnd/aem-boilerplate-xwalk#2

I used a slightly different approach that only works on the DOM without requiring computed styles.

dist/aem.js Outdated
if (blockColumn.hasChildNodes()) {
const hasWrapper = !!blockColumn.querySelector('picture')
|| /* exclude certain elements from being wrapped */ (!!blockColumn.firstElementChild
&& window.getComputedStyle(blockColumn.firstElementChild).display === 'block');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should not use window.getComputedStyle() as that would allow stylesheets to interfere with the logic, e.g. setting an <a> to display: block would cause it not to be wrapped in a <p>.

dist/aem.js Outdated
|| /* exclude certain elements from being wrapped */ (!!blockColumn.firstElementChild
&& window.getComputedStyle(blockColumn.firstElementChild).display === 'block');
if (!hasWrapper) {
const p = document.createElement('p');
Copy link

@buuhuu buuhuu Feb 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are case where we would have to wrap elements in a <pre> instead, e.g. if you have a single code child in the cell. This would at least be equivalent to the default content.

@fkakatie fkakatie mentioned this pull request Feb 29, 2024
10 tasks
…l block div's contain appropriate wrapper paragraph tags
Copy link
Member

@fkakatie fkakatie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@fkakatie fkakatie merged commit 43fb99c into adobe:main Mar 12, 2024
2 checks passed
@shsteimer shsteimer deleted the pipeline-utils branch March 12, 2024 21:54
@adobe-bot
Copy link

🎉 This PR is included in version 1.5.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add paragraph tags to block divs when they are missing
4 participants