Last active
October 25, 2019 18:33
-
-
Save oliverfernandez/fbd25c266296569336d3dc4189c9f087 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import ReactDOM from "react-dom"; | |
class MarfeelAdserverProvider extends React.Component { | |
render() { | |
const adserverHtml = document | |
.createRange() | |
.createContextualFragment( | |
'<ins class="adsense" slot="mySlot" clientId="myClientId"><h1>Adserver Type</h1>Adsense</ins>' | |
); | |
return fromHtmlElementToReact(adserverHtml.firstElementChild); | |
} | |
} | |
function fromHtmlElementToReact(htmlElement) { | |
if (isTextNode(htmlElement)) return htmlElement.data; | |
return React.createElement( | |
htmlElement.tagName, | |
getHTMLElementAttributesAsObject(htmlElement), | |
getHTMLElementChildrenAsReactComponents(htmlElement) | |
); | |
} | |
function isTextNode(htmlElement) { | |
return !(htmlElement instanceof HTMLElement); | |
} | |
function getHTMLElementAttributesAsObject(htmlElement) { | |
const attributes = {}; | |
for (let i = 0; i < htmlElement.attributes.length; i++) { | |
const attribute = htmlElement.attributes.item(i); | |
attributes[attribute.name] = attribute.value; | |
} | |
return attributes; | |
} | |
function getHTMLElementChildrenAsReactComponents(htmlElement) { | |
return [...htmlElement.childNodes].map(child => | |
fromHtmlElementToReact(child) | |
); | |
} | |
ReactDOM.render( | |
React.createElement(MarfeelAdserverProvider, null, null), | |
document.getElementById("root") | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment