Skip to content

Commit aa11224

Browse files
committed
Parse out attributes without values.
Treat #myattr as #myAttr=''.
1 parent fdd8c9b commit aa11224

File tree

2 files changed

+75
-26
lines changed

2 files changed

+75
-26
lines changed

js-libs/easysax/easysax.js

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,12 @@ function EasySAXParser() {
237237
, attr_list = hasSurmiseNS ? [] : false
238238
, name, value = ''
239239
, ok = false
240+
, noValueAttribute = false
240241
, j, w, nn, n
241242
, hasNewMatrix
242243
, alias, newalias
243244
;
244245

245-
246246
aa:
247247
for(; i < l; i++) {
248248
w = s.charCodeAt(i);
@@ -251,7 +251,7 @@ function EasySAXParser() {
251251
continue
252252
};
253253

254-
if ((w < 65 && w !== 40 && w !== 41) || // allow parens () -- used for angular syntax
254+
if ((w < 65 && w !== 40 && w !== 41 && w !== 35) || // allow parens () -- used for angular syntax
255255
w > 122 || (w === 92 || (w > 93 && w < 97)) ) { // ожидаем символ
256256
return attr_res = false; // error. invalid char
257257
};
@@ -260,21 +260,28 @@ function EasySAXParser() {
260260
w = s.charCodeAt(j);
261261

262262
if (w > 96 && w < 123 || w > 64 && w < 91 || w > 47 && w < 59 || w === 45 || w === 95 || w === 46 /* https://github.com/telerik/xPlatCore/issues/179 */) {
263-
continue;
263+
if (noValueAttribute) {
264+
j--; //Started next attribute. Get back and break out of the loop.
265+
break;
266+
} else {
267+
continue;
268+
}
264269
};
265270

266-
if (w === 32 || (w > 8 && w < 14) ) { // \f\n\r\t\v пробел
267-
continue;
268-
};
269-
270-
if (w === 91 || w === 93 || w === 40 || w === 41 || w === 94) { // Angular special attribute chars:[]()^
271+
if (w === 91 || w === 93 || w === 40 || w === 41 || w === 94 || w === 35) { // Angular special attribute chars:[]()^
271272
continue;
272273
}
273274

274-
275-
if (w !== 61) { // "=" == 61
275+
if (w === 32 || (w > 8 && w < 14) ) { // \f\n\r\t\v пробел
276+
noValueAttribute = true;
277+
continue;
278+
} else if (w === 61) { // "=" == 61
279+
noValueAttribute = false;
280+
break;
281+
} else {
276282
//console.log('error 2');
277-
return attr_res = false; // error. invalid char
283+
if (!noValueAttribute)
284+
return attr_res = false; // error. invalid char
278285
};
279286

280287
break;
@@ -298,38 +305,45 @@ function EasySAXParser() {
298305
}
299306
}
300307

301-
if (w === 34) { // '"'
302-
j = s.indexOf('"', i = j+2 );
308+
if (!noValueAttribute) {
309+
if (w === 34) { // '"'
310+
j = s.indexOf('"', i = j+2 );
303311

304-
} else {
305-
if (w === 39) {
306-
j = s.indexOf('\'', i = j+2 );
312+
} else {
313+
if (w === 39) {
314+
j = s.indexOf('\'', i = j+2 );
307315

308-
} else { // "'"
309-
//console.log('error 3')
310-
return attr_res = false; // error. invalid char
311-
};
312-
};
316+
} else { // "'"
317+
return attr_res = false; // error. invalid char
318+
};
319+
};
320+
}
313321

314322
if (j === -1) {
315323
//console.log('error 4')
316324
return attr_res = false; // error. invalid char
317325
};
318326

319327

320-
if (j+1 < l) {
328+
if (j+1 < l && !noValueAttribute) {
321329
w = s.charCodeAt(j+1);
322330

323-
if (w > 32 || w < 9 || (w<32 && w>13)) {
331+
if (w > 32 || w < 9 || (w < 32 && w > 13)) {
324332
// error. invalid char
325333
//console.log('error 5')
326334
return attr_res = false;
327335
};
328336
};
329337

330338

331-
value = s.substring(i, j);
332-
i = j + 1; // след. семвол уже проверен потому проверять нужно следуюший
339+
if (noValueAttribute) {
340+
value = '';
341+
} else {
342+
value = s.substring(i, j);
343+
}
344+
345+
//i = j + 1; // след. семвол уже проверен потому проверять нужно следуюший
346+
i = j; // след. семвол уже проверен потому проверять нужно следуюший
333347

334348
if (isNamespace) { //
335349
if (hasSurmiseNS) {
@@ -382,6 +396,7 @@ function EasySAXParser() {
382396
};
383397

384398
res[name] = value;
399+
noValueAttribute = false;
385400
};
386401

387402

node-tests/test-angular-xml.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,44 @@ describe("angular xml parser", () => {
3131
assert.equal(last_attrs['(tap)'], 'onTap(blah)');
3232
});
3333

34-
it("parsers (^event) binding", () => {
34+
it("parses (^event) binding", () => {
3535
parser.parse("<TextField (^tap)='onTap(blah)' />");
3636

3737
assert.equal('TextField', last_element);
3838
assert.equal(last_attrs['(^tap)'], 'onTap(blah)');
3939
});
40+
41+
it("parses #id attribute", () => {
42+
parser.parse("<TextField #firstName />");
43+
44+
assert.equal('TextField', last_element);
45+
assert.equal(last_attrs['#firstName'], '');
46+
});
47+
48+
it("parses #id attribute followed by another", () => {
49+
parser.parse("<TextField #firstName text='Name' />");
50+
51+
assert.equal('TextField', last_element);
52+
assert.equal(last_attrs['#firstName'], '');
53+
assert.equal(last_attrs['text'], 'Name');
54+
});
55+
56+
return
57+
it("detects equals without value", () => {
58+
parser.parse("<TextField brokenTag= />");
59+
60+
assert.isFalse(last_attrs);
61+
});
62+
63+
it("detects no equals with quoted value", () => {
64+
parser.parse("<TextField noEquals 'value' />");
65+
66+
assert.isFalse(last_attrs);
67+
});
68+
69+
it("detects unclosed tag after no value attribute", () => {
70+
parser.parse("<TextField #myId");
71+
72+
assert.isFalse(last_attrs);
73+
});
4074
});

0 commit comments

Comments
 (0)