Skip to content

Commit

Permalink
Fixed the bug that caused inline code to ignore other inline elements
Browse files Browse the repository at this point in the history
  • Loading branch information
nozer committed Aug 28, 2018
1 parent c73b02b commit cd93ce3
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 44 deletions.
20 changes: 11 additions & 9 deletions dist/browser/QuillDeltaToHtmlConverter.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ var DeltaInsertOp = (function () {
DeltaInsertOp.prototype.isLink = function () {
return this.isText() && !!this.attributes.link;
};
DeltaInsertOp.prototype.isAnchorLink = function () {
return this.isLink() && this.attributes.link.indexOf('#') === 0;
};
DeltaInsertOp.prototype.isCustom = function () {
return this.insert instanceof InsertData_1.InsertDataCustom;
};
Expand Down Expand Up @@ -402,7 +405,7 @@ var OpToHtmlConverter = (function () {
.map(function (item) { return arr.preferSecond(item) + ':' + attrs[item[0]]; });
};
OpToHtmlConverter.prototype.getTagAttributes = function () {
if (this.op.attributes.code) {
if (this.op.attributes.code && !this.op.isLink()) {
return [];
}
var makeAttr = function (k, v) { return ({ key: k, value: v }); };
Expand Down Expand Up @@ -441,10 +444,12 @@ var OpToHtmlConverter = (function () {
var styleAttr = styles.length ? [makeAttr('style', styles.join(';'))] : [];
tagAttrs = tagAttrs.concat(styleAttr);
if (this.op.isLink()) {
var target = this.op.attributes.target || this.options.linkTarget;
tagAttrs = tagAttrs
.concat(makeAttr('href', funcs_html_1.encodeLink(this.op.attributes.link)))
.concat(target ? makeAttr('target', target) : []);
.concat(makeAttr('href', funcs_html_1.encodeLink(this.op.attributes.link)));
if (!this.op.isAnchorLink()) {
var target = this.op.attributes.target || this.options.linkTarget;
tagAttrs = tagAttrs.concat(target ? makeAttr('target', target) : []);
}
if (!!this.options.linkRel && OpToHtmlConverter.IsValidRel(this.options.linkRel)) {
tagAttrs.push(makeAttr('rel', this.options.linkRel));
}
Expand All @@ -453,9 +458,6 @@ var OpToHtmlConverter = (function () {
};
OpToHtmlConverter.prototype.getTags = function () {
var attrs = this.op.attributes;
if (attrs.code) {
return ['code'];
}
if (!this.op.isText()) {
return [this.op.isVideo() ? 'iframe'
: this.op.isImage() ? 'img'
Expand All @@ -474,9 +476,9 @@ var OpToHtmlConverter = (function () {
return firstItem === 'header' ? ['h' + attrs[firstItem]] : [arr.preferSecond(item)];
}
}
return [['link', 'a'], ['script'],
return [['link', 'a'], ['mentions', 'a'], ['script'],
['bold', 'strong'], ['italic', 'em'], ['strike', 's'], ['underline', 'u'],
['mentions', 'a']]
['code']]
.filter(function (item) { return !!attrs[item[0]]; })
.map(function (item) {
return item[0] === 'script' ?
Expand Down
1 change: 1 addition & 0 deletions dist/commonjs/DeltaInsertOp.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ declare class DeltaInsertOp {
isFormula(): boolean;
isVideo(): boolean;
isLink(): boolean;
isAnchorLink(): boolean;
isCustom(): boolean;
isMentions(): boolean;
}
Expand Down
3 changes: 3 additions & 0 deletions dist/commonjs/DeltaInsertOp.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ var DeltaInsertOp = (function () {
DeltaInsertOp.prototype.isLink = function () {
return this.isText() && !!this.attributes.link;
};
DeltaInsertOp.prototype.isAnchorLink = function () {
return this.isLink() && this.attributes.link.indexOf('#') === 0;
};
DeltaInsertOp.prototype.isCustom = function () {
return this.insert instanceof InsertData_1.InsertDataCustom;
};
Expand Down
17 changes: 8 additions & 9 deletions dist/commonjs/OpToHtmlConverter.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ var OpToHtmlConverter = (function () {
.map(function (item) { return arr.preferSecond(item) + ':' + attrs[item[0]]; });
};
OpToHtmlConverter.prototype.getTagAttributes = function () {
if (this.op.attributes.code) {
if (this.op.attributes.code && !this.op.isLink()) {
return [];
}
var makeAttr = function (k, v) { return ({ key: k, value: v }); };
Expand Down Expand Up @@ -123,10 +123,12 @@ var OpToHtmlConverter = (function () {
var styleAttr = styles.length ? [makeAttr('style', styles.join(';'))] : [];
tagAttrs = tagAttrs.concat(styleAttr);
if (this.op.isLink()) {
var target = this.op.attributes.target || this.options.linkTarget;
tagAttrs = tagAttrs
.concat(makeAttr('href', funcs_html_1.encodeLink(this.op.attributes.link)))
.concat(target ? makeAttr('target', target) : []);
.concat(makeAttr('href', funcs_html_1.encodeLink(this.op.attributes.link)));
if (!this.op.isAnchorLink()) {
var target = this.op.attributes.target || this.options.linkTarget;
tagAttrs = tagAttrs.concat(target ? makeAttr('target', target) : []);
}
if (!!this.options.linkRel && OpToHtmlConverter.IsValidRel(this.options.linkRel)) {
tagAttrs.push(makeAttr('rel', this.options.linkRel));
}
Expand All @@ -135,9 +137,6 @@ var OpToHtmlConverter = (function () {
};
OpToHtmlConverter.prototype.getTags = function () {
var attrs = this.op.attributes;
if (attrs.code) {
return ['code'];
}
if (!this.op.isText()) {
return [this.op.isVideo() ? 'iframe'
: this.op.isImage() ? 'img'
Expand All @@ -156,9 +155,9 @@ var OpToHtmlConverter = (function () {
return firstItem === 'header' ? ['h' + attrs[firstItem]] : [arr.preferSecond(item)];
}
}
return [['link', 'a'], ['script'],
return [['link', 'a'], ['mentions', 'a'], ['script'],
['bold', 'strong'], ['italic', 'em'], ['strike', 's'], ['underline', 'u'],
['mentions', 'a']]
['code']]
.filter(function (item) { return !!attrs[item[0]]; })
.map(function (item) {
return item[0] === 'script' ?
Expand Down
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.9.8",
"version": "0.9.9",
"description": "Converts Quill's delta ops to HTML",
"main": "./dist/commonjs/main.js",
"types": "./dist/commonjs/main.d.ts",
Expand Down
4 changes: 4 additions & 0 deletions src/DeltaInsertOp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ class DeltaInsertOp {
return this.isText() && !!this.attributes.link;
}

isAnchorLink() {
return this.isLink() && this.attributes.link!.indexOf('#') === 0
}

isCustom() {
return this.insert instanceof InsertDataCustom;
// return !(this.isText() ||
Expand Down
20 changes: 9 additions & 11 deletions src/OpToHtmlConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class OpToHtmlConverter {
}

getTagAttributes(): Array<ITagKeyValue> {
if (this.op.attributes.code) {
if (this.op.attributes.code && !this.op.isLink()) {
return [];
}

Expand Down Expand Up @@ -182,10 +182,13 @@ class OpToHtmlConverter {

tagAttrs = tagAttrs.concat(styleAttr);
if (this.op.isLink()) {
let target = this.op.attributes.target || this.options.linkTarget;
tagAttrs = tagAttrs
.concat(makeAttr('href', encodeLink(this.op.attributes.link!)))
.concat(target ? makeAttr('target', target) : []);
.concat(makeAttr('href', encodeLink(this.op.attributes.link!)));
//.concat(target ? makeAttr('target', target) : []);
if (!this.op.isAnchorLink()) {
let target = this.op.attributes.target || this.options.linkTarget;
tagAttrs = tagAttrs.concat(target ? makeAttr('target', target) : []);
}
if (!!this.options.linkRel && OpToHtmlConverter.IsValidRel(this.options.linkRel)) {
tagAttrs.push(makeAttr('rel', this.options.linkRel));
}
Expand All @@ -197,11 +200,6 @@ class OpToHtmlConverter {
getTags(): string[] {
var attrs: any = this.op.attributes;

// code
if (attrs.code) {
return ['code'];
}

// embeds
if (!this.op.isText()) {
return [this.op.isVideo() ? 'iframe'
Expand All @@ -225,9 +223,9 @@ class OpToHtmlConverter {
}

// inlines
return [['link', 'a'], ['script'],
return [['link', 'a'], ['mentions', 'a'], ['script'],
['bold', 'strong'], ['italic', 'em'], ['strike', 's'], ['underline', 'u'],
['mentions', 'a']]
['code']]
.filter((item: string[]) => !!attrs[item[0]])
.map((item) => {
return item[0] === 'script' ?
Expand Down
24 changes: 12 additions & 12 deletions test/QuillDeltaToHtmlConverter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,32 +132,32 @@ describe('QuillDeltaToHtmlConverter', function () {
});
it('should render target attr correctly', () => {
let ops = [
{ "attributes": { "target": "_self", "link": "#" }, "insert": "A" },
{ "attributes": { "target": "_blank", "link": "#" }, "insert": "B" },
{ "attributes": { "link": "#" }, "insert": "C" }, { "insert": "\n" }
{ "attributes": { "target": "_self", "link": "http://#" }, "insert": "A" },
{ "attributes": { "target": "_blank", "link": "http://#" }, "insert": "B" },
{ "attributes": { "link": "http://#" }, "insert": "C" }, { "insert": "\n" }
];
let qdc = new QuillDeltaToHtmlConverter(ops, { linkTarget: '' });
let html = qdc.convert();
assert.equal(html, [
`<p><a href="#" target="_self">A</a>`,
`<a href="#" target="_blank">B</a>`,
`<a href="#">C</a></p>`
`<p><a href="http://#" target="_self">A</a>`,
`<a href="http://#" target="_blank">B</a>`,
`<a href="http://#">C</a></p>`
].join(''));

qdc = new QuillDeltaToHtmlConverter(ops);
html = qdc.convert();
assert.equal(html, [
`<p><a href="#" target="_self">A</a>`,
`<a href="#" target="_blank">B</a>`,
`<a href="#" target="_blank">C</a></p>`
`<p><a href="http://#" target="_self">A</a>`,
`<a href="http://#" target="_blank">B</a>`,
`<a href="http://#" target="_blank">C</a></p>`
].join(''));

qdc = new QuillDeltaToHtmlConverter(ops, { linkTarget: '_top' });
html = qdc.convert();
assert.equal(html, [
`<p><a href="#" target="_self">A</a>`,
`<a href="#" target="_blank">B</a>`,
`<a href="#" target="_top">C</a></p>`
`<p><a href="http://#" target="_self">A</a>`,
`<a href="http://#" target="_blank">B</a>`,
`<a href="http://#" target="_top">C</a></p>`
].join(''));
});
});
Expand Down
7 changes: 5 additions & 2 deletions test/data/delta1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ var delta1 = {
{ "insert": "\n", "attributes": { "list": "bullet" } },
{ "insert": "list" },
{ "insert": "\n", "attributes": { "list": "checked" } },

{"insert": "some code", "attributes":{code:true, bold:true}},
{"attributes":{"italic":true,"link":"#top","code":true},"insert":"Top"},{"insert":"\n"},
],
html: [
'<p>', '<a href="http://a.com/?x=a&amp;b=&#40;&#41;" target="_blank">link</a>', 'This ', '<span class="noz-font-monospace">is</span>',
Expand All @@ -33,7 +34,9 @@ var delta1 = {
'<a href="http://yahoo" target="_blank">inline</a>', ' ', '<span class="noz-formula">x=data</span>',
' formats.</p>',
'<ul><li>list</li></ul>',
'<ul><li data-checked="true">list</li></ul>'
'<ul><li data-checked="true">list</li></ul>',
'<p><strong><code>some code</code></strong>',
'<a href="#top"><em><code>Top</code></em></a></p>'
].join('')
};

Expand Down

0 comments on commit cd93ce3

Please sign in to comment.