Skip to content

Commit

Permalink
All tests are green
Browse files Browse the repository at this point in the history
  • Loading branch information
xdan committed Feb 1, 2020
1 parent 18f31a5 commit e8381b2
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 60 deletions.
4 changes: 2 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export const SPACE_REG_EXP_START = /^[\s\n\t\r\uFEFF\u200b]+/g;
export const SPACE_REG_EXP_END = /[\s\n\t\r\uFEFF\u200b]+$/g;

export const IS_BLOCK = /^(PRE|DIV|P|LI|H[1-6]|BLOCKQUOTE|TD|TH|TABLE|BODY|HTML|FIGCAPTION|FIGURE|DT|DD)$/i;
export const IS_INLINE = /^(STRONG|SPAN|I|EM|B|SUP|SUB)$/;
export const MAY_BE_REMOVED_WITH_KEY = /^(IMG|BR|IFRAME|SCRIPT|INPUT|TEXTAREA|HR|JODIT|JODIT-MEDIA)$/;
export const IS_INLINE = /^(STRONG|SPAN|I|EM|B|SUP|SUB)$/i;
export const MAY_BE_REMOVED_WITH_KEY = /^(IMG|BR|IFRAME|SCRIPT|INPUT|TEXTAREA|HR|JODIT|JODIT-MEDIA)$/i;

export const KEY_BACKSPACE = 8;
export const KEY_TAB = 9;
Expand Down
20 changes: 13 additions & 7 deletions src/modules/Dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,14 @@ export class Dom {
* @param node
*/
static unwrap(node: Node) {
const parent: Node | null = node.parentNode,
el = node;
const parent = node.parentNode;

if (parent) {
while (el.firstChild) {
parent.insertBefore(el.firstChild, el);
while (node.firstChild) {
parent.insertBefore(node.firstChild, node);
}

Dom.safeRemove(el);
Dom.safeRemove(node);
}
}

Expand All @@ -154,11 +153,18 @@ export class Dom {

if (node) {
while (node) {
if (callback(node) === false || !Dom.each(node, callback)) {
const next = Dom.next(node, Boolean, elm);

if (callback(node) === false) {
return false;
}

// inside callback - node could be removed
if (node.parentNode && !Dom.each(node, callback)) {
return false;
}

node = Dom.next(node, Boolean, elm);
node = next;
}
}

Expand Down
31 changes: 0 additions & 31 deletions src/modules/helpers/html/clear.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/modules/helpers/html/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@

export * from './applyStyles';
export * from './cleanFromWord';
export * from './clear';
export * from './htmlspecialchars';
export * from './stripTags';
57 changes: 38 additions & 19 deletions src/plugins/clean-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import {
INVISIBLE_SPACE,
INVISIBLE_SPACE_REG_EXP,
INVISIBLE_SPACE_REG_EXP as INV_REG,
SPACE_REG_EXP
SPACE_REG_EXP,
IS_INLINE
} from '../constants';
import { IS_INLINE } from '../constants';
import { Dom } from '../modules/Dom';
import { $$, normalizeNode, trim } from '../modules/helpers/';
import { normalizeNode, trim } from '../modules/helpers/';
import { IDictionary, IJodit } from '../types';
import { Plugin } from '../modules/Plugin';

Expand Down Expand Up @@ -111,6 +111,7 @@ export class cleanHtml extends Plugin {
)
)
.on('keyup.cleanHtml', this.onKeyUpCleanUp)
.on('beforeCommand.cleanHtml', this.beforeCommand)
.on('afterCommand.cleanHtml', this.afterCommand);
}

Expand Down Expand Up @@ -338,16 +339,18 @@ export class cleanHtml extends Plugin {
}
};

private beforeCommand = (command: string): void | false => {
if (command.toLowerCase() === 'removeformat') {
this.onRemoveFormat();
return false;
}
};

private afterCommand = (command: string) => {
if (command.toLowerCase() === 'inserthorizontalrule') {
this.onInsertHorizontalLine();
return;
}

if (command.toLowerCase() === 'removeformat') {
this.onRemoveFormat();
return;
}
};

private onInsertHorizontalLine() {
Expand Down Expand Up @@ -403,33 +406,49 @@ export class cleanHtml extends Plugin {

const range = sel.range;

if (parentNode) {
let fragment: DocumentFragment | null = null;
let fragment: DocumentFragment | null = null;

if (!collapsed) {
fragment = range.extractContents();
}
if (!collapsed) {
fragment = range.extractContents();
}

if (parentNode) {
const tmp = this.jodit.create.inside.text(INVISIBLE_SPACE);
range.insertNode(tmp);
const insideParent = Dom.isOrContains(parentNode, tmp, true);
Dom.safeRemove(tmp);
range.collapse(true);

if (insideParent && parentNode.parentNode && parentNode.parentNode !== fragment) {
const second = this.jodit.selection.splitSelection(parentNode as HTMLElement);
if (
insideParent &&
parentNode.parentNode &&
parentNode.parentNode !== fragment
) {
const second = this.jodit.selection.splitSelection(
parentNode as HTMLElement
);
this.jodit.selection.setCursorAfter(second || parentNode);

if (Dom.isEmpty(parentNode)) {
Dom.safeRemove(parentNode);
}
}
if (fragment && fragment.textContent) {
sel.insertHTML(fragment.textContent);
}
}

$$('font', this.jodit.editor).forEach(Dom.unwrap);
if (fragment) {
sel.insertNode(this.cleanFragment(fragment));
}
}

private cleanFragment(fragment: Node): Node {
Dom.each(fragment, node => {
if (Dom.isElement(node) && IS_INLINE.test(node.nodeName)) {
this.cleanFragment(node);
Dom.unwrap(node);
}
});

return fragment;
}

/**
Expand Down

0 comments on commit e8381b2

Please sign in to comment.