Skip to content
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

Add handling for CDATA #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ node_modules
coverage
lib
*.tgz
**/package-lock.json
.idea/
**/*.iml
20 changes: 20 additions & 0 deletions packages/xmldom-decorators-test/src/xmlserializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ class TextInRoot {
value: string = "";
}

@XMLRoot()
class CDataTextInRoot {
@XMLAttribute()
name: string = "";

@XMLText({cdata: () => true})
value: string = "";
}

@XMLRoot()
class IntegerInRoot {
@XMLElement()
Expand Down Expand Up @@ -279,6 +288,17 @@ export class SetOfTests {
expect(x).toEqual(o);
}

@Test("CDATA text in root")
public cdataTextInRoot() {
const o: CDataTextInRoot = { name: "Hello CDATA", value: "CData text" };
const result = serialize(o, CDataTextInRoot);
expect(result).toBe('<CDataTextInRoot name="Hello CDATA"><![CDATA[CData text]]></CDataTextInRoot>');

const x: any = deserialize(result, CDataTextInRoot);
console.log(x)
expect(x).toEqual(o);
}

@Test("String in root")
public stringInRoot(){
const o: StringInRoot = { name: "Hello World" };
Expand Down
6 changes: 6 additions & 0 deletions packages/xmldom-decorators/src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ type FactoryWriter = (value: any, ctx: SerializerContext) => string;
type FactoryTuple = [FactoryReader, FactoryWriter];
type TypeGetter = () => Function;
type IsTypeGetter = (o: any) => boolean;
type CDataGetter = (t: string) => boolean

const ALWAYS_FALSE_CDATA: CDataGetter = () => false

export interface RootOptions {
/**
Expand Down Expand Up @@ -92,6 +95,7 @@ export interface AttributeOptions {
}

export interface TextOptions {
cdata?: CDataGetter
}

export interface BaseSchema {
Expand Down Expand Up @@ -124,6 +128,7 @@ export interface TextSchema extends BaseSchema {
xmlType: "text";
propertyKey: string;
type: Function;
cdata: CDataGetter
}

export interface AttributeSchema extends BaseSchema {
Expand Down Expand Up @@ -299,6 +304,7 @@ export function XMLText(opts: TextOptions = {}) {
propertyKey: propertyKey,
type: type,
xmlType: "text",
cdata: opts.cdata || ALWAYS_FALSE_CDATA
};

targetChildren.push(textSchema);
Expand Down
9 changes: 6 additions & 3 deletions packages/xmldom-decorators/src/deserializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,20 @@ class DeserializerBuilder implements DOMBuilder, DeserializerContext {
return;
}

// console.log("chars", xt, start, length)
// Normally 'start' will be 0 and 'length' will be the length of the
// character data. However, for CDATA, the xmldom parser passes in the
// entire input XML string and the 'start' index within that string.
let chars = xt.substring(start, start + length)

if (parent.contextType === "root" || parent.contextType === "element") {
if (parent.type === Number || parent.type === Boolean || parent.type === String || parent.type === Date) {
parent.value = this.convertValue(xt, parent.type);
parent.value = this.convertValue(chars, parent.type);
} else if (typeof parent.type === "function") {
// Text inside object, check for a property with XMLText decorator:
const children: BaseSchema[] = Reflect.getMetadata("xml:type:children", parent.type) || [];
const childSchema = children.find(c => isTextSchema(c)) as TextSchema;
if (childSchema) {
parent.value[childSchema.propertyKey] = this.convertValue(xt, childSchema.type);
parent.value[childSchema.propertyKey] = this.convertValue(chars, childSchema.type);
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion packages/xmldom-decorators/src/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,15 @@ export class XMLDecoratorSerializer implements SerializerContext {
} else if (isTextSchema(child)) {
const value = this.convertValue(data[child.propertyKey], child.type);
if (value !== undefined) {
element.appendChild(this.document.createTextNode(value));
let appendCData = false
if (child.cdata) {
appendCData = child.cdata(value)
}
if (appendCData) {
element.appendChild(this.document.createCDATASection(value))
} else {
element.appendChild(this.document.createTextNode(value));
}
}
}
}
Expand Down