Skip to content

Commit

Permalink
fix: XML-encode a value before inserting it into DOM
Browse files Browse the repository at this point in the history
(cherry picked from commit c4665e9)
  • Loading branch information
wazelin committed Jul 16, 2021
1 parent 6dbc604 commit a238ea8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion qtism/data/storage/xml/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public static function valueAsString($value)
if (is_bool($value)) {
return $value === true ? 'true' : 'false';
}
return (string)$value;
return htmlspecialchars($value, ENT_XML1);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,35 @@ public function testUnmarshall21()
$this::assertCount(1, $values);
$this::assertEquals('tplx', $values[0]->getValue());
}

public function testMarshallHtmlEntities21()
{
$values = new ValueCollection([new Value('non breaking space', BaseType::STRING)]);
$defaultValue = new DefaultValue($values);
$templateDeclaration = new TemplateDeclaration('tpl1', BaseType::STRING, Cardinality::SINGLE, $defaultValue);
$element = $this->getMarshallerFactory('2.1.0')->createMarshaller($templateDeclaration)->marshall($templateDeclaration);

$dom = new DOMDocument('1.0', 'UTF-8');
$element = $dom->importNode($element, true);
$this::assertEquals('<templateDeclaration identifier="tpl1" cardinality="single" baseType="string"><defaultValue><value>non&amp;nbsp;breaking&amp;nbsp;space</value></defaultValue></templateDeclaration>', $dom->saveXML($element));
}

public function testUnmarshallHtmlEntities21()
{
$element = $this->createDOMElement('
<templateDeclaration identifier="tpl1" cardinality="single" baseType="string"><defaultValue><value>non&amp;nbsp;breaking&amp;nbsp;space</value></defaultValue></templateDeclaration>
');

$component = $this->getMarshallerFactory('2.1.0')->createMarshaller($element)->unmarshall($element);
$this::assertInstanceOf(TemplateDeclaration::class, $component);
$this::assertEquals('tpl1', $component->getIdentifier());
$this::assertEquals(Cardinality::SINGLE, $component->getCardinality());
$this::assertEquals(BaseType::STRING, $component->getBaseType());

$default = $component->getDefaultValue();
$this::assertInstanceOf(DefaultValue::class, $default);
$values = $default->getValues();
$this::assertCount(1, $values);
$this::assertEquals('non&nbsp;breaking&nbsp;space', $values[0]->getValue());
}
}

0 comments on commit a238ea8

Please sign in to comment.