0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2024-12-22 15:23:29 -05:00

Explicit attributes should override defaults

Fixes #203
This commit is contained in:
Neil Jenkins 2016-07-16 16:50:59 +10:00
parent 766dea7ccf
commit df9b5add68
3 changed files with 42 additions and 32 deletions

View file

@ -2324,16 +2324,20 @@ function getSquireInstance ( doc ) {
return null; return null;
} }
function mergeObjects ( base, extras ) { function mergeObjects ( base, extras, mayOverride ) {
var prop, value; var prop, value;
if ( !base ) { if ( !base ) {
base = {}; base = {};
} }
for ( prop in extras ) { if ( extras ) {
value = extras[ prop ]; for ( prop in extras ) {
base[ prop ] = ( value && value.constructor === Object ) ? if ( mayOverride || !( prop in base ) ) {
mergeObjects( base[ prop ], value ) : value = extras[ prop ];
value; base[ prop ] = ( value && value.constructor === Object ) ?
mergeObjects( base[ prop ], value, mayOverride ) :
value;
}
}
} }
return base; return base;
} }
@ -2482,7 +2486,7 @@ proto.setConfig = function ( config ) {
documentSizeThreshold: -1, // -1 means no threshold documentSizeThreshold: -1, // -1 means no threshold
undoLimit: -1 // -1 means no limit undoLimit: -1 // -1 means no limit
} }
}, config ); }, config, true );
// Users may specify block tag in lower case // Users may specify block tag in lower case
config.blockTag = config.blockTag.toUpperCase(); config.blockTag = config.blockTag.toUpperCase();
@ -3928,7 +3932,7 @@ proto.insertElement = function ( el, range ) {
proto.insertImage = function ( src, attributes ) { proto.insertImage = function ( src, attributes ) {
var img = this.createElement( 'IMG', mergeObjects({ var img = this.createElement( 'IMG', mergeObjects({
src: src src: src
}, attributes )); }, attributes, true ));
this.insertElement( img ); this.insertElement( img );
return img; return img;
}; };
@ -3959,7 +3963,7 @@ var addLinks = function ( frag, root, self ) {
match[1] : match[1] :
'http://' + match[1] : 'http://' + match[1] :
'mailto:' + match[2] 'mailto:' + match[2]
}, defaultAttributes )); }, defaultAttributes, false ));
child.textContent = data.slice( index, endIndex ); child.textContent = data.slice( index, endIndex );
parent.insertBefore( child, node ); parent.insertBefore( child, node );
node.data = data = data.slice( endIndex ); node.data = data = data.slice( endIndex );
@ -4132,12 +4136,13 @@ proto.makeLink = function ( url, attributes ) {
this._doc.createTextNode( url.slice( protocolEnd ) ) this._doc.createTextNode( url.slice( protocolEnd ) )
); );
} }
attributes = mergeObjects(
if ( !attributes ) { mergeObjects({
attributes = {}; href: url
} }, attributes, true ),
mergeObjects( attributes, this._config.tagAttributes.a ); this._config.tagAttributes.a,
attributes.href = url; false
);
this.changeFormat({ this.changeFormat({
tag: 'A', tag: 'A',

File diff suppressed because one or more lines are too long

View file

@ -14,16 +14,20 @@ function getSquireInstance ( doc ) {
return null; return null;
} }
function mergeObjects ( base, extras ) { function mergeObjects ( base, extras, mayOverride ) {
var prop, value; var prop, value;
if ( !base ) { if ( !base ) {
base = {}; base = {};
} }
for ( prop in extras ) { if ( extras ) {
value = extras[ prop ]; for ( prop in extras ) {
base[ prop ] = ( value && value.constructor === Object ) ? if ( mayOverride || !( prop in base ) ) {
mergeObjects( base[ prop ], value ) : value = extras[ prop ];
value; base[ prop ] = ( value && value.constructor === Object ) ?
mergeObjects( base[ prop ], value, mayOverride ) :
value;
}
}
} }
return base; return base;
} }
@ -172,7 +176,7 @@ proto.setConfig = function ( config ) {
documentSizeThreshold: -1, // -1 means no threshold documentSizeThreshold: -1, // -1 means no threshold
undoLimit: -1 // -1 means no limit undoLimit: -1 // -1 means no limit
} }
}, config ); }, config, true );
// Users may specify block tag in lower case // Users may specify block tag in lower case
config.blockTag = config.blockTag.toUpperCase(); config.blockTag = config.blockTag.toUpperCase();
@ -1618,7 +1622,7 @@ proto.insertElement = function ( el, range ) {
proto.insertImage = function ( src, attributes ) { proto.insertImage = function ( src, attributes ) {
var img = this.createElement( 'IMG', mergeObjects({ var img = this.createElement( 'IMG', mergeObjects({
src: src src: src
}, attributes )); }, attributes, true ));
this.insertElement( img ); this.insertElement( img );
return img; return img;
}; };
@ -1649,7 +1653,7 @@ var addLinks = function ( frag, root, self ) {
match[1] : match[1] :
'http://' + match[1] : 'http://' + match[1] :
'mailto:' + match[2] 'mailto:' + match[2]
}, defaultAttributes )); }, defaultAttributes, false ));
child.textContent = data.slice( index, endIndex ); child.textContent = data.slice( index, endIndex );
parent.insertBefore( child, node ); parent.insertBefore( child, node );
node.data = data = data.slice( endIndex ); node.data = data = data.slice( endIndex );
@ -1822,12 +1826,13 @@ proto.makeLink = function ( url, attributes ) {
this._doc.createTextNode( url.slice( protocolEnd ) ) this._doc.createTextNode( url.slice( protocolEnd ) )
); );
} }
attributes = mergeObjects(
if ( !attributes ) { mergeObjects({
attributes = {}; href: url
} }, attributes, true ),
mergeObjects( attributes, this._config.tagAttributes.a ); this._config.tagAttributes.a,
attributes.href = url; false
);
this.changeFormat({ this.changeFormat({
tag: 'A', tag: 'A',