Skip to content

Commit

Permalink
Added Dom.isTag method
Browse files Browse the repository at this point in the history
  • Loading branch information
xdan committed Feb 9, 2020
1 parent 351a1f4 commit bb3696d
Show file tree
Hide file tree
Showing 20 changed files with 100 additions and 85 deletions.
17 changes: 8 additions & 9 deletions src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1029,17 +1029,16 @@ Config.prototype.controls = {

if (
current &&
(current.nodeName === 'A' ||
(Dom.isTag(current, 'a') ||
Dom.closest(current, 'A', editor.editor))
) {
sourceAnchor =
current.nodeName === 'A'
? (current as HTMLAnchorElement)
: (Dom.closest(
current,
'A',
editor.editor
) as HTMLAnchorElement);
sourceAnchor = Dom.isTag(current, 'a')
? current
: (Dom.closest(
current,
'A',
editor.editor
) as HTMLAnchorElement);
}

return FileSelectorWidget(
Expand Down
24 changes: 20 additions & 4 deletions src/modules/Dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,24 +329,24 @@ export class Dom {
* Check if element is text node
* @param node
*/
static isText(node: Node | null): node is Text {
static isText(node: Node | null | false): node is Text {
return Boolean(node && node.nodeType === Node.TEXT_NODE);
}

/**
* Check if element is element node
* @param node
*/
static isElement(node: Node | null): node is Element {
return Boolean(node && node.nodeType === Node.ELEMENT_NODE);
static isElement(node: Node | null | false | EventTarget): node is Element {
return Boolean(node && (node as Node).nodeType === Node.ELEMENT_NODE);
}

/**
* Check element is inline block
*
* @param node
*/
static isInlineBlock(node: Node | null): boolean {
static isInlineBlock(node: Node | null | false): boolean {
return (
Dom.isElement(node) &&
['inline', 'inline-block'].indexOf(
Expand Down Expand Up @@ -849,4 +849,20 @@ export class Dom {
node.style.display = display || '';
}
}

/**
* Check if element is some tag
*
* @param node
* @param tag
*/
static isTag<K extends keyof HTMLElementTagNameMap>(
node: Node | null | false | EventTarget,
tag: K
): node is HTMLElementTagNameMap[K] {
return (
Dom.isElement(node) &&
node.tagName.toLowerCase() === tag.toLowerCase()
);
}
}
18 changes: 10 additions & 8 deletions src/modules/Selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export class Select {
isMarker = (elm: Node): boolean =>
Dom.isNode(elm, this.win) &&
Dom.isElement(elm) &&
elm.nodeName === 'SPAN' &&
Dom.isTag(elm, 'span') &&
(elm as Element).hasAttribute('data-' + consts.MARKER_CLASS);

/**
Expand Down Expand Up @@ -748,7 +748,10 @@ export class Select {
}

// checks parentElement as well because partial selections are not equal to entire element
return node === end || (node && node.contains && node.contains(end));
return (
node === end ||
(node && node.contains && node.contains(end))
);
},
this.area,
true,
Expand All @@ -765,7 +768,7 @@ export class Select {
return Array.from(current.childNodes).forEach(forEvery);
}

if (current.nodeName === 'LI') {
if (Dom.isTag(current, 'li')) {
if (current.firstChild) {
current = current.firstChild;
} else {
Expand Down Expand Up @@ -857,7 +860,7 @@ export class Select {
const container = start ? range.startContainer : range.endContainer;
const offset = start ? range.startOffset : range.endOffset;
const check = (elm: Node | null) =>
elm && elm.nodeName !== 'BR' && !Dom.isEmptyTextNode(elm);
elm && !Dom.isTag(elm, 'br') && !Dom.isEmptyTextNode(elm);

// check right offset
if (Dom.isText(container)) {
Expand Down Expand Up @@ -1157,8 +1160,7 @@ export class Select {
) {
const WRAP = 1,
UNWRAP = 0,
defaultTag = 'SPAN',
FONT = 'FONT';
defaultTag = 'SPAN';

let mode: number;

Expand All @@ -1169,7 +1171,7 @@ export class Select {

const checkCssRulesFor = (elm: HTMLElement): boolean => {
return (
elm.nodeName !== FONT &&
!Dom.isTag(elm, 'font') &&
Dom.isElement(elm) &&
((isPlainObject(options) &&
each(
Expand Down Expand Up @@ -1454,7 +1456,7 @@ export class Select {

if (
next &&
(next.nodeName === 'BR' || Dom.isEmptyTextNode(next))
(Dom.isTag(next, 'br') || Dom.isEmptyTextNode(next))
) {
Dom.safeRemove(next);
} else {
Expand Down
3 changes: 1 addition & 2 deletions src/modules/Table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,7 @@ export class Table {

const nextRow: HTMLTableRowElement = Dom.next(
cell.parentNode,
(elm: Node | null) =>
Dom.isElement(elm) && elm.nodeName === 'TR',
(elm: Node | null) => Dom.isTag(elm, 'tr'),
table
) as HTMLTableRowElement;

Expand Down
5 changes: 2 additions & 3 deletions src/modules/Widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ export namespace Widget {
if (
elm &&
!Dom.isText(elm) &&
(elm.tagName === 'IMG' || $$('img', elm).length)
(Dom.isTag(elm, 'img') || $$('img', elm).length)
) {
currentImage = elm.tagName === 'IMG' ? elm : $$('img', elm)[0];
val(form, 'input[name=url]', currentImage.getAttribute('src'));
Expand All @@ -482,8 +482,7 @@ export namespace Widget {

if (
elm &&
!Dom.isText(elm) &&
elm.nodeName === 'A'
Dom.isTag(elm, 'a')
) {
val(form, 'input[name=url]', elm.getAttribute('href') || '');
val(form, 'input[name=text]', elm.getAttribute('title') || '');
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/clean-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ export class cleanHtml extends Plugin {
this.cleanNode(child, onlyRemoveFont);
});

if (elm.nodeName === 'FONT') {
if (Dom.isTag(elm, 'font')) {
Dom.unwrap(elm);
} else if (!onlyRemoveFont) {
// clean some "style" attributes in selected range
Expand Down Expand Up @@ -519,7 +519,7 @@ export class cleanHtml extends Plugin {
// remove extra br
if (
current &&
node.nodeName === 'BR' &&
Dom.isTag(node, 'br') &&
cleanHtml.hasNotEmptyTextSibling(node) &&
!cleanHtml.hasNotEmptyTextSibling(node, true) &&
Dom.up(
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/clipboard/copyformat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Config.prototype.controls.copyformat = {
| false = editor.selection.current();

if (currentNode) {
if (currentNode.nodeName === 'IMG') {
if (Dom.isTag(currentNode, 'img')) {
css(currentNode as HTMLElement, format);
} else {
editor.selection.applyCSS(format);
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/clipboard/paste-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export class pasteStorage extends Plugin {
'click dblclick',
(e: MouseEvent) => {
const a: HTMLAnchorElement | null = e.target as HTMLAnchorElement;
if (a && a.nodeName === 'A' && a.hasAttribute('data-index')) {
if (Dom.isTag(a, 'a') && a.hasAttribute('data-index')) {
this.selectIndex(
parseInt(a.getAttribute('data-index') || '0', 10)
);
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/drag-and-drop-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export class DragAndDropElement extends Plugin {

this.jodit.selection.insertNode(fragment, true, false);

if (fragment.nodeName === 'IMG' && this.jodit.events) {
if (Dom.isTag(fragment, 'img') && this.jodit.events) {
this.jodit.events.fire('afterInsertImage', fragment);
}

Expand Down
6 changes: 4 additions & 2 deletions src/plugins/drag-and-drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,15 @@ export class DragAndDrop extends Plugin {

if (fragment) {
this.jodit.selection.insertNode(fragment, false, false);

if (range && fragment.firstChild && fragment.lastChild) {
range.setStartBefore(fragment.firstChild);
range.setEndAfter(fragment.lastChild);
this.jodit.selection.selectRange(range);
this.jodit.events.fire('synchro');
}
if (fragment.nodeName === 'IMG' && this.jodit.events) {

if (Dom.isTag(fragment, 'img') && this.jodit.events) {
this.jodit.events.fire('afterInsertImage', fragment);
}
}
Expand Down Expand Up @@ -164,7 +166,7 @@ export class DragAndDrop extends Plugin {
target = target.querySelector('img') as HTMLElement;
}

if (target.nodeName === 'IMG') {
if (Dom.isTag(target, 'img')) {
this.draggable = target.cloneNode(true) as HTMLElement;

dataBind(this.draggable, 'target', target);
Expand Down
10 changes: 5 additions & 5 deletions src/plugins/enter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ export class enter extends Plugin {

let currentBox = this.getBlockWrapper(current);

const isLi = currentBox && currentBox.nodeName === 'LI';
const isLi = Dom.isTag(currentBox, 'li');

// if use <br> defaultTag for break line or when was entered SHIFt key or in <td> or <th> or <blockquote>
if (!isLi && this.checkBR(current, event.shiftKey) === false) {
Expand Down Expand Up @@ -207,8 +207,8 @@ export class enter extends Plugin {
}

if (tagReg.test(node.nodeName)) {
if (node.nodeName === 'LI') {
return node as HTMLLIElement;
if (Dom.isTag(node, 'li')) {
return node;
}

return (
Expand Down Expand Up @@ -313,7 +313,7 @@ export class enter extends Plugin {
if (
!Dom.prev(
currentBox,
(elm: Node | null) => elm && elm.nodeName === 'LI',
(elm: Node | null) => Dom.isTag(elm, 'li'),
ul
)
) {
Expand All @@ -322,7 +322,7 @@ export class enter extends Plugin {
} else if (
!Dom.next(
currentBox,
(elm: Node | null) => elm && elm.nodeName === 'LI',
(elm: Node | null) => Dom.isTag(elm, 'li'),
ul
)
) {
Expand Down
7 changes: 3 additions & 4 deletions src/plugins/format-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Config.prototype.controls.paragraph = {
currentBpx &&
currentBpx !== editor.editor &&
control.list !== undefined &&
currentBpx.nodeName.toLowerCase() !== 'p' &&
!Dom.isTag(currentBpx, 'p') &&
((control.list as any)[
currentBpx.nodeName.toLowerCase()
] as any) !== undefined
Expand Down Expand Up @@ -143,7 +143,7 @@ export function formatBlock(editor: IJodit) {
) as HTMLElement)
: false;

if ((!currentBox || currentBox.nodeName === 'LI') && current) {
if ((!currentBox || Dom.isTag(currentBox, 'li')) && current) {
currentBox = Dom.wrapInline(
current,
editor.options.enter,
Expand All @@ -159,8 +159,7 @@ export function formatBlock(editor: IJodit) {
if (!currentBox.tagName.match(/TD|TH|TBODY|TABLE|THEAD/i)) {
if (
third === editor.options.enterBlock.toLowerCase() &&
currentBox.parentNode &&
currentBox.parentNode.nodeName === 'LI'
Dom.isTag(currentBox.parentNode, 'li')
) {
Dom.unwrap(currentBox);
} else {
Expand Down
25 changes: 11 additions & 14 deletions src/plugins/inline-popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ import { Widget } from '../modules/Widget';
import ColorPickerWidget = Widget.ColorPickerWidget;
import TabsWidget = Widget.TabsWidget;
import { Dom } from '../modules/Dom';
import {
clearCenterAlign,
css,
offset,
splitArray
} from '../modules/helpers/';
import { clearCenterAlign, css, offset, splitArray } from '../modules/helpers/';
import { Plugin } from '../modules/Plugin';
import { Table } from '../modules/Table';
import { Popup } from '../modules/popup/popup';
Expand Down Expand Up @@ -581,14 +576,16 @@ export class inlinePopup extends Plugin {
const elements: string = Object.keys(this.jodit.options.popup).join(
'|'
),
target: HTMLElement | false =
(event.target as Node).nodeName === 'IMG'
? (event.target as HTMLImageElement)
: (Dom.closest(
event.target as Node,
elements,
this.jodit.editor
) as HTMLTableElement);
target: HTMLElement | false = Dom.isTag(
event.target as Node,
'img'
)
? (event.target as HTMLImageElement)
: (Dom.closest(
event.target as Node,
elements,
this.jodit.editor
) as HTMLTableElement);

if (
!target ||
Expand Down
9 changes: 3 additions & 6 deletions src/plugins/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,11 @@ export function link(jodit: IJodit) {

if (command === 'removeFormat') {
node = sel.current();
if (node && node.nodeName !== 'A') {
if (node && !Dom.isTag(node, 'a')) {
node = Dom.closest(node, 'A', jodit.editor);
}
if (node && node.nodeName === 'A') {
if (
(node as HTMLElement).innerHTML ===
(node as HTMLElement).textContent
) {
if (Dom.isTag(node, 'a')) {
if (node.innerHTML === node.textContent) {
newtag = jodit.create.inside.text(
(node as HTMLElement).innerHTML
);
Expand Down
10 changes: 5 additions & 5 deletions src/plugins/orderedlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ export function orderedlist(editor: IJodit) {
editor.editor
);

if (ul && ul.parentNode && ul.parentNode.nodeName === 'P') {
if (ul && Dom.isTag(ul.parentNode, 'p')) {
const selection: markerInfo[] = editor.selection.save();

Dom.unwrap(ul.parentNode);

Array.from(ul.childNodes).forEach((li: Node) => {
if (
Dom.isElement(li.lastChild) &&
li.lastChild.nodeName === 'BR'
) {
if (Dom.isTag(li.lastChild, 'br')) {
Dom.safeRemove(li.lastChild);
}
});

editor.selection.restore(selection);
}
editor.setEditorValue();
Expand Down
Loading

0 comments on commit bb3696d

Please sign in to comment.