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

Apply the ViewSource plugin's filter on the editor's content. #171

Merged
merged 1 commit into from
May 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions _editor/plugins/ViewSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ define([
this.editor = editor;
this._initButton();

// Filter the html content when it is set and retrieved in the editor.
this.removeValueFilterHandles();
this._setValueFilterHandle = aspect.before(this.editor, "setValue", lang.hitch(this, function (html) {
return [this._filter(html)];
}));
this._getValueFilterHandle = aspect.after(this.editor, "getValue", lang.hitch(this, function (html) {
return this._filter(html);
}));

this.editor.addKeyHandler(keys.F12, true, true, lang.hitch(this, function(e){
// Move the focus before switching
// It'll focus back. Hiding a focused
Expand Down Expand Up @@ -152,9 +161,6 @@ define([
return cmd.toLowerCase() === "viewsource";
};
this.editor.onDisplayChanged();
html = ed.get("value");
html = this._filter(html);
ed.set("value", html);
array.forEach(edPlugins, function(p){
// Turn off any plugins not controlled by queryCommandenabled.
if(p && !(p instanceof ViewSource) && p.isInstanceOf(_Plugin)){
Expand All @@ -170,7 +176,7 @@ define([
};
}

this.sourceArea.value = html;
this.sourceArea.value = ed.get("value");

// Since neither iframe nor textarea have margin, border, or padding,
// just set sizes equal.
Expand Down Expand Up @@ -235,7 +241,7 @@ define([

this._setListener = aspect.after(this.editor, "setValue", lang.hitch(this, function(htmlTxt){
htmlTxt = htmlTxt || "";
htmlTxt = this._filter(htmlTxt);
// htmlTxt was filtered in setValue before aspect.
this.sourceArea.value = htmlTxt;
}), true);
}else{
Expand All @@ -262,8 +268,8 @@ define([
ed.queryCommandEnabled = ed._sourceQueryCommandEnabled;
if(!this._readOnly){
html = this.sourceArea.value;
html = this._filter(html);
ed.beginEditing();
// html will be filtered in setValue aspect.
ed.set("value", html);
ed.endEditing();
}
Expand Down Expand Up @@ -523,6 +529,17 @@ define([
return html;
},

removeValueFilterHandles: function () {
if (this._setValueFilterHandle) {
this._setValueFilterHandle.remove();
delete this._setValueFilterHandle;
}
if (this._getValueFilterHandle) {
this._getValueFilterHandle.remove();
delete this._getValueFilterHandle;
}
},

setSourceAreaCaret: function(){
// summary:
// Internal function to set the caret in the sourceArea
Expand Down Expand Up @@ -557,6 +574,7 @@ define([
this._setListener.remove();
delete this._setListener;
}
this.removeValueFilterHandles();
this.inherited(arguments);
}
});
Expand Down
34 changes: 33 additions & 1 deletion tests/editor/test_ViewSource.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,21 @@
<script type="text/javascript">
require([
"dojo/parser",
"dijit/registry",
"dijit/Editor",
"dijit/_editor/plugins/ViewSource",
"dijit/_editor/plugins/FullScreen",
"dojo/domReady!"
], function(parser){
], function(parser, registry){
parser.parse();

var editor = registry.byId("editor7");
editor.set("value", "<h1>ViewSource Plugin with onmouseover in content set via setValue.</h1>" +
"<p onmouseover=\"alert('Mouse over foobar');\">foobar</p>" +
"<h2>Things to test:</h2>\n" +
"<ol>" +
"<li>Move mouse over \"foobar\" and make sure alert is not displayed.</li>" +
"</ol>");
});
</script>
</head>
Expand Down Expand Up @@ -147,6 +156,29 @@ <h2>Things to test:</h2>
</div>
<br>
<br>
<div>
<div id="editor6" data-dojo-type="dijit/Editor"
data-dojo-props='"aria-label":"editor3",extraPlugins:["fullScreen", "viewSource"],
style:"background-color: white; width: 800px;", height:"300px" '>
<h1>ViewSource Plugin with onmouseover in content.</h1>
<p onmouseover="alert('Mouse over foobar');">foobar</p>

<h2>Things to test:</h2>
<ol>
<li>Move mouse over "foobar" and make sure alert is not displayed.</li>
</ol>
</div>
</div>
<br>
<br>
<div>
<div id="editor7" data-dojo-type="dijit/Editor"
data-dojo-props='"aria-label":"editor3",extraPlugins:["fullScreen", "viewSource"],
style:"background-color: white; width: 800px;", height:"300px" '>
</div>
</div>
<br>
<br>
<div>Content after the editors.</div>
</body>
</html>