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

Improve inline element handling #68

Merged
merged 28 commits into from
Jun 11, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
b7654b7
Update karma and jasmine
peyerluk May 21, 2014
1be5ca2
Add change event
peyerluk May 21, 2014
d3721bb
Add dispatcher specs
peyerluk May 22, 2014
5d520d2
Clean out some logs from dispatcher
peyerluk May 22, 2014
21ff5d8
Add getContent() method to API
peyerluk May 22, 2014
f28eed9
Use css class to initialize editables in html files
peyerluk May 22, 2014
b77e03c
Add zeroWidthNonBreakingSpace to empty elements on focus
peyerluk May 22, 2014
03fd38f
Remove Opera from browser testing
peyerluk May 22, 2014
facd357
Improve content.extractContent() method
peyerluk May 30, 2014
dbb6376
Remove bug in content.unwrap()
peyerluk May 30, 2014
d126ca9
Remove spec that doesnt work in Firefox
peyerluk May 30, 2014
2d82b90
Refactoring: use setVisibleSelection() where possible
peyerluk May 30, 2014
5412039
Don't set log: false in main.js
peyerluk May 30, 2014
e858bdb
Build and linting
peyerluk May 30, 2014
8797bf2
Add parser.isInlineElement() method
peyerluk May 30, 2014
941e948
gs
peyerluk May 30, 2014
657f85d
Fix cursor.setVisibleSelection()
peyerluk May 30, 2014
f80611a
Improve extractContent logic
peyerluk May 31, 2014
8672038
Add line-break to empty elements
peyerluk Jun 2, 2014
fee4dbc
Remove logs from default-behaviour
peyerluk Jun 2, 2014
7aeb134
Update changelog
peyerluk Jun 2, 2014
9ce0da3
Fix cursor methods in core
peyerluk Jun 2, 2014
5624c3b
Add triggerChange() method to cursor
peyerluk Jun 2, 2014
017243b
Use the correct document in rangeSaveRestore
peyerluk Jun 3, 2014
b829820
Bugfix in content.unwrapInternalNodes()
peyerluk Jun 3, 2014
0fd2b3e
Lint and build
peyerluk Jun 3, 2014
c7f0969
Remove unused rangy code
peyerluk Jun 3, 2014
7c52fd4
Remove unused grunt task
peyerluk Jun 3, 2014
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
Prev Previous commit
Next Next commit
Improve content.extractContent() method
  • Loading branch information
peyerluk committed May 30, 2014
commit facd3572c89584d25bbccd7983fe9b5e8efe315b
40 changes: 40 additions & 0 deletions spec/content.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,4 +497,44 @@ describe('Content', function() {
expect(exact).toEqual(false);
});
});

describe('extractContent()', function() {
var $host;

beforeEach(function() {
$host = $('<div></div>');
});

it('extracts the content', function() {
$host.html('a')
var result = content.extractContent($host[0])
// escape to show invisible characters
expect(escape(result)).toEqual('a')
});

it('replaces a zeroWidthSpace with a <br> tag', function() {
$host.html('a\u200B')
var result = content.extractContent($host[0])
expect(result).toEqual('a<br>')
});

it('removes zeroWidthNonBreakingSpaces', function() {
$host.html('a\uFEFF')
var result = content.extractContent($host[0])
// escape to show invisible characters
expect(escape(result)).toEqual('a')
});

it('removes saved ranges', function() {
$host.html('<span id="editable-range-boundary-1">a</span>')
var result = content.extractContent($host[0])
expect(result).toEqual('a')
});

it('removes saved ranges but leaves them in the host', function() {
$host.html('<span id="editable-range-boundary-1">a</span>')
var result = content.extractContent($host[0])
expect($host[0].innerHTML).toEqual('<span id="editable-range-boundary-1">a</span>')
});
});
});
40 changes: 35 additions & 5 deletions src/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,44 @@ var content = (function() {
* @param {HTMLElement} element The element to process.
*/
cleanInternals: function(element) {
element.innerHTML = this.removeInternals(element.innerHTML);
// Uses extract content for simplicity. A custom method
// that does not clone the element could be faster if needed.
element.innerHTML = this.extractContent(element);
},


removeInternals: function(innerHtml) {
/**
* Extracts the content from a host element.
* Does not touch or change the host. Just returns
* the content without anything inserted by editable or for
* the user interface.
*/
extractContent: function(element) {
var innerHtml = element.innerHTML;
innerHtml = innerHtml.replace(zeroWidthNonBreakingSpace, ''); // Used for forcing inline elments to have a height
innerHtml = innerHtml.replace(zeroWidthSpace, '<br />'); // Used for cross-browser newlines
return innerHtml;
innerHtml = innerHtml.replace(zeroWidthSpace, '<br>'); // Used for cross-browser newlines

var clone = document.createElement('div');
clone.innerHTML = innerHtml;
this.unwrapInternalNodes(clone);
return clone.innerHTML;
},

/**
* Remove elements that were inserted for internal or user interface purposes
*
* Currently:
* - Saved ranges
*/
unwrapInternalNodes: function(sibling) {
while (sibling != null) {
if (sibling.nodeType === 1 && sibling.firstChild) {
this.unwrapInternalNodes(sibling.firstChild);
}
if (/editable-range-boundary-/.test(sibling.id)) {
this.unwrap(sibling);
}
sibling = sibling.nextSibling;
}
},

/**
Expand Down
2 changes: 1 addition & 1 deletion src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ Editable.prototype.createCursorAfter = function(element) {
};

Editable.prototype.getContent = function(element) {
return content.removeInternals(element.innerHTML);
return content.extractContent(element);
};


Expand Down