Hi!
I've got the following function as an EventListener:
var methods = {
addLink: function () {
// create 'search pattern'-object
var classapplier = rangy.createCssClassApplier('link', {elementTagName: 'a', normalize: true});
if(classapplier.isAppliedToSelection()) {
// remove a-tag if it's already set classapplier.toggleSelection();
} else {
// add a-tag if(link) {
classapplier = rangy.createCssClassApplier('link', {elementTagName: 'a', elementProperties: {href: link}, normalize: true});
classapplier.toggleSelection();
}
}
// avoid losing focus
return false;
}
}
$('.toolbar').mousedown(methods.addLink);
For example, if i have the following Text:
Lorem ipsum dolor sit amet, {selectionStart}consetetur sadipscing elitr{selectionEnd}, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
The first click on '.toolbar' results in:
Lorem ipsum dolor sit amet, {selectionStart}<a href="http://www.example.com/" class="link">consetetur sadipscing elitr</a>{selectionEnd}, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
Perfect!
But when i click '.toolbar' for the second time, it results in:
Lorem ipsum dolor sit amet, {selectionStart}<a href="http://www.example.com/" class>consetetur sadipscing elitr</a>{selectionEnd}, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
You see what happened? Only the class 'link' got removed, but the a-tag with its href is still available.
classapplier.isAppliedToSelection() returned true, like expected.
Lorem ipsum dolor sit amet, {selectionStart}<a href="http://www.example.com/" class="link">consetetur sadipscing elitr</a>{selectionEnd}, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
You can see, the class "link" got added again, but the href was not changed to
another-example.com.
I'm pretty sure the reason for this issue, is the missing or non-equal href parameter. For tests, i changed the function mentioned above, to the following:
var methods = {
addLink: function () {
var classapplier = rangy.createCssClassApplier('link', {elementTagName: 'a', elementProperties: {href: '#'}, normalize: true});
if(classapplier.isAppliedToSelection()) {
// remove a-tag if it's already set classapplier.toggleSelection();
} else {
// add a-tag var link = '#';
if(link) {
classapplier = rangy.createCssClassApplier('link', {elementTagName: 'a', elementProperties: {href: link}, normalize: true});
classapplier.toggleSelection();
}
}
// avoid losing focus
return false;
}
}
Now, the href parameter always has the same contents ('#') and everything's working perfectly.
But in my case, i cannot define a fixed cssClassApplier - because of the dynamic href values.
I wasn't sure if i should submit this as a missing feature to google code - maybe it's my fault ;)
I'm using version 1.2.3 but this also occurs in 1.3alpha.
Tested in Safari and Firefox.
I hope you can understand everything i tried to explain, my english is not the best :P
Thanks in advance!