Skip to content

Commit 35c0ad5

Browse files
authored
Namespaced all discovery styles (#25)
* Namespaced all discovery styles * Refactored style namespacing * Partially fixed styles isolation * Fixed discovery js styles pollution * Small fixes and CHANGELOG
1 parent 184a8d0 commit 35c0ad5

7 files changed

Lines changed: 75 additions & 15 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.5.2 (06-08-2019)
2+
3+
* Fixed style pollution
4+
15
## 1.5.1 (01-08-2019)
26

37
* Fixed issue with broken styles on several sites

core/constants.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
WRAPPER_NODE: 'json-discovery-browser-extension'
3+
};

core/cssTransformLoader.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const csstree = require('css-tree');
2+
const { WRAPPER_NODE } = require('./constants');
3+
4+
const walkTree = ast => {
5+
csstree.walk(ast, {
6+
visit: 'Rule',
7+
enter: function(node) {
8+
// Namespace all discovery styles
9+
node.prelude.children.forEach(child => {
10+
child.children.prependData({
11+
type: 'WhiteSpace',
12+
value: ' '
13+
});
14+
child.children.prependData({
15+
type: 'ClassSelector',
16+
name: WRAPPER_NODE
17+
});
18+
});
19+
}
20+
});
21+
22+
return csstree.generate(ast);
23+
};
24+
25+
/**
26+
* Css Transform loader
27+
* @param {string} source
28+
*/
29+
module.exports = function(source) {
30+
const callback = this.async();
31+
32+
const ast = csstree.parse(source);
33+
34+
try {
35+
callback(null, walkTree(ast));
36+
} catch (error) {
37+
callback(error);
38+
}
39+
};

core/webpack.base.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ const config = ({ manifest, outputPath }) => ({
4545
options: {
4646
import: true
4747
}
48+
},
49+
{
50+
loader: require.resolve('./cssTransformLoader.js')
4851
}
4952
]
5053
},
@@ -99,8 +102,7 @@ const config = ({ manifest, outputPath }) => ({
99102
}
100103
]),
101104
new MiniCssExtractPlugin({
102-
filename: 'css/[name].css',
103-
chunkFilename: 'css/[id].css'
105+
filename: 'css/[name].css'
104106
})
105107
],
106108
performance: { hints: false }

package-lock.json

Lines changed: 14 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
{
22
"name": "jsondiscovery",
3-
"version": "1.5.1",
3+
"version": "1.5.2",
44
"description": "DiscoveryJson",
55
"author": "exsdis@gmail.com",
66
"license": "MIT",
77
"private": false,
8-
"dependencies": {},
98
"scripts": {
109
"lint": "eslint --ext .js src core",
1110
"dev": "webpack --config ./core/webpack.dev.js --hide-modules",
@@ -26,6 +25,7 @@
2625
"copy-webpack-plugin": "^4.5.4",
2726
"cross-env": "^5.1.0",
2827
"css-loader": "^1.0.1",
28+
"css-tree": "^1.0.0-alpha.29",
2929
"enhanced-resolve": "^3.4.1",
3030
"eslint": "^4.9.0",
3131
"file-loader": "^1.1.5",

src/content/inject.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Widget, router, complexViews } from '@discoveryjs/discovery/dist/lib.um
22
import settingsPage from '../settings';
33
import '@discoveryjs/discovery/dist/lib.css';
44
import './index.css';
5+
import { WRAPPER_NODE } from '../../core/constants';
56

67
/**
78
* Discovery initialization
@@ -10,7 +11,7 @@ import './index.css';
1011
*/
1112
function initDiscovery(options) {
1213
const { settings } = options;
13-
const discovery = new Widget(options.wrapper);
14+
const discovery = new Widget(options.discoveryNode);
1415

1516
discovery.apply(router);
1617
discovery.apply(complexViews);
@@ -138,17 +139,18 @@ function getSettings(cb) {
138139
document.body.style.height = '100%';
139140
document.body.style.border = 'none';
140141
document.body.style.webkitTextSizeAdjust = '100%';
142+
document.body.style['background-color'] = '#fff';
143+
document.body.classList.add(WRAPPER_NODE);
141144

142-
const wrapper = document.createElement('div');
143-
wrapper.classList.add('discovery');
145+
const discoveryNode = document.createElement('div');
146+
discoveryNode.style.height = '100%';
147+
document.body.appendChild(discoveryNode);
144148

145-
document.body.appendChild(wrapper);
146-
147-
wrapper.style['background-color'] = '#fff';
149+
document.body.appendChild(discoveryNode);
148150

149151
getSettings(settings => {
150152
initDiscovery({
151-
wrapper,
153+
discoveryNode,
152154
raw,
153155
data: json,
154156
settings

0 commit comments

Comments
 (0)