Skip to content

Commit

Permalink
Retain quote char when transpiling xml attributes (#552)
Browse files Browse the repository at this point in the history
  • Loading branch information
TwitchBronBron authored Apr 1, 2022
1 parent 02b7f70 commit a54f17b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/files/XmlFile.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,25 @@ describe('XmlFile', () => {
});

describe('transpile', () => {
it('handles single quotes properly', () => {
testTranspile(trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="AnimationExample" extends="Scene">
<children>
<Animated frames='["pkg:/images/animation-1.png"]' />
</children>
</component>
`, trim`
<?xml version="1.0" encoding="utf-8" ?>
<component name="AnimationExample" extends="Scene">
<script type="text/brightscript" uri="pkg:/source/bslib.brs" />
<children>
<Animated frames='["pkg:/images/animation-1.png"]' />
</children>
</component>
`, 'none', 'components/Comp.xml');
});

it('supports instantresume <customization> elements', async () => {
fsExtra.outputFileSync(`${rootDir}/manifest`, '');
fsExtra.outputFileSync(`${rootDir}/source/main.brs`, `sub main()\nend sub`);
Expand Down
18 changes: 18 additions & 0 deletions src/parser/SGParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,9 +305,27 @@ function mapAttributes(attributes: AttributeCstNode[]): SGAttribute[] {
return attributes?.map(({ children }) => {
const key = children.Name[0];
const value = children.STRING?.[0];

let openQuote: SGToken;
let closeQuote: SGToken;
//capture the leading and trailing quote tokens
const match = /^(["']).*?(["'])$/.exec(value?.image);
if (match) {
const range = rangeFromTokenValue(value);
openQuote = {
text: match[1],
range: util.createRange(range.start.line, range.start.character, range.start.line, range.start.character + 1)
};
closeQuote = {
text: match[1],
range: util.createRange(range.end.line, range.end.character - 1, range.end.line, range.end.character)
};
}
return {
key: mapToken(key),
openQuote: openQuote,
value: mapToken(value, true),
closeQuote: closeQuote,
range: rangeFromTokens(key, value)
};
}) || [];
Expand Down
7 changes: 5 additions & 2 deletions src/parser/SGTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ export interface SGToken {

export interface SGAttribute {
key: SGToken;
openQuote?: SGToken;
value: SGToken;
closeQuote?: SGToken;
range?: Range;
}

Expand Down Expand Up @@ -77,9 +79,10 @@ export class SGTag {
result.push(
' ',
state.transpileToken(attr.key),
'="',
'=',
state.transpileToken(attr.openQuote ?? { text: '"' }),
state.transpileToken(attr.value),
'"'
state.transpileToken(attr.closeQuote ?? { text: '"' })
);
}
return result;
Expand Down

0 comments on commit a54f17b

Please sign in to comment.