Skip to content
This repository has been archived by the owner on Mar 8, 2019. It is now read-only.

Add javascripts option to Editor constructor #418

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
136 changes: 103 additions & 33 deletions dist/wysihtml5-0.4.0pre.js
Original file line number Diff line number Diff line change
Expand Up @@ -3485,7 +3485,7 @@ wysihtml5.browser = (function() {
* Firefox on OSX navigates through history when hitting CMD + Arrow right/left
*/
hasHistoryIssue: function() {
return isGecko;
return isGecko && navigator.platform.substr(0, 3) === "Mac";
},

/**
Expand Down Expand Up @@ -3725,6 +3725,18 @@ wysihtml5.browser = (function() {
*/
hasIframeFocusIssue: function() {
return isIE;
},

/**
* Chrome + Safari create invalid nested markup after paste
*
* <p>
* foo
* <p>bar</p> <!-- BOO! -->
* </p>
*/
createsNestedInvalidMarkupAfterPaste: function() {
return isWebKit;
}
};
})();wysihtml5.lang.array = function(arr) {
Expand Down Expand Up @@ -3876,7 +3888,14 @@ wysihtml5.browser = (function() {
};
};(function() {
var WHITE_SPACE_START = /^\s+/,
WHITE_SPACE_END = /\s+$/;
WHITE_SPACE_END = /\s+$/,
ENTITY_REG_EXP = /[&<>"]/g,
ENTITY_MAP = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': "&quot;"
};
wysihtml5.lang.string = function(str) {
str = String(str);
return {
Expand Down Expand Up @@ -3912,6 +3931,15 @@ wysihtml5.browser = (function() {
return str.split(search).join(replace);
}
};
},

/**
* @example
* wysihtml5.lang.string("hello<br>").escapeHTML();
* // => "hello&lt;br&gt;"
*/
escapeHTML: function() {
return str.replace(ENTITY_REG_EXP, function(c) { return ENTITY_MAP[c]; });
}
};
};
Expand Down Expand Up @@ -4002,11 +4030,12 @@ wysihtml5.browser = (function() {
*/
function _wrapMatchesInNode(textNode) {
var parentNode = textNode.parentNode,
nodeValue = wysihtml5.lang.string(textNode.data).escapeHTML(),
tempElement = _getTempElement(parentNode.ownerDocument);

// We need to insert an empty/temporary <span /> to fix IE quirks
// Elsewise IE would strip white space in the beginning
tempElement.innerHTML = "<span></span>" + _convertUrlsToLinks(textNode.data);
tempElement.innerHTML = "<span></span>" + _convertUrlsToLinks(nodeValue);
tempElement.removeChild(tempElement.firstChild);

while (tempElement.firstChild) {
Expand Down Expand Up @@ -4792,9 +4821,9 @@ wysihtml5.dom.parse = (function() {
}

while (element.firstChild) {
firstChild = element.firstChild;
element.removeChild(firstChild);
firstChild = element.firstChild;
newNode = _convert(firstChild, cleanUp);
element.removeChild(firstChild);
if (newNode) {
fragment.appendChild(newNode);
}
Expand All @@ -4815,6 +4844,7 @@ wysihtml5.dom.parse = (function() {
oldChildsLength = oldChilds.length,
method = NODE_TYPE_MAPPING[oldNodeType],
i = 0,
fragment,
newNode,
newChild;

Expand All @@ -4833,10 +4863,13 @@ wysihtml5.dom.parse = (function() {

// Cleanup senseless <span> elements
if (cleanUp &&
newNode.childNodes.length <= 1 &&
newNode.nodeName.toLowerCase() === DEFAULT_NODE_NAME &&
!newNode.attributes.length) {
return newNode.firstChild;
(!newNode.childNodes.length || !newNode.attributes.length)) {
fragment = newNode.ownerDocument.createDocumentFragment();
while (newNode.firstChild) {
fragment.appendChild(newNode.firstChild);
}
return fragment;
}

return newNode;
Expand Down Expand Up @@ -5054,8 +5087,17 @@ wysihtml5.dom.parse = (function() {
}
}

var INVISIBLE_SPACE_REG_EXP = /\uFEFF/g;
function _handleText(oldNode) {
return oldNode.ownerDocument.createTextNode(oldNode.data);
var nextSibling = oldNode.nextSibling;
if (nextSibling && nextSibling.nodeType === wysihtml5.TEXT_NODE) {
// Concatenate text nodes
nextSibling.data = oldNode.data + nextSibling.data;
} else {
// \uFEFF = wysihtml5.INVISIBLE_SPACE (used as a hack in certain rich text editing situations)
var data = oldNode.data.replace(INVISIBLE_SPACE_REG_EXP, "");
return oldNode.ownerDocument.createTextNode(data);
}
}


Expand Down Expand Up @@ -5515,6 +5557,8 @@ wysihtml5.dom.replaceWithChildNodes = function(node) {
this.getWindow = function() { return iframe.contentWindow; };
this.getDocument = function() { return iframe.contentWindow.document; };

this._insertJavascripts();

// Catch js errors and pass them to the parent's onerror event
// addEventListener("error") doesn't work properly in some browsers
// TODO: apparently this doesn't work in IE9!
Expand Down Expand Up @@ -5571,6 +5615,26 @@ wysihtml5.dom.replaceWithChildNodes = function(node) {
).interpolate(templateVars);
},

_insertJavascripts: function() {
var script = null,
doc = this.getDocument(),
head = doc.head,
javascripts = this.config.javascripts,
i = 0;

javascripts = typeof(javascripts) === "string" ? [javascripts] : javascripts;

if (javascripts) {
length = javascripts.length;
for (; i<length; i++) {
script = doc.createElement("script");
script.src = javascripts[i];
head.appendChild(script);
}
}
},


/**
* Method to unset/override existing variables
* @example
Expand Down Expand Up @@ -6896,8 +6960,7 @@ wysihtml5.commands.bold = {
* Instead we set a css class
*/
(function(wysihtml5) {
var undef,
REG_EXP = /wysiwyg-font-size-[0-9a-z\-]+/g;
var REG_EXP = /wysiwyg-font-size-[0-9a-z\-]+/g;

wysihtml5.commands.fontSize = {
exec: function(composer, command, size) {
Expand All @@ -6906,10 +6969,6 @@ wysihtml5.commands.bold = {

state: function(composer, command, size) {
return wysihtml5.commands.formatInline.state(composer, command, "span", "wysiwyg-font-size-" + size, REG_EXP);
},

value: function() {
return undef;
}
};
})(wysihtml5);
Expand Down Expand Up @@ -7274,7 +7333,6 @@ wysihtml5.commands.bold = {
var doc = composer.doc,
image = this.state(composer),
textNode,
i,
parent;

if (image) {
Expand All @@ -7297,11 +7355,8 @@ wysihtml5.commands.bold = {

image = doc.createElement(NODE_NAME);

for (i in value) {
if (i === "className") {
i = "class";
}
image.setAttribute(i, value[i]);
for (var i in value) {
image.setAttribute(i === "className" ? "class" : i, value[i]);
}

composer.selection.insertNode(image);
Expand Down Expand Up @@ -7896,11 +7951,6 @@ wysihtml5.views.View = Base.extend(
value = this.parent.parse(value);
}

// Replace all "zero width no breaking space" chars
// which are used as hacks to enable some functionalities
// Also remove all CARET hacks that somehow got left
value = wysihtml5.lang.string(value).replace(wysihtml5.INVISIBLE_SPACE).by("");

return value;
},

Expand Down Expand Up @@ -7987,7 +8037,8 @@ wysihtml5.views.View = Base.extend(
this.sandbox = new dom.Sandbox(function() {
that._create();
}, {
stylesheets: this.config.stylesheets
stylesheets: this.config.stylesheets,
javascripts: this.config.javascripts
});
this.iframe = this.sandbox.getIframe();

Expand Down Expand Up @@ -8229,6 +8280,16 @@ wysihtml5.views.View = Base.extend(
});
}

// Under certain circumstances Chrome + Safari create nested <p> or <hX> tags after paste
// Inserting an invisible white space in front of it fixes the issue
if (browser.createsNestedInvalidMarkupAfterPaste()) {
dom.observe(this.element, "paste", function(event) {
var invisibleSpace = that.doc.createTextNode(wysihtml5.INVISIBLE_SPACE);
that.selection.insertNode(invisibleSpace);
});
}


dom.observe(this.doc, "keydown", function(event) {
var keyCode = event.keyCode;

Expand Down Expand Up @@ -8896,6 +8957,7 @@ wysihtml5.views.Textarea = wysihtml5.views.View.extend(
callbackWrapper(event);
}
if (keyCode === wysihtml5.ESCAPE_KEY) {
that.fire("cancel");
that.hide();
}
});
Expand Down Expand Up @@ -9263,10 +9325,14 @@ wysihtml5.views.Textarea = wysihtml5.views.View.extend(
for (; i<length; i++) {
// 'javascript:;' and unselectable=on Needed for IE, but done in all browsers to make sure that all get the same css applied
// (you know, a:link { ... } doesn't match anchors with missing href attribute)
dom.setAttributes({
href: "javascript:;",
unselectable: "on"
}).on(links[i]);
if (links[i].nodeName === "A") {
dom.setAttributes({
href: "javascript:;",
unselectable: "on"
}).on(links[i]);
} else {
dom.setAttributes({ unselectable: "on" }).on(links[i]);
}
}

// Needed for opera and chrome
Expand Down Expand Up @@ -9456,10 +9522,14 @@ wysihtml5.views.Textarea = wysihtml5.views.View.extend(
useLineBreaks: true,
// Array (or single string) of stylesheet urls to be loaded in the editor's iframe
stylesheets: [],
// Array (or single string) of javascript urls to be loaded in the editor's iframe
javascripts: [],
// Placeholder text to use, defaults to the placeholder attribute on the textarea element
placeholderText: undef,
// Whether the rich text editor should be rendered on touch devices (wysihtml5 >= 0.3.0 comes with basic support for iOS 5)
supportTouchDevices: true
supportTouchDevices: true,
// Whether senseless <span> elements (empty or without attributes) should be removed/replaced with their content
cleanUp: true
};

wysihtml5.Editor = wysihtml5.lang.Dispatcher.extend(
Expand Down Expand Up @@ -9554,7 +9624,7 @@ wysihtml5.views.Textarea = wysihtml5.views.View.extend(
},

parse: function(htmlOrElement) {
var returnValue = this.config.parser(htmlOrElement, this.config.parserRules, this.composer.sandbox.getDocument(), true);
var returnValue = this.config.parser(htmlOrElement, this.config.parserRules, this.composer.sandbox.getDocument(), this.config.cleanUp);
if (typeof(htmlOrElement) === "object") {
wysihtml5.quirks.redraw(htmlOrElement);
}
Expand Down
Loading