Skip to content

Commit

Permalink
DOMDocument can be inherited in PHP program
Browse files Browse the repository at this point in the history
fixes #1132
  • Loading branch information
jakubmisek committed May 2, 2024
1 parent d5db31e commit bf7e45e
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions src/Peachpie.Library.XmlDom/DOMDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,21 +272,28 @@ public bool substituteEntities

#region Construction

public DOMDocument(string version = null, string encoding = null)
{
this.XmlDocument = PhpXmlDocument.Create();

__construct(version, encoding);
}
// used when inheriting this class
// does not call __construct()
[PhpFieldsOnlyCtor]
protected DOMDocument()
: this(xmlDocument: PhpXmlDocument.Create())
{ }

internal DOMDocument(XmlDocument xmlDocument)
{
this.XmlDocument = xmlDocument;
}

public DOMDocument(string version = null, string encoding = null)
: this(xmlDocument: PhpXmlDocument.Create())
{
__initialize(version, encoding);
}

private protected override DOMNode CloneObjectInternal(bool deepCopyFields) => new DOMDocument(XmlDocument);

public virtual void __construct(string version = null, string encoding = null)
/// <summary>Minimal initialization.</summary>
private protected void __initialize(string version = null, string encoding = null)
{
// To prevent problems from subsequent calls
if (XmlDocument.HasChildNodes)
Expand All @@ -295,9 +302,17 @@ public virtual void __construct(string version = null, string encoding = null)
}

// append the corresponding XML declaration to the document
XmlDocument.AppendChild(XmlDocument.CreateXmlDeclaration(version ?? "1.0", encoding, string.Empty));
XmlDocument.AppendChild(
XmlDocument.CreateXmlDeclaration(
version: version ?? "1.0",
encoding: string.IsNullOrEmpty(encoding) ? null : encoding,
standalone: string.Empty
)
);
}

public void __construct(string version = "1.0", string encoding = "") => __initialize(version, encoding);

#endregion

#region Node factory
Expand Down Expand Up @@ -1030,7 +1045,7 @@ public virtual bool schemaValidate(Context ctx, string schemaFile, int flags = 0
public virtual bool schemaValidateSource(Context ctx, string schemaString, int flags = 0)
{
XmlSchema schema;

try
{
schema = XmlSchema.Read(new System.IO.StringReader(schemaString), null);
Expand Down Expand Up @@ -1181,7 +1196,7 @@ private bool ValidateSchemaInternal(XmlSchema schema, int flags)

// By validating externally we prevent the default values from being created
var xpathNavigator = XmlDocument.CreateNavigator();
xpathNavigator.CheckValidity(schemaSet, null);
xpathNavigator.CheckValidity(schemaSet, null);
}
}
catch (XmlSchemaException)
Expand Down

2 comments on commit bf7e45e

@wujwmail
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much !

@jakubmisek
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're welcome!

Please sign in to comment.