Skip to content

Commit

Permalink
fix: the editor cannot apply empty string attributes to wysiwyg node (n…
Browse files Browse the repository at this point in the history
…hn#1781)

* fix: add controls attribute for video

* fix: set attritutes properly with empty string

* chore: add test case(empty string attributes)

* fix: remove attribute when value is null or undefined

* chore: apply code review
  • Loading branch information
js87zz committed Aug 31, 2021
1 parent 4bb1d7f commit a47c4d5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
18 changes: 18 additions & 0 deletions src/__test__/unit/editor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,24 @@ describe('editor', () => {

expect(wwEditor.innerHTML).toContain(result);
});

it('should keep the html attributes with an empty string after changing the mode', () => {
createEditor({
el: container,
initialValue: '<iframe width="" height="" src=""></iframe>',
previewHighlight: false,
// add iframe html block renderer
customHTMLRenderer: createHTMLrenderer(),
});

editor.changeMode('wysiwyg');

const result = oneLineTrim`
<iframe width="" height="" src="" class="html-block"></iframe>
`;

expect(wwEditor.innerHTML).toContain(result);
});
});

describe('hooks option', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/sanitizer/htmlSanitizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const reHTMLAttr = new RegExp(
'ismap|lang|language|nohref|nowrap|rel|rev|rows|rules|' +
'scope|scrolling|shape|size|span|start|summary|tabindex|target|title|type|' +
'valign|value|vspace|width|checked|mathvariant|encoding|id|name|' +
'background|cite|href|longdesc|src|usemap|xlink:href|data-+|checked|style)',
'background|cite|href|longdesc|src|usemap|xlink:href|data-+|checked|style|controls)',
'g'
);

Expand Down
2 changes: 1 addition & 1 deletion src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export function quote(text: string) {
return result[0] + text + result[1];
}

function isNil(value: unknown): value is null | undefined {
export function isNil(value: unknown): value is null | undefined {
return isNull(value) || isUndefined(value);
}

Expand Down
7 changes: 4 additions & 3 deletions src/utils/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import addClass from 'tui-code-snippet/domUtil/addClass';
import removeClass from 'tui-code-snippet/domUtil/removeClass';
import matches from 'tui-code-snippet/domUtil/matches';
import { HTML_TAG, OPEN_TAG } from './constants';
import { isNil } from './common';

export function isPositionInBox(style: CSSStyleDeclaration, offsetX: number, offsetY: number) {
const left = parseInt(style.left, 10);
Expand Down Expand Up @@ -229,10 +230,10 @@ export function prependNode(node: Element, appended: string | ArrayLike<Element>

export function setAttributes(attributes: Record<string, any>, element: HTMLElement) {
Object.keys(attributes).forEach((attrName) => {
if (attributes[attrName]) {
element.setAttribute(attrName, attributes[attrName]);
} else {
if (isNil(attributes[attrName])) {
element.removeAttribute(attrName);
} else {
element.setAttribute(attrName, attributes[attrName]);
}
});
}
Expand Down

0 comments on commit a47c4d5

Please sign in to comment.