Skip to content

Commit

Permalink
Add support for default <a> attributes
Browse files Browse the repository at this point in the history
Resolves #186.
  • Loading branch information
neilj committed May 5, 2016
1 parent f50962d commit 6c4f8e1
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 33 deletions.
3 changes: 2 additions & 1 deletion Demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ <h1>HTML Editor Test</h1>
tagAttributes: {
ul: {'class': 'UL'},
ol: {'class': 'OL'},
li: {'class': 'listItem'}
li: {'class': 'listItem'},
a: {'target': '_blank'}
}
});

Expand Down
34 changes: 19 additions & 15 deletions build/squire-raw.js
Original file line number Diff line number Diff line change
Expand Up @@ -1330,7 +1330,7 @@ var keyHandlers = {
// Remove any zws so we don't think there's content in an empty
// block.
self._recordUndoState( range );
addLinks( range.startContainer );
addLinks( range.startContainer, root, self );
self._removeZWS();
self._getRangeAndRemoveBookmark( range );

Expand Down Expand Up @@ -1596,7 +1596,7 @@ var keyHandlers = {
space: function ( self, _, range ) {
var node, parent;
self._recordUndoState( range );
addLinks( range.startContainer );
addLinks( range.startContainer, self._root, self );
self._getRangeAndRemoveBookmark( range );

// If the cursor is at the end of a link (<a>foo|</a>) then move it
Expand Down Expand Up @@ -2374,7 +2374,8 @@ proto.setConfig = function ( config ) {
blockquote: null,
ul: null,
ol: null,
li: null
li: null,
a: null
}
}, config );

Expand Down Expand Up @@ -3760,7 +3761,7 @@ proto.insertElement = function ( el, range ) {
if ( !canObserveMutations ) {
this._docWasChanged();
}

return this;
};

Expand All @@ -3774,12 +3775,13 @@ proto.insertImage = function ( src, attributes ) {

var linkRegExp = /\b((?:(?:ht|f)tps?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,}\/)(?:[^\s()<>]+|\([^\s()<>]+\))+(?:\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))|([\w\-.%+]+@(?:[\w\-]+\.)+[A-Z]{2,}\b)/i;

var addLinks = function ( frag, root ) {
var addLinks = function ( frag, root, self ) {
var doc = frag.ownerDocument,
walker = new TreeWalker( frag, SHOW_TEXT,
function ( node ) {
return !getNearest( node, root, 'A' );
}, false ),
defaultAttributes = self._config.tagAttributes.a,
node, data, parent, match, index, endIndex, child;
while ( node = walker.nextNode() ) {
data = node.data;
Expand All @@ -3791,13 +3793,14 @@ var addLinks = function ( frag, root ) {
child = doc.createTextNode( data.slice( 0, index ) );
parent.insertBefore( child, node );
}
child = doc.createElement( 'A' );
child = self.createElement( 'A', mergeObjects({
href: match[1] ?
/^(?:ht|f)tps?:/.test( match[1] ) ?
match[1] :
'http://' + match[1] :
'mailto:' + match[2]
}, defaultAttributes ));
child.textContent = data.slice( index, endIndex );
child.href = match[1] ?
/^(?:ht|f)tps?:/.test( match[1] ) ?
match[1] :
'http://' + match[1] :
'mailto:' + match[2];
parent.insertBefore( child, node );
node.data = data = data.slice( endIndex );
}
Expand Down Expand Up @@ -3830,14 +3833,14 @@ proto.insertHTML = function ( html, isPaste ) {
defaultPrevented: false
};

addLinks( frag, root );
addLinks( frag, frag, this );
cleanTree( frag );
cleanupBRs( frag, root );
cleanupBRs( frag, null );

This comment has been minimized.

Copy link
@akauffmanGG

akauffmanGG May 6, 2016

Contributor

Can you explain the reason for passing null here? This is causing some things to break for me, and knowing why this is null will help me fix it.

This comment has been minimized.

Copy link
@neilj

neilj May 7, 2016

Author Member

Huh, that's interesting. This arg is only used by the fixCursor function (passed down cleanupBRs → fixContainer → fixCursor). And there the only thing it is used for is to compare to the node being passed in. But the root node can never be passed in here, because the fragment only contains nodes that are disconnected from the document (where the root is).

So since it can't (in theory!) make any difference, I decided to change it to null here to make that a little more explicit. So… that opens up the question: what is broken for you and how on earth did this change break it?! Do you have a reproduction path?

This comment has been minimized.

Copy link
@akauffmanGG

akauffmanGG May 9, 2016

Contributor

It's breaking in a customization I made to cleanupBRs that relies on the root parameter. You're reason for passing null makes sense and that helps me figure out what to do about it. Thanks

removeEmptyInlines( frag );
frag.normalize();

while ( node = getNextBlock( node, root ) ) {
fixCursor( node, root );
while ( node = getNextBlock( node, frag ) ) {
fixCursor( node, null );
}

if ( isPaste ) {
Expand Down Expand Up @@ -3930,6 +3933,7 @@ proto.makeLink = function ( url, attributes ) {
if ( !attributes ) {
attributes = {};
}
mergeObjects( attributes, this._config.tagAttributes.a );
attributes.href = url;

this.changeFormat({
Expand Down
4 changes: 2 additions & 2 deletions build/squire.js

Large diffs are not rendered by default.

30 changes: 17 additions & 13 deletions source/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ proto.setConfig = function ( config ) {
blockquote: null,
ul: null,
ol: null,
li: null
li: null,
a: null
}
}, config );

Expand Down Expand Up @@ -1546,7 +1547,7 @@ proto.insertElement = function ( el, range ) {
if ( !canObserveMutations ) {
this._docWasChanged();
}

return this;
};

Expand All @@ -1560,12 +1561,13 @@ proto.insertImage = function ( src, attributes ) {

var linkRegExp = /\b((?:(?:ht|f)tps?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,}\/)(?:[^\s()<>]+|\([^\s()<>]+\))+(?:\((?:[^\s()<>]+|(?:\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))|([\w\-.%+]+@(?:[\w\-]+\.)+[A-Z]{2,}\b)/i;

var addLinks = function ( frag, root ) {
var addLinks = function ( frag, root, self ) {
var doc = frag.ownerDocument,
walker = new TreeWalker( frag, SHOW_TEXT,
function ( node ) {
return !getNearest( node, root, 'A' );
}, false ),
defaultAttributes = self._config.tagAttributes.a,
node, data, parent, match, index, endIndex, child;
while ( node = walker.nextNode() ) {
data = node.data;
Expand All @@ -1577,13 +1579,14 @@ var addLinks = function ( frag, root ) {
child = doc.createTextNode( data.slice( 0, index ) );
parent.insertBefore( child, node );
}
child = doc.createElement( 'A' );
child = self.createElement( 'A', mergeObjects({
href: match[1] ?
/^(?:ht|f)tps?:/.test( match[1] ) ?
match[1] :
'http://' + match[1] :
'mailto:' + match[2]
}, defaultAttributes ));
child.textContent = data.slice( index, endIndex );
child.href = match[1] ?
/^(?:ht|f)tps?:/.test( match[1] ) ?
match[1] :
'http://' + match[1] :
'mailto:' + match[2];
parent.insertBefore( child, node );
node.data = data = data.slice( endIndex );
}
Expand Down Expand Up @@ -1616,14 +1619,14 @@ proto.insertHTML = function ( html, isPaste ) {
defaultPrevented: false
};

addLinks( frag, root );
addLinks( frag, frag, this );
cleanTree( frag );
cleanupBRs( frag, root );
cleanupBRs( frag, null );
removeEmptyInlines( frag );
frag.normalize();

while ( node = getNextBlock( node, root ) ) {
fixCursor( node, root );
while ( node = getNextBlock( node, frag ) ) {
fixCursor( node, null );
}

if ( isPaste ) {
Expand Down Expand Up @@ -1716,6 +1719,7 @@ proto.makeLink = function ( url, attributes ) {
if ( !attributes ) {
attributes = {};
}
mergeObjects( attributes, this._config.tagAttributes.a );
attributes.href = url;

this.changeFormat({
Expand Down
4 changes: 2 additions & 2 deletions source/KeyHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ var keyHandlers = {
// Remove any zws so we don't think there's content in an empty
// block.
self._recordUndoState( range );
addLinks( range.startContainer );
addLinks( range.startContainer, root, self );
self._removeZWS();
self._getRangeAndRemoveBookmark( range );

Expand Down Expand Up @@ -420,7 +420,7 @@ var keyHandlers = {
space: function ( self, _, range ) {
var node, parent;
self._recordUndoState( range );
addLinks( range.startContainer );
addLinks( range.startContainer, self._root, self );
self._getRangeAndRemoveBookmark( range );

// If the cursor is at the end of a link (<a>foo|</a>) then move it
Expand Down

0 comments on commit 6c4f8e1

Please sign in to comment.