Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function triggerKeyCommand(editor, string, modifiers=[]) {
if (typeof modifiers === "number") {
modifiers = [modifiers]; // convert singular to array
}
let charCode = (KEY_CODES[string] || string.toUpperCase().charCodeAt(0));
let keyCode = charCode;
let keyEvent = createMockEvent('keydown', editor.element, {
charCode,
keyCode,
shiftKey: contains(modifiers, MODIFIERS.SHIFT),
metaKey: contains(modifiers, MODIFIERS.META),
ctrlKey: contains(modifiers, MODIFIERS.CTRL)
});
_triggerEditorEvent(editor, keyEvent);
}
parseNode(node) {
if (!this.state.section) {
this._updateStateFromElement(node);
}
let nodeFinished = this.runPlugins(node);
if (nodeFinished) {
return;
}
// handle closing the current section and starting a new one if we hit a
// new-section-creating element.
if (this.state.section && !isTextNode(node) && node.tagName) {
let tagName = normalizeTagName(node.tagName);
let isListSection = contains(VALID_LIST_SECTION_TAGNAMES, tagName);
let isListItem = contains(VALID_LIST_ITEM_TAGNAMES, tagName);
let isMarkupSection = contains(VALID_MARKUP_SECTION_TAGNAMES, tagName);
let isNestedListSection = isListSection && this.state.section.isListItem;
let lastSection = this.sections[this.sections.length - 1];
// we can hit a list item after parsing a nested list, when that happens
// and the lists are of different types we need to make sure we switch
// the list type back
if (isListItem && lastSection && lastSection.isListSection) {
let parentElement = node.parentElement;
let parentElementTagName = normalizeTagName(parentElement.tagName);
if (parentElementTagName !== lastSection.tagName) {
this._closeCurrentSection();
this._updateStateFromElement(parentElement);
}
}
parseNode(node) {
if (!this.state.section) {
this._updateStateFromElement(node);
}
let nodeFinished = this.runPlugins(node);
if (nodeFinished) {
return;
}
// handle closing the current section and starting a new one if we hit a
// new-section-creating element.
if (this.state.section && !isTextNode(node) && node.tagName) {
let tagName = normalizeTagName(node.tagName);
let isListSection = contains(VALID_LIST_SECTION_TAGNAMES, tagName);
let isListItem = contains(VALID_LIST_ITEM_TAGNAMES, tagName);
let isMarkupSection = contains(VALID_MARKUP_SECTION_TAGNAMES, tagName);
let isNestedListSection = isListSection && this.state.section.isListItem;
let lastSection = this.sections[this.sections.length - 1];
// we can hit a list item after parsing a nested list, when that happens
// and the lists are of different types we need to make sure we switch
// the list type back
if (isListItem && lastSection && lastSection.isListSection) {
let parentElement = node.parentElement;
let parentElementTagName = normalizeTagName(parentElement.tagName);
if (parentElementTagName !== lastSection.tagName) {
this._closeCurrentSection();
this._updateStateFromElement(parentElement);
}
}
if (!this.state.section) {
this._updateStateFromElement(node);
}
let nodeFinished = this.runPlugins(node);
if (nodeFinished) {
return;
}
// handle closing the current section and starting a new one if we hit a
// new-section-creating element.
if (this.state.section && !isTextNode(node) && node.tagName) {
let tagName = normalizeTagName(node.tagName);
let isListSection = contains(VALID_LIST_SECTION_TAGNAMES, tagName);
let isListItem = contains(VALID_LIST_ITEM_TAGNAMES, tagName);
let isMarkupSection = contains(VALID_MARKUP_SECTION_TAGNAMES, tagName);
let isNestedListSection = isListSection && this.state.section.isListItem;
let lastSection = this.sections[this.sections.length - 1];
// we can hit a list item after parsing a nested list, when that happens
// and the lists are of different types we need to make sure we switch
// the list type back
if (isListItem && lastSection && lastSection.isListSection) {
let parentElement = node.parentElement;
let parentElementTagName = normalizeTagName(parentElement.tagName);
if (parentElementTagName !== lastSection.tagName) {
this._closeCurrentSection();
this._updateStateFromElement(parentElement);
}
}
// if we've broken out of a list due to nested section-level elements we
parseNode(node) {
if (!this.state.section) {
this._updateStateFromElement(node);
}
let nodeFinished = this.runPlugins(node);
if (nodeFinished) {
return;
}
// handle closing the current section and starting a new one if we hit a
// new-section-creating element.
if (this.state.section && !isTextNode(node) && node.tagName) {
let tagName = normalizeTagName(node.tagName);
let isListSection = contains(VALID_LIST_SECTION_TAGNAMES, tagName);
let isListItem = contains(VALID_LIST_ITEM_TAGNAMES, tagName);
let isMarkupSection = contains(VALID_MARKUP_SECTION_TAGNAMES, tagName);
let isNestedListSection = isListSection && this.state.section.isListItem;
let lastSection = this.sections[this.sections.length - 1];
// we can hit a list item after parsing a nested list, when that happens
// and the lists are of different types we need to make sure we switch
// the list type back
if (isListItem && lastSection && lastSection.isListSection) {
let parentElement = node.parentElement;
let parentElementTagName = normalizeTagName(parentElement.tagName);
if (parentElementTagName !== lastSection.tagName) {
this._closeCurrentSection();
this._updateStateFromElement(parentElement);
}
}
if (!this.state.section) {
this._updateStateFromElement(node);
}
let nodeFinished = this.runPlugins(node);
if (nodeFinished) {
return;
}
// handle closing the current section and starting a new one if we hit a
// new-section-creating element.
if (this.state.section && !isTextNode(node) && node.tagName) {
let tagName = normalizeTagName(node.tagName);
let isListSection = contains(VALID_LIST_SECTION_TAGNAMES, tagName);
let isListItem = contains(VALID_LIST_ITEM_TAGNAMES, tagName);
let isMarkupSection = contains(VALID_MARKUP_SECTION_TAGNAMES, tagName);
let isNestedListSection = isListSection && this.state.section.isListItem;
let lastSection = this.sections[this.sections.length - 1];
// we can hit a list item after parsing a nested list, when that happens
// and the lists are of different types we need to make sure we switch
// the list type back
if (isListItem && lastSection && lastSection.isListSection) {
let parentElement = node.parentElement;
let parentElementTagName = normalizeTagName(parentElement.tagName);
if (parentElementTagName !== lastSection.tagName) {
this._closeCurrentSection();
this._updateStateFromElement(parentElement);
}
}
// if we've broken out of a list due to nested section-level elements we
_isSkippable(element) {
return element.nodeType === NODE_TYPES.ELEMENT &&
contains(SKIPPABLE_ELEMENT_TAG_NAMES,
normalizeTagName(element.tagName));
}
}
_getSectionDetails(element) {
let sectionType,
tagName,
inferredTagName = false;
if (isTextNode(element)) {
tagName = DEFAULT_TAG_NAME;
sectionType = MARKUP_SECTION_TYPE;
inferredTagName = true;
} else {
tagName = normalizeTagName(element.tagName);
if (contains(VALID_LIST_SECTION_TAGNAMES, tagName)) {
sectionType = LIST_SECTION_TYPE;
} else if (contains(VALID_LIST_ITEM_TAGNAMES, tagName)) {
sectionType = LIST_ITEM_TYPE;
} else if (contains(VALID_MARKUP_SECTION_TAGNAMES, tagName)) {
sectionType = MARKUP_SECTION_TYPE;
} else {
sectionType = MARKUP_SECTION_TYPE;
tagName = DEFAULT_TAG_NAME;
inferredTagName = true;
}
}
return {sectionType, tagName, inferredTagName};
}
_getSectionDetails(element) {
let sectionType,
tagName,
inferredTagName = false;
if (isTextNode(element)) {
tagName = DEFAULT_TAG_NAME;
sectionType = MARKUP_SECTION_TYPE;
inferredTagName = true;
} else {
tagName = normalizeTagName(element.tagName);
if (contains(VALID_LIST_SECTION_TAGNAMES, tagName)) {
sectionType = LIST_SECTION_TYPE;
} else if (contains(VALID_LIST_ITEM_TAGNAMES, tagName)) {
sectionType = LIST_ITEM_TYPE;
} else if (contains(VALID_MARKUP_SECTION_TAGNAMES, tagName)) {
sectionType = MARKUP_SECTION_TYPE;
} else {
sectionType = MARKUP_SECTION_TYPE;
tagName = DEFAULT_TAG_NAME;
inferredTagName = true;
}
}
return {sectionType, tagName, inferredTagName};
}