pretty-xml icon indicating copy to clipboard operation
pretty-xml copied to clipboard

Second tag is on first level if root element has attributes

Open ruester opened this issue 6 years ago • 2 comments

If the root element has XML attributes the second XML tag appears on the same level as the root tag (tested with version 1.0.2):

$formatter = new Formatter();
echo $formatter->format("<foo><bar></bar></foo>") . PHP_EOL;
echo $formatter->format("<foo xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><bar></bar></foo>") . PHP_EOL;

will output:

<foo>
    <bar>
    </bar>
</foo>
<foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<bar>
</bar>
</foo>

I would expected following output:

<foo>
    <bar>
    </bar>
</foo>
<foo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <bar>
    </bar>
</foo>

ruester avatar Apr 09 '18 15:04 ruester

PR #4 seems to fix the issue, but if I change the spec-test to something like:

$this->format('<?xml version="1.0" encoding="UTF-8"?><foo xsi:a="//"><bar>Baz</bar></foo>')

the phpspec fails. The part // of the URL makes it fail, because xsi:a="b" works.

ruester avatar Apr 09 '18 15:04 ruester

Yeah, I think the regex for detecting an opening tag is not smart enough: '/^<[^\/]*>$/'. It's been several years since I've actually written any PHP, but PRs are very welcome.

shanethehat avatar Apr 09 '18 15:04 shanethehat