-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #200 from stevermeister/paste-service
make possible to filter content that is provided to editor (for example via copy-paste)
- Loading branch information
Showing
10 changed files
with
123 additions
and
4 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
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
31 changes: 31 additions & 0 deletions
31
projects/ngx-wig/src/lib/ngx-wig-filter-styles.service.spec.ts
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { NgxWigFilterStylesService } from './ngx-wig-filter-styles.service'; | ||
|
||
describe('NgxWigFilterStylesService', () => { | ||
let service: NgxWigFilterStylesService; | ||
|
||
beforeEach(() => { | ||
service = new NgxWigFilterStylesService(); | ||
}); | ||
|
||
it('should create an instance', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
|
||
it('should remove style attributes from HTML string', () => { | ||
const htmlString = '<div style="color: red;">Hello, world!</div>'; | ||
const expectedOutput = '<div>Hello, world!</div>'; | ||
expect(service.filter(htmlString)).toEqual(expectedOutput); | ||
}); | ||
|
||
it('should remove style elements from HTML string', () => { | ||
const htmlString = '<style>.red { color: red; }</style><div>Hello, world!</div>'; | ||
const expectedOutput = '<div>Hello, world!</div>'; | ||
expect(service.filter(htmlString)).toEqual(expectedOutput); | ||
}); | ||
|
||
it('should handle empty HTML string', () => { | ||
const htmlString = ''; | ||
const expectedOutput = ''; | ||
expect(service.filter(htmlString)).toEqual(expectedOutput); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Injectable } from "@angular/core"; | ||
|
||
@Injectable({ | ||
providedIn: "root", | ||
}) | ||
export class NgxWigFilterStylesService { | ||
constructor() {} | ||
public filter(htmlString: string): string { | ||
// Parse the HTML string into a DOM object | ||
const parser: DOMParser = new DOMParser(); | ||
const doc: Document = parser.parseFromString(htmlString, "text/html"); | ||
|
||
// Remove all style attributes from all elements | ||
const elementsWithStyle: NodeListOf<Element> = | ||
doc.querySelectorAll("[style]"); | ||
elementsWithStyle.forEach((el: Element) => el.removeAttribute("style")); | ||
|
||
// Remove all <style> elements | ||
const styleElements: NodeListOf<HTMLStyleElement> = | ||
doc.querySelectorAll("style"); | ||
styleElements.forEach((el: HTMLStyleElement) => | ||
el.parentNode?.removeChild(el) | ||
); | ||
|
||
// Get the inner HTML of the body element | ||
const bodyInnerHTML: string = doc.body.innerHTML; | ||
|
||
return bodyInnerHTML; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { NgxWigFilterService } from './ngx-wig-filter.service'; | ||
|
||
describe('NgxWigFilterService', () => { | ||
let service: NgxWigFilterService; | ||
|
||
beforeEach(() => { | ||
service = new NgxWigFilterService(); | ||
}); | ||
|
||
it('should create an instance', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
|
||
it('should return the same content when filtering', () => { | ||
const content = 'Hello, world!'; | ||
expect(service.filter(content)).toEqual(content); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Injectable } from '@angular/core'; | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class NgxWigFilterService { | ||
constructor() {} | ||
public filter(content: string): string { | ||
return content; | ||
} | ||
} |
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