This repository has been archived by the owner on Feb 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Öner Zafer
committed
Jun 29, 2019
1 parent
a9a3757
commit 4ef5c7a
Showing
16 changed files
with
182 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,22 @@ | ||
const utils = require('./utils'); | ||
|
||
module.exports = (dom, extendingAppManifest) => { | ||
const doc = dom.window.document; | ||
const _$ = doc.querySelector.bind(doc); | ||
const _$$ = doc.querySelectorAll.bind(doc); | ||
utils.injectBundleIntoDom(dom, extendingAppManifest.bundle); | ||
_$('[export]').setAttribute('export', extendingAppManifest.appName); | ||
Array.from(_$$('[resource]')).forEach(e => | ||
e.setAttribute('resource', 'resource') | ||
); | ||
Array.from(_$$('script:not([resource]),style:not([resource]),link:not([resource])')).forEach(e => | ||
e.parentElement.removeChild(e) | ||
); | ||
module.exports = ($, extendingAppManifest) => { | ||
utils.injectBundleIntoDom($, extendingAppManifest.bundle); | ||
$('[export]').attr('export', extendingAppManifest.appName); | ||
|
||
$('script:not([resource]),style:not([resource]),link:not([resource])').remove(); | ||
|
||
if (extendingAppManifest.overrides && extendingAppManifest.overrides.length) { | ||
extendingAppManifest.overrides.forEach(override => { | ||
const elem = _$(`${override.tag}[public="${override.target}"]`); | ||
const selector = `${override.tag}[public="${override.target}"]`; | ||
const $elem = $(selector); | ||
let content = override.content; | ||
const superTagPattern = /(<super\s*\/>)|(<super>.*<\/\s*super>)/g; | ||
if (superTagPattern.test(content)) { | ||
content = content.replace(superTagPattern, elem.innerHTML); | ||
content = content.replace(superTagPattern, $elem.html()); | ||
} | ||
const tagName = override.target && override.target !== '' ? override.target : override.tag; | ||
utils.replaceElement(dom, elem, tagName, content); | ||
utils.replaceElement($, selector, tagName, content); | ||
}); | ||
} | ||
return dom; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,21 @@ | ||
const utils = require('./utils'); | ||
|
||
module.exports = (dom, importedApps = {}) => { | ||
module.exports = ($, importedApps = {}) => { | ||
const injected = []; | ||
const doc = dom.window.document; | ||
const _$$ = doc.querySelectorAll.bind(doc); | ||
Array.from(_$$('head [import]')).forEach(elem => | ||
elem.parentNode.removeChild(elem) | ||
); | ||
Array.from(_$$('fragment')).forEach(elem => { | ||
const appName = utils.getAttr(elem, 'name'); | ||
|
||
$('head [import]').remove(); | ||
|
||
$('fragment').each((index, elem) => { | ||
const appName = $(elem).attr('name'); | ||
if (importedApps[appName] && importedApps[appName].content) { | ||
// inject content | ||
utils.replaceElement(dom, elem, appName, importedApps[appName].content); | ||
if (injected.indexOf(appName) === -1) { | ||
utils.injectBundleIntoDom(dom, importedApps[appName].bundle); | ||
utils.replaceElement($, elem, appName, importedApps[appName].content); | ||
if (!injected.includes(appName)) { | ||
utils.injectBundleIntoDom($, importedApps[appName].bundle); | ||
injected.push(appName); | ||
} | ||
} else { | ||
elem.parentNode.removeChild(elem); | ||
$(elem).remove(elem); | ||
} | ||
}); | ||
Array.from(_$$('[resource]')).forEach(e => | ||
e.setAttribute('resource', 'resource') | ||
); | ||
return dom; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,40 @@ | ||
const utils = require('./utils'); | ||
const JSDOM = require('JSDOM').JSDOM; | ||
|
||
module.exports = html => { | ||
if (!html || typeof html !== 'string') { | ||
return; | ||
} | ||
const dom = utils.parse(html); | ||
const _$ = dom.window.document.querySelector.bind(dom.window.document); | ||
const _$$ = dom.window.document.querySelectorAll.bind(dom.window.document); | ||
const manifest = utils.cleanObject({ | ||
appName: utils.getAttr(_$('head meta[export]'), 'export'), | ||
baseUrl: utils.getAttr(_$('head meta[public-url]'), 'public-url'), | ||
extending: utils.getAttr(_$('head meta[export]'), 'extends') || undefined, | ||
alias: utils.getAttr(_$('head meta[export-as]'), 'export-as') || undefined, | ||
type: utils.getAttr(_$('head meta[of-type]'), 'of-type') || 'page', | ||
uses: Array.from(_$$('head meta[import]')).map(elem => | ||
utils.getAttr(elem, 'import') | ||
), | ||
bundle: Array.from(_$$('[resource]')).map(elem => | ||
utils.cleanObject(utils.toBundleItem(elem)) | ||
), | ||
overrides: Array.from(_$$('[override]')).map(elem => | ||
utils.cleanObject(utils.toOverrideItem(elem)) | ||
), | ||
publics: Array.from(_$$('[public]')).map( | ||
elem => utils.getAttr(elem, 'public') || elem.tagName | ||
), | ||
content: utils.toContent(new JSDOM(dom.serialize())) || '', | ||
const $ = utils.parse(html); | ||
|
||
const manifest = { | ||
appName: $('head meta[export]').attr('export'), | ||
baseUrl: $('head meta[public-url]').attr('public-url'), | ||
extending: $('head meta[extends]').attr('extends'), | ||
alias: $('head meta[export-as]').attr('export-as'), | ||
type: $('head meta[of-type]').attr('of-type') || 'page', | ||
uses: [], | ||
bundle: [], | ||
overrides: [], | ||
publics: [], | ||
content: utils.toContent(utils.parse(utils.serialize($))) || '', | ||
raw: utils.minify(html), | ||
}); | ||
}; | ||
|
||
$('meta[import]').each((index, elem) => { | ||
manifest.uses.push($(elem).attr('import'))}); | ||
$('[resource]').each((index, elem) => | ||
manifest.bundle.push(utils.toBundleItem($, elem)) | ||
); | ||
$('[override]').each((index, elem) => | ||
manifest.overrides.push(utils.toOverrideItem($, elem)) | ||
); | ||
$('[public]').each((index, elem) => | ||
manifest.publics.push($(elem).attr('public') || elem.name) | ||
); | ||
|
||
if (!manifest.appName) { | ||
throw new Error('<meta export> is not available'); | ||
} | ||
|
||
return manifest; | ||
return utils.cleanObject(manifest); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.