Skip to content

Commit

Permalink
Update multiple style removal check
Browse files Browse the repository at this point in the history
Fix new style creation error
  • Loading branch information
Oliver Pulges committed Nov 11, 2013
1 parent 7e9b0eb commit 8be6f6b
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions src/selection/html_applier.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
function addStyle(el, cssStyle, regExp) {
if (el.getAttribute('style')) {
removeStyle(el, regExp);
if (el.getAttribute('style') && !/\s+/.test(el.getAttribute('style'))) {
if (el.getAttribute('style') && !(/\s+/).test(el.getAttribute('style'))) {
el.setAttribute('style', cssStyle + ";" + el.getAttribute('style'));
} else {

Expand Down Expand Up @@ -63,7 +63,7 @@
if (el.getAttribute('style')) {
s = el.getAttribute('style').split(';');
for (var i = s.length; i--;) {
if (!s[i].match(regExp) && !/\s/.test(s[i])) {
if (!s[i].match(regExp) && !(/\s/).test(s[i])) {
s2.push(s[i]);
}
}
Expand All @@ -75,13 +75,40 @@
}
}

function removeOrChangeStyle(el, style, regExp) {
var exactRegex = new RegExp("(^|\\s|;)" + style.replace(/\s/gi, '').replace(/([\(\)])/gi, "\\$1").toLowerCase().replace(";", ";?"), "gi"),
function getMatchingStyleRegexp(el, style) {
var regexes = [],
sSplit = style.split(';'),
elStyle = el.getAttribute('style');

if (elStyle) {
elStyle = elStyle.replace(/\s/gi, '').toLowerCase();
regexes.push(new RegExp("(^|\\s|;)" + style.replace(/\s/gi, '').replace(/([\(\)])/gi, "\\$1").toLowerCase().replace(";", ";?"), "gi"));

for (var i = sSplit.length; i-- > 0;) {
if (!(/^\s*$/).test(sSplit[i])) {
regexes.push(new RegExp("(^|\\s|;)" + sSplit[i].replace(/\s/gi, '').replace(/([\(\)])/gi, "\\$1").toLowerCase().replace(";", ";?"), "gi"));
}
}
for (var j = 0, jmax = regexes.length; j < jmax; j++) {
if (elStyle.match(regexes[j])) {
return regexes[j];
}
}
}

return false;
};

function removeOrChangeStyle(el, style, regExp) {

var exactRegex = getMatchingStyleRegexp(el, style);

/*new RegExp("(^|\\s|;)" + style.replace(/\s/gi, '').replace(/([\(\)])/gi, "\\$1").toLowerCase().replace(";", ";?"), "gi"),
elStyle = el.getAttribute('style');*/

if (elStyle && exactRegex.test(elStyle.replace(/\s/gi, '').toLowerCase())) {
if (exactRegex) {
// adding same style value on property again removes style
removeStyle(el, regExp);
removeStyle(el, exactRegex);
return "remove";
} else {
// adding new style value changes value
Expand Down Expand Up @@ -385,12 +412,12 @@

applyToRange: function(range) {
var textNodes;

for (var ri = range.length; ri--;) {
textNodes = range[ri].getNodes([wysihtml5.TEXT_NODE]);

if (!textNodes.length) {
try {
var node = this.createContainer(range.endContainer.ownerDocument);
var node = this.createContainer(range[ri].endContainer.ownerDocument);
range[ri].surroundContents(node);
this.selectNode(range[ri], node);
return;
Expand All @@ -399,7 +426,6 @@

range[ri].splitBoundaries();
textNodes = range[ri].getNodes([wysihtml5.TEXT_NODE]);

if (textNodes.length) {
var textNode;

Expand Down

0 comments on commit 8be6f6b

Please sign in to comment.