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

WIP: Prevent nested links #99

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,19 @@ var content = (function() {
}
},

getParentsAndSelf: function(node, tagName) {
var parents = [];
while (node) {
if (node.nodeType === nodeType.elementNode) {
if (!tagName || node.nodeName === tagName.toUpperCase()) {
parents.push(node);
}
}
node = node.parentNode;
}
return parents;
},
Comment on lines +189 to +200
Copy link
Collaborator

Choose a reason for hiding this comment

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

@peyerluk, would it be possible to merge only this function if you don’t want to merge the patch below? I ask because it comes in handy in other contexts as well, e.g. avoiding <em> inside of a <strong> or others.

To address @marcbachmann’s concern perhaps a flag that stops traversal at a given node would be an agreeable compromise?

Lastly, I’d probably rename the function to getAncestorsAndSelf() to be more in line with the XPath axis ancestor-or-self.

Copy link
Member Author

Choose a reason for hiding this comment

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

This function is a bit of an odd duck currently as this is the only method that is meant to collect info outside of the host element.

It would need some polishing before we can merge it.

Would this method here help your use case? Or are you more interested in knowing which tags affect a cursor or selection within the host? For that we have the method getTags() in this file.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Would this method here help your use case?

@peyerluk, I think I used getTagNames() from PR #198 in its stead.


/**
* Get all tags that start or end inside the range
*/
Expand Down
10 changes: 10 additions & 0 deletions src/selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ var Selection = (function() {
return coords;
},

/**
* Check if a link can be added.
* If the editableHost or a parent is a link element linking
* should not be possible.
*/
canLinkBeSet: function() {
var linkParents = content.getParentsAndSelf(this.host, 'a');
Copy link
Member

Choose a reason for hiding this comment

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

@peyerluk This iterates throught all parents of the dom up to the html tag.
I guess we shouldn't go further than the element in which editable.js is initialized.

Copy link
Member Author

Choose a reason for hiding this comment

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

@marcbachmann This is on purpose since nesting links should not be allowed in general. Especially in case of teasers the link tag can be outside of the editable.

Copy link
Member

Choose a reason for hiding this comment

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

Ok. It's still forbidden in HTML5 :)
It's not allowed to place interactive elements inside an <a> element:
http://www.w3.org/html/wg/drafts/html/master/single-page.html#the-a-element
http://www.w3.org/html/wg/drafts/html/master/single-page.html#interactive-content-2

return linkParents.length ? false : true;
},

/**
*
* @method link
Expand Down