@@ -1736,36 +1736,42 @@ foo
1736
1736
})
1737
1737
})
1738
1738
1739
-
describe('whitespace management', () => {
1739
+
describe('whitespace management when adopting strategy condense', () => {
1740
+
const parse = (content: string, options?: ParserOptions) =>
1741
+
baseParse(content, {
1742
+
whitespace: 'condense',
1743
+
...options
1744
+
})
1745
+
1740
1746
it('should remove whitespaces at start/end inside an element', () => {
1741
-
const ast = baseParse(`<div> <span/> </div>`)
1747
+
const ast = parse(`<div> <span/> </div>`)
1742
1748
expect((ast.children[0] as ElementNode).children.length).toBe(1)
1743
1749
})
1744
1750
1745
1751
it('should remove whitespaces w/ newline between elements', () => {
1746
-
const ast = baseParse(`<div/> \n <div/> \n <div/>`)
1752
+
const ast = parse(`<div/> \n <div/> \n <div/>`)
1747
1753
expect(ast.children.length).toBe(3)
1748
1754
expect(ast.children.every(c => c.type === NodeTypes.ELEMENT)).toBe(true)
1749
1755
})
1750
1756
1751
1757
it('should remove whitespaces adjacent to comments', () => {
1752
-
const ast = baseParse(`<div/> \n <!--foo--> <div/>`)
1758
+
const ast = parse(`<div/> \n <!--foo--> <div/>`)
1753
1759
expect(ast.children.length).toBe(3)
1754
1760
expect(ast.children[0].type).toBe(NodeTypes.ELEMENT)
1755
1761
expect(ast.children[1].type).toBe(NodeTypes.COMMENT)
1756
1762
expect(ast.children[2].type).toBe(NodeTypes.ELEMENT)
1757
1763
})
1758
1764
1759
1765
it('should remove whitespaces w/ newline between comments and elements', () => {
1760
-
const ast = baseParse(`<div/> \n <!--foo--> \n <div/>`)
1766
+
const ast = parse(`<div/> \n <!--foo--> \n <div/>`)
1761
1767
expect(ast.children.length).toBe(3)
1762
1768
expect(ast.children[0].type).toBe(NodeTypes.ELEMENT)
1763
1769
expect(ast.children[1].type).toBe(NodeTypes.COMMENT)
1764
1770
expect(ast.children[2].type).toBe(NodeTypes.ELEMENT)
1765
1771
})
1766
1772
1767
1773
it('should NOT remove whitespaces w/ newline between interpolations', () => {
1768
-
const ast = baseParse(`{{ foo }} \n {{ bar }}`)
1774
+
const ast = parse(`{{ foo }} \n {{ bar }}`)
1769
1775
expect(ast.children.length).toBe(3)
1770
1776
expect(ast.children[0].type).toBe(NodeTypes.INTERPOLATION)
1771
1777
expect(ast.children[1]).toMatchObject({
@@ -1776,7 +1782,7 @@ foo
1776
1782
})
1777
1783
1778
1784
it('should NOT remove whitespaces w/o newline between elements', () => {
1779
-
const ast = baseParse(`<div/> <div/> <div/>`)
1785
+
const ast = parse(`<div/> <div/> <div/>`)
1780
1786
expect(ast.children.length).toBe(5)
1781
1787
expect(ast.children.map(c => c.type)).toMatchObject([
1782
1788
NodeTypes.ELEMENT,
@@ -1788,7 +1794,7 @@ foo
1788
1794
})
1789
1795
1790
1796
it('should condense consecutive whitespaces in text', () => {
1791
-
const ast = baseParse(` foo \n bar baz `)
1797
+
const ast = parse(` foo \n bar baz `)
1792
1798
expect((ast.children[0] as TextNode).content).toBe(` foo bar baz `)
1793
1799
})
1794
1800
@@ -1824,6 +1830,84 @@ foo
1824
1830
})
1825
1831
})
1826
1832
1833
+
describe('whitespace management when adopting strategy preserve', () => {
1834
+
const parse = (content: string, options?: ParserOptions) =>
1835
+
baseParse(content, {
1836
+
whitespace: 'preserve',
1837
+
...options
1838
+
})
1839
+
1840
+
it('should preserve whitespaces at start/end inside an element', () => {
1841
+
const ast = parse(`<div> <span/> </div>`)
1842
+
expect((ast.children[0] as ElementNode).children.length).toBe(3)
1843
+
})
1844
+
1845
+
it('should preserve whitespaces w/ newline between elements', () => {
1846
+
const ast = parse(`<div/> \n <div/> \n <div/>`)
1847
+
expect(ast.children.length).toBe(5)
1848
+
expect(ast.children.map(c => c.type)).toMatchObject([
1849
+
NodeTypes.ELEMENT,
1850
+
NodeTypes.TEXT,
1851
+
NodeTypes.ELEMENT,
1852
+
NodeTypes.TEXT,
1853
+
NodeTypes.ELEMENT
1854
+
])
1855
+
})
1856
+
1857
+
it('should preserve whitespaces adjacent to comments', () => {
1858
+
const ast = parse(`<div/> \n <!--foo--> <div/>`)
1859
+
expect(ast.children.length).toBe(5)
1860
+
expect(ast.children.map(c => c.type)).toMatchObject([
1861
+
NodeTypes.ELEMENT,
1862
+
NodeTypes.TEXT,
1863
+
NodeTypes.COMMENT,
1864
+
NodeTypes.TEXT,
1865
+
NodeTypes.ELEMENT
1866
+
])
1867
+
})
1868
+
1869
+
it('should preserve whitespaces w/ newline between comments and elements', () => {
1870
+
const ast = parse(`<div/> \n <!--foo--> \n <div/>`)
1871
+
expect(ast.children.length).toBe(5)
1872
+
expect(ast.children.map(c => c.type)).toMatchObject([
1873
+
NodeTypes.ELEMENT,
1874
+
NodeTypes.TEXT,
1875
+
NodeTypes.COMMENT,
1876
+
NodeTypes.TEXT,
1877
+
NodeTypes.ELEMENT
1878
+
])
1879
+
})
1880
+
1881
+
it('should preserve whitespaces w/ newline between interpolations', () => {
1882
+
const ast = parse(`{{ foo }} \n {{ bar }}`)
1883
+
expect(ast.children.length).toBe(3)
1884
+
expect(ast.children[0].type).toBe(NodeTypes.INTERPOLATION)
1885
+
expect(ast.children[1]).toMatchObject({
1886
+
type: NodeTypes.TEXT,
1887
+
content: ' \n '
1888
+
})
1889
+
expect(ast.children[2].type).toBe(NodeTypes.INTERPOLATION)
1890
+
})
1891
+
1892
+
it('should preserve whitespaces w/o newline between elements', () => {
1893
+
const ast = parse(`<div/> <div/> <div/>`)
1894
+
expect(ast.children.length).toBe(5)
1895
+
expect(ast.children.map(c => c.type)).toMatchObject([
1896
+
NodeTypes.ELEMENT,
1897
+
NodeTypes.TEXT,
1898
+
NodeTypes.ELEMENT,
1899
+
NodeTypes.TEXT,
1900
+
NodeTypes.ELEMENT
1901
+
])
1902
+
})
1903
+
1904
+
it('should preserve consecutive whitespaces in text', () => {
1905
+
const content = ` foo \n bar baz `
1906
+
const ast = parse(content)
1907
+
expect((ast.children[0] as TextNode).content).toBe(content)
1908
+
})
1909
+
})
1910
+
1827
1911
describe('Errors', () => {
1828
1912
const patterns: {
1829
1913
[key: string]: Array<{
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4