Skip to content

Commit

Permalink
refactor: postpone include feature (#308)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Aug 17, 2020
1 parent 3584ae6 commit b92ed21
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 103 deletions.
2 changes: 1 addition & 1 deletion src/options.json
Expand Up @@ -13,7 +13,7 @@
"minLength": 1
},
"type": {
"enum": ["src", "srcset", "include"]
"enum": ["src", "srcset"]
},
"filter": {
"instanceof": "Function"
Expand Down
93 changes: 47 additions & 46 deletions src/plugins/source-plugin.js
Expand Up @@ -193,52 +193,53 @@ export default (options) =>

break;
}
case 'include': {
let source;

// eslint-disable-next-line no-underscore-dangle
if (parser._tokenizer._state === 4) {
return;
}

try {
source = parseSrc(value);
} catch (error) {
options.errors.push(
new HtmlSourceError(
`Bad value for attribute "${attribute}" on element "${tag}": ${error.message}`,
parser.startIndex,
parser.endIndex,
html
)
);

return;
}

if (!urlFilter(attribute, source.value, resourcePath)) {
return;
}

const { startIndex } = parser;
const closingTag = html
.slice(startIndex - 1)
.match(
new RegExp(`<s*${tag}[^>]*>(?:.*?)</${tag}[^<>]*>`, 's')
);

if (!closingTag) {
return;
}

const endIndex = startIndex + closingTag[0].length;
const importItem = getImportItem(source.value);
const replacementItem = getReplacementItem(importItem);

sources.push({ replacementItem, startIndex, endIndex });

break;
}
// Need improve
// case 'include': {
// let source;
//
// // eslint-disable-next-line no-underscore-dangle
// if (parser._tokenizer._state === 4) {
// return;
// }
//
// try {
// source = parseSrc(value);
// } catch (error) {
// options.errors.push(
// new HtmlSourceError(
// `Bad value for attribute "${attribute}" on element "${tag}": ${error.message}`,
// parser.startIndex,
// parser.endIndex,
// html
// )
// );
//
// return;
// }
//
// if (!urlFilter(attribute, source.value, resourcePath)) {
// return;
// }
//
// const { startIndex } = parser;
// const closingTag = html
// .slice(startIndex - 1)
// .match(
// new RegExp(`<s*${tag}[^>]*>(?:.*?)</${tag}[^<>]*>`, 's')
// );
//
// if (!closingTag) {
// return;
// }
//
// const endIndex = startIndex + closingTag[0].length;
// const importItem = getImportItem(source.value);
// const replacementItem = getReplacementItem(importItem);
//
// sources.push({ replacementItem, startIndex, endIndex });
//
// break;
// }
}
});

Expand Down
54 changes: 0 additions & 54 deletions test/__snapshots__/attributes-option.test.js.snap
Expand Up @@ -1862,60 +1862,6 @@ or
exports[`'attributes' option should handle attributes with a "boolean" notation equals "true": warnings 1`] = `Array []`;
exports[`'attributes' option should handle the "include" tags: errors 1`] = `Array []`;
exports[`'attributes' option should handle the "include" tags: module 1`] = `
"// Imports
var ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ = require(\\"../../src/runtime/getUrl.js\\");
var ___HTML_LOADER_IMPORT_0___ = require(\\"./include-header.html\\");
var ___HTML_LOADER_IMPORT_1___ = require(\\"./include-footer.html\\");
var ___HTML_LOADER_IMPORT_2___ = require(\\"./include-content.html\\");
// Module
var ___HTML_LOADER_REPLACEMENT_0___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_0___);
var ___HTML_LOADER_REPLACEMENT_1___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_1___);
var ___HTML_LOADER_REPLACEMENT_2___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_2___);
var code = \\"<div>\\\\n <article>\\\\n BeforeHeaderText\\" + ___HTML_LOADER_REPLACEMENT_0___ + \\"AfterHeaderText\\\\n <ol>\\\\n <li>Grow a long, majestic beard.</li>\\\\n <li>Wear a tall, pointed hat.</li>\\\\n <li>Have I mentioned the beard?</li>\\\\n </ol>\\\\n BeforeFooterText\\" + ___HTML_LOADER_REPLACEMENT_1___ + \\"AfterFooterText\\\\n TextBeforeOpenDiv<div>TextAfterOpenDiv\\" + ___HTML_LOADER_REPLACEMENT_2___ + \\"TextBeforeCloseDiv</div>TextAfterCloseDiv\\\\n </article>\\\\n</div>\\\\n\\\\n<!-- Future improvements -->\\\\nBEFORE\\\\n\\" + ___HTML_LOADER_REPLACEMENT_2___ + \\"\\\\nAFTER\\\\n\\\\n<div>TEXT</div>\\\\n<div>\\\\n \\" + ___HTML_LOADER_REPLACEMENT_2___ + \\"\\\\n</div>\\\\n<div>TEXT</div>\\\\n\\\\n<!-- WRONG -->\\\\n<div><include src=\\\\\\"./include-conten\\" + ___HTML_LOADER_REPLACEMENT_2___ + \\"\\\\n</include>\\\\n\\";
// Exports
module.exports = code;"
`;
exports[`'attributes' option should handle the "include" tags: result 1`] = `
"<div>
<article>
BeforeHeaderText<header>
<h1>How to be a wizard</h1>
</header>AfterHeaderText
<ol>
<li>Grow a long, majestic beard.</li>
<li>Wear a tall, pointed hat.</li>
<li>Have I mentioned the beard?</li>
</ol>
BeforeFooterText<footer>
<p>© 2018 Gandalf</p>
</footer>AfterFooterText
TextBeforeOpenDiv<div>TextAfterOpenDiv<span>Text</span>TextBeforeCloseDiv</div>TextAfterCloseDiv
</article>
</div>
<!-- Future improvements -->
BEFORE
<span>Text</span>
AFTER
<div>TEXT</div>
<div>
<span>Text</span>
</div>
<div>TEXT</div>
<!-- WRONG -->
<div><include src=\\"./include-conten<span>Text</span>
</include>
"
`;
exports[`'attributes' option should handle the "include" tags: warnings 1`] = `Array []`;
exports[`'attributes' option should not handle attributes with a "boolean" notation equals "false": errors 1`] = `Array []`;
exports[`'attributes' option should not handle attributes with a "boolean" notation equals "false": module 1`] = `
Expand Down
2 changes: 1 addition & 1 deletion test/__snapshots__/validate-options.test.js.snap
Expand Up @@ -33,7 +33,7 @@ exports[`validate options should throw an error on the "attributes" option with
exports[`validate options should throw an error on the "attributes" option with "{"list":[{"tag":"img","attribute":"src"}]}" value 1`] = `
"Invalid options object. HTML Loader has been initialized using an options object that does not match the API schema.
- options.attributes.list[0] misses the property 'type'. Should be:
\\"src\\" | \\"srcset\\" | \\"include\\""
\\"src\\" | \\"srcset\\""
`;
exports[`validate options should throw an error on the "attributes" option with "{"root":false}" value 1`] = `
Expand Down
2 changes: 1 addition & 1 deletion test/attributes-option.test.js
Expand Up @@ -35,7 +35,7 @@ describe("'attributes' option", () => {
expect(getErrors(stats)).toMatchSnapshot('errors');
});

it('should handle the "include" tags', async () => {
it.skip('should handle the "include" tags', async () => {
const compiler = getCompiler('include.js', {
attributes: {
list: [
Expand Down

0 comments on commit b92ed21

Please sign in to comment.