Skip to content
This repository was archived by the owner on May 4, 2020. It is now read-only.

Commit 43aa6a7

Browse files
author
Long Ho
committed
feat(intl-messageformat-parser): Allow - in embedded HTML tag
fix(intl-messageformat): Allow - in embedded HTML tag fix #545
1 parent 634967a commit 43aa6a7

File tree

4 files changed

+51
-11
lines changed

4 files changed

+51
-11
lines changed

packages/intl-messageformat-parser/src/parser.pegjs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ poundElement = '#' {
6565
tagElement 'tagElement'
6666
=
6767
// Special case for self-closing. We treat it as regular text
68-
value:('<' _ argNameOrNumber _ '/>') {
68+
value:('<' validTag _ '/>') {
6969
return {
7070
type: TYPE.literal,
7171
value: value.join(''),
@@ -84,11 +84,11 @@ tagElement 'tagElement'
8484
}
8585
}
8686

87-
openingTag = '<' &{ messageCtx.push('openingTag'); return true; } tag:argNameOrNumber '>' &{ messageCtx.pop(); return true; } {
87+
openingTag = '<' &{ messageCtx.push('openingTag'); return true; } tag:validTag '>' &{ messageCtx.pop(); return true; } {
8888
return tag
8989
}
9090

91-
closingTag = '</' &{ messageCtx.push('closingTag'); return true; } tag:argNameOrNumber '>' &{ messageCtx.pop(); return true; } {
91+
closingTag = '</' &{ messageCtx.push('closingTag'); return true; } tag:validTag '>' &{ messageCtx.pop(); return true; } {
9292
return tag
9393
}
9494

@@ -284,8 +284,10 @@ escapedChar = $(x:. &{
284284
})
285285

286286
argNameOrNumber 'argNameOrNumber' = $(argNumber / argName)
287+
validTag 'validTag' = $(argNumber / tagName)
287288
argNumber 'argNumber' = '0' { return 0 }
288289
/ digits:([1-9][0-9]*) {
289290
return parseInt(digits.join(''), 10);
290291
}
291-
argName 'argName' = $((!(whiteSpace / patternSyntax) .)+)
292+
argName 'argName' = $((!(whiteSpace / patternSyntax).)+)
293+
tagName 'tagName' = $(('-' / (!(whiteSpace / patternSyntax).))+)

packages/intl-messageformat-parser/test/__snapshots__/tag.test.ts.snap

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,43 @@ Array [
101101
]
102102
`;
103103

104+
exports[`tag with dash 1`] = `
105+
Array [
106+
Object {
107+
"type": 0,
108+
"value": "this is ",
109+
},
110+
Object {
111+
"type": 0,
112+
"value": "<br/>",
113+
},
114+
Object {
115+
"type": 0,
116+
"value": " ",
117+
},
118+
Object {
119+
"children": Array [
120+
Object {
121+
"type": 0,
122+
"value": "nested ",
123+
},
124+
Object {
125+
"children": Array [
126+
Object {
127+
"type": 1,
128+
"value": "placeholder",
129+
},
130+
],
131+
"type": 8,
132+
"value": "b",
133+
},
134+
],
135+
"type": 8,
136+
"value": "dash-tag",
137+
},
138+
]
139+
`;
140+
104141
exports[`tag with number arg 1`] = `
105142
Array [
106143
Object {

packages/intl-messageformat-parser/test/tag.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,9 @@ test('self-closing tag', function() {
4343
pegParse('this is <br/> <a>nested <b>{placeholder}</b></a>')
4444
).toMatchSnapshot();
4545
});
46+
47+
test('tag with dash', function() {
48+
expect(
49+
pegParse('this is <br/> <dash-tag>nested <b>{placeholder}</b></dash-tag>')
50+
).toMatchSnapshot();
51+
});

packages/intl-messageformat/README.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,16 +187,11 @@ new IntlMessageFormat(
187187
"raw '<b>HTML</b>' with '<a>'{placeholder}'</a>'"
188188
).format({placeholder: 'some word'}); // "raw <b>HTML</b> with <a>some word</a>"
189189
```
190+
5. Embedded valid HTML tag is a bit of a grey area right now since we're not supporting the full HTML/XHTML/XML spec.
190191

191192
### `getAst` Method
192193

193-
Return the underlying AST for the compiled message
194-
195-
#### Caveats
196-
197-
- List of self-closing tags is defined [here](https://html.spec.whatwg.org/multipage/syntax.html#void-elements).
198-
199-
If you don't have `DOMParser` available in your environment (e.g. Node.js), you need to add a polyfill. One of the packages that provides such a polyfill is [jsdom](https://www.npmjs.com/package/jsdom).
194+
Return the underlying AST for the compiled message.
200195

201196
### User Defined Formats
202197

0 commit comments

Comments
 (0)