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

HTML Purifier EnableID doesn't work #304

Closed
handplant opened this issue Oct 11, 2024 · 6 comments
Closed

HTML Purifier EnableID doesn't work #304

handplant opened this issue Oct 11, 2024 · 6 comments
Labels

Comments

@handplant
Copy link

Hello, I would like to allow IDs in Richtext. However, they are being stripped when saving.

Here is my config:

{
  "Attr.AllowedFrameTargets": [
    "_blank"
  ],
  "Attr.EnableID": true,
  "HTML.AllowedComments": [
    "pagebreak"
  ],
  "HTML.SafeIframe": true,
  "URI.SafeIframeRegexp": "%^(https?:)?//(www.youtube.com/|player.vimeo.com/)%"
}

Here my field:
Image

Thanks in advance!

Additional info

  • Craft version: Craft Pro 5.4.7.1
  • PHP version: 8.2.21
  • Plugins & versions: CKEditor | 4.2.0
@handplant handplant added the bug label Oct 11, 2024
@i-just
Copy link
Contributor

i-just commented Oct 14, 2024

Hi, thanks for reaching out!

Custom attributes and classes are stripped out (by default) by CKEditor itself. In addition to configuring your HTML Purifier, you also have to configure the CKEditor to allow IDs.

For example, this snippet allows for IDs for paragraph tags only:

return {
  htmlSupport: {
    allow: [
      {
        attributes: [
          'id',
        ],
        name: 'p',
      },
    ],
  },
}

You can find more info on CKEditor and HTML Purifier configuration here.

I hope this helps!

I’ll close this now, but feel free to reach out again if you run into any further problems.

@i-just i-just closed this as completed Oct 14, 2024
@handplant
Copy link
Author

handplant commented Oct 17, 2024

@i-just I’ve disabled the HTML Purifier for a specific field and tried the following CKEditor configurations:

return {
	htmlSupport: {
		allow: [
			{
				name: /.*/,
				attributes: true,
				classes: true,
				styles: true
			},
			{
				attributes: ['id'],
				name: 'a',
			},
		]
	},
}

Unfortunately, when I add HTML through the “Source” mode, the id attributes on links are still being stripped out. I need to preserve these id attributes on the anchor () tags like this:

<sup>
   <a id="fnref_1" href="#footnote_1">1</a>
</sup>
<div>
   <sup id="footnote_1">1</sup>
   <a href="#fnref_1">back</a>
</div>

Thx

@handplant
Copy link
Author

It seems to be a bug related to the handling of id attributes on anchor () tags. When I paste the following code into the “Source” mode and save the content, the id attribute is preserved in the frontend:

<a id="fnref_1" href="#footnote_1">1</a>

However, when I edit or update the content in the field (manually, not in “Source” mode) and save again, the id attribute on the anchor tag is stripped out.

<a href="#footnote_1">1</a>

@i-just
Copy link
Contributor

i-just commented Oct 17, 2024

I tested your CKEditor configuration (with HTML Purifier disabled), and it does preserve the ID attribute on the A tag (and on the SUP tag too). Can you double-check that the field you’re testing this on uses the CKEditor configuration you adjusted?

Screen.Recording.2024-10-17.at.14.52.51.mov

That said, you now have two rules in there: one that allows any styles, attributes and classes on all elements and the other that only allows an ID attribute on A tags, so one of them seems redundant, depending on the full solution you want to achieve.

Let me know if I missed your point!

@handplant
Copy link
Author

Thank you for your response. It’s very strange. Here’s my video showing the same process.

Bildschirmaufnahme.2024-10-17.um.16.17.47.mov

@handplant
Copy link
Author

@i-just Okay, I think I found the problem. It’s caused by the Anchor plugin! When I remove it, everything works as expected.

Image

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

No branches or pull requests

2 participants