Skip to content
This repository has been archived by the owner on Apr 2, 2021. It is now read-only.

Commit

Permalink
Merge pull request #431 from wymeditor/issue_431
Browse files Browse the repository at this point in the history
Fixes #431 Regression: BR tags are now allowed at the document root
  • Loading branch information
winhamwr committed Oct 22, 2013
2 parents 77fa041 + 0c36794 commit e140771
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -29,6 +29,10 @@ WYMeditor.
still be applied to all of the list items in each of the selected lists. This
behavior has been adjusted so that the indent and outdent tools will not
apply to lists unless the selection is entirely contained within one list.
[#418](https://github.com/wymeditor/wymeditor/issues/418)
* 1.0.0b5 introduced a regression bug which allowed `br` tags to exist at the
document root, even after parsing. This is no longer the case.
[#431](https://github.com/wymeditor/wymeditor/issues/431)

### Skins

Expand Down
27 changes: 27 additions & 0 deletions src/test/unit/specific_feature_tests/xml_parser.js
Expand Up @@ -1625,3 +1625,30 @@ test("Unwrap content of nested DIV elements in list item", function () {
"Unwrap content of nested `div` elements in a list item",
true);
});

module("XmlParser-allowed_block_elements", {setup: setupWym});

var blockElementsHtml = {};
blockElementsHtml.expected = [""
, '<p>p1</p>'
, '<p>p2</p>'
].join('');
blockElementsHtml.brInRoot = [""
, '<br>'
, '<p>p1</p>'
, '<br />'
, '<p>p2</p>'
, '<br />'
].join('');

test("BR isn't allowed at the root", function () {
expect(1);
var wymeditor = jQuery.wymeditors(0);

wymeditor._html(blockElementsHtml.brInRoot);
htmlEquals(
wymeditor,
blockElementsHtml.expected,
"BR removed from root"
);
});
2 changes: 1 addition & 1 deletion src/test/unit/utils.js
Expand Up @@ -169,7 +169,7 @@ function normalizeHtml(node) {
*
* fixListSpacing is a boolean that specifies if leading spaces before line
* breaks and list type elements should be removed in older versions of Internet
* Explorer (i.e. IE7-8). This paramater is optional.
* Explorer (i.e. IE7-8). Defaults to false.
*/
function htmlEquals(wymeditor, expected, assertionString, fixListSpacing) {
var xhtml = '',
Expand Down
2 changes: 1 addition & 1 deletion src/wymeditor/parser/xhtml-parser.js
Expand Up @@ -25,7 +25,7 @@ WYMeditor.XhtmlParser.prototype.parse = function(raw) {

WYMeditor.XhtmlParser.prototype.beforeParsing = function(raw) {
if (raw.match(/class="MsoNormal"/) || raw.match(/ns = "urn:schemas-microsoft-com/)) {
// Usefull for cleaning up content pasted from other sources (MSWord)
// Useful for cleaning up content pasted from other sources (MSWord)
this._Listener.avoidStylingTagsAndAttributes();
}

Expand Down
50 changes: 45 additions & 5 deletions src/wymeditor/parser/xhtml-sax-listener.js
Expand Up @@ -27,6 +27,9 @@ WYMeditor.XhtmlSaxListener = function() {
this.tagsToUnwrapInLists =
WYMeditor.DocumentStructureManager.VALID_DEFAULT_ROOT_CONTAINERS;

// If any of these inline tags is found in the root, just remove them.
this._rootInlineTagsToRemove = ['br'];

// A counter to keep track of the number of extra block closing tags that
// should be expected by the parser because of the removal of that
// element's opening tag.
Expand Down Expand Up @@ -374,12 +377,12 @@ WYMeditor.XhtmlSaxListener.prototype.openBlockTag = function(tag, attributes) {
};

WYMeditor.XhtmlSaxListener.prototype.inlineTag = function(tag, attributes) {
this._last_node_was_text = false;
if (this._insideTagToRemove || this._shouldRemoveTag(tag, attributes)) {
// If we're currently in a block marked for removal or if this tag is
// marked for removal, don't add it to the output.
return;
}
this._last_node_was_text = false;

attributes = this.validator.getValidTagAttributes(tag, attributes);
attributes = this.removeUnwantedClasses(attributes);
Expand Down Expand Up @@ -592,21 +595,58 @@ WYMeditor.XhtmlSaxListener.prototype._getClosingTagContent = function(position,
================
Specifies if the passed tag with the passed attributes should be removed
from the output or not. This is determined by whether the tag has the class
WYMeditor.EDITOR_ONLY_CLASS or not. If the tag should be removed, the
function returns true. Otherwise, the function returns false.
from the output or not, based on the current state.
*/
WYMeditor.XhtmlSaxListener.prototype._shouldRemoveTag = function(tag, attributes) {
if (this._isEditorOnlyTag(tag, attributes)) {
return true;
}
if (this._isRootInlineTagToRemove(tag, attributes, this._tag_stack)) {
return true;
}

return false;
};

/*
_isEditorOnlyTag
================
Is the passed-in tag, as evaluated in the current state, a tag that should
only exist in the editor, but not in the final output. Editor-only tags
exist to aid with manipulation and browser-bug workarounds, but aren't
actual content that should be kept in the authoritative HTML.
*/
WYMeditor.XhtmlSaxListener.prototype._isEditorOnlyTag = function(tag, attributes) {
var classes;

if (!attributes["class"]) {
return false;
}

classes = attributes["class"].split(" ");
if (jQuery.inArray(WYMeditor.EDITOR_ONLY_CLASS, classes) > -1) {
if (WYMeditor.Helper.contains(classes, WYMeditor.EDITOR_ONLY_CLASS)) {
return true;
}
return false;
};

/*
Is this tag one of the tags that should be removed if found at the root.
*/
WYMeditor.XhtmlSaxListener.prototype._isRootInlineTagToRemove = function(
tag, attributes, currentTagStack
) {
if (!this.isInlineTag(tag)) {
return false;
}
if (currentTagStack.length > 0) {
// We're not at the root
return false;
}

if (WYMeditor.Helper.contains(this._rootInlineTagsToRemove, tag)) {
return true;
}
return false;
};

0 comments on commit e140771

Please sign in to comment.