wysihtml5
wysihtml5 copied to clipboard
How to prevent the reset of a link's title and target attributes?
I have to add internal and external links to the content. I made a custom dialog where you can choose the type of the link and title.
Internal link generates markup like:
<a href="http://sample.url" title="Custom title">sample</a>
and external:
<a href="http://sample.url" class="external" title="Custom title" target="_blank">sample</a>
The first save is successful, but when I open the editor again and save it second time, editor resets link's attributes with default values:
<a href="http://sample.url" class="external" title="Link: http://sample.url">sample</a>
Same happens when I copy paste HTML with e.g. following markup to the editor:
<a href="http://sample.url" class="external" title="Custom title" target="_blank">sample</a>
Editor will reset the title and the target from the link within the paste.
Could it be achieved with dynamic parser rules like followed? Or can I use directly setAttributes? If so, how would that work?
set_attributes: { "target": <use _blank if external link otherwise empty>, "title": <use current title> }
One option would be to use regexp in editor.getValue() to add attributes before save, but that does not seem very wise thing.
+1
A quick hack was to add the following to check_attributes
:
var attributeCheckMethods = {
...,
preserve: (function() {
return function(attributeValue) {
return attributeValue;
}
})(),
};
What this allowed me to do was:
parserRules: {
tags: {
check_attributes: {
href: "url", // important to avoid XSS
target: "preserve",
title: "preserve",
}
}
}
A better solution might be the addition of a preserve_attributes
field, although that might be overkill.
The PR is probably wishful thinking, since this project seems extremely unmaintained, but here's hoping!
Edit: Actually, disregard my comment about being unmaintained, I just saw 40 open pull requests-- I didn't look at when the last one was accepted.