2

I am using redactor for creating a rich text field.

The thing is that I'm using the exact same field on two separate sections.

On one it is working fine, but on the other one, it is creating <p> tags outside the parent's div.

this is the one without p tags

developer tool

On the upper screenshots redactor is working how I want it to work.

On the followings screenshots is where it's creating extra <p> tags

and on the following section it is creating <p> tags outside the parent it should. enter image description here

enter image description here

Here you can see, that on this section it is creating the tags outside it's parent, I tried a lot of things like changing settings on my default.json from redactor's plugin, and these are my advanced settings on the redactor field settings:

enter image description here

I don't know why this is occurring as it is really strange that the same redactor field is acting differently on both sides. It's really annoying me... I will be very grateful for any help.

1 Answer 1

4

It's not creating tags outside the parent, that's simply how the browser interprets incorrect HTML. The inspector always display the DOM tree created by the browser, not the HTML source. Looks like your redactor field includes a <p> tag in its value, but you're also wrapping that in a <p> tag in your template. This results in output like this (which you can check using right click -> View Page Source in your browser):

<p class="section__description padding-left">
    <p>
        Lorem Ipsum …
    </p>
</p>

But that isn't valid HTML, because a <p> tag can't contain other <p> tags. Browsers try to fix broken HTML, so the first paragraph is automatically closed before the second one opens. This is why you see multiple paragraphs in the inspector.

To fix this, you have to options:

  1. If you want the editors to be able to enter multiple paragraphs in the redactor field, use a <div> (or another element that can contain <p> tags) as the wrapper instead of a <p>.
  2. If you really only want to allow a single paragraph in the redactor field, you can switch to inline mode 1. This way, the redactor output won't be wrapped in <p> tags so you can safely use the <p> tag in your template. Sidenote, this setting is quite unreliable, you may want to use striptags with a whitelist of allowed inline tags to ensure the output doesn't include superfluous <p> tags.

1 Inline mode is broken, see this issue for details. Instead, set enterKey: false to prevent editors from adding new paragraphs or breakline: true to make the enter key add <br> tags instead of starting a new paragraph. Remove all block formatting options with the buttons setting. Then you can use the striptags filter in the output template to only allow inline tags and remove the <p> tag if necessary. If this sounds tedious, see my answer here regarding alternative WYSIWYG editors.

1
  • 1
    Thank you very much! Lesson learnt. Commented Feb 1, 2023 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.