如何向JSDOM广告样式表 [英] How do you ad stylesheets to JSDOM
问题描述
我目前正在开发一个项目,需要我有计算样式通过JSDOM发送到浏览器。我目前正在寻找一种方法来注入一些基本的CSS到JSDOM,以便它可以计算机正确的内联样式(是的,我知道那是坏的)。
I am currently working on a project that requires me to have computed styles send to the browser via JSDOM. I am currently looking for a way to inject some basic CSS into JSDOM so that it can computer the correct inline style (Yes I know that's bad).
发现我可以使用JSDOM Level 2,但从那里我找不到任何关于如何注入样式的文档
这是我到目前为止
From what I have found out I can use JSDOM Level 2, but from there I can't find any documentation on how to inject the styles This is what I have so far
var document = jsdom.jsdom('<!DOCTYPE html><html><head></head><body id="abody" ></body></html>', jsdom.level(2, 'style'), {
features : {
FetchExternalResources : ['script', 'css'],
QuerySelector : true
}
});
我已经将css插入head标签,但没有效果。
I have been inserting the css into the head tag but to no avail. And I know I could be doing the above code wrong as well.
任何帮助将是巨大的。
推荐答案
好吧,这听起来有点蠢,但这是我做的:
Well, this is going to sounds kinda dumb but this is what I did:
var path = require('path');
var fs = require('fs');
var mainCss = fs.readFileSync(path.normalize(__dirname + "web_main.css"), 'utf8');
var document = jsdom.jsdom('<!DOCTYPE html><html><meta http-equiv="content-type" content="text/html; charset=utf-8"><head></head><body id="abody" ></body></html>', jsdom.level(3, 'index'), {
features : {
FetchExternalResources : ['script', 'css'],
QuerySelector : true
}
});
var window = document.createWindow();
var head = document.getElementsByTagName('head')[0];
style = document.createElement("style");
style.type = 'text/css';
style.innerHTML = mainCss;
head.appendChild(style);
所以基本上我改变的是将级别移动到3索引,而不是直接在
So basically all I changed was moving the level to 3 index, and instead of directly having it in the starting html, I appended it afterwards.
它很简单,我希望它能帮助别人。
Its pretty simple and I hope it helps someone else out.
这篇关于如何向JSDOM广告样式表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!