module-linker icon indicating copy to clipboard operation
module-linker copied to clipboard

Manipulate element objects instead of innerHTML

Open BehindTheMath opened this issue 8 years ago • 4 comments

I think it would be more elegant to manipulate the element objects instead of rewriting innerHTML.

For example, this could be substituted with something like this:

var newElem = link.get(0);
var oldTextElem = Array.from(elem.querySelector(".pl-s").childNodes)
    .filter(function (node) { return node.nodeType === 3 })[0];
elem.querySelector(".pl-s").replaceChild(newElem, oldTextElem);

BehindTheMath avatar Oct 17 '17 17:10 BehindTheMath

Sorry, but I don't understand. It wouldn't work at all if I replaced that code with this.

The problem is that we can't rely on .pl-s, each language is formatted differently on GitHub. I don't remember now where exactly the problem is, but I hope I'm wrong about it and someday we discover a way to stop using regex and .innerHTML.

fiatjaf avatar Oct 17 '17 23:10 fiatjaf

I see what you mean.

I think it could be done, but it would require a separate workflow for each language.

BehindTheMath avatar Oct 18 '17 02:10 BehindTheMath

Maybe it is not that hard to implement a separate workflow for each language, and it may be our only choice if the innerHTML replacements continue to generate bugs.

fiatjaf avatar Nov 25 '17 16:11 fiatjaf

It can be more difficult and hard to implement, but it's right way for JS, I think.

AlexWayfer avatar Nov 25 '17 17:11 AlexWayfer