-
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 #61 from ericnorris/disallowed-tags-option
Closes #32.
- Loading branch information
Showing
7 changed files
with
154 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
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
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,28 +1,69 @@ | ||
import { StateMachine, striptags } from "../src/striptags"; | ||
import { StateMachineOptions, StateMachine, striptags } from "../src/striptags"; | ||
|
||
const OptionsWithAllowedTags: Partial<StateMachineOptions> = { | ||
allowedTags: new Set(["atag"]), | ||
tagReplacementText: " ", | ||
}; | ||
|
||
const OptionsWithDisallowedTags: Partial<StateMachineOptions> = { | ||
disallowedTags: new Set(["atag"]), | ||
tagReplacementText: " ", | ||
}; | ||
|
||
const ExampleText = `<atag someattr="value">some text<btag>more text</btag></atag>`; | ||
|
||
const WantWhenUsingDefault = "some textmore text"; | ||
const WantWhenUsingAllowedTags = `<atag someattr="value">some text more text </atag>`; | ||
const WantWhenUsingDisallowedTags = " some text<btag>more text</btag> "; | ||
|
||
describe("StateMachine", () => { | ||
it("sanity check", () => { | ||
const machine = new StateMachine({ allowedTags: new Set(["atag"]) }); | ||
it("defaults sanity check", () => { | ||
const machine = new StateMachine(); | ||
|
||
const got = ExampleText.split(/(?= )/g) | ||
.map((partial) => machine.consume(partial)) | ||
.join(""); | ||
|
||
expect(got).toEqual(WantWhenUsingDefault); | ||
}); | ||
|
||
it("allowed tags sanity check", () => { | ||
const machine = new StateMachine(OptionsWithAllowedTags); | ||
|
||
const want = "a string </atag some attr> some text"; | ||
const got = ExampleText.split(/(?= )/g) | ||
.map((partial) => machine.consume(partial)) | ||
.join(""); | ||
|
||
const got = | ||
machine.consume("a string </a") + | ||
machine.consume("tag some attr> <a") + | ||
machine.consume("nothertag> some text"); | ||
expect(got).toEqual(WantWhenUsingAllowedTags); | ||
}); | ||
|
||
it("disallowed tags sanity check", () => { | ||
const machine = new StateMachine(OptionsWithDisallowedTags); | ||
|
||
expect(got).toEqual(want); | ||
const got = ExampleText.split(/(?= )/g) | ||
.map((partial) => machine.consume(partial)) | ||
.join(""); | ||
|
||
expect(got).toEqual(WantWhenUsingDisallowedTags); | ||
}); | ||
}); | ||
|
||
describe("striptags", () => { | ||
it("sanity check", () => { | ||
const want = "a string </atag some attr> some text"; | ||
it("defaults sanity check", () => { | ||
const got = striptags(ExampleText); | ||
|
||
expect(got).toEqual(WantWhenUsingDefault); | ||
}); | ||
|
||
it("allowed tags sanity check", () => { | ||
const got = striptags(ExampleText, OptionsWithAllowedTags); | ||
|
||
expect(got).toEqual(WantWhenUsingAllowedTags); | ||
}); | ||
|
||
const got = striptags("a string </atag some attr> <anothertag> some text", { | ||
allowedTags: new Set(["atag"]), | ||
}); | ||
it("disallowed tags sanity check", () => { | ||
const got = striptags(ExampleText, OptionsWithDisallowedTags); | ||
|
||
expect(got).toEqual(want); | ||
expect(got).toEqual(WantWhenUsingDisallowedTags); | ||
}); | ||
}); |