Browse Source

generate html should keep id when script-export is set (#6276)

release-v0.22.2-rc.0
Singwai Chan 1 year ago
committed by GitHub
parent
commit
e369c03285
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 1
      packages/core/src/code_manager/model/HtmlGenerator.ts
  2. 31
      packages/core/test/specs/code_manager/model/CodeModels.js

1
packages/core/src/code_manager/model/HtmlGenerator.ts

@ -34,6 +34,7 @@ export default class HTMLGenerator extends Model {
id &&
id[0] === 'i' && // all autogenerated IDs start with 'i'
!mod.get('script') && // if the component has script, we have to leave the ID
!mod.get('script-export') && // if the component has script, we have to leave the ID
!mod.get('attributes')!.id && // id is not intentionally in attributes
idRules.indexOf(id) < 0 // we shouldn't have any rule with this ID
) {

31
packages/core/test/specs/code_manager/model/CodeModels.js

@ -62,6 +62,37 @@ describe('HtmlGenerator', () => {
});
expect(obj.build(m1)).toEqual('<article data-test1="value1" data-test2="value2" class="class1 class2"></article>');
});
test('Build correctly component with id preserved when script is defined', () => {
const m1 = comp.get('components').add({
tagName: 'article',
});
m1.set('script', 'anything');
expect(obj.build(m1, { cleanId: true, em })).toEqual(`<article id="${m1.getId()}"></article>`);
});
test('Build correctly component with id preserved when script-export is defined', () => {
const m1 = comp.get('components').add({
tagName: 'article',
});
m1.set('script-export', 'anything');
expect(obj.build(m1, { cleanId: true, em })).toEqual(`<article id="${m1.getId()}"></article>`);
});
test('Build correctly component with id preserved when id is explicitly set ', () => {
const m1 = comp.get('components').add({
tagName: 'article',
});
m1.setId('i11');
expect(obj.build(m1, { cleanId: true, em })).toEqual(`<article id="i11"></article>`);
});
test('Build correctly component with cleanId is enabled and id is not required ', () => {
const m1 = comp.get('components').add({
tagName: 'article',
});
expect(obj.build(m1, { cleanId: true, em })).toEqual(`<article></article>`);
});
});
describe('CssGenerator', () => {

Loading…
Cancel
Save