8000 [Web Components] Use Light DOM instead of Shadow DOM by tushuhei · Pull Request #291 · google/budoux · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[Web Components] Use Light DOM instead of Shadow DOM #291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions javascript/src/tests/test_webcomponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('Web Components', () => {
mirroredElement.textContent = inputText;
parser.applyElement(mirroredElement);

expect(budouxElement.shadowRoot!.innerHTML).toBe(mirroredElement.outerHTML);
expect(budouxElement.innerHTML).toBe(mirroredElement.outerHTML);
});

it('should react to the text content change after attached.', resolve => {
Expand All @@ -47,13 +47,12 @@ describe('Web Components', () => {
const mirroredElement = window.document.createElement('span');
mirroredElement.textContent = inputText;
parser.applyElement(mirroredElement);
const budouxShadowRoot = budouxElement.shadowRoot!;

const observer = new window.MutationObserver(() => {
expect(budouxShadowRoot.innerHTML).toBe(mirroredElement.outerHTML);
expect(budouxElement.innerHTML).toBe(mirroredElement.outerHTML);
resolve();
});
observer.observe(budouxShadowRoot, {
observer.observe(budouxElement, {
childList: true,
});

Expand Down
23 changes: 13 additions & 10 deletions javascript/src/webcomponents/budoux-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,19 @@
import {setInnerHtml} from '../dom.js';
import {HTMLProcessingParser} from '../html_processor.js';

const MUTATION_OBSERVER_OPTIONS = {
attributes: false,
characterData: true,
childList: true,
subtree: true,
};

/**
* Base BudouX Web component.
*/
export abstract class BudouXBaseElement extends HTMLElement {
shadow: ShadowRoot;
parser: HTMLProcessingParser;
observer: MutationObserver;

/**
* Base BudouX Web component constructor.
Expand All @@ -31,14 +38,8 @@ export abstract class BudouXBaseElement extends HTMLElement {
super();

this.parser = new HTMLProcessingParser({});
this.shadow = this.attachShadow({mode: 'open'});
const observer = new MutationObserver(this.sync.bind(this));
observer.observe(this, {
attributes: false,
characterData: true,
childList: true,
subtree: true,
});
this.observer = new MutationObserver(this.sync.bind(this));
this.observer.observe(this, MUTATION_OBSERVER_OPTIONS);
}

connectedCallback() {
Expand All @@ -50,6 +51,8 @@ export abstract class BudouXBaseElement extends HTMLElement {
}

sync() {
setInnerHtml(this.shadow, this.parser.translateHTMLString(this.innerHTML));
this.observer.disconnect();
setInnerHtml(this, this.parser.translateHTMLString(this.innerHTML));
this.observer.observe(this, MUTATION_OBSERVER_OPTIONS);
}
}
0