-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
32 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,4 +33,3 @@ export class UIPTextSetting extends UIPSetting { | |
return true; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,34 @@ | ||
import {TraversingQuery} from '@exadel/esl'; | ||
|
||
export class UIPStateModel { | ||
public html: string; | ||
protected root: Element; | ||
|
||
protected get root(): Element { | ||
return new DOMParser().parseFromString(this.html, 'text/html').body; | ||
public set html(markup: string) { | ||
this.root = new DOMParser().parseFromString(markup, 'text/html').body; | ||
} | ||
|
||
public get html(): string { | ||
return this.root.innerHTML; | ||
} | ||
|
||
public getAttribute(target: string, name: string): (string | null)[] { | ||
return TraversingQuery.all(target, this.root).map(el => el.getAttribute(name)); | ||
} | ||
|
||
public setAttribute(target: string, name: string, value: string | boolean): void { | ||
const root = this.root; | ||
const elements = TraversingQuery.all(target, root); | ||
const elements = TraversingQuery.all(target, this.root); | ||
|
||
if (typeof value === 'string') { | ||
elements.forEach(el => el.setAttribute(name, value)); | ||
} else { | ||
elements.forEach(el => value ? el.setAttribute(name, '') : el.removeAttribute(name)); | ||
} | ||
|
||
this.html = root.innerHTML; | ||
} | ||
|
||
public transformAttribute(target: string, name: string, transform: (current: string | null) => string | null) { | ||
const root = this.root; | ||
const values = this.getAttribute(target, name); | ||
|
||
TraversingQuery.all(target, root).forEach((el, index) => { | ||
const transformed = transform(values[index]); | ||
transformed ? el.setAttribute(name, transformed) : el.removeAttribute(name); | ||
}); | ||
|
||
this.html = root.innerHTML; | ||
TraversingQuery.all(target, this.root).forEach(el => { | ||
const transformed = transform(el.getAttribute(name)); | ||
transformed === null ? el.removeAttribute(name) : el.setAttribute(name, transformed); | ||
}); | ||
} | ||
} |