Skip to content

Commit 57e6748

Browse files
arobase-chewooorm
authored andcommitted
Fix elements that switch to non-data states
Closes GH-6. Closes GH-7. Closes GH-8.
1 parent a880e5f commit 57e6748

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ var xtend = require('xtend');
1212
module.exports = wrap;
1313

1414
var IN_TEMPLATE_MODE = 'IN_TEMPLATE_MODE';
15+
var DATA_MODE = 'DATA_STATE';
1516
var CHARACTER_TOKEN = 'CHARACTER_TOKEN';
1617
var START_TAG_TOKEN = 'START_TAG_TOKEN';
1718
var END_TAG_TOKEN = 'END_TAG_TOKEN';
@@ -130,6 +131,12 @@ function wrap(tree, file) {
130131

131132
if (!empty) {
132133
parser._processToken(endTag(node));
134+
135+
// Put the parser back in data mode: some elements, like
136+
// textareas and iframes, change the state.
137+
// See syntax-tree/hast-util-raw/issues/7.
138+
// See https://github.com/inikulin/parse5/blob/2528196/packages/parse5/lib/tokenizer/index.js#L222
139+
tokenizer.state = DATA_MODE;
133140
}
134141
}
135142

test.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,56 @@ test('raw', function (t) {
102102
'should pass raw nodes through (#2)'
103103
);
104104

105+
t.deepEqual(
106+
raw(u('root', [
107+
h('iframe', {height: 500, src: 'https://ddg.gg'}),
108+
u('raw', '<img alt="foo" src="bar.jpg">')
109+
])),
110+
u('root', {data: {quirksMode: false}}, [
111+
h('iframe', {height: 500, src: 'https://ddg.gg'}),
112+
h('img', {alt: 'foo', src: 'bar.jpg'})
113+
]),
114+
'should pass raw nodes through even after iframe'
115+
);
116+
117+
t.deepEqual(
118+
raw(u('root', [
119+
h('textarea', u('text', 'Some text that is <i>not</i> HTML.')),
120+
u('raw', '<img alt="foo" src="bar.jpg">')
121+
])),
122+
u('root', {data: {quirksMode: false}}, [
123+
h('textarea', u('text', 'Some text that is <i>not</i> HTML.')),
124+
h('img', {alt: 'foo', src: 'bar.jpg'})
125+
]),
126+
'should pass raw nodes through even after textarea (#1)'
127+
);
128+
129+
t.deepEqual(
130+
raw(u('root', [
131+
u('raw', '<textarea>Some text that is <i>not</i> HTML.</textarea>'),
132+
u('raw', '<img alt="foo" src="bar.jpg">')
133+
])),
134+
u('root', {data: {quirksMode: false}}, [
135+
h('textarea', u('text', 'Some text that is <i>not</i> HTML.')),
136+
h('img', {alt: 'foo', src: 'bar.jpg'})
137+
]),
138+
'should pass raw nodes through even after textarea (#2)'
139+
);
140+
141+
t.deepEqual(
142+
raw(u('root', [
143+
u('raw', '<textarea>'),
144+
u('text', 'Some text that is <i>not</i> HTML.'),
145+
u('raw', '</textarea>'),
146+
u('raw', '<p>but this is</p>')
147+
])),
148+
u('root', {data: {quirksMode: false}}, [
149+
h('textarea', u('text', 'Some text that is <i>not</i> HTML.')),
150+
h('p', u('text', 'but this is'))
151+
]),
152+
'should pass raw nodes through even after textarea (#3)'
153+
);
154+
105155
t.end();
106156
});
107157

0 commit comments

Comments
 (0)