-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding support for RoXMLElement
#599
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f7429ed
added code for RoXMLElement
44e73d8
fix wrong comparation
ea5ebbb
added test for EoXMLElement
fc3feb3
fix wrong code style
81f881c
fix crash
135748b
resolve PR comments about RoXMLElement implementation
982bb1b
resolve PR commetns for RoXMLElement tests
0a2a8e3
add e2e test for roXMLElement
8588d9a
remove unused string parameter in getAttributes and getName
301cca0
fix typing on RoXmlElement#xmlNode
7d7847f
fix(test): Remove test.only
sjbarag File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { RoAssociativeArray } from "./RoAssociativeArray"; | ||
import { RoArray } from "./RoArray"; | ||
import { BrsComponent } from "./BrsComponent"; | ||
import { BrsBoolean, BrsString, BrsValue, ValueKind } from "../BrsType"; | ||
import { BrsType } from ".."; | ||
import { Callable, StdlibArgument } from "../Callable"; | ||
import { Interpreter } from "../../interpreter"; | ||
import { XmlDocument, XmlElement } from "xmldoc"; | ||
|
||
export class RoXMLElement extends BrsComponent implements BrsValue { | ||
readonly kind = ValueKind.Object; | ||
private xmlNode: any; | ||
|
||
constructor() { | ||
super("roXMLElement"); | ||
|
||
this.registerMethods({ | ||
ifXMLElement: [this.parse, this.getName, this.getNamedElementsCi, this.getAttributes], | ||
}); | ||
} | ||
|
||
toString(parent?: BrsType) { | ||
return "<Component: roXMLElement>"; | ||
} | ||
|
||
equalTo(other: BrsType) { | ||
return BrsBoolean.False; | ||
} | ||
|
||
private parse = new Callable("parse", { | ||
signature: { | ||
args: [new StdlibArgument("str", ValueKind.String)], | ||
returns: ValueKind.Boolean, | ||
}, | ||
impl: (interpreter: Interpreter, str: BrsString) => { | ||
try { | ||
this.xmlNode = new XmlDocument(str.value); | ||
return BrsBoolean.True; | ||
} catch (err) { | ||
this.xmlNode = undefined; | ||
return BrsBoolean.False; | ||
} | ||
}, | ||
}); | ||
|
||
private getName = new Callable("getName", { | ||
signature: { | ||
args: [], | ||
returns: ValueKind.String, | ||
}, | ||
impl: (interpreter: Interpreter, str: BrsString) => { | ||
return new BrsString(this.xmlNode?.name ?? ""); | ||
}, | ||
}); | ||
|
||
private getNamedElementsCi = new Callable("getNamedElementsCi", { | ||
signature: { | ||
args: [new StdlibArgument("str", ValueKind.String)], | ||
returns: ValueKind.Object, | ||
}, | ||
impl: (interpreter: Interpreter, str: BrsString) => { | ||
let neededKey = str.value.toLowerCase(); | ||
let arr = []; | ||
for (let item of this.xmlNode?.children ?? []) { | ||
if (item.name?.toLowerCase() === neededKey) { | ||
let childXmlElement = new RoXMLElement(); | ||
childXmlElement.xmlNode = item; | ||
arr.push(childXmlElement); | ||
} | ||
} | ||
return new RoArray(arr); | ||
}, | ||
}); | ||
|
||
private getAttributes = new Callable("getAttributes", { | ||
signature: { | ||
args: [], | ||
returns: ValueKind.Object, | ||
}, | ||
impl: (interpreter: Interpreter, str: BrsString) => { | ||
sjbarag marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let array = []; | ||
let attrs = this.xmlNode?.attr ?? {}; | ||
for (let key in attrs) { | ||
array.push({ | ||
name: new BrsString(key), | ||
value: new BrsString(attrs[key]), | ||
}); | ||
} | ||
return new RoAssociativeArray(array); | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
const brs = require("brs"); | ||
const { BrsString, RoXMLElement } = brs.types; | ||
const { Interpreter } = require("../../../lib/interpreter"); | ||
|
||
describe("RoXMLElement", () => { | ||
let xmlParser; | ||
let interpreter; | ||
|
||
beforeEach(() => { | ||
xmlParser = new RoXMLElement(); | ||
interpreter = new Interpreter(); | ||
}); | ||
|
||
describe("test methods for object with successful parsed xml", () => { | ||
beforeEach(() => { | ||
let parse = xmlParser.getMethod("parse"); | ||
parse.call(interpreter, new BrsString(getXmlString())); | ||
}); | ||
|
||
it("getName", () => { | ||
let getName = xmlParser.getMethod("getName"); | ||
expect(getName.call(interpreter)).toEqual(new BrsString("tag1")); | ||
}); | ||
|
||
it("getNamedElementsCi", () => { | ||
let getNamedElementsCi = xmlParser.getMethod("getNamedElementsCi"); | ||
expect(getNamedElementsCi.call(interpreter, new BrsString("any")).elements).toEqual([]); | ||
|
||
let children = getNamedElementsCi.call(interpreter, new BrsString("CHiLd1")); | ||
expect(children.elements.length).toEqual(2); | ||
|
||
let getName = children.elements[0].getMethod("getName"); | ||
expect(getName.call(interpreter)).toEqual(new BrsString("Child1")); | ||
|
||
getName = children.elements[1].getMethod("getName"); | ||
expect(getName.call(interpreter)).toEqual(new BrsString("CHILD1")); | ||
}); | ||
|
||
it("getAttributes", () => { | ||
let getAttributes = xmlParser.getMethod("getAttributes"); | ||
expect(getAttributes.call(interpreter).elements).not.toEqual(new Map()); | ||
expect(getAttributes.call(interpreter).elements).toEqual( | ||
new Map([ | ||
["id", new BrsString("someId")], | ||
["attr1", new BrsString("0")], | ||
]) | ||
); | ||
}); | ||
}); | ||
|
||
describe.each([ | ||
["test methods for object with no parsed xml", () => {}], | ||
[ | ||
"test methods for object with failed parsing of xml", | ||
() => { | ||
let parse = xmlParser.getMethod("parse"); | ||
parse.call(interpreter, new BrsString('>bad_tag id="12" < some text >/bad_tag<')); | ||
}, | ||
], | ||
])("%s", (name, tryParse) => { | ||
beforeEach(() => { | ||
tryParse(); | ||
}); | ||
|
||
it("getName", () => { | ||
let getName = xmlParser.getMethod("getName"); | ||
expect(getName).toBeTruthy(); | ||
expect(getName.call(interpreter)).toEqual(new BrsString("")); | ||
}); | ||
|
||
it("getNamedElementsCi", () => { | ||
let getNamedElementsCi = xmlParser.getMethod("getNamedElementsCi"); | ||
expect(getNamedElementsCi).toBeTruthy(); | ||
expect(getNamedElementsCi.call(interpreter, new BrsString("any")).elements).toEqual([]); | ||
}); | ||
|
||
it("getAttributes", () => { | ||
let getAttributes = xmlParser.getMethod("getAttributes"); | ||
expect(getAttributes).toBeTruthy(); | ||
expect(getAttributes.call(interpreter).elements).toEqual(new Map()); | ||
}); | ||
}); | ||
|
||
describe("test parse method with different xml strings", () => { | ||
test.each([ | ||
["<tag>some text<tag>", true], | ||
["<tag>some text <child1> child's text </child1> </tag>", true], | ||
[getXmlString(), true], | ||
['>bad_tag id="12" < some text >/bad_tag<', false], | ||
["", false], | ||
])("test parse with string %s", (xmlString, expected) => { | ||
let parse = xmlParser.getMethod("parse"); | ||
expect(parse.call(interpreter, new BrsString(xmlString)).value).toBe(expected); | ||
}); | ||
}); | ||
}); | ||
|
||
function getXmlString() { | ||
return '<tag1 id="someId" attr1="0"> <Child1 id="id1"></Child1> <CHILD1 id="id2"></CHILD1> </tag1>'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
sub main() | ||
xmlParser = createObject("roXMLElement") | ||
?"xmlParser = "xmlParser | ||
?"type(xmlParser) = "type(xmlParser) | ||
?"parse bad xml string, result = "xmlParser.parse("some_xml_doc") | ||
?"parse good xml string, result = "xmlParser.parse("<tag1 id=""someId"" attr1=""0""> <Child1 id=""id1""></Child1> <CHILD1 id=""id2""></CHILD1> </tag1>") | ||
?"getName() = " xmlParser.getName() | ||
?"getAttributes() = " xmlParser.getAttributes() | ||
children = xmlParser.getNamedElementsCi("child1") | ||
?"getNamedElementsCi(""child1"") count = " children.count() | ||
?"name of first child = "children[0].getName() | ||
?"mame of second child = "children[1].getName() | ||
end sub |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does
XmlDocument
return anything to indicate that the string was parsed correctly? something like a boolean value? or does it throws an exception in every case?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It throws an exception only if the XML can not be parsed at all, but it doesn't if it can parse at least part of XML and it returns true in that case. The similar behavior we have on Roku.