0
Fork 0
mirror of https://github.com/fastmail/Squire.git synced 2024-12-22 07:13:08 -05:00

Merge branch 'farooqu-insertHtml'

This commit is contained in:
Neil Jenkins 2015-04-15 16:25:37 +10:00
commit 3676651e6f
4 changed files with 100 additions and 2 deletions

View file

@ -136,6 +136,16 @@ The method takes one argument:
Returns a reference to the newly inserted image element.
### insertHTML
Inserts an HTML fragment at the current cursor location, or replaces the selection if selected. The value supplied should not contain `<body>` tags or anything outside of that.
The method takes one argument:
* **html**: The html to insert.
Returns self (the Squire instance).
### getPath
Returns the path through the DOM tree from the `<body>` element to the current current cursor position. This is a string consisting of the tag, id and class names in CSS format. For example `BODY>BLOCKQUOTE>DIV#id>STRONG>SPAN.font>EM`. If a selection has been made, so different parts of the selection may have different paths, the value will be `(selection)`. The path is useful for efficiently determining the current formatting for bold, italic, underline etc, and thus determining button state. If a selection has been made, you can has the `hasFormat` method instead to get the current state for the properties you care about.

View file

@ -3319,6 +3319,50 @@ proto.insertImage = function ( src ) {
return img;
};
// Insert HTML at the cursor location. If the selection is not collapsed
// insertTreeFragmentIntoRange will delete the selection so that it is replaced
// by the html being inserted.
proto.insertHTML = function ( html ) {
var range = this.getSelection(),
frag = this._doc.createDocumentFragment(),
div = this.createElement( 'DIV' );
// Parse HTML into DOM tree
div.innerHTML = html;
frag.appendChild( empty( div ) );
// Record undo checkpoint
this._recordUndoState( range );
this._getRangeAndRemoveBookmark( range );
try {
frag.normalize();
addLinks( frag );
cleanTree( frag, true );
cleanupBRs( frag );
removeEmptyInlines( frag );
fixContainer( frag );
var node = frag;
while ( node = getNextBlock( node ) ) {
fixCursor( node );
}
insertTreeFragmentIntoRange( range, frag );
if ( !canObserveMutations ) {
this._docWasChanged();
}
range.collapse( false );
this._ensureBottomLine();
this.setSelection( range );
this._updatePath( range, true );
} catch ( error ) {
this.didError( error );
}
return this;
};
// --- Formatting ---
var command = function ( method, arg, arg2 ) {

File diff suppressed because one or more lines are too long

View file

@ -2263,6 +2263,50 @@ proto.insertImage = function ( src ) {
return img;
};
// Insert HTML at the cursor location. If the selection is not collapsed
// insertTreeFragmentIntoRange will delete the selection so that it is replaced
// by the html being inserted.
proto.insertHTML = function ( html ) {
var range = this.getSelection(),
frag = this._doc.createDocumentFragment(),
div = this.createElement( 'DIV' );
// Parse HTML into DOM tree
div.innerHTML = html;
frag.appendChild( empty( div ) );
// Record undo checkpoint
this._recordUndoState( range );
this._getRangeAndRemoveBookmark( range );
try {
frag.normalize();
addLinks( frag );
cleanTree( frag, true );
cleanupBRs( frag );
removeEmptyInlines( frag );
fixContainer( frag );
var node = frag;
while ( node = getNextBlock( node ) ) {
fixCursor( node );
}
insertTreeFragmentIntoRange( range, frag );
if ( !canObserveMutations ) {
this._docWasChanged();
}
range.collapse( false );
this._ensureBottomLine();
this.setSelection( range );
this._updatePath( range, true );
} catch ( error ) {
this.didError( error );
}
return this;
};
// --- Formatting ---
var command = function ( method, arg, arg2 ) {