Skip to content

Commit

Permalink
Fixed extra lines in <pre> and added validation and tests for mentions
Browse files Browse the repository at this point in the history
  • Loading branch information
nozer committed Jan 13, 2018
1 parent 1229628 commit 564db89
Show file tree
Hide file tree
Showing 14 changed files with 259 additions and 105 deletions.
99 changes: 76 additions & 23 deletions dist/browser/QuillDeltaToHtmlConverter.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ var DeltaInsertOp = (function () {
}());
exports.DeltaInsertOp = DeltaInsertOp;

},{"./InsertData":2,"./value-types":15}],2:[function(require,module,exports){
},{"./InsertData":2,"./value-types":16}],2:[function(require,module,exports){
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var InsertData = (function () {
Expand Down Expand Up @@ -129,7 +129,7 @@ var InsertOpDenormalizer = (function () {
}());
exports.InsertOpDenormalizer = InsertOpDenormalizer;

},{"./extensions/Object":9,"./extensions/String":10,"./value-types":15}],4:[function(require,module,exports){
},{"./extensions/Object":9,"./extensions/String":10,"./value-types":16}],4:[function(require,module,exports){
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var DeltaInsertOp_1 = require("./DeltaInsertOp");
Expand Down Expand Up @@ -180,10 +180,11 @@ var InsertOpsConverter = (function () {
}());
exports.InsertOpsConverter = InsertOpsConverter;

},{"./DeltaInsertOp":1,"./InsertData":2,"./InsertOpDenormalizer":3,"./OpAttributeSanitizer":5,"./value-types":15}],5:[function(require,module,exports){
},{"./DeltaInsertOp":1,"./InsertData":2,"./InsertOpDenormalizer":3,"./OpAttributeSanitizer":5,"./value-types":16}],5:[function(require,module,exports){
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var value_types_1 = require("./value-types");
var MentionSanitizer_1 = require("./mentions/MentionSanitizer");
require("./extensions/String");
var OpAttributeSanitizer = (function () {
function OpAttributeSanitizer() {
Expand All @@ -193,7 +194,7 @@ var OpAttributeSanitizer = (function () {
if (!dirtyAttrs || typeof dirtyAttrs !== 'object') {
return cleanAttrs;
}
var font = dirtyAttrs.font, size = dirtyAttrs.size, link = dirtyAttrs.link, script = dirtyAttrs.script, list = dirtyAttrs.list, header = dirtyAttrs.header, align = dirtyAttrs.align, direction = dirtyAttrs.direction, indent = dirtyAttrs.indent, mentions = dirtyAttrs.mentions;
var font = dirtyAttrs.font, size = dirtyAttrs.size, link = dirtyAttrs.link, script = dirtyAttrs.script, list = dirtyAttrs.list, header = dirtyAttrs.header, align = dirtyAttrs.align, direction = dirtyAttrs.direction, indent = dirtyAttrs.indent, mentions = dirtyAttrs.mentions, mention = dirtyAttrs.mention, width = dirtyAttrs.width;
['bold', 'italic', 'underline', 'strike', 'code', 'blockquote', 'code-block']
.forEach(function (prop) {
var v = dirtyAttrs[prop];
Expand All @@ -214,6 +215,9 @@ var OpAttributeSanitizer = (function () {
if (size && OpAttributeSanitizer.IsValidSize(size + '')) {
cleanAttrs.size = size;
}
if (width && OpAttributeSanitizer.IsValidWidth(width + '')) {
cleanAttrs.width = width;
}
if (link) {
cleanAttrs.link = (link + '')._scrubUrl();
}
Expand All @@ -235,10 +239,12 @@ var OpAttributeSanitizer = (function () {
if (indent && Number(indent)) {
cleanAttrs.indent = Math.min(Number(indent), 30);
}
if (mentions) {
var mention = dirtyAttrs.mention;
cleanAttrs.mentions = mentions;
cleanAttrs.mention = mention;
if (mentions && mention) {
var sanitizedMention = MentionSanitizer_1.MentionSanitizer.sanitize(mention);
if (Object.keys(sanitizedMention).length > 0) {
cleanAttrs.mentions = !!mentions;
cleanAttrs.mention = mention;
}
}
return cleanAttrs;
};
Expand All @@ -261,7 +267,7 @@ var OpAttributeSanitizer = (function () {
}());
exports.OpAttributeSanitizer = OpAttributeSanitizer;

},{"./extensions/String":10,"./value-types":15}],6:[function(require,module,exports){
},{"./extensions/String":10,"./mentions/MentionSanitizer":15,"./value-types":16}],6:[function(require,module,exports){
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var funcs_html_1 = require("./funcs-html");
Expand Down Expand Up @@ -366,7 +372,19 @@ var OpToHtmlConverter = (function () {
}
if (this.op.isMentions()) {
var mention = this.op.attributes.mention;
return tagAttrs.concat(makeAttr('class', mention.class), makeAttr('href', mention['end-point'] + '/' + mention.slug || 'javascript:void(0)'), makeAttr('target', mention.target));
if (mention.class) {
tagAttrs = tagAttrs.concat(makeAttr('class', mention.class));
}
if (mention['end-point'] && mention.slug) {
tagAttrs = tagAttrs.concat(makeAttr('href', mention['end-point'] + '/' + mention.slug));
}
else {
tagAttrs = tagAttrs.concat(makeAttr('href', 'javascript:void(0)'));
}
if (mention.target) {
tagAttrs = tagAttrs.concat(makeAttr('target', mention.target));
}
return tagAttrs;
}
var styles = this.getCssStyles();
var styleAttr = styles.length ? [makeAttr('style', styles.join(';'))] : [];
Expand Down Expand Up @@ -418,7 +436,7 @@ var OpToHtmlConverter = (function () {
}());
exports.OpToHtmlConverter = OpToHtmlConverter;

},{"./OpAttributeSanitizer":5,"./extensions/Array":8,"./extensions/Object":9,"./extensions/String":10,"./funcs-html":11,"./value-types":15}],7:[function(require,module,exports){
},{"./OpAttributeSanitizer":5,"./extensions/Array":8,"./extensions/Object":9,"./extensions/String":10,"./funcs-html":11,"./value-types":16}],7:[function(require,module,exports){
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var InsertOpsConverter_1 = require("./InsertOpsConverter");
Expand Down Expand Up @@ -537,7 +555,7 @@ var QuillDeltaToHtmlConverter = (function () {
var htmlParts = converter.getHtmlParts();
if (op.isCodeBlock()) {
return htmlParts.openingTag +
ops.map(function (op) { return op.insert.value; }).join(value_types_1.NewLine)
ops.map(function (op) { return op.insert.value; }).join("")
+ htmlParts.closingTag;
}
var inlines = ops.map(function (op) {
Expand Down Expand Up @@ -578,7 +596,7 @@ var QuillDeltaToHtmlConverter = (function () {
}());
exports.QuillDeltaToHtmlConverter = QuillDeltaToHtmlConverter;

},{"./InsertOpsConverter":4,"./OpToHtmlConverter":6,"./extensions/Object":9,"./funcs-html":11,"./grouper/Grouper":12,"./grouper/ListNester":13,"./grouper/group-types":14,"./value-types":15}],8:[function(require,module,exports){
},{"./InsertOpsConverter":4,"./OpToHtmlConverter":6,"./extensions/Object":9,"./funcs-html":11,"./grouper/Grouper":12,"./grouper/ListNester":13,"./grouper/group-types":14,"./value-types":16}],8:[function(require,module,exports){
Array.prototype._preferSecond = function () {
if (this.length === 0) {
return null;
Expand Down Expand Up @@ -959,6 +977,51 @@ exports.ListItem = ListItem;
},{}],15:[function(require,module,exports){
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
require("./../extensions/String");
var MentionSanitizer = (function () {
function MentionSanitizer() {
}
MentionSanitizer.sanitize = function (dirtyObj) {
var cleanObj = {};
if (!dirtyObj || typeof dirtyObj !== 'object') {
return cleanObj;
}
if (dirtyObj.class && MentionSanitizer.IsValidClass(dirtyObj.class)) {
cleanObj.class = dirtyObj.class;
}
if (dirtyObj.id && MentionSanitizer.IsValidId(dirtyObj.id)) {
cleanObj.id = dirtyObj.id;
}
if (MentionSanitizer.IsValidTarget(dirtyObj.target)) {
cleanObj.target = dirtyObj.target;
}
if (dirtyObj.avatar) {
cleanObj.avatar = (dirtyObj.avatar + '')._scrubUrl();
}
if (dirtyObj['end-point']) {
cleanObj['end-point'] = (dirtyObj['end-point'] + '')._scrubUrl();
}
if (dirtyObj.slug) {
cleanObj.slug = (dirtyObj.slug + '')._scrubUrl();
}
return cleanObj;
};
MentionSanitizer.IsValidClass = function (classAttr) {
return !!classAttr.match(/^[a-zA-Z0-9_\-]{1,500}$/i);
};
MentionSanitizer.IsValidId = function (idAttr) {
return !!idAttr.match(/^[a-zA-Z0-9_\-\:\.]{1,500}$/i);
};
MentionSanitizer.IsValidTarget = function (target) {
return ['_self', '_blank', '_parent', '_top'].indexOf(target) > -1;
};
return MentionSanitizer;
}());
exports.MentionSanitizer = MentionSanitizer;

},{"./../extensions/String":10}],16:[function(require,module,exports){
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var NewLine = "\n";
exports.NewLine = NewLine;
var ListType = {
Expand Down Expand Up @@ -994,16 +1057,6 @@ var GroupType = {
Video: 'video'
};
exports.GroupType = GroupType;
var MentionType = {
Name: 'name',
Target: 'target',
Slug: 'slug',
Class: 'class',
Avatar: 'avatar',
Id: 'id',
EndPoint: 'end-point'
};
exports.MentionType = MentionType;

},{}]},{},[7])(7)
});; window.QuillDeltaToHtmlConverter = window.QuillDeltaToHtmlConverter.QuillDeltaToHtmlConverter;
13 changes: 8 additions & 5 deletions dist/commonjs/OpAttributeSanitizer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var value_types_1 = require("./value-types");
var MentionSanitizer_1 = require("./mentions/MentionSanitizer");
require("./extensions/String");
var OpAttributeSanitizer = (function () {
function OpAttributeSanitizer() {
Expand All @@ -10,7 +11,7 @@ var OpAttributeSanitizer = (function () {
if (!dirtyAttrs || typeof dirtyAttrs !== 'object') {
return cleanAttrs;
}
var font = dirtyAttrs.font, size = dirtyAttrs.size, link = dirtyAttrs.link, script = dirtyAttrs.script, list = dirtyAttrs.list, header = dirtyAttrs.header, align = dirtyAttrs.align, direction = dirtyAttrs.direction, indent = dirtyAttrs.indent, mentions = dirtyAttrs.mentions, width = dirtyAttrs.width;
var font = dirtyAttrs.font, size = dirtyAttrs.size, link = dirtyAttrs.link, script = dirtyAttrs.script, list = dirtyAttrs.list, header = dirtyAttrs.header, align = dirtyAttrs.align, direction = dirtyAttrs.direction, indent = dirtyAttrs.indent, mentions = dirtyAttrs.mentions, mention = dirtyAttrs.mention, width = dirtyAttrs.width;
['bold', 'italic', 'underline', 'strike', 'code', 'blockquote', 'code-block']
.forEach(function (prop) {
var v = dirtyAttrs[prop];
Expand Down Expand Up @@ -55,10 +56,12 @@ var OpAttributeSanitizer = (function () {
if (indent && Number(indent)) {
cleanAttrs.indent = Math.min(Number(indent), 30);
}
if (mentions) {
var mention = dirtyAttrs.mention;
cleanAttrs.mentions = mentions;
cleanAttrs.mention = mention;
if (mentions && mention) {
var sanitizedMention = MentionSanitizer_1.MentionSanitizer.sanitize(mention);
if (Object.keys(sanitizedMention).length > 0) {
cleanAttrs.mentions = !!mentions;
cleanAttrs.mention = mention;
}
}
return cleanAttrs;
};
Expand Down
14 changes: 13 additions & 1 deletion dist/commonjs/OpToHtmlConverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,19 @@ var OpToHtmlConverter = (function () {
}
if (this.op.isMentions()) {
var mention = this.op.attributes.mention;
return tagAttrs.concat(makeAttr('class', mention.class), makeAttr('href', mention['end-point'] + '/' + mention.slug || 'javascript:void(0)'), makeAttr('target', mention.target));
if (mention.class) {
tagAttrs = tagAttrs.concat(makeAttr('class', mention.class));
}
if (mention['end-point'] && mention.slug) {
tagAttrs = tagAttrs.concat(makeAttr('href', mention['end-point'] + '/' + mention.slug));
}
else {
tagAttrs = tagAttrs.concat(makeAttr('href', 'javascript:void(0)'));
}
if (mention.target) {
tagAttrs = tagAttrs.concat(makeAttr('target', mention.target));
}
return tagAttrs;
}
var styles = this.getCssStyles();
var styleAttr = styles.length ? [makeAttr('style', styles.join(';'))] : [];
Expand Down
2 changes: 1 addition & 1 deletion dist/commonjs/QuillDeltaToHtmlConverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ var QuillDeltaToHtmlConverter = (function () {
var htmlParts = converter.getHtmlParts();
if (op.isCodeBlock()) {
return htmlParts.openingTag +
ops.map(function (op) { return op.insert.value; }).join(value_types_1.NewLine)
ops.map(function (op) { return op.insert.value; }).join("")
+ htmlParts.closingTag;
}
var inlines = ops.map(function (op) {
Expand Down
10 changes: 0 additions & 10 deletions dist/commonjs/value-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,3 @@ var GroupType = {
Video: 'video'
};
exports.GroupType = GroupType;
var MentionType = {
Name: 'name',
Target: 'target',
Slug: 'slug',
Class: 'class',
Avatar: 'avatar',
Id: 'id',
EndPoint: 'end-point'
};
exports.MentionType = MentionType;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "quill-delta-to-html",
"version": "0.5.5",
"version": "0.5.7",
"description": "Converts Quill's delta ops to HTML",
"main": "dist/commonjs/main.js",
"dependencies": {},
Expand Down
2 changes: 1 addition & 1 deletion src/DeltaInsertOp.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import { NewLine, ListType, DataType } from './value-types';
import { IOpAttributes } from './IOpAttributes';
import { IOpAttributes } from "./OpAttributeSanitizer";
import { InsertData } from './InsertData';

class DeltaInsertOp {
Expand Down
32 changes: 0 additions & 32 deletions src/IOpAttributes.ts

This file was deleted.

47 changes: 39 additions & 8 deletions src/OpAttributeSanitizer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,36 @@

import { IOpAttributes } from './IOpAttributes';
import { ListType, AlignType, DirectionType, ScriptType } from './value-types';
import { ListType, AlignType, DirectionType, ScriptType} from './value-types';
import {MentionSanitizer} from "./mentions/MentionSanitizer";
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
}

class OpAttributeSanitizer {

Expand All @@ -15,7 +44,7 @@ class OpAttributeSanitizer {

let {
font, size, link, script, list, header, align, direction, indent,
mentions, width
mentions, mention, width
} = dirtyAttrs;

['bold', 'italic', 'underline', 'strike', 'code', 'blockquote', 'code-block']
Expand Down Expand Up @@ -74,10 +103,12 @@ class OpAttributeSanitizer {
cleanAttrs.indent = Math.min(Number(indent), 30);
}

if (mentions) {
let { mention } = dirtyAttrs;
cleanAttrs.mentions = mentions;
cleanAttrs.mention = mention;
if (mentions && mention) {
let sanitizedMention = MentionSanitizer.sanitize(mention);
if (Object.keys(sanitizedMention).length > 0) {
cleanAttrs.mentions = !!mentions;
cleanAttrs.mention = mention;
}
}

return cleanAttrs;
Expand All @@ -104,4 +135,4 @@ class OpAttributeSanitizer {
}
}

export { OpAttributeSanitizer }
export { OpAttributeSanitizer, IOpAttributes }
Loading

0 comments on commit 564db89

Please sign in to comment.