Skip to content

Commit cd93ce3

Browse files
committed
Fixed the bug that caused inline code to ignore other inline elements
1 parent c73b02b commit cd93ce3

File tree

9 files changed

+54
-44
lines changed

9 files changed

+54
-44
lines changed

dist/browser/QuillDeltaToHtmlConverter.bundle.js

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ var DeltaInsertOp = (function () {
8989
DeltaInsertOp.prototype.isLink = function () {
9090
return this.isText() && !!this.attributes.link;
9191
};
92+
DeltaInsertOp.prototype.isAnchorLink = function () {
93+
return this.isLink() && this.attributes.link.indexOf('#') === 0;
94+
};
9295
DeltaInsertOp.prototype.isCustom = function () {
9396
return this.insert instanceof InsertData_1.InsertDataCustom;
9497
};
@@ -402,7 +405,7 @@ var OpToHtmlConverter = (function () {
402405
.map(function (item) { return arr.preferSecond(item) + ':' + attrs[item[0]]; });
403406
};
404407
OpToHtmlConverter.prototype.getTagAttributes = function () {
405-
if (this.op.attributes.code) {
408+
if (this.op.attributes.code && !this.op.isLink()) {
406409
return [];
407410
}
408411
var makeAttr = function (k, v) { return ({ key: k, value: v }); };
@@ -441,10 +444,12 @@ var OpToHtmlConverter = (function () {
441444
var styleAttr = styles.length ? [makeAttr('style', styles.join(';'))] : [];
442445
tagAttrs = tagAttrs.concat(styleAttr);
443446
if (this.op.isLink()) {
444-
var target = this.op.attributes.target || this.options.linkTarget;
445447
tagAttrs = tagAttrs
446-
.concat(makeAttr('href', funcs_html_1.encodeLink(this.op.attributes.link)))
447-
.concat(target ? makeAttr('target', target) : []);
448+
.concat(makeAttr('href', funcs_html_1.encodeLink(this.op.attributes.link)));
449+
if (!this.op.isAnchorLink()) {
450+
var target = this.op.attributes.target || this.options.linkTarget;
451+
tagAttrs = tagAttrs.concat(target ? makeAttr('target', target) : []);
452+
}
448453
if (!!this.options.linkRel && OpToHtmlConverter.IsValidRel(this.options.linkRel)) {
449454
tagAttrs.push(makeAttr('rel', this.options.linkRel));
450455
}
@@ -453,9 +458,6 @@ var OpToHtmlConverter = (function () {
453458
};
454459
OpToHtmlConverter.prototype.getTags = function () {
455460
var attrs = this.op.attributes;
456-
if (attrs.code) {
457-
return ['code'];
458-
}
459461
if (!this.op.isText()) {
460462
return [this.op.isVideo() ? 'iframe'
461463
: this.op.isImage() ? 'img'
@@ -474,9 +476,9 @@ var OpToHtmlConverter = (function () {
474476
return firstItem === 'header' ? ['h' + attrs[firstItem]] : [arr.preferSecond(item)];
475477
}
476478
}
477-
return [['link', 'a'], ['script'],
479+
return [['link', 'a'], ['mentions', 'a'], ['script'],
478480
['bold', 'strong'], ['italic', 'em'], ['strike', 's'], ['underline', 'u'],
479-
['mentions', 'a']]
481+
['code']]
480482
.filter(function (item) { return !!attrs[item[0]]; })
481483
.map(function (item) {
482484
return item[0] === 'script' ?

dist/commonjs/DeltaInsertOp.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ declare class DeltaInsertOp {
2727
isFormula(): boolean;
2828
isVideo(): boolean;
2929
isLink(): boolean;
30+
isAnchorLink(): boolean;
3031
isCustom(): boolean;
3132
isMentions(): boolean;
3233
}

dist/commonjs/DeltaInsertOp.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ var DeltaInsertOp = (function () {
8888
DeltaInsertOp.prototype.isLink = function () {
8989
return this.isText() && !!this.attributes.link;
9090
};
91+
DeltaInsertOp.prototype.isAnchorLink = function () {
92+
return this.isLink() && this.attributes.link.indexOf('#') === 0;
93+
};
9194
DeltaInsertOp.prototype.isCustom = function () {
9295
return this.insert instanceof InsertData_1.InsertDataCustom;
9396
};

dist/commonjs/OpToHtmlConverter.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ var OpToHtmlConverter = (function () {
8484
.map(function (item) { return arr.preferSecond(item) + ':' + attrs[item[0]]; });
8585
};
8686
OpToHtmlConverter.prototype.getTagAttributes = function () {
87-
if (this.op.attributes.code) {
87+
if (this.op.attributes.code && !this.op.isLink()) {
8888
return [];
8989
}
9090
var makeAttr = function (k, v) { return ({ key: k, value: v }); };
@@ -123,10 +123,12 @@ var OpToHtmlConverter = (function () {
123123
var styleAttr = styles.length ? [makeAttr('style', styles.join(';'))] : [];
124124
tagAttrs = tagAttrs.concat(styleAttr);
125125
if (this.op.isLink()) {
126-
var target = this.op.attributes.target || this.options.linkTarget;
127126
tagAttrs = tagAttrs
128-
.concat(makeAttr('href', funcs_html_1.encodeLink(this.op.attributes.link)))
129-
.concat(target ? makeAttr('target', target) : []);
127+
.concat(makeAttr('href', funcs_html_1.encodeLink(this.op.attributes.link)));
128+
if (!this.op.isAnchorLink()) {
129+
var target = this.op.attributes.target || this.options.linkTarget;
130+
tagAttrs = tagAttrs.concat(target ? makeAttr('target', target) : []);
131+
}
130132
if (!!this.options.linkRel && OpToHtmlConverter.IsValidRel(this.options.linkRel)) {
131133
tagAttrs.push(makeAttr('rel', this.options.linkRel));
132134
}
@@ -135,9 +137,6 @@ var OpToHtmlConverter = (function () {
135137
};
136138
OpToHtmlConverter.prototype.getTags = function () {
137139
var attrs = this.op.attributes;
138-
if (attrs.code) {
139-
return ['code'];
140-
}
141140
if (!this.op.isText()) {
142141
return [this.op.isVideo() ? 'iframe'
143142
: this.op.isImage() ? 'img'
@@ -156,9 +155,9 @@ var OpToHtmlConverter = (function () {
156155
return firstItem === 'header' ? ['h' + attrs[firstItem]] : [arr.preferSecond(item)];
157156
}
158157
}
159-
return [['link', 'a'], ['script'],
158+
return [['link', 'a'], ['mentions', 'a'], ['script'],
160159
['bold', 'strong'], ['italic', 'em'], ['strike', 's'], ['underline', 'u'],
161-
['mentions', 'a']]
160+
['code']]
162161
.filter(function (item) { return !!attrs[item[0]]; })
163162
.map(function (item) {
164163
return item[0] === 'script' ?

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "quill-delta-to-html",
3-
"version": "0.9.8",
3+
"version": "0.9.9",
44
"description": "Converts Quill's delta ops to HTML",
55
"main": "./dist/commonjs/main.js",
66
"types": "./dist/commonjs/main.d.ts",

src/DeltaInsertOp.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ class DeltaInsertOp {
123123
return this.isText() && !!this.attributes.link;
124124
}
125125

126+
isAnchorLink() {
127+
return this.isLink() && this.attributes.link!.indexOf('#') === 0
128+
}
129+
126130
isCustom() {
127131
return this.insert instanceof InsertDataCustom;
128132
// return !(this.isText() ||

src/OpToHtmlConverter.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class OpToHtmlConverter {
129129
}
130130

131131
getTagAttributes(): Array<ITagKeyValue> {
132-
if (this.op.attributes.code) {
132+
if (this.op.attributes.code && !this.op.isLink()) {
133133
return [];
134134
}
135135

@@ -182,10 +182,13 @@ class OpToHtmlConverter {
182182

183183
tagAttrs = tagAttrs.concat(styleAttr);
184184
if (this.op.isLink()) {
185-
let target = this.op.attributes.target || this.options.linkTarget;
186185
tagAttrs = tagAttrs
187-
.concat(makeAttr('href', encodeLink(this.op.attributes.link!)))
188-
.concat(target ? makeAttr('target', target) : []);
186+
.concat(makeAttr('href', encodeLink(this.op.attributes.link!)));
187+
//.concat(target ? makeAttr('target', target) : []);
188+
if (!this.op.isAnchorLink()) {
189+
let target = this.op.attributes.target || this.options.linkTarget;
190+
tagAttrs = tagAttrs.concat(target ? makeAttr('target', target) : []);
191+
}
189192
if (!!this.options.linkRel && OpToHtmlConverter.IsValidRel(this.options.linkRel)) {
190193
tagAttrs.push(makeAttr('rel', this.options.linkRel));
191194
}
@@ -197,11 +200,6 @@ class OpToHtmlConverter {
197200
getTags(): string[] {
198201
var attrs: any = this.op.attributes;
199202

200-
// code
201-
if (attrs.code) {
202-
return ['code'];
203-
}
204-
205203
// embeds
206204
if (!this.op.isText()) {
207205
return [this.op.isVideo() ? 'iframe'
@@ -225,9 +223,9 @@ class OpToHtmlConverter {
225223
}
226224

227225
// inlines
228-
return [['link', 'a'], ['script'],
226+
return [['link', 'a'], ['mentions', 'a'], ['script'],
229227
['bold', 'strong'], ['italic', 'em'], ['strike', 's'], ['underline', 'u'],
230-
['mentions', 'a']]
228+
['code']]
231229
.filter((item: string[]) => !!attrs[item[0]])
232230
.map((item) => {
233231
return item[0] === 'script' ?

test/QuillDeltaToHtmlConverter.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,32 +132,32 @@ describe('QuillDeltaToHtmlConverter', function () {
132132
});
133133
it('should render target attr correctly', () => {
134134
let ops = [
135-
{ "attributes": { "target": "_self", "link": "#" }, "insert": "A" },
136-
{ "attributes": { "target": "_blank", "link": "#" }, "insert": "B" },
137-
{ "attributes": { "link": "#" }, "insert": "C" }, { "insert": "\n" }
135+
{ "attributes": { "target": "_self", "link": "http://#" }, "insert": "A" },
136+
{ "attributes": { "target": "_blank", "link": "http://#" }, "insert": "B" },
137+
{ "attributes": { "link": "http://#" }, "insert": "C" }, { "insert": "\n" }
138138
];
139139
let qdc = new QuillDeltaToHtmlConverter(ops, { linkTarget: '' });
140140
let html = qdc.convert();
141141
assert.equal(html, [
142-
`<p><a href="#" target="_self">A</a>`,
143-
`<a href="#" target="_blank">B</a>`,
144-
`<a href="#">C</a></p>`
142+
`<p><a href="http://#" target="_self">A</a>`,
143+
`<a href="http://#" target="_blank">B</a>`,
144+
`<a href="http://#">C</a></p>`
145145
].join(''));
146146

147147
qdc = new QuillDeltaToHtmlConverter(ops);
148148
html = qdc.convert();
149149
assert.equal(html, [
150-
`<p><a href="#" target="_self">A</a>`,
151-
`<a href="#" target="_blank">B</a>`,
152-
`<a href="#" target="_blank">C</a></p>`
150+
`<p><a href="http://#" target="_self">A</a>`,
151+
`<a href="http://#" target="_blank">B</a>`,
152+
`<a href="http://#" target="_blank">C</a></p>`
153153
].join(''));
154154

155155
qdc = new QuillDeltaToHtmlConverter(ops, { linkTarget: '_top' });
156156
html = qdc.convert();
157157
assert.equal(html, [
158-
`<p><a href="#" target="_self">A</a>`,
159-
`<a href="#" target="_blank">B</a>`,
160-
`<a href="#" target="_top">C</a></p>`
158+
`<p><a href="http://#" target="_self">A</a>`,
159+
`<a href="http://#" target="_blank">B</a>`,
160+
`<a href="http://#" target="_top">C</a></p>`
161161
].join(''));
162162
});
163163
});

test/data/delta1.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ var delta1 = {
2020
{ "insert": "\n", "attributes": { "list": "bullet" } },
2121
{ "insert": "list" },
2222
{ "insert": "\n", "attributes": { "list": "checked" } },
23-
23+
{"insert": "some code", "attributes":{code:true, bold:true}},
24+
{"attributes":{"italic":true,"link":"#top","code":true},"insert":"Top"},{"insert":"\n"},
2425
],
2526
html: [
2627
'<p>', '<a href="http://a.com/?x=a&amp;b=&#40;&#41;" target="_blank">link</a>', 'This ', '<span class="noz-font-monospace">is</span>',
@@ -33,7 +34,9 @@ var delta1 = {
3334
'<a href="http://yahoo" target="_blank">inline</a>', ' ', '<span class="noz-formula">x=data</span>',
3435
' formats.</p>',
3536
'<ul><li>list</li></ul>',
36-
'<ul><li data-checked="true">list</li></ul>'
37+
'<ul><li data-checked="true">list</li></ul>',
38+
'<p><strong><code>some code</code></strong>',
39+
'<a href="#top"><em><code>Top</code></em></a></p>'
3740
].join('')
3841
};
3942

0 commit comments

Comments
 (0)