From 8123ae9bc69f456c60046d47353bc4c368a95cee Mon Sep 17 00:00:00 2001 From: Nihat Ozer Date: Sun, 10 Jun 2018 17:18:09 +0300 Subject: [PATCH] Added type definitions --- .../QuillDeltaToHtmlConverter.bundle.js | 100 ++++++++++-------- dist/commonjs/DeltaInsertOp.d.ts | 30 ++++++ dist/commonjs/InsertData.d.ts | 13 +++ dist/commonjs/InsertOpDenormalizer.d.ts | 6 ++ dist/commonjs/InsertOpsConverter.d.ts | 7 ++ dist/commonjs/OpAttributeSanitizer.d.ts | 37 +++++++ dist/commonjs/OpToHtmlConverter.d.ts | 34 ++++++ dist/commonjs/QuillDeltaToHtmlConverter.d.ts | 39 +++++++ dist/commonjs/QuillDeltaToHtmlConverter.js | 38 +++---- dist/commonjs/extensions/Array.d.ts | 11 ++ dist/commonjs/extensions/Object.d.ts | 3 + dist/commonjs/extensions/String.d.ts | 4 + dist/commonjs/funcs-html.d.ts | 10 ++ dist/commonjs/grouper/Grouper.d.ts | 16 +++ dist/commonjs/grouper/ListNester.d.ts | 10 ++ dist/commonjs/grouper/group-types.d.ts | 25 +++++ dist/commonjs/main.d.ts | 2 + dist/commonjs/mentions/MentionSanitizer.d.ts | 18 ++++ dist/commonjs/value-types.d.ts | 30 ++++++ dist/commonjs/value-types.js | 62 ++++++----- package.json | 12 ++- src/QuillDeltaToHtmlConverter.ts | 42 ++++---- src/value-types.ts | 48 ++++----- test/QuillDeltaToHtmlConverter.test.ts | 40 +++---- tsconfig.json | 3 +- 25 files changed, 474 insertions(+), 166 deletions(-) create mode 100644 dist/commonjs/DeltaInsertOp.d.ts create mode 100644 dist/commonjs/InsertData.d.ts create mode 100644 dist/commonjs/InsertOpDenormalizer.d.ts create mode 100644 dist/commonjs/InsertOpsConverter.d.ts create mode 100644 dist/commonjs/OpAttributeSanitizer.d.ts create mode 100644 dist/commonjs/OpToHtmlConverter.d.ts create mode 100644 dist/commonjs/QuillDeltaToHtmlConverter.d.ts create mode 100644 dist/commonjs/extensions/Array.d.ts create mode 100644 dist/commonjs/extensions/Object.d.ts create mode 100644 dist/commonjs/extensions/String.d.ts create mode 100644 dist/commonjs/funcs-html.d.ts create mode 100644 dist/commonjs/grouper/Grouper.d.ts create mode 100644 dist/commonjs/grouper/ListNester.d.ts create mode 100644 dist/commonjs/grouper/group-types.d.ts create mode 100644 dist/commonjs/main.d.ts create mode 100644 dist/commonjs/mentions/MentionSanitizer.d.ts create mode 100644 dist/commonjs/value-types.d.ts diff --git a/dist/browser/QuillDeltaToHtmlConverter.bundle.js b/dist/browser/QuillDeltaToHtmlConverter.bundle.js index 4577790..486553f 100644 --- a/dist/browser/QuillDeltaToHtmlConverter.bundle.js +++ b/dist/browser/QuillDeltaToHtmlConverter.bundle.js @@ -514,7 +514,7 @@ var QuillDeltaToHtmlConverter = (function () { }; this.rawDeltaOps = deltaOps; } - QuillDeltaToHtmlConverter.prototype.getListTag = function (op) { + QuillDeltaToHtmlConverter.prototype._getListTag = function (op) { return op.isOrderedList() ? this.options.orderedListTag + '' : op.isBulletList() ? this.options.bulletListTag + '' : ''; @@ -536,28 +536,28 @@ var QuillDeltaToHtmlConverter = (function () { return this.getGroupedOps() .map(function (group) { if (group instanceof group_types_1.ListGroup) { - return _this.renderWithCallbacks(value_types_1.GroupType.List, group, function () { return _this.renderList(group); }); + return _this._renderWithCallbacks(value_types_1.GroupType.List, group, function () { return _this._renderList(group); }); } else if (group instanceof group_types_1.BlockGroup) { var g = group; - return _this.renderWithCallbacks(value_types_1.GroupType.Block, group, function () { return _this.renderBlock(g.op, g.ops); }); + return _this._renderWithCallbacks(value_types_1.GroupType.Block, group, function () { return _this._renderBlock(g.op, g.ops); }); } else if (group instanceof group_types_1.VideoItem) { - return _this.renderWithCallbacks(value_types_1.GroupType.Video, group, function () { + return _this._renderWithCallbacks(value_types_1.GroupType.Video, group, function () { var g = group; var converter = new OpToHtmlConverter_1.OpToHtmlConverter(g.op, _this.converterOptions); return converter.getHtml(); }); } else { - return _this.renderWithCallbacks(value_types_1.GroupType.InlineGroup, group, function () { - return _this.renderInlines(group.ops); + return _this._renderWithCallbacks(value_types_1.GroupType.InlineGroup, group, function () { + return _this._renderInlines(group.ops); }); } }) .join(""); }; - QuillDeltaToHtmlConverter.prototype.renderWithCallbacks = function (groupType, group, myRenderFn) { + QuillDeltaToHtmlConverter.prototype._renderWithCallbacks = function (groupType, group, myRenderFn) { var html = ''; var beforeCb = this.callbacks['beforeRender_cb']; html = typeof beforeCb === 'function' ? beforeCb.apply(null, [groupType, group]) : ''; @@ -568,39 +568,39 @@ var QuillDeltaToHtmlConverter = (function () { html = typeof afterCb === 'function' ? afterCb.apply(null, [groupType, html]) : html; return html; }; - QuillDeltaToHtmlConverter.prototype.renderList = function (list, isOuterMost) { + QuillDeltaToHtmlConverter.prototype._renderList = function (list, isOuterMost) { var _this = this; if (isOuterMost === void 0) { isOuterMost = true; } var firstItem = list.items[0]; - return funcs_html_1.makeStartTag(this.getListTag(firstItem.item.op)) - + list.items.map(function (li) { return _this.renderListItem(li, isOuterMost); }).join('') - + funcs_html_1.makeEndTag(this.getListTag(firstItem.item.op)); + return funcs_html_1.makeStartTag(this._getListTag(firstItem.item.op)) + + list.items.map(function (li) { return _this._renderListItem(li, isOuterMost); }).join('') + + funcs_html_1.makeEndTag(this._getListTag(firstItem.item.op)); }; - QuillDeltaToHtmlConverter.prototype.renderListItem = function (li, isOuterMost) { + QuillDeltaToHtmlConverter.prototype._renderListItem = function (li, isOuterMost) { var converterOptions = Object._assign({}, this.converterOptions); li.item.op.attributes.indent = 0; var converter = new OpToHtmlConverter_1.OpToHtmlConverter(li.item.op, this.converterOptions); var parts = converter.getHtmlParts(); - var liElementsHtml = this.renderInlines(li.item.ops, false); + var liElementsHtml = this._renderInlines(li.item.ops, false); return parts.openingTag + (liElementsHtml) + - (li.innerList ? this.renderList(li.innerList, false) : '') + (li.innerList ? this._renderList(li.innerList, false) : '') + parts.closingTag; }; - QuillDeltaToHtmlConverter.prototype.renderBlock = function (bop, ops) { + QuillDeltaToHtmlConverter.prototype._renderBlock = function (bop, ops) { var _this = this; var converter = new OpToHtmlConverter_1.OpToHtmlConverter(bop, this.converterOptions); var htmlParts = converter.getHtmlParts(); if (bop.isCodeBlock()) { return htmlParts.openingTag + funcs_html_1.encodeHtml(ops.map(function (iop) { - return iop.isCustom() ? _this.renderCustom(iop, bop) : iop.insert.value; + return iop.isCustom() ? _this._renderCustom(iop, bop) : iop.insert.value; }).join("")) + htmlParts.closingTag; } var inlines = ops.map(function (op) { return _this._renderInline(op, bop); }).join(''); return htmlParts.openingTag + (inlines || BrTag) + htmlParts.closingTag; }; - QuillDeltaToHtmlConverter.prototype.renderInlines = function (ops, wrapInParagraphTag) { + QuillDeltaToHtmlConverter.prototype._renderInlines = function (ops, wrapInParagraphTag) { var _this = this; if (wrapInParagraphTag === void 0) { wrapInParagraphTag = true; } var opsLen = ops.length - 1; @@ -618,12 +618,12 @@ var QuillDeltaToHtmlConverter = (function () { }; QuillDeltaToHtmlConverter.prototype._renderInline = function (op, contextOp) { if (op.isCustom()) { - return this.renderCustom(op, contextOp); + return this._renderCustom(op, contextOp); } var converter = new OpToHtmlConverter_1.OpToHtmlConverter(op, this.converterOptions); return converter.getHtml().replace(/\n/g, BrTag); }; - QuillDeltaToHtmlConverter.prototype.renderCustom = function (op, contextOp) { + QuillDeltaToHtmlConverter.prototype._renderCustom = function (op, contextOp) { var renderCb = this.callbacks['renderCustomOp_cb']; if (typeof renderCb === 'function') { return renderCb.apply(null, [op, contextOp]); @@ -1104,39 +1104,47 @@ exports.MentionSanitizer = MentionSanitizer; Object.defineProperty(exports, "__esModule", { value: true }); var NewLine = "\n"; exports.NewLine = NewLine; -var ListType = { - Ordered: 'ordered', - Bullet: 'bullet' -}; +var ListType; +(function (ListType) { + ListType["Ordered"] = "ordered"; + ListType["Bullet"] = "bullet"; +})(ListType || (ListType = {})); exports.ListType = ListType; -var ScriptType = { - Sub: "sub", - Super: "super" -}; +var ScriptType; +(function (ScriptType) { + ScriptType["Sub"] = "sub"; + ScriptType["Super"] = "super"; +})(ScriptType || (ScriptType = {})); exports.ScriptType = ScriptType; -var DirectionType = { - Rtl: "rtl" -}; +var DirectionType; +(function (DirectionType) { + DirectionType["Rtl"] = "rtl"; +})(DirectionType || (DirectionType = {})); exports.DirectionType = DirectionType; -var AlignType = { - Center: "center", - Right: "right" -}; +var AlignType; +(function (AlignType) { + AlignType["Center"] = "center"; + AlignType["Right"] = "right"; +})(AlignType || (AlignType = {})); exports.AlignType = AlignType; -var DataType = { - Image: "image", - Video: "video", - Formula: "formula", - Text: "text" -}; +var DataType; +(function (DataType) { + DataType["Image"] = "image"; + DataType["Video"] = "video"; + DataType["Formula"] = "formula"; + DataType["Text"] = "text"; +})(DataType || (DataType = {})); exports.DataType = DataType; -var GroupType = { - Block: 'block', - InlineGroup: 'inline-group', - List: 'list', - Video: 'video' -}; +; +var GroupType; +(function (GroupType) { + GroupType["Block"] = "block"; + GroupType["InlineGroup"] = "inline-group"; + GroupType["List"] = "list"; + GroupType["Video"] = "video"; +})(GroupType || (GroupType = {})); exports.GroupType = GroupType; +; },{}]},{},[7])(7) });; window.QuillDeltaToHtmlConverter = window.QuillDeltaToHtmlConverter.QuillDeltaToHtmlConverter; diff --git a/dist/commonjs/DeltaInsertOp.d.ts b/dist/commonjs/DeltaInsertOp.d.ts new file mode 100644 index 0000000..c305fbf --- /dev/null +++ b/dist/commonjs/DeltaInsertOp.d.ts @@ -0,0 +1,30 @@ +import { IOpAttributes } from "./OpAttributeSanitizer"; +import { InsertData } from './InsertData'; +declare class DeltaInsertOp { + readonly insert: InsertData; + readonly attributes: IOpAttributes; + constructor(insertVal: InsertData | string, attributes?: IOpAttributes); + static createNewLineOp(): DeltaInsertOp; + isContainerBlock(): boolean; + isBlockquote(): boolean; + isHeader(): boolean; + isSameHeaderAs(op: DeltaInsertOp): boolean; + hasSameAdiAs(op: DeltaInsertOp): boolean; + hasSameIndentationAs(op: DeltaInsertOp): boolean; + hasHigherIndentThan(op: DeltaInsertOp): boolean; + isInline(): boolean; + isCodeBlock(): boolean; + isJustNewline(): boolean; + isList(): boolean; + isOrderedList(): boolean; + isBulletList(): boolean; + isSameListAs(op: DeltaInsertOp): boolean; + isText(): boolean; + isImage(): boolean; + isFormula(): boolean; + isVideo(): boolean; + isLink(): boolean; + isCustom(): boolean; + isMentions(): boolean; +} +export { DeltaInsertOp }; diff --git a/dist/commonjs/InsertData.d.ts b/dist/commonjs/InsertData.d.ts new file mode 100644 index 0000000..4875691 --- /dev/null +++ b/dist/commonjs/InsertData.d.ts @@ -0,0 +1,13 @@ +import { DataType } from './value-types'; +declare class InsertDataQuill { + readonly type: DataType; + readonly value: string; + constructor(type: DataType, value: string); +} +declare class InsertDataCustom { + readonly type: string; + readonly value: any; + constructor(type: string, value: any); +} +declare type InsertData = InsertDataCustom | InsertDataQuill; +export { InsertData, InsertDataCustom, InsertDataQuill }; diff --git a/dist/commonjs/InsertOpDenormalizer.d.ts b/dist/commonjs/InsertOpDenormalizer.d.ts new file mode 100644 index 0000000..343a566 --- /dev/null +++ b/dist/commonjs/InsertOpDenormalizer.d.ts @@ -0,0 +1,6 @@ +import './extensions/String'; +import './extensions/Object'; +declare class InsertOpDenormalizer { + static denormalize(op: any): any[]; +} +export { InsertOpDenormalizer }; diff --git a/dist/commonjs/InsertOpsConverter.d.ts b/dist/commonjs/InsertOpsConverter.d.ts new file mode 100644 index 0000000..96dab18 --- /dev/null +++ b/dist/commonjs/InsertOpsConverter.d.ts @@ -0,0 +1,7 @@ +import { DeltaInsertOp } from './DeltaInsertOp'; +import { InsertData } from './InsertData'; +declare class InsertOpsConverter { + static convert(deltaOps: any[]): DeltaInsertOp[]; + static convertInsertVal(insertPropVal: any): InsertData | null; +} +export { InsertOpsConverter }; diff --git a/dist/commonjs/OpAttributeSanitizer.d.ts b/dist/commonjs/OpAttributeSanitizer.d.ts new file mode 100644 index 0000000..ab5b16b --- /dev/null +++ b/dist/commonjs/OpAttributeSanitizer.d.ts @@ -0,0 +1,37 @@ +import { ListType, AlignType, DirectionType, ScriptType } from './value-types'; +import './extensions/String'; +import { IMention } from "./mentions/MentionSanitizer"; +interface IOpAttributes { + background?: string; + color?: string; + font?: string; + size?: string; + width?: string; + link?: string; + bold?: boolean; + italic?: boolean; + underline?: boolean; + strike?: boolean; + script?: ScriptType; + code?: boolean; + list?: ListType; + blockquote?: boolean; + 'code-block'?: boolean; + header?: number; + align?: AlignType; + direction?: DirectionType; + indent?: number; + mentions?: boolean; + mention?: IMention; + target?: string; +} +declare class OpAttributeSanitizer { + static sanitize(dirtyAttrs: IOpAttributes): IOpAttributes; + static IsValidHexColor(colorStr: string): boolean; + static IsValidColorLiteral(colorStr: string): boolean; + static IsValidFontName(fontName: string): boolean; + static IsValidSize(size: string): boolean; + static IsValidWidth(width: string): boolean; + static isValidTarget(target: string): boolean; +} +export { OpAttributeSanitizer, IOpAttributes }; diff --git a/dist/commonjs/OpToHtmlConverter.d.ts b/dist/commonjs/OpToHtmlConverter.d.ts new file mode 100644 index 0000000..6d758a6 --- /dev/null +++ b/dist/commonjs/OpToHtmlConverter.d.ts @@ -0,0 +1,34 @@ +import { ITagKeyValue } from './funcs-html'; +import { DeltaInsertOp } from './DeltaInsertOp'; +import './extensions/String'; +import './extensions/Object'; +import './extensions/Array'; +interface IOpToHtmlConverterOptions { + classPrefix?: string; + encodeHtml?: boolean; + listItemTag?: string; + paragraphTag?: string; + linkRel?: string; + linkTarget?: string; + allowBackgroundClasses?: boolean; +} +interface IHtmlParts { + openingTag: string; + content: string; + closingTag: string; +} +declare class OpToHtmlConverter { + private options; + private op; + constructor(op: DeltaInsertOp, options?: IOpToHtmlConverterOptions); + prefixClass(className: string): string; + getHtml(): string; + getHtmlParts(): IHtmlParts; + getContent(): string; + getCssClasses(): string[]; + getCssStyles(): string[]; + getTagAttributes(): Array; + getTags(): string[]; + static IsValidRel(relStr: string): boolean; +} +export { OpToHtmlConverter, IOpToHtmlConverterOptions, IHtmlParts }; diff --git a/dist/commonjs/QuillDeltaToHtmlConverter.d.ts b/dist/commonjs/QuillDeltaToHtmlConverter.d.ts new file mode 100644 index 0000000..daf557b --- /dev/null +++ b/dist/commonjs/QuillDeltaToHtmlConverter.d.ts @@ -0,0 +1,39 @@ +import { DeltaInsertOp } from './DeltaInsertOp'; +import { ListGroup, ListItem, TDataGroup } from './grouper/group-types'; +import './extensions/Object'; +import { GroupType } from './value-types'; +interface IQuillDeltaToHtmlConverterOptions { + orderedListTag?: string; + bulletListTag?: string; + listItemTag?: string; + paragraphTag?: string; + classPrefix?: string; + encodeHtml?: boolean; + multiLineBlockquote?: boolean; + multiLineHeader?: boolean; + multiLineCodeblock?: boolean; + linkRel?: string; + linkTarget?: string; + allowBackgroundClasses?: boolean; +} +declare class QuillDeltaToHtmlConverter { + private options; + private rawDeltaOps; + private converterOptions; + private callbacks; + constructor(deltaOps: any[], options?: IQuillDeltaToHtmlConverterOptions); + _getListTag(op: DeltaInsertOp): string; + getGroupedOps(): TDataGroup[]; + convert(): string; + _renderWithCallbacks(groupType: GroupType, group: TDataGroup, myRenderFn: () => string): string; + _renderList(list: ListGroup, isOuterMost?: boolean): string; + _renderListItem(li: ListItem, isOuterMost: boolean): string; + _renderBlock(bop: DeltaInsertOp, ops: DeltaInsertOp[]): string; + _renderInlines(ops: DeltaInsertOp[], wrapInParagraphTag?: boolean): string; + _renderInline(op: DeltaInsertOp, contextOp: DeltaInsertOp): any; + _renderCustom(op: DeltaInsertOp, contextOp: DeltaInsertOp): any; + beforeRender(cb: (group: GroupType, data: TDataGroup) => string): void; + afterRender(cb: (group: GroupType, html: string) => string): void; + renderCustomWith(cb: (op: DeltaInsertOp, contextOp: DeltaInsertOp) => string): void; +} +export { QuillDeltaToHtmlConverter }; diff --git a/dist/commonjs/QuillDeltaToHtmlConverter.js b/dist/commonjs/QuillDeltaToHtmlConverter.js index 0400a45..1528acd 100644 --- a/dist/commonjs/QuillDeltaToHtmlConverter.js +++ b/dist/commonjs/QuillDeltaToHtmlConverter.js @@ -38,7 +38,7 @@ var QuillDeltaToHtmlConverter = (function () { }; this.rawDeltaOps = deltaOps; } - QuillDeltaToHtmlConverter.prototype.getListTag = function (op) { + QuillDeltaToHtmlConverter.prototype._getListTag = function (op) { return op.isOrderedList() ? this.options.orderedListTag + '' : op.isBulletList() ? this.options.bulletListTag + '' : ''; @@ -60,28 +60,28 @@ var QuillDeltaToHtmlConverter = (function () { return this.getGroupedOps() .map(function (group) { if (group instanceof group_types_1.ListGroup) { - return _this.renderWithCallbacks(value_types_1.GroupType.List, group, function () { return _this.renderList(group); }); + return _this._renderWithCallbacks(value_types_1.GroupType.List, group, function () { return _this._renderList(group); }); } else if (group instanceof group_types_1.BlockGroup) { var g = group; - return _this.renderWithCallbacks(value_types_1.GroupType.Block, group, function () { return _this.renderBlock(g.op, g.ops); }); + return _this._renderWithCallbacks(value_types_1.GroupType.Block, group, function () { return _this._renderBlock(g.op, g.ops); }); } else if (group instanceof group_types_1.VideoItem) { - return _this.renderWithCallbacks(value_types_1.GroupType.Video, group, function () { + return _this._renderWithCallbacks(value_types_1.GroupType.Video, group, function () { var g = group; var converter = new OpToHtmlConverter_1.OpToHtmlConverter(g.op, _this.converterOptions); return converter.getHtml(); }); } else { - return _this.renderWithCallbacks(value_types_1.GroupType.InlineGroup, group, function () { - return _this.renderInlines(group.ops); + return _this._renderWithCallbacks(value_types_1.GroupType.InlineGroup, group, function () { + return _this._renderInlines(group.ops); }); } }) .join(""); }; - QuillDeltaToHtmlConverter.prototype.renderWithCallbacks = function (groupType, group, myRenderFn) { + QuillDeltaToHtmlConverter.prototype._renderWithCallbacks = function (groupType, group, myRenderFn) { var html = ''; var beforeCb = this.callbacks['beforeRender_cb']; html = typeof beforeCb === 'function' ? beforeCb.apply(null, [groupType, group]) : ''; @@ -92,39 +92,39 @@ var QuillDeltaToHtmlConverter = (function () { html = typeof afterCb === 'function' ? afterCb.apply(null, [groupType, html]) : html; return html; }; - QuillDeltaToHtmlConverter.prototype.renderList = function (list, isOuterMost) { + QuillDeltaToHtmlConverter.prototype._renderList = function (list, isOuterMost) { var _this = this; if (isOuterMost === void 0) { isOuterMost = true; } var firstItem = list.items[0]; - return funcs_html_1.makeStartTag(this.getListTag(firstItem.item.op)) - + list.items.map(function (li) { return _this.renderListItem(li, isOuterMost); }).join('') - + funcs_html_1.makeEndTag(this.getListTag(firstItem.item.op)); + return funcs_html_1.makeStartTag(this._getListTag(firstItem.item.op)) + + list.items.map(function (li) { return _this._renderListItem(li, isOuterMost); }).join('') + + funcs_html_1.makeEndTag(this._getListTag(firstItem.item.op)); }; - QuillDeltaToHtmlConverter.prototype.renderListItem = function (li, isOuterMost) { + QuillDeltaToHtmlConverter.prototype._renderListItem = function (li, isOuterMost) { var converterOptions = Object._assign({}, this.converterOptions); li.item.op.attributes.indent = 0; var converter = new OpToHtmlConverter_1.OpToHtmlConverter(li.item.op, this.converterOptions); var parts = converter.getHtmlParts(); - var liElementsHtml = this.renderInlines(li.item.ops, false); + var liElementsHtml = this._renderInlines(li.item.ops, false); return parts.openingTag + (liElementsHtml) + - (li.innerList ? this.renderList(li.innerList, false) : '') + (li.innerList ? this._renderList(li.innerList, false) : '') + parts.closingTag; }; - QuillDeltaToHtmlConverter.prototype.renderBlock = function (bop, ops) { + QuillDeltaToHtmlConverter.prototype._renderBlock = function (bop, ops) { var _this = this; var converter = new OpToHtmlConverter_1.OpToHtmlConverter(bop, this.converterOptions); var htmlParts = converter.getHtmlParts(); if (bop.isCodeBlock()) { return htmlParts.openingTag + funcs_html_1.encodeHtml(ops.map(function (iop) { - return iop.isCustom() ? _this.renderCustom(iop, bop) : iop.insert.value; + return iop.isCustom() ? _this._renderCustom(iop, bop) : iop.insert.value; }).join("")) + htmlParts.closingTag; } var inlines = ops.map(function (op) { return _this._renderInline(op, bop); }).join(''); return htmlParts.openingTag + (inlines || BrTag) + htmlParts.closingTag; }; - QuillDeltaToHtmlConverter.prototype.renderInlines = function (ops, wrapInParagraphTag) { + QuillDeltaToHtmlConverter.prototype._renderInlines = function (ops, wrapInParagraphTag) { var _this = this; if (wrapInParagraphTag === void 0) { wrapInParagraphTag = true; } var opsLen = ops.length - 1; @@ -142,12 +142,12 @@ var QuillDeltaToHtmlConverter = (function () { }; QuillDeltaToHtmlConverter.prototype._renderInline = function (op, contextOp) { if (op.isCustom()) { - return this.renderCustom(op, contextOp); + return this._renderCustom(op, contextOp); } var converter = new OpToHtmlConverter_1.OpToHtmlConverter(op, this.converterOptions); return converter.getHtml().replace(/\n/g, BrTag); }; - QuillDeltaToHtmlConverter.prototype.renderCustom = function (op, contextOp) { + QuillDeltaToHtmlConverter.prototype._renderCustom = function (op, contextOp) { var renderCb = this.callbacks['renderCustomOp_cb']; if (typeof renderCb === 'function') { return renderCb.apply(null, [op, contextOp]); diff --git a/dist/commonjs/extensions/Array.d.ts b/dist/commonjs/extensions/Array.d.ts new file mode 100644 index 0000000..e10f0a6 --- /dev/null +++ b/dist/commonjs/extensions/Array.d.ts @@ -0,0 +1,11 @@ +interface IArraySlice { + sliceStartsAt: number; + elements: any[]; +} +interface Array { + _preferSecond(): T | null; + _flatten(): any[]; + _groupConsecutiveElementsWhile(predicate: (currElm: any, prevElm: any) => boolean): any[]; + _sliceFromReverseWhile(startIndex: number, predicate: (currElm: any) => boolean): IArraySlice; + _intersperse(item: any): any[]; +} diff --git a/dist/commonjs/extensions/Object.d.ts b/dist/commonjs/extensions/Object.d.ts new file mode 100644 index 0000000..69b8e05 --- /dev/null +++ b/dist/commonjs/extensions/Object.d.ts @@ -0,0 +1,3 @@ +interface ObjectConstructor { + _assign(target: any, obj1: any, obj2?: any): any; +} diff --git a/dist/commonjs/extensions/String.d.ts b/dist/commonjs/extensions/String.d.ts new file mode 100644 index 0000000..a7f50cc --- /dev/null +++ b/dist/commonjs/extensions/String.d.ts @@ -0,0 +1,4 @@ +interface String { + _tokenizeWithNewLines(): string[]; + _scrubUrl(): string; +} diff --git a/dist/commonjs/funcs-html.d.ts b/dist/commonjs/funcs-html.d.ts new file mode 100644 index 0000000..1922525 --- /dev/null +++ b/dist/commonjs/funcs-html.d.ts @@ -0,0 +1,10 @@ +interface ITagKeyValue { + key: string; + value?: string; +} +declare function makeStartTag(tag: any, attrs?: ITagKeyValue | ITagKeyValue[]): string; +declare function makeEndTag(tag?: any): string; +declare function decodeHtml(str: string): string; +declare function encodeHtml(str: string, preventDoubleEncoding?: boolean): string; +declare function encodeLink(str: string): string; +export { makeStartTag, makeEndTag, encodeHtml, decodeHtml, encodeLink, ITagKeyValue }; diff --git a/dist/commonjs/grouper/Grouper.d.ts b/dist/commonjs/grouper/Grouper.d.ts new file mode 100644 index 0000000..3481f51 --- /dev/null +++ b/dist/commonjs/grouper/Grouper.d.ts @@ -0,0 +1,16 @@ +import { DeltaInsertOp } from './../DeltaInsertOp'; +import './../extensions/Array'; +import { BlockGroup, TDataGroup } from './group-types'; +declare class Grouper { + static pairOpsWithTheirBlock(ops: DeltaInsertOp[]): TDataGroup[]; + static groupConsecutiveSameStyleBlocks(groups: TDataGroup[], blocksOf?: { + header: boolean; + codeBlocks: boolean; + blockquotes: boolean; + }): Array; + static reduceConsecutiveSameStyleBlocksToOne(groups: Array): TDataGroup[]; + static areBothCodeblocks(g1: BlockGroup, gOther: BlockGroup): boolean; + static areBothSameHeadersWithSameAdi(g1: BlockGroup, gOther: BlockGroup): boolean; + static areBothBlockquotesWithSameAdi(g: BlockGroup, gOther: BlockGroup): boolean; +} +export { Grouper }; diff --git a/dist/commonjs/grouper/ListNester.d.ts b/dist/commonjs/grouper/ListNester.d.ts new file mode 100644 index 0000000..13c2dcc --- /dev/null +++ b/dist/commonjs/grouper/ListNester.d.ts @@ -0,0 +1,10 @@ +import { TDataGroup } from './group-types'; +declare class ListNester { + nest(groups: TDataGroup[]): TDataGroup[]; + private convertListBlocksToListGroups; + private groupConsecutiveListGroups; + private nestListSection; + private groupByIndent; + private placeUnderParent; +} +export { ListNester }; diff --git a/dist/commonjs/grouper/group-types.d.ts b/dist/commonjs/grouper/group-types.d.ts new file mode 100644 index 0000000..64ff470 --- /dev/null +++ b/dist/commonjs/grouper/group-types.d.ts @@ -0,0 +1,25 @@ +import { DeltaInsertOp } from './../DeltaInsertOp'; +declare class InlineGroup { + readonly ops: DeltaInsertOp[]; + constructor(ops: DeltaInsertOp[]); +} +declare class VideoItem { + readonly op: DeltaInsertOp; + constructor(op: DeltaInsertOp); +} +declare class BlockGroup { + readonly op: DeltaInsertOp; + ops: DeltaInsertOp[]; + constructor(op: DeltaInsertOp, ops: DeltaInsertOp[]); +} +declare class ListGroup { + items: ListItem[]; + constructor(items: ListItem[]); +} +declare class ListItem { + readonly item: BlockGroup; + innerList: ListGroup; + constructor(item: BlockGroup, innerList?: ListGroup); +} +declare type TDataGroup = VideoItem | InlineGroup | BlockGroup | ListItem | ListGroup; +export { VideoItem, InlineGroup, BlockGroup, ListGroup, ListItem, TDataGroup }; diff --git a/dist/commonjs/main.d.ts b/dist/commonjs/main.d.ts new file mode 100644 index 0000000..5dec978 --- /dev/null +++ b/dist/commonjs/main.d.ts @@ -0,0 +1,2 @@ +import { QuillDeltaToHtmlConverter } from './QuillDeltaToHtmlConverter'; +export = QuillDeltaToHtmlConverter; diff --git a/dist/commonjs/mentions/MentionSanitizer.d.ts b/dist/commonjs/mentions/MentionSanitizer.d.ts new file mode 100644 index 0000000..d7b97e8 --- /dev/null +++ b/dist/commonjs/mentions/MentionSanitizer.d.ts @@ -0,0 +1,18 @@ +import './../extensions/String'; +interface IMention { + [index: string]: string; + 'name'?: string; + 'target'?: string; + 'slug'?: string; + 'class'?: string; + 'avatar'?: string; + 'id'?: string; + 'end-point'?: string; +} +declare class MentionSanitizer { + static sanitize(dirtyObj: IMention): IMention; + static IsValidClass(classAttr: string): boolean; + static IsValidId(idAttr: string): boolean; + static IsValidTarget(target: string): boolean; +} +export { MentionSanitizer, IMention }; diff --git a/dist/commonjs/value-types.d.ts b/dist/commonjs/value-types.d.ts new file mode 100644 index 0000000..40bb7ac --- /dev/null +++ b/dist/commonjs/value-types.d.ts @@ -0,0 +1,30 @@ +declare type NewLine = "\n"; +declare const NewLine: "\n"; +declare enum ListType { + Ordered = "ordered", + Bullet = "bullet" +} +declare enum ScriptType { + Sub = "sub", + Super = "super" +} +declare enum DirectionType { + Rtl = "rtl" +} +declare enum AlignType { + Center = "center", + Right = "right" +} +declare enum DataType { + Image = "image", + Video = "video", + Formula = "formula", + Text = "text" +} +declare enum GroupType { + Block = "block", + InlineGroup = "inline-group", + List = "list", + Video = "video" +} +export { NewLine, ListType, ScriptType, DirectionType, AlignType, DataType, GroupType }; diff --git a/dist/commonjs/value-types.js b/dist/commonjs/value-types.js index af8e669..96ab594 100644 --- a/dist/commonjs/value-types.js +++ b/dist/commonjs/value-types.js @@ -2,36 +2,44 @@ Object.defineProperty(exports, "__esModule", { value: true }); var NewLine = "\n"; exports.NewLine = NewLine; -var ListType = { - Ordered: 'ordered', - Bullet: 'bullet' -}; +var ListType; +(function (ListType) { + ListType["Ordered"] = "ordered"; + ListType["Bullet"] = "bullet"; +})(ListType || (ListType = {})); exports.ListType = ListType; -var ScriptType = { - Sub: "sub", - Super: "super" -}; +var ScriptType; +(function (ScriptType) { + ScriptType["Sub"] = "sub"; + ScriptType["Super"] = "super"; +})(ScriptType || (ScriptType = {})); exports.ScriptType = ScriptType; -var DirectionType = { - Rtl: "rtl" -}; +var DirectionType; +(function (DirectionType) { + DirectionType["Rtl"] = "rtl"; +})(DirectionType || (DirectionType = {})); exports.DirectionType = DirectionType; -var AlignType = { - Center: "center", - Right: "right" -}; +var AlignType; +(function (AlignType) { + AlignType["Center"] = "center"; + AlignType["Right"] = "right"; +})(AlignType || (AlignType = {})); exports.AlignType = AlignType; -var DataType = { - Image: "image", - Video: "video", - Formula: "formula", - Text: "text" -}; +var DataType; +(function (DataType) { + DataType["Image"] = "image"; + DataType["Video"] = "video"; + DataType["Formula"] = "formula"; + DataType["Text"] = "text"; +})(DataType || (DataType = {})); exports.DataType = DataType; -var GroupType = { - Block: 'block', - InlineGroup: 'inline-group', - List: 'list', - Video: 'video' -}; +; +var GroupType; +(function (GroupType) { + GroupType["Block"] = "block"; + GroupType["InlineGroup"] = "inline-group"; + GroupType["List"] = "list"; + GroupType["Video"] = "video"; +})(GroupType || (GroupType = {})); exports.GroupType = GroupType; +; diff --git a/package.json b/package.json index cad0236..9a8da9f 100644 --- a/package.json +++ b/package.json @@ -1,17 +1,18 @@ { "name": "quill-delta-to-html", - "version": "0.8.2", + "version": "0.8.3", "description": "Converts Quill's delta ops to HTML", - "main": "dist/commonjs/main.js", + "main": "./dist/commonjs/main.js", + "types":"./dist/commonjs/main.d.ts", "dependencies": {}, "devDependencies": { "@types/mocha": "^2.2.40", "@types/node": "^7.0.12", "coveralls": "^2.13.0", "mocha": "^3.2.0", - "nyc": "^10.2.0", + "nyc": "^12.0.2", "ts-node": "^3.0.2", - "typescript": "^2.2.2" + "typescript": "^2.9.0" }, "scripts": { "browserbundle": "browserify ./dist/commonjs/QuillDeltaToHtmlConverter.js -o ./dist/browser/QuillDeltaToHtmlConverter.bundle.js -s QuillDeltaToHtmlConverter ", @@ -42,7 +43,8 @@ ], "exclude": [ "typings", - "src/main.ts" + "src/main.ts", + "**/*.d.ts" ], "extension": [ ".ts" diff --git a/src/QuillDeltaToHtmlConverter.ts b/src/QuillDeltaToHtmlConverter.ts index 2d4dbb8..61c77b9 100644 --- a/src/QuillDeltaToHtmlConverter.ts +++ b/src/QuillDeltaToHtmlConverter.ts @@ -73,7 +73,7 @@ class QuillDeltaToHtmlConverter { } - getListTag(op: DeltaInsertOp): string { + _getListTag(op: DeltaInsertOp): string { return op.isOrderedList() ? this.options.orderedListTag + '' : op.isBulletList() ? this.options.bulletListTag + '' : ''; @@ -100,34 +100,34 @@ class QuillDeltaToHtmlConverter { .map(group => { if (group instanceof ListGroup) { - return this.renderWithCallbacks( - GroupType.List, group, () => this.renderList(group)); + return this._renderWithCallbacks( + GroupType.List, group, () => this._renderList(group)); } else if (group instanceof BlockGroup) { var g = group; - return this.renderWithCallbacks( - GroupType.Block, group, () => this.renderBlock(g.op, g.ops)); + return this._renderWithCallbacks( + GroupType.Block, group, () => this._renderBlock(g.op, g.ops)); } else if (group instanceof VideoItem) { - return this.renderWithCallbacks(GroupType.Video, group, () => { + return this._renderWithCallbacks(GroupType.Video, group, () => { var g = group; var converter = new OpToHtmlConverter(g.op, this.converterOptions); return converter.getHtml(); }); } else { // InlineGroup - return this.renderWithCallbacks(GroupType.InlineGroup, group, () => { - return this.renderInlines((group).ops); + return this._renderWithCallbacks(GroupType.InlineGroup, group, () => { + return this._renderInlines((group).ops); }); } }) .join(""); } - renderWithCallbacks(groupType: GroupType, group: TDataGroup, myRenderFn: () => string) { + _renderWithCallbacks(groupType: GroupType, group: TDataGroup, myRenderFn: () => string) { var html = ''; var beforeCb = this.callbacks['beforeRender_cb']; html = typeof beforeCb === 'function' ? beforeCb.apply(null, [groupType, group]) : ''; @@ -142,28 +142,28 @@ class QuillDeltaToHtmlConverter { return html; } - renderList(list: ListGroup, isOuterMost = true): string { + _renderList(list: ListGroup, isOuterMost = true): string { var firstItem = list.items[0]; - return makeStartTag(this.getListTag(firstItem.item.op)) - + list.items.map((li: ListItem) => this.renderListItem(li, isOuterMost)).join('') - + makeEndTag(this.getListTag(firstItem.item.op)); + return makeStartTag(this._getListTag(firstItem.item.op)) + + list.items.map((li: ListItem) => this._renderListItem(li, isOuterMost)).join('') + + makeEndTag(this._getListTag(firstItem.item.op)); } - renderListItem(li: ListItem, isOuterMost: boolean): string { + _renderListItem(li: ListItem, isOuterMost: boolean): string { var converterOptions = Object._assign({}, this.converterOptions); //if (!isOuterMost) { li.item.op.attributes.indent = 0; //} var converter = new OpToHtmlConverter(li.item.op, this.converterOptions); var parts = converter.getHtmlParts(); - var liElementsHtml = this.renderInlines(li.item.ops, false); + var liElementsHtml = this._renderInlines(li.item.ops, false); return parts.openingTag + (liElementsHtml) + - (li.innerList ? this.renderList(li.innerList, false) : '') + (li.innerList ? this._renderList(li.innerList, false) : '') + parts.closingTag; } - renderBlock(bop: DeltaInsertOp, ops: DeltaInsertOp[]) { + _renderBlock(bop: DeltaInsertOp, ops: DeltaInsertOp[]) { var converter = new OpToHtmlConverter(bop, this.converterOptions); var htmlParts = converter.getHtmlParts(); @@ -171,7 +171,7 @@ class QuillDeltaToHtmlConverter { return htmlParts.openingTag + encodeHtml( ops.map((iop) => - iop.isCustom() ? this.renderCustom(iop, bop) : iop.insert.value + iop.isCustom() ? this._renderCustom(iop, bop) : iop.insert.value ).join("") ) + htmlParts.closingTag; @@ -181,7 +181,7 @@ class QuillDeltaToHtmlConverter { return htmlParts.openingTag + (inlines || BrTag) + htmlParts.closingTag; } - renderInlines(ops: DeltaInsertOp[], wrapInParagraphTag = true) { + _renderInlines(ops: DeltaInsertOp[], wrapInParagraphTag = true) { var opsLen = ops.length - 1; var html = ops.map((op: DeltaInsertOp, i: number) => { if (i > 0 && i === opsLen && op.isJustNewline()) { @@ -198,13 +198,13 @@ class QuillDeltaToHtmlConverter { _renderInline(op: DeltaInsertOp, contextOp: DeltaInsertOp) { if (op.isCustom()) { - return this.renderCustom(op, contextOp); + return this._renderCustom(op, contextOp); } var converter = new OpToHtmlConverter(op, this.converterOptions); return converter.getHtml().replace(/\n/g, BrTag); } - renderCustom(op: DeltaInsertOp, contextOp: DeltaInsertOp) { + _renderCustom(op: DeltaInsertOp, contextOp: DeltaInsertOp) { var renderCb = this.callbacks['renderCustomOp_cb']; if (typeof renderCb === 'function') { return renderCb.apply(null, [op, contextOp]); diff --git a/src/value-types.ts b/src/value-types.ts index 1272e94..f883038 100644 --- a/src/value-types.ts +++ b/src/value-types.ts @@ -2,43 +2,37 @@ type NewLine = "\n"; const NewLine = "\n" as NewLine; -type ListType = "ordered" | "bullet"; -const ListType = { - Ordered: 'ordered' as ListType, - Bullet: 'bullet' as ListType +enum ListType { + Ordered = 'ordered', + Bullet = 'bullet' } -type ScriptType = "sub" | "super"; -const ScriptType = { - Sub: "sub" as ScriptType, - Super: "super" as ScriptType +enum ScriptType { + Sub = "sub", + Super = "super" } -type DirectionType = "rtl"; -const DirectionType = { - Rtl: "rtl" as DirectionType +enum DirectionType { + Rtl = 'rtl' } -type AlignType = "center" | "right"; -const AlignType = { - Center: "center" as AlignType, - Right: "right" as AlignType +enum AlignType { + Center = "center", + Right = "right" } -type DataType = "text" | "image" | "video" | "formula"; -const DataType = { - Image: "image" as DataType, - Video: "video" as DataType, - Formula: "formula" as DataType, - Text: "text" as DataType +enum DataType { + Image = "image", + Video = "video", + Formula = "formula", + Text = "text" }; -type GroupType = "block" | "inline-group" | "list" | "video"; -const GroupType = { - Block: 'block' as GroupType, - InlineGroup: 'inline-group' as GroupType, - List: 'list' as GroupType, - Video: 'video' as GroupType +enum GroupType { + Block = 'block', + InlineGroup = 'inline-group', + List = 'list', + Video = 'video' }; export { NewLine, ListType, ScriptType, DirectionType, AlignType, DataType, GroupType }; diff --git a/test/QuillDeltaToHtmlConverter.test.ts b/test/QuillDeltaToHtmlConverter.test.ts index 7a2e2d4..969e629 100644 --- a/test/QuillDeltaToHtmlConverter.test.ts +++ b/test/QuillDeltaToHtmlConverter.test.ts @@ -8,7 +8,7 @@ import { QuillDeltaToHtmlConverter } from "./../src/QuillDeltaToHtmlConverter"; import { callWhenAlltrue, callWhenXEqualY } from './_helper'; import { delta1 } from './data/delta1'; -import { GroupType } from './../src/value-types'; +import { GroupType, ListType } from './../src/value-types'; import { encodeHtml } from './../src/funcs-html'; @@ -213,18 +213,18 @@ describe('QuillDeltaToHtmlConverter', function () { }); }); - describe('getListTag()', function () { + describe('_getListTag()', function () { it('should return proper list tag', function () { - var op = new DeltaInsertOp("\n", { list: 'ordered' }); + var op = new DeltaInsertOp("\n", { list: ListType.Ordered }); var qdc = new QuillDeltaToHtmlConverter(delta1.ops) - assert.equal(qdc.getListTag(op), 'ol'); + assert.equal(qdc._getListTag(op), 'ol'); - var op = new DeltaInsertOp("\n", { list: 'bullet' }); - assert.equal(qdc.getListTag(op), 'ul'); + var op = new DeltaInsertOp("\n", { list: ListType.Bullet }); + assert.equal(qdc._getListTag(op), 'ul'); var op = new DeltaInsertOp("d"); - assert.equal(qdc.getListTag(op), ''); + assert.equal(qdc._getListTag(op), ''); }); }); @@ -243,46 +243,46 @@ describe('QuillDeltaToHtmlConverter', function () { describe('renderInlines()', function () { it('should render inlines', function () { var qdc = new QuillDeltaToHtmlConverter([]); - var inlines = qdc.renderInlines(ops); + var inlines = qdc._renderInlines(ops); assert.equal(inlines, ['

Hello', ' my
name is joey

'].join('')); qdc = new QuillDeltaToHtmlConverter([], { paragraphTag: 'div' }); - var inlines = qdc.renderInlines(ops); + var inlines = qdc._renderInlines(ops); assert.equal(inlines, '
Hello my
name is joey
'); qdc = new QuillDeltaToHtmlConverter([], { paragraphTag: '' }); - var inlines = qdc.renderInlines(ops); + var inlines = qdc._renderInlines(ops); assert.equal(inlines, 'Hello my
name is joey'); }); it('should render plain new line string', function () { var ops = [new DeltaInsertOp("\n")]; var qdc = new QuillDeltaToHtmlConverter([]); - assert.equal(qdc.renderInlines(ops), '


'); + assert.equal(qdc._renderInlines(ops), '


'); }); it('should render styled new line string', function () { var ops = [new DeltaInsertOp("\n", { font: 'arial' })]; var qdc = new QuillDeltaToHtmlConverter([]); - assert.equal(qdc.renderInlines(ops), + assert.equal(qdc._renderInlines(ops), '


'); var qdc = new QuillDeltaToHtmlConverter([], { paragraphTag: '' }); - assert.equal(qdc.renderInlines(ops), '
'); + assert.equal(qdc._renderInlines(ops), '
'); }); it('should render when first line is new line', function () { var ops = [new DeltaInsertOp("\n"), new DeltaInsertOp("aa")]; var qdc = new QuillDeltaToHtmlConverter([]); - assert.equal(qdc.renderInlines(ops), '


aa

'); + assert.equal(qdc._renderInlines(ops), '


aa

'); }); it('should render when last line is new line', function () { var ops = [new DeltaInsertOp("aa"), new DeltaInsertOp("\n")]; var qdc = new QuillDeltaToHtmlConverter([]); - assert.equal(qdc.renderInlines(ops), '

aa

'); + assert.equal(qdc._renderInlines(ops), '

aa

'); }); it('should render mixed lines', function () { @@ -290,13 +290,13 @@ describe('QuillDeltaToHtmlConverter', function () { var nlop = new DeltaInsertOp("\n"); var stylednlop = new DeltaInsertOp("\n", { color: '#333', italic: true }); var qdc = new QuillDeltaToHtmlConverter([]); - assert.equal(qdc.renderInlines(ops), '

aabb

'); + assert.equal(qdc._renderInlines(ops), '

aabb

'); var ops0 = [nlop, ops[0], nlop, ops[1]] - assert.equal(qdc.renderInlines(ops0), '


aa
bb

'); + assert.equal(qdc._renderInlines(ops0), '


aa
bb

'); var ops4 = [ops[0], stylednlop, stylednlop, stylednlop, ops[1]] - assert.equal(qdc.renderInlines(ops4), + assert.equal(qdc._renderInlines(ops4), ['

aa


bb

'].join('')); }); @@ -308,12 +308,12 @@ describe('QuillDeltaToHtmlConverter', function () { it('should render container block', function () { var qdc = new QuillDeltaToHtmlConverter([]); - var blockhtml = qdc.renderBlock(op, [inlineop]); + var blockhtml = qdc._renderBlock(op, [inlineop]); assert.equal(blockhtml, ['

', 'hi there

'].join('')); var qdc = new QuillDeltaToHtmlConverter([]); - var blockhtml = qdc.renderBlock(op, []); + var blockhtml = qdc._renderBlock(op, []); assert.equal(blockhtml, ['

', '

'].join('')); diff --git a/tsconfig.json b/tsconfig.json index 9f40ea6..132a2c6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,7 +13,8 @@ "types": [ "node", "mocha" - ] + ], + "declaration": true }, "include": [ "src/**/*.ts"