Skip to content

Commit

Permalink
Fix for issue oozcitak#105
Browse files Browse the repository at this point in the history
Entitities do not contain spaces, changed regex accordingly.
  • Loading branch information
sladage committed Sep 15, 2021
1 parent 174e755 commit a7ad0d5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/writers/BaseWriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ export abstract class BaseWriter<T extends BaseWriterOptions, U extends XMLSeria
* 5. Replace any occurrences of ">" in markup by "&gt;".
* 6. Return the value of markup.
*/
const markup = node.data.replace(/(?!&([^&;]*);)&/g, '&amp;')
const markup = node.data.replace(/(?!&([^&; ]*);)&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')

Expand Down Expand Up @@ -1597,7 +1597,7 @@ export abstract class BaseWriter<T extends BaseWriterOptions, U extends XMLSeria
* grammar requirement in the XML specification's AttValue production by
* also replacing ">" characters.
*/
return value.replace(/(?!&([^&;]*);)&/g, '&amp;')
return value.replace(/(?!&([^&; ]*);)&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
Expand Down
11 changes: 11 additions & 0 deletions test/issues/issue-105.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import $$ from "../TestHelpers";

describe("Replicate issue", () => {
// https://github.com/oozcitak/xmlbuilder2/issues/90
test(`#105 - Illegal character does not get sanitized.`, () => {
const b = $$.create()
b.ele('doc').ele('test').txt('some & text; foo')
expect(b.end()).toBe($$.t`<?xml version="1.0"?><doc><test>some &amp; text; foo</test></doc>`);
})

})

0 comments on commit a7ad0d5

Please sign in to comment.