From b92ed211783e3ae8a3e7d1d26ab3b9b5600a6de7 Mon Sep 17 00:00:00 2001 From: Evilebot Tnawi Date: Mon, 17 Aug 2020 19:45:12 +0300 Subject: [PATCH] refactor: postpone `include` feature (#308) --- src/options.json | 2 +- src/plugins/source-plugin.js | 93 ++++++++++--------- .../attributes-option.test.js.snap | 54 ----------- .../validate-options.test.js.snap | 2 +- test/attributes-option.test.js | 2 +- 5 files changed, 50 insertions(+), 103 deletions(-) diff --git a/src/options.json b/src/options.json index 1991a355..8291e308 100644 --- a/src/options.json +++ b/src/options.json @@ -13,7 +13,7 @@ "minLength": 1 }, "type": { - "enum": ["src", "srcset", "include"] + "enum": ["src", "srcset"] }, "filter": { "instanceof": "Function" diff --git a/src/plugins/source-plugin.js b/src/plugins/source-plugin.js index 7258fc84..5d89f558 100644 --- a/src/plugins/source-plugin.js +++ b/src/plugins/source-plugin.js @@ -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') - ); - - 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') + // ); + // + // if (!closingTag) { + // return; + // } + // + // const endIndex = startIndex + closingTag[0].length; + // const importItem = getImportItem(source.value); + // const replacementItem = getReplacementItem(importItem); + // + // sources.push({ replacementItem, startIndex, endIndex }); + // + // break; + // } } }); diff --git a/test/__snapshots__/attributes-option.test.js.snap b/test/__snapshots__/attributes-option.test.js.snap index ddeb9484..418a0a4a 100644 --- a/test/__snapshots__/attributes-option.test.js.snap +++ b/test/__snapshots__/attributes-option.test.js.snap @@ -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 = \\"
\\\\n
\\\\n BeforeHeaderText\\" + ___HTML_LOADER_REPLACEMENT_0___ + \\"AfterHeaderText\\\\n
    \\\\n
  1. Grow a long, majestic beard.
  2. \\\\n
  3. Wear a tall, pointed hat.
  4. \\\\n
  5. Have I mentioned the beard?
  6. \\\\n
\\\\n BeforeFooterText\\" + ___HTML_LOADER_REPLACEMENT_1___ + \\"AfterFooterText\\\\n TextBeforeOpenDiv
TextAfterOpenDiv\\" + ___HTML_LOADER_REPLACEMENT_2___ + \\"TextBeforeCloseDiv
TextAfterCloseDiv\\\\n
\\\\n
\\\\n\\\\n\\\\nBEFORE\\\\n\\" + ___HTML_LOADER_REPLACEMENT_2___ + \\"\\\\nAFTER\\\\n\\\\n
TEXT
\\\\n
\\\\n \\" + ___HTML_LOADER_REPLACEMENT_2___ + \\"\\\\n
\\\\n
TEXT
\\\\n\\\\n\\\\n
\\\\n\\"; -// Exports -module.exports = code;" -`; - -exports[`'attributes' option should handle the "include" tags: result 1`] = ` -"
-
- BeforeHeaderText
-

How to be a wizard

-
AfterHeaderText -
    -
  1. Grow a long, majestic beard.
  2. -
  3. Wear a tall, pointed hat.
  4. -
  5. Have I mentioned the beard?
  6. -
- BeforeFooterText
-

© 2018 Gandalf

-
AfterFooterText - TextBeforeOpenDiv
TextAfterOpenDivTextTextBeforeCloseDiv
TextAfterCloseDiv -
-
- - -BEFORE -Text -AFTER - -
TEXT
-
- Text -
-
TEXT
- - -
Text - -" -`; - -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`] = ` diff --git a/test/__snapshots__/validate-options.test.js.snap b/test/__snapshots__/validate-options.test.js.snap index 8bd095a1..be2674c3 100644 --- a/test/__snapshots__/validate-options.test.js.snap +++ b/test/__snapshots__/validate-options.test.js.snap @@ -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`] = ` diff --git a/test/attributes-option.test.js b/test/attributes-option.test.js index 3890a562..f5d1f480 100644 --- a/test/attributes-option.test.js +++ b/test/attributes-option.test.js @@ -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: [