Skip to content

Commit 61c9f29

Browse files
authored
Merge pull request #1348 from mathjax/fix/style-sanitize
Better sanitization of styles.
2 parents 98a31f5 + e9d899a commit 61c9f29

File tree

5 files changed

+10
-7
lines changed

5 files changed

+10
-7
lines changed

testsuite/tests/input/tex/Base.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2503,7 +2503,7 @@ describe('MmlToken', () => {
25032503

25042504
it('Token Invalid Attribute', () => {
25052505
expectTexError('\\mmlToken{mi}[m1=true]{}')
2506-
.toBe('Invalid MathML attribute: m1=true');
2506+
.toBe('Invalid MathML attribute: m1');
25072507
});
25082508

25092509
/********************************************************************************/

testsuite/tests/input/tex/Noerrors.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ describe('NoError', () => {
288288
toXmlMatch(
289289
tex2mml('\\mmlToken{mi}[m1=true]{}'),
290290
`<math xmlns="http://www.w3.org/1998/Math/MathML" data-latex="\\mmlToken{mi}[m1=true]{}" display="block">
291-
<merror data-mjx-error="Invalid MathML attribute: m1=true" title="Invalid MathML attribute: m1=true">
291+
<merror data-mjx-error="Invalid MathML attribute: m1" title="Invalid MathML attribute: m1">
292292
<mtext>\\mmlToken{mi}[m1=true]{}</mtext>
293293
</merror>
294294
</math>`

ts/adaptors/HTMLAdaptor.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,9 @@ export class HTMLAdaptor<
463463
*/
464464
public setAttribute(node: N, name: string, value: string, ns: string = null) {
465465
if (!ns) {
466+
if (name === 'style') {
467+
value = value.replace(/\n/g, ' ');
468+
}
466469
return node.setAttribute(name, value);
467470
}
468471
name = ns.replace(/.*\//, '') + ':' + name.replace(/^.*:/, '');
@@ -538,14 +541,14 @@ export class HTMLAdaptor<
538541
* @override
539542
*/
540543
public setStyle(node: N, name: string, value: string) {
541-
(node.style as OptionList)[name] = value;
544+
node.style[name] = String(value).replace(/\n/g, ' ');
542545
}
543546

544547
/**
545548
* @override
546549
*/
547550
public getStyle(node: N, name: string) {
548-
return (node.style as OptionList)[name];
551+
return node.style[name];
549552
}
550553

551554
/**

ts/input/tex/base/BaseMethods.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,14 +1089,14 @@ const BaseMethods: { [key: string]: ParseMethod } = {
10891089
}
10901090
while (attr !== '') {
10911091
const match = attr.match(
1092-
/^([a-z]+)\s*=\s*('[^']*'|"[^"]*"|[^ ,]*)\s*,?\s*/i
1092+
/^([a-z]+)\s*=\s*('[^'\n]*'|"[^"\n]*"|[^ ,\n]*)[\s\n]*,?[\s\n]*/i
10931093
);
10941094
if (!match) {
10951095
// @test Token Invalid Attribute
10961096
throw new TexError(
10971097
'InvalidMathMLAttr',
10981098
'Invalid MathML attribute: %1',
1099-
attr
1099+
attr.split(/[\s\n=]/)[0]
11001100
);
11011101
}
11021102
if (!node.attributes.hasDefault(match[1]) && !MmlTokenAllow[match[1]]) {

ts/util/Styles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export const WSC = ['width', 'style', 'color'];
5656
* @returns {string[]} Array of parts of the style (separated by spaces)
5757
*/
5858
function splitSpaces(text: string): string[] {
59-
const parts = text.split(/((?:'[^']*'|"[^"]*"|,[\s\n]|[^\s\n])*)/g);
59+
const parts = text.split(/((?:'[^'\n]*'|"[^"\n]*"|,[\s\n]|[^\s\n])*)/g);
6060
const split = [] as string[];
6161
while (parts.length > 1) {
6262
parts.shift();

0 commit comments

Comments
 (0)