I am currently facing a challenge where users can input any HTML into a text box, and I need to manipulate that HTML by identifying elements such as anchor tags or divs.
To achieve this, I have created a hidden div and copied the pasted HTML into it using JavaScript:
const pastedHtml = $("#textbox-id").val();
$("#hiddendiv").html(pastedHtml);
After doing this, I can easily access the pasted HTML document as a DOM element and manipulate it using jQuery or JavaScript.
The issue I'm encountering is when the pasted HTML contains selectors that are also present in the main page, causing the main page's styles to be affected. I am looking for a way to encapsulate the CSS styles within the pasted HTML so they do not impact the outer page.
The pasted HTML may include inline styling, style tags, external stylesheets, or anything else.
Is there a library or method available to address this? Or what approach should I take to resolve this problem?
Any assistance would be greatly appreciated.
Please let me know if you need more information.
Thank you in advance.