Skip to content

Commit b577f84

Browse files
committed
Merge pull request #1125 from NativeScript/cankov/xml-source-tracing
Cankov/xml source tracing
2 parents b55c16c + 5447b04 commit b577f84

File tree

14 files changed

+753
-361
lines changed

14 files changed

+753
-361
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Page xmlns="http://schemas.nativescript.org/tns.xsd"
2+
xmlns:tc="xml-declaration/template-builder-tests/template-view">
3+
<tc:TemplateView id="template-view">
4+
<tc:TemplateView.template>
5+
<StackLayout>
6+
<!--
7+
At first I was going to put "MenuItem",
8+
as per https://github.com/NativeScript/NativeScript/issues/501,
9+
but then again we may re-introduce "MenuItem" in future and blow this test up.
10+
So here is something unique that has better chance not to appear.
11+
This comment also offsets error's row and column numbers so if you edit,
12+
please do so beyond the unicorn.
13+
-->
14+
<Unicorn backgroundColor="pink" />
15+
<Label text="Modal Page" />
16+
</StackLayout>
17+
</tc:TemplateView.template>
18+
</tc:TemplateView>
19+
</Page>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Page shownModally="onShownModally">
2+
<StackLayout>
3+
<!--
4+
At first I was going to put "MenuItem",
5+
as per https://github.com/NativeScript/NativeScript/issues/501,
6+
but then again we may re-introduce "MenuItem" in future and blow this test up.
7+
So here is something unique that has better chance not to appear.
8+
This comment also offsets error's row and column numbers so if you edit,
9+
please do so beyond the unicorn.
10+
-->
11+
<Unicorn backgroundColor="pink" />
12+
<Label text="Modal Page" />
13+
</StackLayout>
14+
</Page>

apps/tests/xml-declaration/xml-declaration-tests.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,4 +835,49 @@ export function test_parse_template_property() {
835835
var button = <Button>templateView.getChildAt(0);
836836
TKUnit.assert(button, "Expected the TemplateView's template to create a button child.");
837837
TKUnit.assertEqual(button.text, "Click!", "Expected child Button to have text 'Click!'");
838+
}
839+
840+
export function test_NonExistingElementError() {
841+
var basePath = "xml-declaration/";
842+
var expectedErrorStart =
843+
"Building UI from XML. @file:///app/" + basePath + "errors/non-existing-element.xml:11:5\n" +
844+
" ↳Module 'ui/unicorn' not found for element 'Unicorn'.\n";
845+
if (global.android) {
846+
expectedErrorStart += " ↳Module \"ui/unicorn\" not found";
847+
} else {
848+
expectedErrorStart += " ↳Failed to find module 'ui/unicorn'";
849+
}
850+
851+
var message;
852+
try {
853+
builder.load(__dirname + "/errors/non-existing-element.xml");
854+
} catch(e) {
855+
message = e.message;
856+
}
857+
TKUnit.assertEqual(message.substr(0, expectedErrorStart.length), expectedErrorStart, "Expected load to throw, and the message to start with specific string");
858+
}
859+
860+
export function test_NonExistingElementInTemplateError() {
861+
var basePath = "xml-declaration/";
862+
var expectedErrorStart =
863+
"Building UI from XML. @file:///app/" + basePath + "errors/non-existing-element-in-template.xml:14:17\n" +
864+
" ↳Module 'ui/unicorn' not found for element 'Unicorn'.\n";
865+
if (global.android) {
866+
expectedErrorStart += " ↳Module \"ui/unicorn\" not found";
867+
} else {
868+
expectedErrorStart += " ↳Failed to find module 'ui/unicorn'";
869+
}
870+
871+
var message;
872+
var page = builder.load(__dirname + "/errors/non-existing-element-in-template.xml");
873+
TKUnit.assert(view, "Expected the xml to generate a page");
874+
var templateView = <TemplateView>page.getViewById("template-view");
875+
TKUnit.assert(templateView, "Expected the page to have a TemplateView with 'temaplte-view' id.");
876+
877+
try {
878+
templateView.parseTemplate();
879+
} catch(e) {
880+
message = e.message;
881+
}
882+
TKUnit.assertEqual(message.substr(0, expectedErrorStart.length), expectedErrorStart, "Expected load to throw, and the message to start with specific string");
838883
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"eventType":"StartElement","elementName":"DocumentElement","attributes":{"param":"value"}}{"eventType":"StartElement","elementName":"First.Element","attributes":{"some.attr":"some.value"}}{"eventType":"Text","data":"\n ¶ Some Text ®\n "}{"eventType":"EndElement","elementName":"First.Element"}{"eventType":"StartElement","elementName":"SecondElement","attributes":{"param2":"something"}}{"eventType":"Text","data":"\n Pre-Text "}{"eventType":"StartElement","elementName":"Inline"}{"eventType":"Text","data":"Inlined text"}{"eventType":"EndElement","elementName":"Inline"}{"eventType":"Text","data":" Post-text.\n "}{"eventType":"EndElement","elementName":"SecondElement"}{"eventType":"StartElement","elementName":"entities"}{"eventType":"Text","data":"Xml tags begin with \"<\" and end with \">\" Ampersand is & and apostrophe is '"}{"eventType":"EndElement","elementName":"entities"}{"eventType":"StartElement","elementName":"script"}{"eventType":"CDATA","data":"\nfunction sum(a,b)\n{\n return a+b;\n}\n"}{"eventType":"EndElement","elementName":"script"}{"eventType":"Comment","data":"\n Hello,\n I am a multi-line XML comment.\n"}{"eventType":"EndElement","elementName":"DocumentElement"}
1+
{"eventType":"StartElement","position":{"line":2,"column":1},"elementName":"DocumentElement","attributes":{"param":"value"}}{"eventType":"StartElement","position":{"line":3,"column":3},"elementName":"First.Element","attributes":{"some.attr":"some.value"}}{"eventType":"Text","position":{"line":3,"column":41},"data":"\n ¶ Some Text ®\n "}{"eventType":"EndElement","position":{"line":5,"column":3},"elementName":"First.Element"}{"eventType":"StartElement","position":{"line":7,"column":3},"elementName":"SecondElement","attributes":{"param2":"something"}}{"eventType":"Text","position":{"line":7,"column":37},"data":"\n Pre-Text "}{"eventType":"StartElement","position":{"line":8,"column":14},"elementName":"Inline"}{"eventType":"Text","position":{"line":8,"column":22},"data":"Inlined text"}{"eventType":"EndElement","position":{"line":8,"column":34},"elementName":"Inline"}{"eventType":"Text","position":{"line":8,"column":43},"data":" Post-text.\n "}{"eventType":"EndElement","position":{"line":9,"column":3},"elementName":"SecondElement"}{"eventType":"StartElement","position":{"line":10,"column":3},"elementName":"entities"}{"eventType":"Text","position":{"line":10,"column":13},"data":"Xml tags begin with \"<\" and end with \">\" Ampersand is & and apostrophe is '"}{"eventType":"EndElement","position":{"line":10,"column":123},"elementName":"entities"}{"eventType":"StartElement","position":{"line":11,"column":3},"elementName":"script"}{"eventType":"CDATA","position":{"line":12,"column":5},"data":"\nfunction sum(a,b)\n{\n return a+b;\n}\n"}{"eventType":"EndElement","position":{"line":18,"column":3},"elementName":"script"}{"eventType":"Comment","position":{"line":19,"column":3},"data":"\n Hello,\n I am a multi-line XML comment.\n"}{"eventType":"EndElement","position":{"line":23,"column":1},"elementName":"DocumentElement"}

