0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2025-01-03 05:00:13 -05:00

Add support for default <a> attributes

Resolves #186.
This commit is contained in:
Neil Jenkins 2016-05-05 11:50:36 +10:00
parent f50962db7f
commit 6c4f8e1aaf
5 changed files with 42 additions and 33 deletions

View file

@ -110,7 +110,8 @@
tagAttributes: { tagAttributes: {
ul: {'class': 'UL'}, ul: {'class': 'UL'},
ol: {'class': 'OL'}, ol: {'class': 'OL'},
li: {'class': 'listItem'} li: {'class': 'listItem'},
a: {'target': '_blank'}
} }
}); });

View file

@ -1330,7 +1330,7 @@ var keyHandlers = {
// Remove any zws so we don't think there's content in an empty // Remove any zws so we don't think there's content in an empty
// block. // block.
self._recordUndoState( range ); self._recordUndoState( range );
addLinks( range.startContainer ); addLinks( range.startContainer, root, self );
self._removeZWS(); self._removeZWS();
self._getRangeAndRemoveBookmark( range ); self._getRangeAndRemoveBookmark( range );
@ -1596,7 +1596,7 @@ var keyHandlers = {
space: function ( self, _, range ) { space: function ( self, _, range ) {
var node, parent; var node, parent;
self._recordUndoState( range ); self._recordUndoState( range );
addLinks( range.startContainer ); addLinks( range.startContainer, self._root, self );
self._getRangeAndRemoveBookmark( range ); self._getRangeAndRemoveBookmark( range );
// If the cursor is at the end of a link (<a>foo|</a>) then move it // If the cursor is at the end of a link (<a>foo|</a>) then move it
@ -2374,7 +2374,8 @@ proto.setConfig = function ( config ) {
blockquote: null, blockquote: null,
ul: null, ul: null,
ol: null, ol: null,
li: null li: null,
a: null
} }
}, config ); }, config );
@ -3760,7 +3761,7 @@ proto.insertElement = function ( el, range ) {
if ( !canObserveMutations ) { if ( !canObserveMutations ) {
this._docWasChanged(); this._docWasChanged();
} }
return this; return this;
}; };
@ -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 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, var doc = frag.ownerDocument,
walker = new TreeWalker( frag, SHOW_TEXT, walker = new TreeWalker( frag, SHOW_TEXT,
function ( node ) { function ( node ) {
return !getNearest( node, root, 'A' ); return !getNearest( node, root, 'A' );
}, false ), }, false ),
defaultAttributes = self._config.tagAttributes.a,
node, data, parent, match, index, endIndex, child; node, data, parent, match, index, endIndex, child;
while ( node = walker.nextNode() ) { while ( node = walker.nextNode() ) {
data = node.data; data = node.data;
@ -3791,13 +3793,14 @@ var addLinks = function ( frag, root ) {
child = doc.createTextNode( data.slice( 0, index ) ); child = doc.createTextNode( data.slice( 0, index ) );
parent.insertBefore( child, node ); 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.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 ); parent.insertBefore( child, node );
node.data = data = data.slice( endIndex ); node.data = data = data.slice( endIndex );
} }
@ -3830,14 +3833,14 @@ proto.insertHTML = function ( html, isPaste ) {
defaultPrevented: false defaultPrevented: false
}; };
addLinks( frag, root ); addLinks( frag, frag, this );
cleanTree( frag ); cleanTree( frag );
cleanupBRs( frag, root ); cleanupBRs( frag, null );
removeEmptyInlines( frag ); removeEmptyInlines( frag );
frag.normalize(); frag.normalize();
while ( node = getNextBlock( node, root ) ) { while ( node = getNextBlock( node, frag ) ) {
fixCursor( node, root ); fixCursor( node, null );
} }
if ( isPaste ) { if ( isPaste ) {
@ -3930,6 +3933,7 @@ proto.makeLink = function ( url, attributes ) {
if ( !attributes ) { if ( !attributes ) {
attributes = {}; attributes = {};
} }
mergeObjects( attributes, this._config.tagAttributes.a );
attributes.href = url; attributes.href = url;
this.changeFormat({ this.changeFormat({

File diff suppressed because one or more lines are too long

View file

@ -160,7 +160,8 @@ proto.setConfig = function ( config ) {
blockquote: null, blockquote: null,
ul: null, ul: null,
ol: null, ol: null,
li: null li: null,
a: null
} }
}, config ); }, config );
@ -1546,7 +1547,7 @@ proto.insertElement = function ( el, range ) {
if ( !canObserveMutations ) { if ( !canObserveMutations ) {
this._docWasChanged(); this._docWasChanged();
} }
return this; return this;
}; };
@ -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 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, var doc = frag.ownerDocument,
walker = new TreeWalker( frag, SHOW_TEXT, walker = new TreeWalker( frag, SHOW_TEXT,
function ( node ) { function ( node ) {
return !getNearest( node, root, 'A' ); return !getNearest( node, root, 'A' );
}, false ), }, false ),
defaultAttributes = self._config.tagAttributes.a,
node, data, parent, match, index, endIndex, child; node, data, parent, match, index, endIndex, child;
while ( node = walker.nextNode() ) { while ( node = walker.nextNode() ) {
data = node.data; data = node.data;
@ -1577,13 +1579,14 @@ var addLinks = function ( frag, root ) {
child = doc.createTextNode( data.slice( 0, index ) ); child = doc.createTextNode( data.slice( 0, index ) );
parent.insertBefore( child, node ); 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.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 ); parent.insertBefore( child, node );
node.data = data = data.slice( endIndex ); node.data = data = data.slice( endIndex );
} }
@ -1616,14 +1619,14 @@ proto.insertHTML = function ( html, isPaste ) {
defaultPrevented: false defaultPrevented: false
}; };
addLinks( frag, root ); addLinks( frag, frag, this );
cleanTree( frag ); cleanTree( frag );
cleanupBRs( frag, root ); cleanupBRs( frag, null );
removeEmptyInlines( frag ); removeEmptyInlines( frag );
frag.normalize(); frag.normalize();
while ( node = getNextBlock( node, root ) ) { while ( node = getNextBlock( node, frag ) ) {
fixCursor( node, root ); fixCursor( node, null );
} }
if ( isPaste ) { if ( isPaste ) {
@ -1716,6 +1719,7 @@ proto.makeLink = function ( url, attributes ) {
if ( !attributes ) { if ( !attributes ) {
attributes = {}; attributes = {};
} }
mergeObjects( attributes, this._config.tagAttributes.a );
attributes.href = url; attributes.href = url;
this.changeFormat({ this.changeFormat({

View file

@ -154,7 +154,7 @@ var keyHandlers = {
// Remove any zws so we don't think there's content in an empty // Remove any zws so we don't think there's content in an empty
// block. // block.
self._recordUndoState( range ); self._recordUndoState( range );
addLinks( range.startContainer ); addLinks( range.startContainer, root, self );
self._removeZWS(); self._removeZWS();
self._getRangeAndRemoveBookmark( range ); self._getRangeAndRemoveBookmark( range );
@ -420,7 +420,7 @@ var keyHandlers = {
space: function ( self, _, range ) { space: function ( self, _, range ) {
var node, parent; var node, parent;
self._recordUndoState( range ); self._recordUndoState( range );
addLinks( range.startContainer ); addLinks( range.startContainer, self._root, self );
self._getRangeAndRemoveBookmark( range ); self._getRangeAndRemoveBookmark( range );
// If the cursor is at the end of a link (<a>foo|</a>) then move it // If the cursor is at the end of a link (<a>foo|</a>) then move it