Skip to content

Commit

Permalink
Passthrough HTML attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
bobisjan committed Oct 27, 2021
1 parent 44ae45e commit 621b885
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
23 changes: 20 additions & 3 deletions packages/core/src/ember-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ function immediatelyAfter(node: Node) {

export class PreparedEmberHTML {
dom: JSDOM;
origin: JSDOM;
javascript: NodeRange;
styles: NodeRange;
implicitScripts: NodeRange;
Expand All @@ -60,7 +61,9 @@ export class PreparedEmberHTML {
implicitTestStyles: NodeRange;

constructor(private asset: EmberAsset) {
this.dom = new JSDOM(readFileSync(asset.sourcePath, 'utf8'));
let source = readFileSync(asset.sourcePath, 'utf8');
this.dom = new JSDOM(source);
this.origin = new JSDOM(source);
let html = asset.prepare(this.dom);
this.javascript = new NodeRange(html.javascript);
this.styles = new NodeRange(html.styles);
Expand Down Expand Up @@ -97,7 +100,14 @@ export class PreparedEmberHTML {
// root-relative via the configured rootURL
insertScriptTag(location: NodeRange, relativeSrc: string, opts?: { type?: string; tag?: string }) {
let newTag = this.dom.window.document.createElement(opts && opts.tag ? opts.tag : 'script');
newTag.setAttribute('src', this.asset.rootURL + relativeSrc);
let src = this.asset.rootURL + relativeSrc;
let origin = this.origin.window.document.querySelector(`script[src="${src}"]`);
if (origin) {
for (let { name, value } of [...origin.attributes]) {
newTag.setAttribute(name, value);
}
}
newTag.setAttribute('src', src);
if (opts && opts.type) {
newTag.setAttribute('type', opts.type);
}
Expand All @@ -109,8 +119,15 @@ export class PreparedEmberHTML {
// root-relative via the configured rootURL
insertStyleLink(location: NodeRange, relativeHref: string) {
let newTag = this.dom.window.document.createElement('link');
let href = this.asset.rootURL + relativeHref;
let origin = this.origin.window.document.querySelector(`link[href="${href}"][rel="stylesheet"]`);
if (origin) {
for (let { name, value } of [...origin.attributes]) {
newTag.setAttribute(name, value);
}
}
newTag.rel = 'stylesheet';
newTag.href = this.asset.rootURL + relativeHref;
newTag.href = href;
location.insert(this.dom.window.document.createTextNode('\n'));
location.insert(newTag);
}
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/html-placeholder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ export default class Placeholder {

insertStyleLink(href: string) {
let newTag = this.end.ownerDocument.createElement('link');
for (let { name, value } of [...this.target.attributes]) {
newTag.setAttribute(name, value);
}
newTag.href = href;
newTag.rel = 'stylesheet';
this.insert(newTag);
Expand Down

0 comments on commit 621b885

Please sign in to comment.