js-libs/easysax/easysax.js

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -469,11 +469,24 @@ EasySAXParser.prototype.parse = function(xml) {
469469
, stop // используется при разборе "namespace" . если встретился неизвестное пространство то события не генерируются
470470
, _nsmatrix
471471
, ok
472+
, pos = 0, ln = 0, lnStart = -2, lnEnd = -1
472473
;
473474

474475
function getStringNode() {
475476
return xml.substring(i, j+1)
476477
};
478+
function findLineAndColumnFromPos() {
479+
while (lnStart < lnEnd && lnEnd < pos) {
480+
lnStart = lnEnd;
481+
lnEnd = xml.indexOf("\n", lnEnd + 1);
482+
++ln;
483+
}
484+
return { line: ln, column: pos - lnStart };
485+
}
486+
function position(p) {
487+
pos = p;
488+
return findLineAndColumnFromPos;
489+
}
477490

478491
while(j !== -1) {
479492
stop = stopIndex > 0;
@@ -487,15 +500,15 @@ EasySAXParser.prototype.parse = function(xml) {
487500
if (i === -1) { // конец разбора
488501

489502
if (nodestack.length) {
490-
this.onError('end file');
503+
this.onError('end file', position(j));
491504
return;
492505
};
493506

494507
return;
495508
};
496509

497510
if (j !== i && !stop) {
498-
ok = this.onTextNode(xml.substring(j, i), unEntities);
511+
ok = this.onTextNode(xml.substring(j, i), unEntities, position(j));
499512
if (ok === false) return;
500513
};
501514

@@ -506,13 +519,13 @@ EasySAXParser.prototype.parse = function(xml) {
506519
if (w === 91 && xml.substr(i+3, 6) === 'CDATA[') { // 91 == "["
507520
j = xml.indexOf(']]>', i);
508521
if (j === -1) {
509-
this.onError('cdata');
522+
this.onError('cdata', position(i));
510523
return;
511524
};
512525

513526
//x = xml.substring(i+9, j);
514527
if (!stop) {
515-
ok = this.onCDATA(xml.substring(i+9, j), false);
528+
ok = this.onCDATA(xml.substring(i+9, j), false, position(i));
516529
if (ok === false) return;
517530
};
518531

@@ -523,13 +536,13 @@ EasySAXParser.prototype.parse = function(xml) {
523536
if (w === 45 && xml.charCodeAt(i+3) === 45) { // 45 == "-"
524537
j = xml.indexOf('-->', i);
525538
if (j === -1) {
526-
this.onError('expected -->');
539+
this.onError('expected -->', position(i));
527540
return;
528541
};
529542

530543

531544
if (this.is_onComment && !stop) {
532-
ok = this.onComment(xml.substring(i+4, j), unEntities);
545+
ok = this.onComment(xml.substring(i+4, j), unEntities, position(i));
533546
if (ok === false) return;
534547
};
535548

@@ -539,12 +552,12 @@ EasySAXParser.prototype.parse = function(xml) {
539552

540553
j = xml.indexOf('>', i+1);
541554
if (j === -1) {
542-
this.onError('expected ">"');
555+
this.onError('expected ">"', position(i + 1));
543556
return;
544557
};
545558

546559
if (this.is_onAttention && !stop) {
547-
ok = this.onAttention(xml.substring(i, j+1), unEntities);
560+
ok = this.onAttention(xml.substring(i, j+1), unEntities, position(i));
548561
if (ok === false) return;
549562
};
550563

@@ -555,12 +568,12 @@ EasySAXParser.prototype.parse = function(xml) {
555568
if (w === 63) { // "?"
556569
j = xml.indexOf('?>', i);
557570
if (j === -1) { // error
558-
this.onError('...?>');
571+
this.onError('...?>', position(i));
559572
return;
560573
};
561574

562575
if (this.is_onQuestion) {
563-
ok = this.onQuestion(xml.substring(i, j+2));
576+
ok = this.onQuestion(xml.substring(i, j+2), position(i));
564577
if (ok === false) return;
565578
};
566579

@@ -572,7 +585,7 @@ EasySAXParser.prototype.parse = function(xml) {
572585
j = xml.indexOf('>', i+1);
573586

574587
if (j == -1) { // error
575-
this.onError('...>');
588+
this.onError('...>', position(i + 1));
576589
return;
577590
};
578591

@@ -589,7 +602,7 @@ EasySAXParser.prototype.parse = function(xml) {
589602

590603
//console.log()
591604
if (xml.substring(i+2, q) !== x) {
592-
this.onError('close tagname');
605+
this.onError('close tagname', position(i + 2));
593606
return;
594607
};
595608

@@ -601,7 +614,7 @@ EasySAXParser.prototype.parse = function(xml) {
601614
continue;
602615
};
603616

604-
this.onError('close tag');
617+
this.onError('close tag', position(i + 2));
605618
return;
606619
};
607620

@@ -619,7 +632,7 @@ EasySAXParser.prototype.parse = function(xml) {
619632
};
620633

621634
if ( !(w > 96 && w < 123 || w > 64 && w <91) ) {
622-
this.onError('first char nodeName');
635+
this.onError('first char nodeName', position(i + 1));
623636
return;
624637
};
625638

@@ -636,7 +649,7 @@ EasySAXParser.prototype.parse = function(xml) {
636649
break;
637650
};
638651

639-
this.onError('invalid nodeName');
652+
this.onError('invalid nodeName', position(i + 1));
640653
return;
641654
};
642655

@@ -718,7 +731,7 @@ EasySAXParser.prototype.parse = function(xml) {
718731

719732
var that = this;
720733
ok = this.onStartNode(elem, function() { return that.getAttrs() }, unEntities, tagend
721-
, getStringNode
734+
, getStringNode, position(i)
722735
);
723736

724737
if (ok === false) {
@@ -730,7 +743,7 @@ EasySAXParser.prototype.parse = function(xml) {
730743

731744
if (tagend) {
732745
ok = this.onEndNode(elem, unEntities, tagstart
733-
, getStringNode
746+
, getStringNode, position(i)
734747
);
735748

736749
if (ok === false) {

tsconfig.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,8 +319,8 @@
319319
"apps/tests/xml-declaration/mainPage.ts",
320320
"apps/tests/xml-declaration/mymodule/MyControl.ts",
321321
"apps/tests/xml-declaration/mymodulewithxml/MyControl.ts",
322-
"apps/tests/xml-declaration/xml-declaration-tests.ts",
323322
"apps/tests/xml-declaration/template-builder-tests/template-view.ts",
323+
"apps/tests/xml-declaration/xml-declaration-tests.ts",
324324
"apps/tests/xml-parser-tests/xml-parser-tests.ts",
325325
"apps/transforms/app.ts",
326326
"apps/transforms/main-page.ts",
@@ -478,8 +478,6 @@
478478
"ui/builder/component-builder.ts",
479479
"ui/builder/special-properties.d.ts",
480480
"ui/builder/special-properties.ts",
481-
"ui/builder/template-builder.d.ts",
482-
"ui/builder/template-builder.ts",
483481
"ui/button/button-common.ts",
484482
"ui/button/button.android.ts",
485483
"ui/button/button.d.ts",
@@ -658,6 +656,8 @@
658656
"ui/web-view/web-view.android.ts",
659657
"ui/web-view/web-view.d.ts",
660658
"ui/web-view/web-view.ios.ts",
659+
"utils/debug.d.ts",
660+
"utils/debug.ts",
661661
"utils/module-merge.ts",
662662
"utils/number-utils.ts",
663663
"utils/types.d.ts",

0 commit comments

Comments
 (0)