diff --git a/.editorconfig b/.editorconfig index 5f795282..df2c4701 100644 --- a/.editorconfig +++ b/.editorconfig @@ -10,3 +10,6 @@ trim_trailing_whitespace = true [*.md] trim_trailing_whitespace = false + +[test/fixtures/*.html] +insert_final_newline = false diff --git a/src/plugins/source-plugin.js b/src/plugins/source-plugin.js index 7b7426d0..57e63f83 100644 --- a/src/plugins/source-plugin.js +++ b/src/plugins/source-plugin.js @@ -340,6 +340,11 @@ export default (options) => case 'include': { let source; + // eslint-disable-next-line no-underscore-dangle + if (parser._tokenizer._state === 4) { + return; + } + try { source = parseSrc(value); } catch (error) { @@ -359,15 +364,22 @@ export default (options) => return; } - const { startIndex, endIndex } = parser; + 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: endIndex + 1, - }); + 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 fd0f6725..b99819d1 100644 --- a/test/__snapshots__/attributes-option.test.js.snap +++ b/test/__snapshots__/attributes-option.test.js.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`'attributes' option should handle "src" and "srcset" tags correctly: errors 1`] = ` +exports[`'attributes' option should handle "sources" tags: errors 1`] = ` Array [ "ModuleError: Module Error (from /path/to/file.js): HtmlSourceError: Bad value for attribute \\"src\\" on element \\"img\\": Must be non-empty (From line 10, column 1; to line 10, column 15)", @@ -447,7 +447,7 @@ HtmlSourceError: Bad value for attribute \\"srcset\\" on element \\"img\\": Must ] `; -exports[`'attributes' option should handle "src" and "srcset" tags correctly: module 1`] = ` +exports[`'attributes' option should handle "sources" tags: module 1`] = ` "// Imports var ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ = require(\\"../../src/runtime/getUrl.js\\"); var ___HTML_LOADER_IMPORT_0___ = require(\\"./image.png\\"); @@ -458,7 +458,7 @@ var code = \\"\\"Elva \\"Elva @@ -736,7 +736,7 @@ exports[`'attributes' option should handle "src" and "srcset" tags correctly: re " `; -exports[`'attributes' option should handle "src" and "srcset" tags correctly: warnings 1`] = `Array []`; +exports[`'attributes' option should handle "sources" tags: warnings 1`] = `Array []`; exports[`'attributes' option should handle all src attributes in all HTML tags except img tag (testing filter option): errors 1`] = `Array []`; @@ -1778,9 +1778,9 @@ or exports[`'attributes' option should handle attributes with a "boolean" notation equals "true": warnings 1`] = `Array []`; -exports[`'attributes' option should handle the "include" type of tags: errors 1`] = `Array []`; +exports[`'attributes' option should handle the "include" tags: errors 1`] = `Array []`; -exports[`'attributes' option should handle the "include" type of tags: module 1`] = ` +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\\"); @@ -1790,18 +1790,17 @@ var ___HTML_LOADER_IMPORT_2___ = require(\\"./include-content.html\\"); 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 \\\\n TextBeforeOpenDiv
TextAfterOpenDiv\\" + ___HTML_LOADER_REPLACEMENT_2___ + \\"TextBeforeCloseDiv
TextAfterCloseDiv\\\\n
\\\\n
\\\\n\\"; +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" type of tags: result 1`] = ` +exports[`'attributes' option should handle the "include" tags: result 1`] = ` "
BeforeHeaderText

How to be a wizard

-
-AfterHeaderText +AfterHeaderText
  1. Grow a long, majestic beard.
  2. Wear a tall, pointed hat.
  3. @@ -1809,17 +1808,29 @@ AfterHeaderText
BeforeFooterText

© 2018 Gandalf

-
-AfterFooterText - - TextBeforeOpenDiv
TextAfterOpenDivText -TextBeforeCloseDiv
TextAfterCloseDiv +AfterFooterText + TextBeforeOpenDiv
TextAfterOpenDivTextTextBeforeCloseDiv
TextAfterCloseDiv
+ + +BEFORE +Text +AFTER + +
TEXT
+
+ Text +
+
TEXT
+ + +
Text + " `; -exports[`'attributes' option should handle the "include" type of tags: warnings 1`] = `Array []`; +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 []`; diff --git a/test/attributes-option.test.js b/test/attributes-option.test.js index b106eb03..3890a562 100644 --- a/test/attributes-option.test.js +++ b/test/attributes-option.test.js @@ -23,7 +23,7 @@ describe("'attributes' option", () => { expect(getErrors(stats)).toMatchSnapshot('errors'); }); - it('should handle "src" and "srcset" tags correctly', async () => { + it('should handle "sources" tags', async () => { const compiler = getCompiler('sources.js'); const stats = await compile(compiler); @@ -35,7 +35,7 @@ describe("'attributes' option", () => { expect(getErrors(stats)).toMatchSnapshot('errors'); }); - it('should handle the "include" type of tags', async () => { + it('should handle the "include" tags', async () => { const compiler = getCompiler('include.js', { attributes: { list: [ diff --git a/test/fixtures/include-content.html b/test/fixtures/include-content.html index d9107199..6c3e1f07 100644 --- a/test/fixtures/include-content.html +++ b/test/fixtures/include-content.html @@ -1 +1 @@ -Text +Text \ No newline at end of file diff --git a/test/fixtures/include-footer.html b/test/fixtures/include-footer.html index e1d70317..e6ece295 100644 --- a/test/fixtures/include-footer.html +++ b/test/fixtures/include-footer.html @@ -1,3 +1,3 @@

© 2018 Gandalf

-
+ \ No newline at end of file diff --git a/test/fixtures/include-header.html b/test/fixtures/include-header.html index 28ec4f3e..4d06819d 100644 --- a/test/fixtures/include-header.html +++ b/test/fixtures/include-header.html @@ -1,3 +1,3 @@

How to be a wizard

-
+ \ No newline at end of file diff --git a/test/fixtures/include-partial.html b/test/fixtures/include-partial.html new file mode 100644 index 00000000..58c5b3c6 --- /dev/null +++ b/test/fixtures/include-partial.html @@ -0,0 +1,13 @@ +
+ Example +
+ +
+
+
+ +
+ +
\ No newline at end of file diff --git a/test/fixtures/include.html b/test/fixtures/include.html index 9ba73419..28653212 100644 --- a/test/fixtures/include.html +++ b/test/fixtures/include.html @@ -1,13 +1,33 @@
- BeforeHeaderTextAfterHeaderText + BeforeHeaderTextAfterHeaderText
  1. Grow a long, majestic beard.
  2. Wear a tall, pointed hat.
  3. Have I mentioned the beard?
- BeforeFooterTextAfterFooterText - - TextBeforeOpenDiv
TextAfterOpenDivTextBeforeCloseDiv
TextAfterCloseDiv + BeforeFooterTextAfterFooterText + TextBeforeOpenDiv
TextAfterOpenDivTextBeforeCloseDiv
TextAfterCloseDiv
+ + +BEFORE + +
Header
+
Header
+
Footer
+
+AFTER + +
TEXT
+
+ +
+
TEXT
+ + +
+ + +