Possible to download and apply stylesheet to angular ngBindHtml? - javascript

I'm using angular 1.6.
I'm dynamically pulling html from my nodejs server and writing it into a div on the page.
<p ng-bind-html="htmlSource"></p>
$scope.htmlSource = '<link rel="stylesheet" type="text/css" href="http://NOT-MY-SERVER/style.css"><div class='specialcss'></div>';
I'm able to display the rendered html fine. But I don't have the right formatting.
Things I need to do
Fetch the css, which is not from my server. (Shouldn't encounter cross-site scripting issues with css, right?)
Apply it just to the html snippet (and not override my site's default css)
Prevent any accompanying javascript in the html from running
Is this possible?

To add a css stylesheet to an 'html snippet' can be accomplished with shadow dom. However, shadow dom is not supported by all browsers.
It'd look be something like this:
var $compile = angular.element('body').injector().get('$compile');
var div = document.createElement('div');
var innerHtml = '<div ng-bind-html='myTemplate.html"/>'
innerHtml+='<link rel="stylesheet" type="text/css" href="http://NOT-MY-SERVER/style.css">';
var shadow = div.attachShadow({mode: 'open'});
shadow.innerHTML=innerHTML;
var element = $compile(shadow)(angular.element('body').scope());
angular.element('#parentElementInDomWhereYouWantYourTemplate').append(element)
The stylesheet would only cause CORS problems if it's not hosted on your server, or the server that is accessing the stylesheet is not whitelisted.
Plunkr

Related

Import CSS from an HTML-file as external CSS

As the title reads, I want to import an HTML-file as external CSS to a website.
Just hear me out: my problem is that I'm working with a very inconvenient CMS that doesn't let me upload CSS-files no matter what.
I'm able to write CSS inside the page directly via HTML-style-tag but that generates a lot of text on every site and also makes maintaining CSS tedious.
As I can't upload CSS-files, I thought maybe I can create a dummy-site inside the CMS with only CSS in it and then later import that site as CSS.
The idea was: when parsed, the HTML of the site (header, body, etc.) will just be skipped (as when CSS has i.e. type-errors) while any valid CSS found is going to be imported.
Now when I try importing this website with
<style type="text/css"> #import url(dummyCSSWebsiteURL); </style>
(as the CMS also doesn't grant me access to the header of a page),
I - of course - get the error:
"Resource interpreted as Stylesheet but transferred with MIME type text/html"
as I am obviously requesting an HTML-file and not CSS.
I also tried jQuery to simply include all the dummy-HTML into an element (that I would have just not displayed):
$("#cssDummy").load(dummyCSSWebsiteURL);
but I get 2 errors that are probably just showing what a horribly inefficient idea this is:
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience
A parser-blocking, cross site (i.e. different eTLD+1) script, ["..."], is invoked via document.write. The network request for this script MAY be blocked by the browser in this or a future page load due to poor network connectivity. If blocked in this page load, it will be confirmed in a subsequent console message.
maybe I am just disregarding (or not understanding) things on a conceptual level at all but I still wonder if there is a workaround for this problem?
EDIT: I found a workaround
Definitely don't recommend. Try using different server as pointed out in the comments if you can.
I used an XMLHttpRequest to get the external site's HTML, then used regEx to match the content of the div on the page that contains the css and - with added style-tags - inserted the matched css into a div on the page.
Code for external site that contains the CSS:
<div id="generalCode">.testBox{background-color: red; min-height: 200px;}</div>
Code on site that imports the external CSS:
<div class="testBox">
</div>
<div id="cssCodeOnPage">
</div>
<script>
// use onload if you want
getCssCode();
function getCssCode(){
// send request to page where div #generalCode contains css
var xhr = new XMLHttpRequest();
xhr.open('GET', dummyCSSWebsiteURL);
xhr.onload = function(){
// use regex to separate css from rest of html
var re = /<div id="generalCode">([\s\S]*?)<\/div>/;
var cssString = xhr.response.match(re)[1];
cssString = "<style>" + cssString +"</style>";
// insert css into div
var cssDivOnPage = document.getElementById('cssCodeOnPage');
cssDivOnPage.innerHTML = cssString;
}
xhr.send();
}
(sorry for this monstrosity of a question..)
I think the best option is to load that HTML page into an iframe, query the styles out of hte iframe, and then attach that to the current document. I created a live example on Glitch here.
Nothing but the <style> block will be copied from the HTML. The external HTML document does need to be an actual HTML document though (<html><head><style>....), otherwise the page won't be queryable to retrieve the CSS.
This is an example in plain JavaScript:
// Create an iframe
const iframe = document.createElement("iframe");
// Make it not visible
iframe.style.visibility = "hidden";
// This will run once the HTML page has loaded in the <iframe>
iframe.onload = () => {
// Find the <style> element in the HTML document
const style = iframe.contentDocument.querySelector("style");
// Take that <style> element and append it to the current document's <head>
document.querySelector("head").appendChild(style);
// Remove the <iframe> after we are done.
iframe.parentElement.removeChild(iframe);
};
// Setting a source starts the loading process
iframe.src = "css.html";
// The <iframe> doesn't actually load until it is appended to a document
document.querySelector("body").appendChild(iframe);
Its possible in a different way. However not through JS, but PHP include
For that however, your server needs to support PHP and need to use PHP instead of HTML as documents. Sounds more complicated now then it actually is. To use PHP the document has to becalled .php at the end instead of .html e.g. index.php.
The document itself can be written the very same way as you write HTML websites.
Now for the issue to use PHP include. you inlcude the CSS as head style:
index.php:
<html>
<head>
<?php
include('styles.css');
?>
</head>
<body>
content
</body>
</html>
the line <?php include('url'); ?> will load a file mentioned in the url server sided into the document. Therefor you only need to create another file with the name styles.css and write your css in there the normal way.
You Do
<link rel="stylesheet" href="css/style.css">

How to apply Bootstrap styling classes globally before publishing/loading a web page

I'd like to apply Bootstrap classes globally to my website and not have all the website elements jump around as the Bootstrap classes get applied through jQuery.
For example, I'd like to make every h1 element have the Bootstrap classes text-center and border-bottom. To do this, I can use jQuery's .addClass() method to apply the appropriate Bootstrap classes to all matching elements. The problem is that every time a user goes to a different page on the website, the un-styled elements load first, then they jump around as the Bootstrap classes get applied. I'm assuming this happens because my jQuery script only runs after the page has finished loading.
To avoid this, I could just put the Bootstrap classes directly in the h1 element, like so: <h1 class="text-center border-bottom">...</h1>. Obviously this is not a good solution though because I don't want to have to "hard-code" the classes onto every h1 element across the site.
Is there a way to apply Bootstrap classes before loading/publishing the website so that the "jumping" of web page elements doesn't happen?
I'm currently hosting the website locally using XAMPP and I am using PHP to include the same header and footer across all pages. The header and footer contain the necessary Bootstrap styling and script resources.
What I like to do is wrap my HTML in a div, lets call it wrap. And in the page's head:
<style>.wrap{display:none;}</style>
then in my 'CSS` file have
.wrap{display:block;}
Then in the footer defer the loading of my css:
<noscript id="deferred-styles">
<link rel="stylesheet" type="text/css" href="compressed.css" />
</noscript>
<script>
var loadDeferredStyles = function() {
var addStylesNode = document.getElementById("deferred-styles");
var replacement = document.createElement("div");
replacement.innerHTML = addStylesNode.textContent;
document.body.appendChild(replacement)
addStylesNode.parentElement.removeChild(addStylesNode);
};
var raf = requestAnimationFrame || mozRequestAnimationFrame ||
webkitRequestAnimationFrame || msRequestAnimationFrame;
if (raf) raf(function() { window.setTimeout(loadDeferredStyles, 0); });
else window.addEventListener('load', loadDeferredStyles);
</script>
This will "hide" the contents of the page until the CSS is full loaded.
I also compress all of my CSS into one file, which is easy as I developed a custom CMS and don't use wordpress, but that is a different conversation.

Extend HTML file with script and override/extend some section tags

There is open source (client side) which I can use to extend HTML,
for example I need to add scripts to it or change some of the src values and add additional tags, etc.
I found the following: https://www.npmjs.com/package/gulp-html-extend
but I'm not sure if I can use it in the client (we don't use gulp in our project) By client I mean for example to use it in jsFiddle.
The input should be HTML content with some object/json with the new content and the output should be extended HTML.
If there is no open source , and I need to develop it myself, is there is some guide line I should follow from good design aspects?
UPDATE:
For example if I've the following HTML doc as JS input variable
THIS IS THE INPUT WHICH I GOT AS STRING
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta charset="UTF-8">
<title>td</title>
<script id="test-ui-bootstrap"
src="resources/test-ui-core.js"
data-test-ui-libs="test.m"
data-test-ui-xx-bindingSyntax="complex"
data-test-ui-resourceroots='{"tdrun": "./"}'>
</script>
<link rel="stylesheet" type="text/css" href="css/style.css">
<script>
test.ui.get().attachInit(function() {
});
</script>
</head>
<body class="testUiBody" id="content">
</body>
</html>
For example I need the following:
1.
I want to add additional script (e.g. with alert inside) after
<script id="test-ui-bootstrap" ....
if there is in the file script with id "test-ui-bootstrap"
I want to add immediately after this script another script e.g.
script with alert inside
2.
To add additional property inside the first script(with id id="test-ui-bootstrap") after the last script...
data-test-ui-libs="test.m"
To add
data-test-ui-libs123 ="test.bbb"
3.
If I want to modify the value of existing property e.g. change
src="resources/test-ui-core.js"
to
src="resources/aaaa/test-ui-core.js"
I got string with HTML and I need to create new string with the modified HTML I can I do it right with nice way?
UPDATE 2
THIS IS THE OUTPUT AFTER THE HTML WAS CHANGED
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta charset="UTF-8">
<title>td</title>
<script id="test-ui-bootstrap"
src="resources/aaaa/test-ui-core.js"
data-test-ui-libs="test.m"
data-test-ui-libs123 ="test.bbb"
data-test-ui-xx-bindingSyntax="complex"
data-test-ui-resourceroots='{"tdrun": "./"}'>
</script>
<script>
alert("test)
</script>
<link rel="stylesheet" type="text/css" href="css/style.css">
<script>
test.ui.get().attachInit(function() {
});
</script>
</head>
<body class="testUiBody" id="content">
</body>
</html>
You can create a sandboxed element outside of the DOM, then insert your HTML into it.
var sandbox = document.createElement('div');
sandbox.innerHTML = yourHTMLString;
The browser will parse your HTML, then you'll be able to traverse/modify it with the DOM APIs.
You can use it to find elements and add attributes.
var script = sandbox.querySelectorAll('#test-ui-bootstrap');
script.setAttribute('data-test-ui-libs', 'test.m');
script.setAttribute('src', 'resources/aaaa/test-ui-core.js');
Or insert new elements after existing ones.
var newScript = document.createElement('script');
newScript.innerText = 'your script contents';
script.parentNode.insertBefore(newScript, script.nextSibling);
As soon as you're ready to work with it as a string again, you can read it out as a property.
var html = sandbox.innerHTML;
Note. Different browsers handle the innerHTML mechanism differently and you might find that they strip the <body> and <head> tags when you insert your HTML into your sandbox.
If this is the case then you can workaround it with a hack.
var escapedTags = yourHTMLString
.replace(/body/ig, 'body$')
.replace(/head/ig, 'head$')
// now the browser won't recognize the tags
// and therefore won't strip them out.
sandbox.innerHTML = escapedTags;
// do some work
// ...
// don't forget to unescape them!
var unescapedTags = sandbox.innerHTML
.replace(/body\$/g, 'body')
.replace(/head\$/g, 'head');
This makes use of the fact that the browser won't understand what a <body$> or a <head$> tag is, so it just leaves in intact.
You can use:
DOMParser and XMLSerializer.
The most important thing is; this is not a sandbox. It only uses a parser & serializer; and therefore it will not execute the scripts within the input; until you inject the output into an actual DOM.
// HTML string to be modified
var strHTML = '<html>...</html>'; // your HTML
// We'll parse this string into DOM in memory.
var parser = new DOMParser(),
doc = parser.parseFromString(strHTML, 'text/html'),
// in this example, we'll get the script elements and change/set
// some attributes of the first and the content of the second
scripts = doc.getElementsByTagName('script');
scripts[0].setAttribute('data-test-ui-libs123', 'test.bbb');
scripts[0].setAttribute('src', 'resources/aaaa/test-ui-core.js');
scripts[1].innerHTML = 'alert("test")';
// now that we've modified the HTML, we can serialize it into string
var serializer = new XMLSerializer(),
outputHTML = serializer.serializeToString(doc);
Example Pen.
DOMParser and XMLSerializer on MDN.
Browser support: IE10+ and modern browsers.
jQuery.parseHTML()
The document.implementation.createHTMLDocument() API also does not execute scripts or fetch resources via HTTP (such as videos, images, etc). This is the approach used by jQuery.parseHTML() method. See source here.
From jQuery docs; security considerations:
Most jQuery APIs that accept HTML strings will run scripts that are included in the HTML. jQuery.parseHTML does not run scripts in the parsed HTML unless keepScripts is explicitly true. However, it is still possible in most environments to execute scripts indirectly, for example via the attribute. The caller should be aware of this and guard against it by cleaning or escaping any untrusted inputs from sources such as the URL or cookies. For future compatibility, callers should not depend on the ability to run any script content when keepScripts is unspecified or false.
INITIAL (Node.js)
I understand your question as follows: You want to parse a HTML string in a Node.js environment (you mentioned Gulp), extend it and get the resulting string back.
First, you need to parse the string into a structure, on which you can make queries. There are several libraries available to achieve this. Cheerio.js was recommended and explained in a StackOverflow answer. Other solutions are also explaind there. The library provides you then an interface to the DOM of your HTML code. In the example of Cheerio.js, you can access the DOM similarly as in jQuery. The official example of their GitHub page is depicted below. In a similar manner, you can do your logic by selecting the elements and add your content (modify it, etc.). By calling the $.html() function, you get the modified structure back.
var cheerio = require('cheerio'),
$ = cheerio.load('<h2 class="title">Hello world</h2>');
$('h2.title').text('Hello there!');
$('h2').addClass('welcome');
$.html();
// => returns '<h2 class="title welcome">Hello there!</h2>'
If you want to use this logic in a Gulp build process, you need to wrap it into a Gulp plugin with Cheerio.js as a dependency. On this official GitHub readme file of Gulp it is explained in detail how you can create a Gulp plugin.
EDIT (Browser)
According to your edited question, I'll add this section about editing the HTML in the browser.
It is very convenient to use jQuery to modify a DOM in the browser. You can also modify a virtual DOM with jQuery. To do that, you just need to create the element but not append it to the real DOM. Unfortunately, the browser acts special when it comes to the following tags: <html>, <body>, <head> and <!DOCTYPE html>. As a workaround, you can just edit those tags with a regular expression and rename them to something like <body_temp> and so on. You need to have a good regular expression to only match tags and not content like class="testUiBody" which does also contain the word body. The special behavior is decribed here in detail.
The following code makes all the desired changes in the HTML. You can test it in an updated JSFiddle. Just click the Submit button and you can see the changes. The upper textarea acts as HTML input and the lower one as HTML output.
var html = "<!DOCTYPE html><html><head><meta.....";
// replace html, head and body tag with html_temp, head_temp and body_temp
html = html.replace(/<!DOCTYPE HTML>/i, '<doctype></doctype>');
html = html.replace(/(<\/?(?:html)|<\/?(?:head)|<\/?(?:body))/ig, '$1_temp');
// wrap the dom into a <container>: the html() function returns only the contents of an element
html = "<container>"+html+"</container>";
// parse the HTML
var element = $(html);
// do your calculations on the parsed html
$("<script>alert(\"test\");<\/script>").insertAfter(element.find('#test-ui-bootstrap'));
element.find("#test-ui-bootstrap").attr('data-test-ui-libs123', "test.bbb");
element.find("#test-ui-bootstrap").attr('src', 'resources/aaaa/test-ui-core.js');
// reset the initial changes (_temp)
var extended_html = element.html();
extended_html = extended_html.replace(/<doctype><\/doctype>/, '<!DOCTYPE HTML>');
extended_html = extended_html.replace(/(<\/?html)_temp/ig, '$1');
extended_html = extended_html.replace(/(<\/?head)_temp/ig, '$1');
extended_html = extended_html.replace(/(<\/?body)_temp/ig, '$1');
// replace all " inside data-something=""
while(extended_html.match(/(<.*?\sdata.*?=".*?)(")(.*?".*?>)/g)) {
extended_html = extended_html.replace(/(<.*?\sdata.*?=".*?)(")(.*?".*?>)/g, "$1'$3");
}
// => extended_html contains now your edited HTML

Dynamically made page doesn't receive jQuery Mobile "enhanced" CSS classes when inside an iframe

I am writing a script that transforms an older existing site into a "mobile optimized" one. I'm doing this by picking out the pieces that I want from the existing HTML and creating jQuery Mobile markup from scratch using jQuery, for example (coffee):
page = $('<div data-role="page"></div>')
header = $('<div data-role="header"></div>').append(...)
body = $('<div data-role="main" class="ui-content"></div>').append(...)
page.append header
page.append content
I'm then replacing the iframe body with the page, followed by injecting JQM (#context here is the iframe):
$('head', #context).append $('<link rel="stylesheet" type="text/css" href="//code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.css">')
$('head', #context).append $('<script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.min.js">')
$('head', #context).append $('<script type="text/javascript" src="//code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.js">')
(I've also tried injecting JQM before appending to the body)
Most answers on SO suggest using trigger('create') to "refresh" the enhancements, but that doesn't seem to work. I'm doing it on the page, is that correct?
$('[data-role="page"]').trigger 'create'
Also, this works when I do it outside of the iframe (replacing the entire document's body instead of 1 frame) - only fails inside of an iframe. Unfortunately I've found that I need to be working within the iframe. What am I doing wrong?
When inspecting the HTML, I noticed that the tags do not receive the enhanced classes (I think stuff like "ui-mobile"?) - they do when I'm not using an iframe. My only problem is that the jQM styles do not apply, the elements themselves are there but they look like the default controls.

CRM, Javascript and Dynamic CSS

I need to include a link to a css file, but, the name changes.
The problem is, I am working with MSCRM 2011, and I am integrating a custom pop up window, that I need to have the "CRM style"
The link looks like this:
<LINK rel=stylesheet type=text/css href="/MyOrgtest/_common/styles/fonts.css.aspx?lcid=1033&ver=-900509389">
The thing is, when I do it in a test environment (organization "MyOrgTest") the css link names includes the organization.
So, I need to, somehow, dynamically change this link, with something like a wildcard... I don't know, so I do not have to change the link manually.
Is this possible???
If you open your solution (Settings > Solutions > Open your solution) and select "Web Resources" you should be able to add an html page like you have done with your css file. It will have a url just like your css file:
<Microsoft CRM URL>/WebResources/<name of Web resource>
You can then reference your css file by a relative path like:
<link rel="stylesheet" type="text/css" href="/styles/fonts.css" />
An un-necessary alternative would be to dynamically generate the url of the css via javascript, using the context to get the server url:
var context = GetGlobalContext();
var serverUrl = context.getServerUrl();
var cssPath = serverUrl + "/WebResources/styles/fonts.css";
Once you had this you could check questions like this one to add the css file via javascript.

Categories