Support Match.wrap(Element) for wrapping an element with attributes
naoto-ogawa opened this issue · comments
We can wrap tag without attributes, like this:
Match tag = ...
tags.wrap("aaaa"); // OK
But we can't wrap tag with attributes.
Match tag = ...
tags.wrap("<aaaa id='abc'/>"); // NG
It would be nice if we can do something like this
Match tag = ...
Match m = $("xxxx");
m.attr("id", "abc");
tags.wrap(m.get(0)); // Match#wrap(Element)
At this moment, I have to write a helper method like that:
document = getDocument(file);
Match tags = $(document).find("body");
Match m = $("xxxx");
m.attr("id", "abc");
wrap(tags, m.get(0));
public final Element wrap(Match base, Element newParent) {
final int size = base.size();
for (int matchIndex = 0; matchIndex < size; matchIndex++) {
Element match = base.get(matchIndex);
Node parent = match.getParentNode();
Document doc = match.getOwnerDocument();
Node wrapper = doc.importNode(newParent, true);
parent.replaceChild(wrapper, match);
wrapper.appendChild(match);
}
return newParent;
}
Versions:
- jOOX:joox-java-6-1.6.0
- Java:1.8.0_05
Related issue
Why not just write:
Match x = $("<abc><xyz/></abc>");
System.out.println(x.find("xyz").wrap("hehe").parent().attr("test", "foo"));
System.out.println(x);
This prints:
<hehe test="foo"><xyz/></hehe>
<abc><hehe test="foo"><xyz/></hehe></abc>
In any case, there is a lot of potential of improving attribute support in jOOX, no doubt. I'll leave this open to think about it in a larger context
Oh, simple enough. I couldn't figure out. Thank you for your answer.
Personally, I don't find any reason to add another interface.