-
-
Notifications
You must be signed in to change notification settings - Fork 357
Support a new option "stopNodes". #150
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
amitguptagwl
merged 3 commits into
NaturalIntelligence:master
from
jinq102030:support_stopNodes
Mar 23, 2019
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,187 @@ | ||
| "use strict"; | ||
|
|
||
| const parser = require("../src/parser"); | ||
| const validator = require("../src/validator"); | ||
| const he = require("he"); | ||
|
|
||
| describe("XMLParser", function() { | ||
| it("1a. should support single stopNode", function() { | ||
| const xmlData = `<issue><title>test 1</title><fix1><p>p 1</p><div class="show">div 1</div></fix1></issue>`; | ||
| const expected = { | ||
| "issue": { | ||
| "title": "test 1", | ||
| "fix1": "<p>p 1</p><div class=\"show\">div 1</div>" | ||
| } | ||
| }; | ||
|
|
||
| let result = parser.parse(xmlData, { | ||
| attributeNamePrefix: "", | ||
| ignoreAttributes: false, | ||
| parseAttributeValue: true, | ||
| stopNodes: ["fix1"] | ||
| }); | ||
|
|
||
| //console.log(JSON.stringify(result,null,4)); | ||
| expect(result).toEqual(expected); | ||
|
|
||
| result = validator.validate(xmlData); | ||
| expect(result).toBe(true); | ||
| }); | ||
|
|
||
| it("1b. 3. should support more than one stopNodes, with or without attr", function() { | ||
| const xmlData = `<issue><title>test 1</title><fix1 lang="en"><p>p 1</p><div class="show">div 1</div></fix1><fix2><p>p 2</p><div class="show">div 2</div></fix2></issue>`; | ||
| const expected = { | ||
| "issue": { | ||
| "title": "test 1", | ||
| "fix1": { | ||
| "#text": "<p>p 1</p><div class=\"show\">div 1</div>", | ||
| "lang": "en" | ||
| }, | ||
| "fix2": "<p>p 2</p><div class=\"show\">div 2</div>" | ||
| } | ||
| }; | ||
|
|
||
| let result = parser.parse(xmlData, { | ||
| attributeNamePrefix: "", | ||
| ignoreAttributes: false, | ||
| parseAttributeValue: true, | ||
| stopNodes: ["fix1", "fix2"] | ||
| }); | ||
|
|
||
| //console.log(JSON.stringify(result,null,4)); | ||
| expect(result).toEqual(expected); | ||
|
|
||
| result = validator.validate(xmlData); | ||
| expect(result).toBe(true); | ||
| }); | ||
|
|
||
| it("1c. have two stopNodes, one within the other", function() { | ||
| const xmlData = `<issue><title>test 1</title><fix1 lang="en"><p>p 1</p><fix2><p>p 2</p><div class="show">div 2</div></fix2><div class="show">div 1</div></fix1></issue>`; | ||
| const expected = { | ||
| "issue": { | ||
| "title": "test 1", | ||
| "fix1": { | ||
| "#text": "<p>p 1</p><fix2><p>p 2</p><div class=\"show\">div 2</div></fix2><div class=\"show\">div 1</div>", | ||
| "lang": "en" | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| let result = parser.parse(xmlData, { | ||
| attributeNamePrefix: "", | ||
| ignoreAttributes: false, | ||
| parseAttributeValue: true, | ||
| stopNodes: ["fix1", "fix2"] | ||
| }); | ||
|
|
||
| //console.log(JSON.stringify(result,null,4)); | ||
| expect(result).toEqual(expected); | ||
|
|
||
| result = validator.validate(xmlData); | ||
| expect(result).toBe(true); | ||
| }); | ||
|
|
||
| it("2a. stop node has nothing in it", function() { | ||
| const xmlData = `<issue><title>test 1</title><fix1></fix1></issue>`; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a test when fix1 is a self closing tag.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. Thanks for the suggestion. |
||
| const expected = { | ||
| "issue": { | ||
| "title": "test 1", | ||
| "fix1": "" | ||
| } | ||
| }; | ||
|
|
||
| let result = parser.parse(xmlData, { | ||
| attributeNamePrefix: "", | ||
| ignoreAttributes: false, | ||
| parseAttributeValue: true, | ||
| stopNodes: ["fix1", "fix2"] | ||
| }); | ||
|
|
||
| //console.log(JSON.stringify(result,null,4)); | ||
| expect(result).toEqual(expected); | ||
|
|
||
| result = validator.validate(xmlData); | ||
| expect(result).toBe(true); | ||
| }); | ||
|
|
||
| it("2b. stop node is self-closing", function() { | ||
| const xmlData = `<issue><title>test 1</title><fix1/></issue>`; | ||
| const expected = { | ||
| "issue": { | ||
| "title": "test 1", | ||
| "fix1": "" | ||
| } | ||
| }; | ||
|
|
||
| let result = parser.parse(xmlData, { | ||
| attributeNamePrefix: "", | ||
| ignoreAttributes: false, | ||
| parseAttributeValue: true, | ||
| stopNodes: ["fix1", "fix2"] | ||
| }); | ||
|
|
||
| //console.log(JSON.stringify(result,null,4)); | ||
| expect(result).toEqual(expected); | ||
|
|
||
| result = validator.validate(xmlData); | ||
| expect(result).toBe(true); | ||
| }); | ||
|
|
||
| it("4. cdata", function() { | ||
| const xmlData = `<?xml version='1.0'?> | ||
| <issue> | ||
| <fix1> | ||
| <phone>+122233344550</phone> | ||
| <fix2><![CDATA[<fix1>Jack</fix1>]]><![CDATA[Jack]]></fix2> | ||
| <name><![CDATA[<some>Mohan</some>]]></name> | ||
| <blank><![CDATA[]]></blank> | ||
| <regx><![CDATA[^[ ].*$]]></regx> | ||
| </fix1> | ||
| <fix2> | ||
| <![CDATA[<some>Mohan</some>]]> | ||
| </fix2> | ||
| </issue>`; | ||
| const expected = { | ||
| "issue": { | ||
| "fix1": "\n <phone>+122233344550</phone>\n <fix2><![CDATA[<fix1>Jack</fix1>]]><![CDATA[Jack]]></fix2>\n <name><![CDATA[<some>Mohan</some>]]></name>\n <blank><![CDATA[]]></blank>\n <regx><![CDATA[^[ ].*$]]></regx>\n ", | ||
| "fix2": "\n\t\t<![CDATA[<some>Mohan</some>]]>\n\t" | ||
| } | ||
| }; | ||
|
|
||
| let result = parser.parse(xmlData, { | ||
| attributeNamePrefix: "", | ||
| ignoreAttributes: false, | ||
| parseAttributeValue: true, | ||
| stopNodes: ["fix1", "fix2"] | ||
| }); | ||
|
|
||
| //console.log(JSON.stringify(result,null,4)); | ||
| expect(result).toEqual(expected); | ||
|
|
||
| result = validator.validate(xmlData, { | ||
| allowBooleanAttributes: true | ||
| }); | ||
| expect(result).toBe(true); | ||
| }); | ||
|
|
||
| it("5. stopNode at root level", function() { | ||
| const xmlData = `<fix1><p>p 1</p><div class="show">div 1</div></fix1>`; | ||
| const expected = { | ||
| "fix1": "<p>p 1</p><div class=\"show\">div 1</div>" | ||
| }; | ||
|
|
||
| let result = parser.parse(xmlData, { | ||
| attributeNamePrefix: "", | ||
| ignoreAttributes: false, | ||
| stopNodes: ["fix1", "fix2"] | ||
| }); | ||
|
|
||
| //console.log(JSON.stringify(result,null,4)); | ||
| expect(result).toEqual(expected); | ||
|
|
||
| result = validator.validate(xmlData, { | ||
| allowBooleanAttributes: true | ||
| }); | ||
| expect(result).toBe(true); | ||
| }); | ||
| }); | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please write a test when fix2 is inside fix1. In this case, fix2 should be empty in the result.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Thanks for the suggestion.