Skip to content

Commit

Permalink
Merge pull request #709 from yabwe/prepare-4.12.6
Browse files Browse the repository at this point in the history
Prepare 4.12.6
  • Loading branch information
nmielnik committed Jun 27, 2015
2 parents 1bc86ab + f915ad5 commit bbbf2bd
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 12 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"Gruntfile.js",
"demo",
"package.json",
"src/js",
"src",
"README.md",
"CHANGES.md"
]
Expand Down
9 changes: 9 additions & 0 deletions spec/setup.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ describe('Setup/Destroy TestCase', function () {
spyOn(MediumEditor.prototype, 'setup').and.callThrough();
editor.setup();
expect(editor.setup).toHaveBeenCalled();
expect(document.querySelector('[data-medium-element]')).toBeTruthy();
expect(document.querySelector('[aria-multiline]')).toBeTruthy();
expect(document.querySelector('[medium-editor-index]')).toBeTruthy();
expect(document.querySelector('[role]')).toBeTruthy();
expect(document.querySelector('[spellcheck]')).toBeTruthy();
expect(document.querySelector('[contenteditable]')).toBeTruthy();
});

it('should know about defaults', function () {
Expand All @@ -61,6 +67,9 @@ describe('Setup/Destroy TestCase', function () {
expect(document.querySelector('.medium-editor-toolbar')).toBeTruthy();
editor.destroy();
expect(document.querySelector('.medium-editor-toolbar')).toBeFalsy();
// ensure only initial attributes are here: the editor class
expect(this.el.getAttribute('class')).toBe('editor');
expect(this.el.attributes.length).toBe(1);
});

it('should remove all the added events', function () {
Expand Down
19 changes: 19 additions & 0 deletions spec/textarea.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,25 @@ describe('Textarea TestCase', function () {
expect(editor.elements[0].className).toBe('editor test-class test-class-2');
});

it('should create unique div ids for multiple textareas', function () {
var tas = [];
for (var i = 0; i < 12; i++) {
var ta = document.createElement('textarea');
ta.className = 'editor';
ta.value = 'test content';
document.body.appendChild(ta);
tas.push(ta);
}
var editor = this.newMediumEditor('.editor');
editor.elements.forEach(function (el) {
expect(document.querySelectorAll('div#' + el.id).length).toEqual(1);
});
editor.destroy();
tas.forEach(function (el) {
document.body.removeChild(el);
});
});

it('should cleanup after destroy', function () {
var editor = this.newMediumEditor('.editor');
expect(this.el.classList.contains('medium-editor-hidden')).toBe(true);
Expand Down
17 changes: 10 additions & 7 deletions src/js/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ function MediumEditor(elements, options) {

// Loop through elements and convert textarea's into divs
this.elements = [];
elements.forEach(function (element) {
elements.forEach(function (element, index) {
if (element.tagName.toLowerCase() === 'textarea') {
this.elements.push(createContentEditable.call(this, element));
this.elements.push(createContentEditable.call(this, element, index));
} else {
this.elements.push(element);
}
Expand Down Expand Up @@ -285,9 +285,9 @@ function MediumEditor(elements, options) {
return this.options.imageDragging !== false;
}

function createContentEditable(textarea) {
function createContentEditable(textarea, id) {
var div = this.options.ownerDocument.createElement('div'),
id = (+new Date()),
uniqueId = 'medium-editor-' + Date.now() + '-' + id,
attributesToClone = [
'data-disable-editing',
'data-disable-toolbar',
Expand All @@ -299,7 +299,7 @@ function MediumEditor(elements, options) {
];

div.className = textarea.className;
div.id = id;
div.id = uniqueId;
div.innerHTML = textarea.value;
div.setAttribute('medium-editor-textarea-id', id);
attributesToClone.forEach(function (attr) {
Expand Down Expand Up @@ -637,6 +637,8 @@ function MediumEditor(elements, options) {
delete this.toolbar;
}

this.events.destroy();

this.elements.forEach(function (element) {
// Reset elements content, fix for issue where after editor destroyed the red underlines on spelling errors are left
if (this.options.spellcheck) {
Expand All @@ -646,6 +648,9 @@ function MediumEditor(elements, options) {
element.removeAttribute('contentEditable');
element.removeAttribute('spellcheck');
element.removeAttribute('data-medium-element');
element.removeAttribute('medium-editor-index');
element.removeAttribute('role');
element.removeAttribute('aria-multiline');

// Remove any elements created for textareas
if (element.hasAttribute('medium-editor-textarea-id')) {
Expand All @@ -660,8 +665,6 @@ function MediumEditor(elements, options) {
}
}, this);
this.elements = [];

this.events.destroy();
},

on: function (target, event, listener, useCapture) {
Expand Down
6 changes: 6 additions & 0 deletions src/js/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ var Events;
this.detachAllDOMEvents();
this.detachAllCustomEvents();
this.detachExecCommand();

if (this.base.elements) {
this.base.elements.forEach(function (element) {
element.removeAttribute('data-medium-focused');
});
}
},

// Listening to calls to document.execCommand
Expand Down
8 changes: 4 additions & 4 deletions src/js/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,10 @@ var Toolbar;

// Checks for existance of multiple block elements in the current selection
multipleBlockElementsSelected: function () {
/*jslint regexp: true*/
var selectionHtml = Selection.getSelectionHtml.call(this).replace(/<[\S]+><\/[\S]+>/gim, ''),
hasMultiParagraphs = selectionHtml.match(/<(p|h[1-6]|blockquote)[^>]*>/g);
/*jslint regexp: false*/
var regexEmptyHTMLTags = /<[^\/>][^>]*><\/[^>]+>/gim, // http://stackoverflow.com/questions/3129738/remove-empty-tags-using-regex
regexBlockElements = new RegExp('<(' + Util.parentElements.join('|') + ')[^>]*>', 'g'),
selectionHTML = Selection.getSelectionHtml.call(this).replace(regexEmptyHTMLTags, ''), // Filter out empty blocks from selection
hasMultiParagraphs = selectionHTML.match(regexBlockElements); // Find how many block elements are within the html

return !!hasMultiParagraphs && hasMultiParagraphs.length > 1;
},
Expand Down

0 comments on commit bbbf2bd

Please sign in to comment.