HTMLparser.c 201 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952
  1. /*
  2. * HTMLparser.c : an HTML 4.0 non-verifying parser
  3. *
  4. * See Copyright for the status of this software.
  5. *
  6. * daniel@veillard.com
  7. */
  8. #define IN_LIBXML
  9. #include "libxml.h"
  10. #ifdef LIBXML_HTML_ENABLED
  11. #include <string.h>
  12. #include <ctype.h>
  13. #include <stdlib.h>
  14. #include <libxml/HTMLparser.h>
  15. #include <libxml/xmlmemory.h>
  16. #include <libxml/tree.h>
  17. #include <libxml/parser.h>
  18. #include <libxml/parserInternals.h>
  19. #include <libxml/xmlerror.h>
  20. #include <libxml/HTMLtree.h>
  21. #include <libxml/entities.h>
  22. #include <libxml/encoding.h>
  23. #include <libxml/xmlIO.h>
  24. #include <libxml/uri.h>
  25. #include "private/buf.h"
  26. #include "private/enc.h"
  27. #include "private/error.h"
  28. #include "private/html.h"
  29. #include "private/io.h"
  30. #include "private/parser.h"
  31. #include "private/tree.h"
  32. #define HTML_MAX_NAMELEN 1000
  33. #define HTML_PARSER_BIG_BUFFER_SIZE 1000
  34. #define HTML_PARSER_BUFFER_SIZE 100
  35. static int htmlOmittedDefaultValue = 1;
  36. xmlChar * htmlDecodeEntities(htmlParserCtxtPtr ctxt, int len,
  37. xmlChar end, xmlChar end2, xmlChar end3);
  38. static void htmlParseComment(htmlParserCtxtPtr ctxt);
  39. /************************************************************************
  40. * *
  41. * Some factorized error routines *
  42. * *
  43. ************************************************************************/
  44. /**
  45. * htmlErrMemory:
  46. * @ctxt: an HTML parser context
  47. * @extra: extra information
  48. *
  49. * Handle a redefinition of attribute error
  50. */
  51. static void
  52. htmlErrMemory(xmlParserCtxtPtr ctxt, const char *extra)
  53. {
  54. if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
  55. (ctxt->instate == XML_PARSER_EOF))
  56. return;
  57. if (ctxt != NULL) {
  58. ctxt->errNo = XML_ERR_NO_MEMORY;
  59. ctxt->instate = XML_PARSER_EOF;
  60. ctxt->disableSAX = 1;
  61. }
  62. if (extra)
  63. __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
  64. XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
  65. NULL, NULL, 0, 0,
  66. "Memory allocation failed : %s\n", extra);
  67. else
  68. __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_PARSER,
  69. XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, NULL,
  70. NULL, NULL, 0, 0, "Memory allocation failed\n");
  71. }
  72. /**
  73. * htmlParseErr:
  74. * @ctxt: an HTML parser context
  75. * @error: the error number
  76. * @msg: the error message
  77. * @str1: string infor
  78. * @str2: string infor
  79. *
  80. * Handle a fatal parser error, i.e. violating Well-Formedness constraints
  81. */
  82. static void LIBXML_ATTR_FORMAT(3,0)
  83. htmlParseErr(xmlParserCtxtPtr ctxt, xmlParserErrors error,
  84. const char *msg, const xmlChar *str1, const xmlChar *str2)
  85. {
  86. if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
  87. (ctxt->instate == XML_PARSER_EOF))
  88. return;
  89. if (ctxt != NULL)
  90. ctxt->errNo = error;
  91. __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_HTML, error,
  92. XML_ERR_ERROR, NULL, 0,
  93. (const char *) str1, (const char *) str2,
  94. NULL, 0, 0,
  95. msg, str1, str2);
  96. if (ctxt != NULL)
  97. ctxt->wellFormed = 0;
  98. }
  99. /**
  100. * htmlParseErrInt:
  101. * @ctxt: an HTML parser context
  102. * @error: the error number
  103. * @msg: the error message
  104. * @val: integer info
  105. *
  106. * Handle a fatal parser error, i.e. violating Well-Formedness constraints
  107. */
  108. static void LIBXML_ATTR_FORMAT(3,0)
  109. htmlParseErrInt(xmlParserCtxtPtr ctxt, xmlParserErrors error,
  110. const char *msg, int val)
  111. {
  112. if ((ctxt != NULL) && (ctxt->disableSAX != 0) &&
  113. (ctxt->instate == XML_PARSER_EOF))
  114. return;
  115. if (ctxt != NULL)
  116. ctxt->errNo = error;
  117. __xmlRaiseError(NULL, NULL, NULL, ctxt, NULL, XML_FROM_HTML, error,
  118. XML_ERR_ERROR, NULL, 0, NULL, NULL,
  119. NULL, val, 0, msg, val);
  120. if (ctxt != NULL)
  121. ctxt->wellFormed = 0;
  122. }
  123. /************************************************************************
  124. * *
  125. * Parser stacks related functions and macros *
  126. * *
  127. ************************************************************************/
  128. /**
  129. * htmlnamePush:
  130. * @ctxt: an HTML parser context
  131. * @value: the element name
  132. *
  133. * Pushes a new element name on top of the name stack
  134. *
  135. * Returns -1 in case of error, the index in the stack otherwise
  136. */
  137. static int
  138. htmlnamePush(htmlParserCtxtPtr ctxt, const xmlChar * value)
  139. {
  140. if ((ctxt->html < 3) && (xmlStrEqual(value, BAD_CAST "head")))
  141. ctxt->html = 3;
  142. if ((ctxt->html < 10) && (xmlStrEqual(value, BAD_CAST "body")))
  143. ctxt->html = 10;
  144. if (ctxt->nameNr >= ctxt->nameMax) {
  145. size_t newSize = ctxt->nameMax * 2;
  146. const xmlChar **tmp;
  147. tmp = xmlRealloc((xmlChar **) ctxt->nameTab,
  148. newSize * sizeof(ctxt->nameTab[0]));
  149. if (tmp == NULL) {
  150. htmlErrMemory(ctxt, NULL);
  151. return (-1);
  152. }
  153. ctxt->nameTab = tmp;
  154. ctxt->nameMax = newSize;
  155. }
  156. ctxt->nameTab[ctxt->nameNr] = value;
  157. ctxt->name = value;
  158. return (ctxt->nameNr++);
  159. }
  160. /**
  161. * htmlnamePop:
  162. * @ctxt: an HTML parser context
  163. *
  164. * Pops the top element name from the name stack
  165. *
  166. * Returns the name just removed
  167. */
  168. static const xmlChar *
  169. htmlnamePop(htmlParserCtxtPtr ctxt)
  170. {
  171. const xmlChar *ret;
  172. if (ctxt->nameNr <= 0)
  173. return (NULL);
  174. ctxt->nameNr--;
  175. if (ctxt->nameNr < 0)
  176. return (NULL);
  177. if (ctxt->nameNr > 0)
  178. ctxt->name = ctxt->nameTab[ctxt->nameNr - 1];
  179. else
  180. ctxt->name = NULL;
  181. ret = ctxt->nameTab[ctxt->nameNr];
  182. ctxt->nameTab[ctxt->nameNr] = NULL;
  183. return (ret);
  184. }
  185. /**
  186. * htmlNodeInfoPush:
  187. * @ctxt: an HTML parser context
  188. * @value: the node info
  189. *
  190. * Pushes a new element name on top of the node info stack
  191. *
  192. * Returns 0 in case of error, the index in the stack otherwise
  193. */
  194. static int
  195. htmlNodeInfoPush(htmlParserCtxtPtr ctxt, htmlParserNodeInfo *value)
  196. {
  197. if (ctxt->nodeInfoNr >= ctxt->nodeInfoMax) {
  198. if (ctxt->nodeInfoMax == 0)
  199. ctxt->nodeInfoMax = 5;
  200. ctxt->nodeInfoMax *= 2;
  201. ctxt->nodeInfoTab = (htmlParserNodeInfo *)
  202. xmlRealloc((htmlParserNodeInfo *)ctxt->nodeInfoTab,
  203. ctxt->nodeInfoMax *
  204. sizeof(ctxt->nodeInfoTab[0]));
  205. if (ctxt->nodeInfoTab == NULL) {
  206. htmlErrMemory(ctxt, NULL);
  207. return (0);
  208. }
  209. }
  210. ctxt->nodeInfoTab[ctxt->nodeInfoNr] = *value;
  211. ctxt->nodeInfo = &ctxt->nodeInfoTab[ctxt->nodeInfoNr];
  212. return (ctxt->nodeInfoNr++);
  213. }
  214. /**
  215. * htmlNodeInfoPop:
  216. * @ctxt: an HTML parser context
  217. *
  218. * Pops the top element name from the node info stack
  219. *
  220. * Returns 0 in case of error, the pointer to NodeInfo otherwise
  221. */
  222. static htmlParserNodeInfo *
  223. htmlNodeInfoPop(htmlParserCtxtPtr ctxt)
  224. {
  225. if (ctxt->nodeInfoNr <= 0)
  226. return (NULL);
  227. ctxt->nodeInfoNr--;
  228. if (ctxt->nodeInfoNr < 0)
  229. return (NULL);
  230. if (ctxt->nodeInfoNr > 0)
  231. ctxt->nodeInfo = &ctxt->nodeInfoTab[ctxt->nodeInfoNr - 1];
  232. else
  233. ctxt->nodeInfo = NULL;
  234. return &ctxt->nodeInfoTab[ctxt->nodeInfoNr];
  235. }
  236. /*
  237. * Macros for accessing the content. Those should be used only by the parser,
  238. * and not exported.
  239. *
  240. * Dirty macros, i.e. one need to make assumption on the context to use them
  241. *
  242. * CUR_PTR return the current pointer to the xmlChar to be parsed.
  243. * CUR returns the current xmlChar value, i.e. a 8 bit value if compiled
  244. * in ISO-Latin or UTF-8, and the current 16 bit value if compiled
  245. * in UNICODE mode. This should be used internally by the parser
  246. * only to compare to ASCII values otherwise it would break when
  247. * running with UTF-8 encoding.
  248. * NXT(n) returns the n'th next xmlChar. Same as CUR is should be used only
  249. * to compare on ASCII based substring.
  250. * UPP(n) returns the n'th next xmlChar converted to uppercase. Same as CUR
  251. * it should be used only to compare on ASCII based substring.
  252. * SKIP(n) Skip n xmlChar, and must also be used only to skip ASCII defined
  253. * strings without newlines within the parser.
  254. *
  255. * Clean macros, not dependent of an ASCII context, expect UTF-8 encoding
  256. *
  257. * NEXT Skip to the next character, this does the proper decoding
  258. * in UTF-8 mode. It also pop-up unfinished entities on the fly.
  259. * NEXTL(l) Skip the current unicode character of l xmlChars long.
  260. * COPY(to) copy one char to *to, increment CUR_PTR and to accordingly
  261. */
  262. #define UPPER (toupper(*ctxt->input->cur))
  263. #define SKIP(val) ctxt->input->cur += (val),ctxt->input->col+=(val)
  264. #define NXT(val) ctxt->input->cur[(val)]
  265. #define UPP(val) (toupper(ctxt->input->cur[(val)]))
  266. #define CUR_PTR ctxt->input->cur
  267. #define BASE_PTR ctxt->input->base
  268. #define SHRINK if ((ctxt->input->cur - ctxt->input->base > 2 * INPUT_CHUNK) && \
  269. (ctxt->input->end - ctxt->input->cur < 2 * INPUT_CHUNK)) \
  270. xmlParserShrink(ctxt)
  271. #define GROW if ((ctxt->progressive == 0) && \
  272. (ctxt->input->end - ctxt->input->cur < INPUT_CHUNK)) \
  273. xmlParserGrow(ctxt)
  274. #define SKIP_BLANKS htmlSkipBlankChars(ctxt)
  275. /* Imported from XML */
  276. #define CUR (*ctxt->input->cur)
  277. #define NEXT xmlNextChar(ctxt)
  278. #define RAW (ctxt->token ? -1 : (*ctxt->input->cur))
  279. #define NEXTL(l) do { \
  280. if (*(ctxt->input->cur) == '\n') { \
  281. ctxt->input->line++; ctxt->input->col = 1; \
  282. } else ctxt->input->col++; \
  283. ctxt->token = 0; ctxt->input->cur += l; \
  284. } while (0)
  285. /************
  286. \
  287. if (*ctxt->input->cur == '%') xmlParserHandlePEReference(ctxt); \
  288. if (*ctxt->input->cur == '&') xmlParserHandleReference(ctxt);
  289. ************/
  290. #define CUR_CHAR(l) htmlCurrentChar(ctxt, &l)
  291. #define COPY_BUF(l,b,i,v) \
  292. if (l == 1) b[i++] = v; \
  293. else i += xmlCopyChar(l,&b[i],v)
  294. /**
  295. * htmlFindEncoding:
  296. * @the HTML parser context
  297. *
  298. * Ty to find and encoding in the current data available in the input
  299. * buffer this is needed to try to switch to the proper encoding when
  300. * one face a character error.
  301. * That's an heuristic, since it's operating outside of parsing it could
  302. * try to use a meta which had been commented out, that's the reason it
  303. * should only be used in case of error, not as a default.
  304. *
  305. * Returns an encoding string or NULL if not found, the string need to
  306. * be freed
  307. */
  308. static xmlChar *
  309. htmlFindEncoding(xmlParserCtxtPtr ctxt) {
  310. const xmlChar *start, *cur, *end;
  311. if ((ctxt == NULL) || (ctxt->input == NULL) ||
  312. (ctxt->input->flags & XML_INPUT_HAS_ENCODING))
  313. return(NULL);
  314. if ((ctxt->input->cur == NULL) || (ctxt->input->end == NULL))
  315. return(NULL);
  316. start = ctxt->input->cur;
  317. end = ctxt->input->end;
  318. /* we also expect the input buffer to be zero terminated */
  319. if (*end != 0)
  320. return(NULL);
  321. cur = xmlStrcasestr(start, BAD_CAST "HTTP-EQUIV");
  322. if (cur == NULL)
  323. return(NULL);
  324. cur = xmlStrcasestr(cur, BAD_CAST "CONTENT");
  325. if (cur == NULL)
  326. return(NULL);
  327. cur = xmlStrcasestr(cur, BAD_CAST "CHARSET=");
  328. if (cur == NULL)
  329. return(NULL);
  330. cur += 8;
  331. start = cur;
  332. while (((*cur >= 'A') && (*cur <= 'Z')) ||
  333. ((*cur >= 'a') && (*cur <= 'z')) ||
  334. ((*cur >= '0') && (*cur <= '9')) ||
  335. (*cur == '-') || (*cur == '_') || (*cur == ':') || (*cur == '/'))
  336. cur++;
  337. if (cur == start)
  338. return(NULL);
  339. return(xmlStrndup(start, cur - start));
  340. }
  341. /**
  342. * htmlCurrentChar:
  343. * @ctxt: the HTML parser context
  344. * @len: pointer to the length of the char read
  345. *
  346. * The current char value, if using UTF-8 this may actually span multiple
  347. * bytes in the input buffer. Implement the end of line normalization:
  348. * 2.11 End-of-Line Handling
  349. * If the encoding is unspecified, in the case we find an ISO-Latin-1
  350. * char, then the encoding converter is plugged in automatically.
  351. *
  352. * Returns the current char value and its length
  353. */
  354. static int
  355. htmlCurrentChar(xmlParserCtxtPtr ctxt, int *len) {
  356. const unsigned char *cur;
  357. unsigned char c;
  358. unsigned int val;
  359. if (ctxt->instate == XML_PARSER_EOF)
  360. return(0);
  361. if (ctxt->token != 0) {
  362. *len = 0;
  363. return(ctxt->token);
  364. }
  365. if (ctxt->input->end - ctxt->input->cur < INPUT_CHUNK) {
  366. xmlParserGrow(ctxt);
  367. if (ctxt->instate == XML_PARSER_EOF)
  368. return(0);
  369. }
  370. if ((ctxt->input->flags & XML_INPUT_HAS_ENCODING) == 0) {
  371. xmlChar * guess;
  372. xmlCharEncodingHandlerPtr handler;
  373. /*
  374. * Assume it's a fixed length encoding (1) with
  375. * a compatible encoding for the ASCII set, since
  376. * HTML constructs only use < 128 chars
  377. */
  378. if (*ctxt->input->cur < 0x80) {
  379. *len = 1;
  380. if ((*ctxt->input->cur == 0) &&
  381. (ctxt->input->cur < ctxt->input->end)) {
  382. htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
  383. "Char 0x%X out of allowed range\n", 0);
  384. return(' ');
  385. }
  386. return(*ctxt->input->cur);
  387. }
  388. /*
  389. * Humm this is bad, do an automatic flow conversion
  390. */
  391. guess = htmlFindEncoding(ctxt);
  392. if (guess == NULL) {
  393. xmlSwitchEncoding(ctxt, XML_CHAR_ENCODING_8859_1);
  394. } else {
  395. handler = xmlFindCharEncodingHandler((const char *) guess);
  396. if (handler != NULL) {
  397. /*
  398. * Don't use UTF-8 encoder which isn't required and
  399. * can produce invalid UTF-8.
  400. */
  401. if (!xmlStrEqual(BAD_CAST handler->name, BAD_CAST "UTF-8"))
  402. xmlSwitchToEncoding(ctxt, handler);
  403. } else {
  404. htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING,
  405. "Unsupported encoding %s", guess, NULL);
  406. }
  407. xmlFree(guess);
  408. }
  409. ctxt->input->flags |= XML_INPUT_HAS_ENCODING;
  410. }
  411. /*
  412. * We are supposed to handle UTF8, check it's valid
  413. * From rfc2044: encoding of the Unicode values on UTF-8:
  414. *
  415. * UCS-4 range (hex.) UTF-8 octet sequence (binary)
  416. * 0000 0000-0000 007F 0xxxxxxx
  417. * 0000 0080-0000 07FF 110xxxxx 10xxxxxx
  418. * 0000 0800-0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
  419. *
  420. * Check for the 0x110000 limit too
  421. */
  422. cur = ctxt->input->cur;
  423. c = *cur;
  424. if (c & 0x80) {
  425. size_t avail;
  426. if ((c & 0x40) == 0)
  427. goto encoding_error;
  428. avail = ctxt->input->end - ctxt->input->cur;
  429. if ((avail < 2) || ((cur[1] & 0xc0) != 0x80))
  430. goto encoding_error;
  431. if ((c & 0xe0) == 0xe0) {
  432. if ((avail < 3) || ((cur[2] & 0xc0) != 0x80))
  433. goto encoding_error;
  434. if ((c & 0xf0) == 0xf0) {
  435. if (((c & 0xf8) != 0xf0) ||
  436. (avail < 4) || ((cur[3] & 0xc0) != 0x80))
  437. goto encoding_error;
  438. /* 4-byte code */
  439. *len = 4;
  440. val = (cur[0] & 0x7) << 18;
  441. val |= (cur[1] & 0x3f) << 12;
  442. val |= (cur[2] & 0x3f) << 6;
  443. val |= cur[3] & 0x3f;
  444. if (val < 0x10000)
  445. goto encoding_error;
  446. } else {
  447. /* 3-byte code */
  448. *len = 3;
  449. val = (cur[0] & 0xf) << 12;
  450. val |= (cur[1] & 0x3f) << 6;
  451. val |= cur[2] & 0x3f;
  452. if (val < 0x800)
  453. goto encoding_error;
  454. }
  455. } else {
  456. /* 2-byte code */
  457. *len = 2;
  458. val = (cur[0] & 0x1f) << 6;
  459. val |= cur[1] & 0x3f;
  460. if (val < 0x80)
  461. goto encoding_error;
  462. }
  463. if (!IS_CHAR(val)) {
  464. htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
  465. "Char 0x%X out of allowed range\n", val);
  466. }
  467. return(val);
  468. } else {
  469. if ((*ctxt->input->cur == 0) &&
  470. (ctxt->input->cur < ctxt->input->end)) {
  471. htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
  472. "Char 0x%X out of allowed range\n", 0);
  473. *len = 1;
  474. return(' ');
  475. }
  476. /* 1-byte code */
  477. *len = 1;
  478. return(*ctxt->input->cur);
  479. }
  480. encoding_error:
  481. {
  482. char buffer[150];
  483. if (ctxt->input->end - ctxt->input->cur >= 4) {
  484. snprintf(buffer, 149, "Bytes: 0x%02X 0x%02X 0x%02X 0x%02X\n",
  485. ctxt->input->cur[0], ctxt->input->cur[1],
  486. ctxt->input->cur[2], ctxt->input->cur[3]);
  487. } else {
  488. snprintf(buffer, 149, "Bytes: 0x%02X\n", ctxt->input->cur[0]);
  489. }
  490. htmlParseErr(ctxt, XML_ERR_INVALID_ENCODING,
  491. "Input is not proper UTF-8, indicate encoding !\n",
  492. BAD_CAST buffer, NULL);
  493. }
  494. if ((ctxt->input->flags & XML_INPUT_HAS_ENCODING) == 0)
  495. xmlSwitchEncoding(ctxt, XML_CHAR_ENCODING_8859_1);
  496. *len = 1;
  497. return(*ctxt->input->cur);
  498. }
  499. /**
  500. * htmlSkipBlankChars:
  501. * @ctxt: the HTML parser context
  502. *
  503. * skip all blanks character found at that point in the input streams.
  504. *
  505. * Returns the number of space chars skipped
  506. */
  507. static int
  508. htmlSkipBlankChars(xmlParserCtxtPtr ctxt) {
  509. int res = 0;
  510. while (IS_BLANK_CH(*(ctxt->input->cur))) {
  511. if (*(ctxt->input->cur) == '\n') {
  512. ctxt->input->line++; ctxt->input->col = 1;
  513. } else ctxt->input->col++;
  514. ctxt->input->cur++;
  515. if (*ctxt->input->cur == 0)
  516. xmlParserGrow(ctxt);
  517. if (res < INT_MAX)
  518. res++;
  519. }
  520. return(res);
  521. }
  522. /************************************************************************
  523. * *
  524. * The list of HTML elements and their properties *
  525. * *
  526. ************************************************************************/
  527. /*
  528. * Start Tag: 1 means the start tag can be omitted
  529. * End Tag: 1 means the end tag can be omitted
  530. * 2 means it's forbidden (empty elements)
  531. * 3 means the tag is stylistic and should be closed easily
  532. * Depr: this element is deprecated
  533. * DTD: 1 means that this element is valid only in the Loose DTD
  534. * 2 means that this element is valid only in the Frameset DTD
  535. *
  536. * Name,Start Tag,End Tag,Save End,Empty,Deprecated,DTD,inline,Description
  537. , subElements , impliedsubelt , Attributes, userdata
  538. */
  539. /* Definitions and a couple of vars for HTML Elements */
  540. #define FONTSTYLE "tt", "i", "b", "u", "s", "strike", "big", "small"
  541. #define NB_FONTSTYLE 8
  542. #define PHRASE "em", "strong", "dfn", "code", "samp", "kbd", "var", "cite", "abbr", "acronym"
  543. #define NB_PHRASE 10
  544. #define SPECIAL "a", "img", "applet", "embed", "object", "font", "basefont", "br", "script", "map", "q", "sub", "sup", "span", "bdo", "iframe"
  545. #define NB_SPECIAL 16
  546. #define INLINE FONTSTYLE, PHRASE, SPECIAL, FORMCTRL
  547. #define NB_INLINE NB_PCDATA + NB_FONTSTYLE + NB_PHRASE + NB_SPECIAL + NB_FORMCTRL
  548. #define BLOCK HEADING, LIST, "pre", "p", "dl", "div", "center", "noscript", "noframes", "blockquote", "form", "isindex", "hr", "table", "fieldset", "address"
  549. #define NB_BLOCK NB_HEADING + NB_LIST + 14
  550. #define FORMCTRL "input", "select", "textarea", "label", "button"
  551. #define NB_FORMCTRL 5
  552. #define PCDATA
  553. #define NB_PCDATA 0
  554. #define HEADING "h1", "h2", "h3", "h4", "h5", "h6"
  555. #define NB_HEADING 6
  556. #define LIST "ul", "ol", "dir", "menu"
  557. #define NB_LIST 4
  558. #define MODIFIER
  559. #define NB_MODIFIER 0
  560. #define FLOW BLOCK,INLINE
  561. #define NB_FLOW NB_BLOCK + NB_INLINE
  562. #define EMPTY NULL
  563. static const char* const html_flow[] = { FLOW, NULL } ;
  564. static const char* const html_inline[] = { INLINE, NULL } ;
  565. /* placeholders: elts with content but no subelements */
  566. static const char* const html_pcdata[] = { NULL } ;
  567. #define html_cdata html_pcdata
  568. /* ... and for HTML Attributes */
  569. #define COREATTRS "id", "class", "style", "title"
  570. #define NB_COREATTRS 4
  571. #define I18N "lang", "dir"
  572. #define NB_I18N 2
  573. #define EVENTS "onclick", "ondblclick", "onmousedown", "onmouseup", "onmouseover", "onmouseout", "onkeypress", "onkeydown", "onkeyup"
  574. #define NB_EVENTS 9
  575. #define ATTRS COREATTRS,I18N,EVENTS
  576. #define NB_ATTRS NB_NB_COREATTRS + NB_I18N + NB_EVENTS
  577. #define CELLHALIGN "align", "char", "charoff"
  578. #define NB_CELLHALIGN 3
  579. #define CELLVALIGN "valign"
  580. #define NB_CELLVALIGN 1
  581. static const char* const html_attrs[] = { ATTRS, NULL } ;
  582. static const char* const core_i18n_attrs[] = { COREATTRS, I18N, NULL } ;
  583. static const char* const core_attrs[] = { COREATTRS, NULL } ;
  584. static const char* const i18n_attrs[] = { I18N, NULL } ;
  585. /* Other declarations that should go inline ... */
  586. static const char* const a_attrs[] = { ATTRS, "charset", "type", "name",
  587. "href", "hreflang", "rel", "rev", "accesskey", "shape", "coords",
  588. "tabindex", "onfocus", "onblur", NULL } ;
  589. static const char* const target_attr[] = { "target", NULL } ;
  590. static const char* const rows_cols_attr[] = { "rows", "cols", NULL } ;
  591. static const char* const alt_attr[] = { "alt", NULL } ;
  592. static const char* const src_alt_attrs[] = { "src", "alt", NULL } ;
  593. static const char* const href_attrs[] = { "href", NULL } ;
  594. static const char* const clear_attrs[] = { "clear", NULL } ;
  595. static const char* const inline_p[] = { INLINE, "p", NULL } ;
  596. static const char* const flow_param[] = { FLOW, "param", NULL } ;
  597. static const char* const applet_attrs[] = { COREATTRS , "codebase",
  598. "archive", "alt", "name", "height", "width", "align",
  599. "hspace", "vspace", NULL } ;
  600. static const char* const area_attrs[] = { "shape", "coords", "href", "nohref",
  601. "tabindex", "accesskey", "onfocus", "onblur", NULL } ;
  602. static const char* const basefont_attrs[] =
  603. { "id", "size", "color", "face", NULL } ;
  604. static const char* const quote_attrs[] = { ATTRS, "cite", NULL } ;
  605. static const char* const body_contents[] = { FLOW, "ins", "del", NULL } ;
  606. static const char* const body_attrs[] = { ATTRS, "onload", "onunload", NULL } ;
  607. static const char* const body_depr[] = { "background", "bgcolor", "text",
  608. "link", "vlink", "alink", NULL } ;
  609. static const char* const button_attrs[] = { ATTRS, "name", "value", "type",
  610. "disabled", "tabindex", "accesskey", "onfocus", "onblur", NULL } ;
  611. static const char* const col_attrs[] = { ATTRS, "span", "width", CELLHALIGN, CELLVALIGN, NULL } ;
  612. static const char* const col_elt[] = { "col", NULL } ;
  613. static const char* const edit_attrs[] = { ATTRS, "datetime", "cite", NULL } ;
  614. static const char* const compact_attrs[] = { ATTRS, "compact", NULL } ;
  615. static const char* const dl_contents[] = { "dt", "dd", NULL } ;
  616. static const char* const compact_attr[] = { "compact", NULL } ;
  617. static const char* const label_attr[] = { "label", NULL } ;
  618. static const char* const fieldset_contents[] = { FLOW, "legend" } ;
  619. static const char* const font_attrs[] = { COREATTRS, I18N, "size", "color", "face" , NULL } ;
  620. static const char* const form_contents[] = { HEADING, LIST, INLINE, "pre", "p", "div", "center", "noscript", "noframes", "blockquote", "isindex", "hr", "table", "fieldset", "address", NULL } ;
  621. static const char* const form_attrs[] = { ATTRS, "method", "enctype", "accept", "name", "onsubmit", "onreset", "accept-charset", NULL } ;
  622. static const char* const frame_attrs[] = { COREATTRS, "longdesc", "name", "src", "frameborder", "marginwidth", "marginheight", "noresize", "scrolling" , NULL } ;
  623. static const char* const frameset_attrs[] = { COREATTRS, "rows", "cols", "onload", "onunload", NULL } ;
  624. static const char* const frameset_contents[] = { "frameset", "frame", "noframes", NULL } ;
  625. static const char* const head_attrs[] = { I18N, "profile", NULL } ;
  626. static const char* const head_contents[] = { "title", "isindex", "base", "script", "style", "meta", "link", "object", NULL } ;
  627. static const char* const hr_depr[] = { "align", "noshade", "size", "width", NULL } ;
  628. static const char* const version_attr[] = { "version", NULL } ;
  629. static const char* const html_content[] = { "head", "body", "frameset", NULL } ;
  630. static const char* const iframe_attrs[] = { COREATTRS, "longdesc", "name", "src", "frameborder", "marginwidth", "marginheight", "scrolling", "align", "height", "width", NULL } ;
  631. static const char* const img_attrs[] = { ATTRS, "longdesc", "name", "height", "width", "usemap", "ismap", NULL } ;
  632. static const char* const embed_attrs[] = { COREATTRS, "align", "alt", "border", "code", "codebase", "frameborder", "height", "hidden", "hspace", "name", "palette", "pluginspace", "pluginurl", "src", "type", "units", "vspace", "width", NULL } ;
  633. static const char* const input_attrs[] = { ATTRS, "type", "name", "value", "checked", "disabled", "readonly", "size", "maxlength", "src", "alt", "usemap", "ismap", "tabindex", "accesskey", "onfocus", "onblur", "onselect", "onchange", "accept", NULL } ;
  634. static const char* const prompt_attrs[] = { COREATTRS, I18N, "prompt", NULL } ;
  635. static const char* const label_attrs[] = { ATTRS, "for", "accesskey", "onfocus", "onblur", NULL } ;
  636. static const char* const legend_attrs[] = { ATTRS, "accesskey", NULL } ;
  637. static const char* const align_attr[] = { "align", NULL } ;
  638. static const char* const link_attrs[] = { ATTRS, "charset", "href", "hreflang", "type", "rel", "rev", "media", NULL } ;
  639. static const char* const map_contents[] = { BLOCK, "area", NULL } ;
  640. static const char* const name_attr[] = { "name", NULL } ;
  641. static const char* const action_attr[] = { "action", NULL } ;
  642. static const char* const blockli_elt[] = { BLOCK, "li", NULL } ;
  643. static const char* const meta_attrs[] = { I18N, "http-equiv", "name", "scheme", "charset", NULL } ;
  644. static const char* const content_attr[] = { "content", NULL } ;
  645. static const char* const type_attr[] = { "type", NULL } ;
  646. static const char* const noframes_content[] = { "body", FLOW MODIFIER, NULL } ;
  647. static const char* const object_contents[] = { FLOW, "param", NULL } ;
  648. static const char* const object_attrs[] = { ATTRS, "declare", "classid", "codebase", "data", "type", "codetype", "archive", "standby", "height", "width", "usemap", "name", "tabindex", NULL } ;
  649. static const char* const object_depr[] = { "align", "border", "hspace", "vspace", NULL } ;
  650. static const char* const ol_attrs[] = { "type", "compact", "start", NULL} ;
  651. static const char* const option_elt[] = { "option", NULL } ;
  652. static const char* const optgroup_attrs[] = { ATTRS, "disabled", NULL } ;
  653. static const char* const option_attrs[] = { ATTRS, "disabled", "label", "selected", "value", NULL } ;
  654. static const char* const param_attrs[] = { "id", "value", "valuetype", "type", NULL } ;
  655. static const char* const width_attr[] = { "width", NULL } ;
  656. static const char* const pre_content[] = { PHRASE, "tt", "i", "b", "u", "s", "strike", "a", "br", "script", "map", "q", "span", "bdo", "iframe", NULL } ;
  657. static const char* const script_attrs[] = { "charset", "src", "defer", "event", "for", NULL } ;
  658. static const char* const language_attr[] = { "language", NULL } ;
  659. static const char* const select_content[] = { "optgroup", "option", NULL } ;
  660. static const char* const select_attrs[] = { ATTRS, "name", "size", "multiple", "disabled", "tabindex", "onfocus", "onblur", "onchange", NULL } ;
  661. static const char* const style_attrs[] = { I18N, "media", "title", NULL } ;
  662. static const char* const table_attrs[] = { ATTRS, "summary", "width", "border", "frame", "rules", "cellspacing", "cellpadding", "datapagesize", NULL } ;
  663. static const char* const table_depr[] = { "align", "bgcolor", NULL } ;
  664. static const char* const table_contents[] = { "caption", "col", "colgroup", "thead", "tfoot", "tbody", "tr", NULL} ;
  665. static const char* const tr_elt[] = { "tr", NULL } ;
  666. static const char* const talign_attrs[] = { ATTRS, CELLHALIGN, CELLVALIGN, NULL} ;
  667. static const char* const th_td_depr[] = { "nowrap", "bgcolor", "width", "height", NULL } ;
  668. static const char* const th_td_attr[] = { ATTRS, "abbr", "axis", "headers", "scope", "rowspan", "colspan", CELLHALIGN, CELLVALIGN, NULL } ;
  669. static const char* const textarea_attrs[] = { ATTRS, "name", "disabled", "readonly", "tabindex", "accesskey", "onfocus", "onblur", "onselect", "onchange", NULL } ;
  670. static const char* const tr_contents[] = { "th", "td", NULL } ;
  671. static const char* const bgcolor_attr[] = { "bgcolor", NULL } ;
  672. static const char* const li_elt[] = { "li", NULL } ;
  673. static const char* const ul_depr[] = { "type", "compact", NULL} ;
  674. static const char* const dir_attr[] = { "dir", NULL} ;
  675. #define DECL (const char**)
  676. static const htmlElemDesc
  677. html40ElementTable[] = {
  678. { "a", 0, 0, 0, 0, 0, 0, 1, "anchor ",
  679. DECL html_inline , NULL , DECL a_attrs , DECL target_attr, NULL
  680. },
  681. { "abbr", 0, 0, 0, 0, 0, 0, 1, "abbreviated form",
  682. DECL html_inline , NULL , DECL html_attrs, NULL, NULL
  683. },
  684. { "acronym", 0, 0, 0, 0, 0, 0, 1, "",
  685. DECL html_inline , NULL , DECL html_attrs, NULL, NULL
  686. },
  687. { "address", 0, 0, 0, 0, 0, 0, 0, "information on author ",
  688. DECL inline_p , NULL , DECL html_attrs, NULL, NULL
  689. },
  690. { "applet", 0, 0, 0, 0, 1, 1, 2, "java applet ",
  691. DECL flow_param , NULL , NULL , DECL applet_attrs, NULL
  692. },
  693. { "area", 0, 2, 2, 1, 0, 0, 0, "client-side image map area ",
  694. EMPTY , NULL , DECL area_attrs , DECL target_attr, DECL alt_attr
  695. },
  696. { "b", 0, 3, 0, 0, 0, 0, 1, "bold text style",
  697. DECL html_inline , NULL , DECL html_attrs, NULL, NULL
  698. },
  699. { "base", 0, 2, 2, 1, 0, 0, 0, "document base uri ",
  700. EMPTY , NULL , NULL , DECL target_attr, DECL href_attrs
  701. },
  702. { "basefont", 0, 2, 2, 1, 1, 1, 1, "base font size " ,
  703. EMPTY , NULL , NULL, DECL basefont_attrs, NULL
  704. },
  705. { "bdo", 0, 0, 0, 0, 0, 0, 1, "i18n bidi over-ride ",
  706. DECL html_inline , NULL , DECL core_i18n_attrs, NULL, DECL dir_attr
  707. },
  708. { "big", 0, 3, 0, 0, 0, 0, 1, "large text style",
  709. DECL html_inline , NULL , DECL html_attrs, NULL, NULL
  710. },
  711. { "blockquote", 0, 0, 0, 0, 0, 0, 0, "long quotation ",
  712. DECL html_flow , NULL , DECL quote_attrs , NULL, NULL
  713. },
  714. { "body", 1, 1, 0, 0, 0, 0, 0, "document body ",
  715. DECL body_contents , "div" , DECL body_attrs, DECL body_depr, NULL
  716. },
  717. { "br", 0, 2, 2, 1, 0, 0, 1, "forced line break ",
  718. EMPTY , NULL , DECL core_attrs, DECL clear_attrs , NULL
  719. },
  720. { "button", 0, 0, 0, 0, 0, 0, 2, "push button ",
  721. DECL html_flow MODIFIER , NULL , DECL button_attrs, NULL, NULL
  722. },
  723. { "caption", 0, 0, 0, 0, 0, 0, 0, "table caption ",
  724. DECL html_inline , NULL , DECL html_attrs, NULL, NULL
  725. },
  726. { "center", 0, 3, 0, 0, 1, 1, 0, "shorthand for div align=center ",
  727. DECL html_flow , NULL , NULL, DECL html_attrs, NULL
  728. },
  729. { "cite", 0, 0, 0, 0, 0, 0, 1, "citation",
  730. DECL html_inline , NULL , DECL html_attrs, NULL, NULL
  731. },
  732. { "code", 0, 0, 0, 0, 0, 0, 1, "computer code fragment",
  733. DECL html_inline , NULL , DECL html_attrs, NULL, NULL
  734. },
  735. { "col", 0, 2, 2, 1, 0, 0, 0, "table column ",
  736. EMPTY , NULL , DECL col_attrs , NULL, NULL
  737. },
  738. { "colgroup", 0, 1, 0, 0, 0, 0, 0, "table column group ",
  739. DECL col_elt , "col" , DECL col_attrs , NULL, NULL
  740. },
  741. { "dd", 0, 1, 0, 0, 0, 0, 0, "definition description ",
  742. DECL html_flow , NULL , DECL html_attrs, NULL, NULL
  743. },
  744. { "del", 0, 0, 0, 0, 0, 0, 2, "deleted text ",
  745. DECL html_flow , NULL , DECL edit_attrs , NULL, NULL
  746. },
  747. { "dfn", 0, 0, 0, 0, 0, 0, 1, "instance definition",
  748. DECL html_inline , NULL , DECL html_attrs, NULL, NULL
  749. },
  750. { "dir", 0, 0, 0, 0, 1, 1, 0, "directory list",
  751. DECL blockli_elt, "li" , NULL, DECL compact_attrs, NULL
  752. },
  753. { "div", 0, 0, 0, 0, 0, 0, 0, "generic language/style container",
  754. DECL html_flow, NULL, DECL html_attrs, DECL align_attr, NULL
  755. },
  756. { "dl", 0, 0, 0, 0, 0, 0, 0, "definition list ",
  757. DECL dl_contents , "dd" , DECL html_attrs, DECL compact_attr, NULL
  758. },
  759. { "dt", 0, 1, 0, 0, 0, 0, 0, "definition term ",
  760. DECL html_inline, NULL, DECL html_attrs, NULL, NULL
  761. },
  762. { "em", 0, 3, 0, 0, 0, 0, 1, "emphasis",
  763. DECL html_inline, NULL, DECL html_attrs, NULL, NULL
  764. },
  765. { "embed", 0, 1, 0, 0, 1, 1, 1, "generic embedded object ",
  766. EMPTY, NULL, DECL embed_attrs, NULL, NULL
  767. },
  768. { "fieldset", 0, 0, 0, 0, 0, 0, 0, "form control group ",
  769. DECL fieldset_contents , NULL, DECL html_attrs, NULL, NULL
  770. },
  771. { "font", 0, 3, 0, 0, 1, 1, 1, "local change to font ",
  772. DECL html_inline, NULL, NULL, DECL font_attrs, NULL
  773. },
  774. { "form", 0, 0, 0, 0, 0, 0, 0, "interactive form ",
  775. DECL form_contents, "fieldset", DECL form_attrs , DECL target_attr, DECL action_attr
  776. },
  777. { "frame", 0, 2, 2, 1, 0, 2, 0, "subwindow " ,
  778. EMPTY, NULL, NULL, DECL frame_attrs, NULL
  779. },
  780. { "frameset", 0, 0, 0, 0, 0, 2, 0, "window subdivision" ,
  781. DECL frameset_contents, "noframes" , NULL , DECL frameset_attrs, NULL
  782. },
  783. { "h1", 0, 0, 0, 0, 0, 0, 0, "heading ",
  784. DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
  785. },
  786. { "h2", 0, 0, 0, 0, 0, 0, 0, "heading ",
  787. DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
  788. },
  789. { "h3", 0, 0, 0, 0, 0, 0, 0, "heading ",
  790. DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
  791. },
  792. { "h4", 0, 0, 0, 0, 0, 0, 0, "heading ",
  793. DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
  794. },
  795. { "h5", 0, 0, 0, 0, 0, 0, 0, "heading ",
  796. DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
  797. },
  798. { "h6", 0, 0, 0, 0, 0, 0, 0, "heading ",
  799. DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
  800. },
  801. { "head", 1, 1, 0, 0, 0, 0, 0, "document head ",
  802. DECL head_contents, NULL, DECL head_attrs, NULL, NULL
  803. },
  804. { "hr", 0, 2, 2, 1, 0, 0, 0, "horizontal rule " ,
  805. EMPTY, NULL, DECL html_attrs, DECL hr_depr, NULL
  806. },
  807. { "html", 1, 1, 0, 0, 0, 0, 0, "document root element ",
  808. DECL html_content , NULL , DECL i18n_attrs, DECL version_attr, NULL
  809. },
  810. { "i", 0, 3, 0, 0, 0, 0, 1, "italic text style",
  811. DECL html_inline, NULL, DECL html_attrs, NULL, NULL
  812. },
  813. { "iframe", 0, 0, 0, 0, 0, 1, 2, "inline subwindow ",
  814. DECL html_flow, NULL, NULL, DECL iframe_attrs, NULL
  815. },
  816. { "img", 0, 2, 2, 1, 0, 0, 1, "embedded image ",
  817. EMPTY, NULL, DECL img_attrs, DECL align_attr, DECL src_alt_attrs
  818. },
  819. { "input", 0, 2, 2, 1, 0, 0, 1, "form control ",
  820. EMPTY, NULL, DECL input_attrs , DECL align_attr, NULL
  821. },
  822. { "ins", 0, 0, 0, 0, 0, 0, 2, "inserted text",
  823. DECL html_flow, NULL, DECL edit_attrs, NULL, NULL
  824. },
  825. { "isindex", 0, 2, 2, 1, 1, 1, 0, "single line prompt ",
  826. EMPTY, NULL, NULL, DECL prompt_attrs, NULL
  827. },
  828. { "kbd", 0, 0, 0, 0, 0, 0, 1, "text to be entered by the user",
  829. DECL html_inline, NULL, DECL html_attrs, NULL, NULL
  830. },
  831. { "label", 0, 0, 0, 0, 0, 0, 1, "form field label text ",
  832. DECL html_inline MODIFIER, NULL, DECL label_attrs , NULL, NULL
  833. },
  834. { "legend", 0, 0, 0, 0, 0, 0, 0, "fieldset legend ",
  835. DECL html_inline, NULL, DECL legend_attrs , DECL align_attr, NULL
  836. },
  837. { "li", 0, 1, 1, 0, 0, 0, 0, "list item ",
  838. DECL html_flow, NULL, DECL html_attrs, NULL, NULL
  839. },
  840. { "link", 0, 2, 2, 1, 0, 0, 0, "a media-independent link ",
  841. EMPTY, NULL, DECL link_attrs, DECL target_attr, NULL
  842. },
  843. { "map", 0, 0, 0, 0, 0, 0, 2, "client-side image map ",
  844. DECL map_contents , NULL, DECL html_attrs , NULL, DECL name_attr
  845. },
  846. { "menu", 0, 0, 0, 0, 1, 1, 0, "menu list ",
  847. DECL blockli_elt , NULL, NULL, DECL compact_attrs, NULL
  848. },
  849. { "meta", 0, 2, 2, 1, 0, 0, 0, "generic metainformation ",
  850. EMPTY, NULL, DECL meta_attrs , NULL , DECL content_attr
  851. },
  852. { "noframes", 0, 0, 0, 0, 0, 2, 0, "alternate content container for non frame-based rendering ",
  853. DECL noframes_content, "body" , DECL html_attrs, NULL, NULL
  854. },
  855. { "noscript", 0, 0, 0, 0, 0, 0, 0, "alternate content container for non script-based rendering ",
  856. DECL html_flow, "div", DECL html_attrs, NULL, NULL
  857. },
  858. { "object", 0, 0, 0, 0, 0, 0, 2, "generic embedded object ",
  859. DECL object_contents , "div" , DECL object_attrs, DECL object_depr, NULL
  860. },
  861. { "ol", 0, 0, 0, 0, 0, 0, 0, "ordered list ",
  862. DECL li_elt , "li" , DECL html_attrs, DECL ol_attrs, NULL
  863. },
  864. { "optgroup", 0, 0, 0, 0, 0, 0, 0, "option group ",
  865. DECL option_elt , "option", DECL optgroup_attrs, NULL, DECL label_attr
  866. },
  867. { "option", 0, 1, 0, 0, 0, 0, 0, "selectable choice " ,
  868. DECL html_pcdata, NULL, DECL option_attrs, NULL, NULL
  869. },
  870. { "p", 0, 1, 0, 0, 0, 0, 0, "paragraph ",
  871. DECL html_inline, NULL, DECL html_attrs, DECL align_attr, NULL
  872. },
  873. { "param", 0, 2, 2, 1, 0, 0, 0, "named property value ",
  874. EMPTY, NULL, DECL param_attrs, NULL, DECL name_attr
  875. },
  876. { "pre", 0, 0, 0, 0, 0, 0, 0, "preformatted text ",
  877. DECL pre_content, NULL, DECL html_attrs, DECL width_attr, NULL
  878. },
  879. { "q", 0, 0, 0, 0, 0, 0, 1, "short inline quotation ",
  880. DECL html_inline, NULL, DECL quote_attrs, NULL, NULL
  881. },
  882. { "s", 0, 3, 0, 0, 1, 1, 1, "strike-through text style",
  883. DECL html_inline, NULL, NULL, DECL html_attrs, NULL
  884. },
  885. { "samp", 0, 0, 0, 0, 0, 0, 1, "sample program output, scripts, etc.",
  886. DECL html_inline, NULL, DECL html_attrs, NULL, NULL
  887. },
  888. { "script", 0, 0, 0, 0, 0, 0, 2, "script statements ",
  889. DECL html_cdata, NULL, DECL script_attrs, DECL language_attr, DECL type_attr
  890. },
  891. { "select", 0, 0, 0, 0, 0, 0, 1, "option selector ",
  892. DECL select_content, NULL, DECL select_attrs, NULL, NULL
  893. },
  894. { "small", 0, 3, 0, 0, 0, 0, 1, "small text style",
  895. DECL html_inline, NULL, DECL html_attrs, NULL, NULL
  896. },
  897. { "span", 0, 0, 0, 0, 0, 0, 1, "generic language/style container ",
  898. DECL html_inline, NULL, DECL html_attrs, NULL, NULL
  899. },
  900. { "strike", 0, 3, 0, 0, 1, 1, 1, "strike-through text",
  901. DECL html_inline, NULL, NULL, DECL html_attrs, NULL
  902. },
  903. { "strong", 0, 3, 0, 0, 0, 0, 1, "strong emphasis",
  904. DECL html_inline, NULL, DECL html_attrs, NULL, NULL
  905. },
  906. { "style", 0, 0, 0, 0, 0, 0, 0, "style info ",
  907. DECL html_cdata, NULL, DECL style_attrs, NULL, DECL type_attr
  908. },
  909. { "sub", 0, 3, 0, 0, 0, 0, 1, "subscript",
  910. DECL html_inline, NULL, DECL html_attrs, NULL, NULL
  911. },
  912. { "sup", 0, 3, 0, 0, 0, 0, 1, "superscript ",
  913. DECL html_inline, NULL, DECL html_attrs, NULL, NULL
  914. },
  915. { "table", 0, 0, 0, 0, 0, 0, 0, "",
  916. DECL table_contents , "tr" , DECL table_attrs , DECL table_depr, NULL
  917. },
  918. { "tbody", 1, 0, 0, 0, 0, 0, 0, "table body ",
  919. DECL tr_elt , "tr" , DECL talign_attrs, NULL, NULL
  920. },
  921. { "td", 0, 0, 0, 0, 0, 0, 0, "table data cell",
  922. DECL html_flow, NULL, DECL th_td_attr, DECL th_td_depr, NULL
  923. },
  924. { "textarea", 0, 0, 0, 0, 0, 0, 1, "multi-line text field ",
  925. DECL html_pcdata, NULL, DECL textarea_attrs, NULL, DECL rows_cols_attr
  926. },
  927. { "tfoot", 0, 1, 0, 0, 0, 0, 0, "table footer ",
  928. DECL tr_elt , "tr" , DECL talign_attrs, NULL, NULL
  929. },
  930. { "th", 0, 1, 0, 0, 0, 0, 0, "table header cell",
  931. DECL html_flow, NULL, DECL th_td_attr, DECL th_td_depr, NULL
  932. },
  933. { "thead", 0, 1, 0, 0, 0, 0, 0, "table header ",
  934. DECL tr_elt , "tr" , DECL talign_attrs, NULL, NULL
  935. },
  936. { "title", 0, 0, 0, 0, 0, 0, 0, "document title ",
  937. DECL html_pcdata, NULL, DECL i18n_attrs, NULL, NULL
  938. },
  939. { "tr", 0, 0, 0, 0, 0, 0, 0, "table row ",
  940. DECL tr_contents , "td" , DECL talign_attrs, DECL bgcolor_attr, NULL
  941. },
  942. { "tt", 0, 3, 0, 0, 0, 0, 1, "teletype or monospaced text style",
  943. DECL html_inline, NULL, DECL html_attrs, NULL, NULL
  944. },
  945. { "u", 0, 3, 0, 0, 1, 1, 1, "underlined text style",
  946. DECL html_inline, NULL, NULL, DECL html_attrs, NULL
  947. },
  948. { "ul", 0, 0, 0, 0, 0, 0, 0, "unordered list ",
  949. DECL li_elt , "li" , DECL html_attrs, DECL ul_depr, NULL
  950. },
  951. { "var", 0, 0, 0, 0, 0, 0, 1, "instance of a variable or program argument",
  952. DECL html_inline, NULL, DECL html_attrs, NULL, NULL
  953. }
  954. };
  955. typedef struct {
  956. const char *oldTag;
  957. const char *newTag;
  958. } htmlStartCloseEntry;
  959. /*
  960. * start tags that imply the end of current element
  961. */
  962. static const htmlStartCloseEntry htmlStartClose[] = {
  963. { "a", "a" },
  964. { "a", "fieldset" },
  965. { "a", "table" },
  966. { "a", "td" },
  967. { "a", "th" },
  968. { "address", "dd" },
  969. { "address", "dl" },
  970. { "address", "dt" },
  971. { "address", "form" },
  972. { "address", "li" },
  973. { "address", "ul" },
  974. { "b", "center" },
  975. { "b", "p" },
  976. { "b", "td" },
  977. { "b", "th" },
  978. { "big", "p" },
  979. { "caption", "col" },
  980. { "caption", "colgroup" },
  981. { "caption", "tbody" },
  982. { "caption", "tfoot" },
  983. { "caption", "thead" },
  984. { "caption", "tr" },
  985. { "col", "col" },
  986. { "col", "colgroup" },
  987. { "col", "tbody" },
  988. { "col", "tfoot" },
  989. { "col", "thead" },
  990. { "col", "tr" },
  991. { "colgroup", "colgroup" },
  992. { "colgroup", "tbody" },
  993. { "colgroup", "tfoot" },
  994. { "colgroup", "thead" },
  995. { "colgroup", "tr" },
  996. { "dd", "dt" },
  997. { "dir", "dd" },
  998. { "dir", "dl" },
  999. { "dir", "dt" },
  1000. { "dir", "form" },
  1001. { "dir", "ul" },
  1002. { "dl", "form" },
  1003. { "dl", "li" },
  1004. { "dt", "dd" },
  1005. { "dt", "dl" },
  1006. { "font", "center" },
  1007. { "font", "td" },
  1008. { "font", "th" },
  1009. { "form", "form" },
  1010. { "h1", "fieldset" },
  1011. { "h1", "form" },
  1012. { "h1", "li" },
  1013. { "h1", "p" },
  1014. { "h1", "table" },
  1015. { "h2", "fieldset" },
  1016. { "h2", "form" },
  1017. { "h2", "li" },
  1018. { "h2", "p" },
  1019. { "h2", "table" },
  1020. { "h3", "fieldset" },
  1021. { "h3", "form" },
  1022. { "h3", "li" },
  1023. { "h3", "p" },
  1024. { "h3", "table" },
  1025. { "h4", "fieldset" },
  1026. { "h4", "form" },
  1027. { "h4", "li" },
  1028. { "h4", "p" },
  1029. { "h4", "table" },
  1030. { "h5", "fieldset" },
  1031. { "h5", "form" },
  1032. { "h5", "li" },
  1033. { "h5", "p" },
  1034. { "h5", "table" },
  1035. { "h6", "fieldset" },
  1036. { "h6", "form" },
  1037. { "h6", "li" },
  1038. { "h6", "p" },
  1039. { "h6", "table" },
  1040. { "head", "a" },
  1041. { "head", "abbr" },
  1042. { "head", "acronym" },
  1043. { "head", "address" },
  1044. { "head", "b" },
  1045. { "head", "bdo" },
  1046. { "head", "big" },
  1047. { "head", "blockquote" },
  1048. { "head", "body" },
  1049. { "head", "br" },
  1050. { "head", "center" },
  1051. { "head", "cite" },
  1052. { "head", "code" },
  1053. { "head", "dd" },
  1054. { "head", "dfn" },
  1055. { "head", "dir" },
  1056. { "head", "div" },
  1057. { "head", "dl" },
  1058. { "head", "dt" },
  1059. { "head", "em" },
  1060. { "head", "fieldset" },
  1061. { "head", "font" },
  1062. { "head", "form" },
  1063. { "head", "frameset" },
  1064. { "head", "h1" },
  1065. { "head", "h2" },
  1066. { "head", "h3" },
  1067. { "head", "h4" },
  1068. { "head", "h5" },
  1069. { "head", "h6" },
  1070. { "head", "hr" },
  1071. { "head", "i" },
  1072. { "head", "iframe" },
  1073. { "head", "img" },
  1074. { "head", "kbd" },
  1075. { "head", "li" },
  1076. { "head", "listing" },
  1077. { "head", "map" },
  1078. { "head", "menu" },
  1079. { "head", "ol" },
  1080. { "head", "p" },
  1081. { "head", "pre" },
  1082. { "head", "q" },
  1083. { "head", "s" },
  1084. { "head", "samp" },
  1085. { "head", "small" },
  1086. { "head", "span" },
  1087. { "head", "strike" },
  1088. { "head", "strong" },
  1089. { "head", "sub" },
  1090. { "head", "sup" },
  1091. { "head", "table" },
  1092. { "head", "tt" },
  1093. { "head", "u" },
  1094. { "head", "ul" },
  1095. { "head", "var" },
  1096. { "head", "xmp" },
  1097. { "hr", "form" },
  1098. { "i", "center" },
  1099. { "i", "p" },
  1100. { "i", "td" },
  1101. { "i", "th" },
  1102. { "legend", "fieldset" },
  1103. { "li", "li" },
  1104. { "link", "body" },
  1105. { "link", "frameset" },
  1106. { "listing", "dd" },
  1107. { "listing", "dl" },
  1108. { "listing", "dt" },
  1109. { "listing", "fieldset" },
  1110. { "listing", "form" },
  1111. { "listing", "li" },
  1112. { "listing", "table" },
  1113. { "listing", "ul" },
  1114. { "menu", "dd" },
  1115. { "menu", "dl" },
  1116. { "menu", "dt" },
  1117. { "menu", "form" },
  1118. { "menu", "ul" },
  1119. { "ol", "form" },
  1120. { "option", "optgroup" },
  1121. { "option", "option" },
  1122. { "p", "address" },
  1123. { "p", "blockquote" },
  1124. { "p", "body" },
  1125. { "p", "caption" },
  1126. { "p", "center" },
  1127. { "p", "col" },
  1128. { "p", "colgroup" },
  1129. { "p", "dd" },
  1130. { "p", "dir" },
  1131. { "p", "div" },
  1132. { "p", "dl" },
  1133. { "p", "dt" },
  1134. { "p", "fieldset" },
  1135. { "p", "form" },
  1136. { "p", "frameset" },
  1137. { "p", "h1" },
  1138. { "p", "h2" },
  1139. { "p", "h3" },
  1140. { "p", "h4" },
  1141. { "p", "h5" },
  1142. { "p", "h6" },
  1143. { "p", "head" },
  1144. { "p", "hr" },
  1145. { "p", "li" },
  1146. { "p", "listing" },
  1147. { "p", "menu" },
  1148. { "p", "ol" },
  1149. { "p", "p" },
  1150. { "p", "pre" },
  1151. { "p", "table" },
  1152. { "p", "tbody" },
  1153. { "p", "td" },
  1154. { "p", "tfoot" },
  1155. { "p", "th" },
  1156. { "p", "title" },
  1157. { "p", "tr" },
  1158. { "p", "ul" },
  1159. { "p", "xmp" },
  1160. { "pre", "dd" },
  1161. { "pre", "dl" },
  1162. { "pre", "dt" },
  1163. { "pre", "fieldset" },
  1164. { "pre", "form" },
  1165. { "pre", "li" },
  1166. { "pre", "table" },
  1167. { "pre", "ul" },
  1168. { "s", "p" },
  1169. { "script", "noscript" },
  1170. { "small", "p" },
  1171. { "span", "td" },
  1172. { "span", "th" },
  1173. { "strike", "p" },
  1174. { "style", "body" },
  1175. { "style", "frameset" },
  1176. { "tbody", "tbody" },
  1177. { "tbody", "tfoot" },
  1178. { "td", "tbody" },
  1179. { "td", "td" },
  1180. { "td", "tfoot" },
  1181. { "td", "th" },
  1182. { "td", "tr" },
  1183. { "tfoot", "tbody" },
  1184. { "th", "tbody" },
  1185. { "th", "td" },
  1186. { "th", "tfoot" },
  1187. { "th", "th" },
  1188. { "th", "tr" },
  1189. { "thead", "tbody" },
  1190. { "thead", "tfoot" },
  1191. { "title", "body" },
  1192. { "title", "frameset" },
  1193. { "tr", "tbody" },
  1194. { "tr", "tfoot" },
  1195. { "tr", "tr" },
  1196. { "tt", "p" },
  1197. { "u", "p" },
  1198. { "u", "td" },
  1199. { "u", "th" },
  1200. { "ul", "address" },
  1201. { "ul", "form" },
  1202. { "ul", "menu" },
  1203. { "ul", "pre" },
  1204. { "xmp", "dd" },
  1205. { "xmp", "dl" },
  1206. { "xmp", "dt" },
  1207. { "xmp", "fieldset" },
  1208. { "xmp", "form" },
  1209. { "xmp", "li" },
  1210. { "xmp", "table" },
  1211. { "xmp", "ul" }
  1212. };
  1213. /*
  1214. * The list of HTML elements which are supposed not to have
  1215. * CDATA content and where a p element will be implied
  1216. *
  1217. * TODO: extend that list by reading the HTML SGML DTD on
  1218. * implied paragraph
  1219. */
  1220. static const char *const htmlNoContentElements[] = {
  1221. "html",
  1222. "head",
  1223. NULL
  1224. };
  1225. /*
  1226. * The list of HTML attributes which are of content %Script;
  1227. * NOTE: when adding ones, check htmlIsScriptAttribute() since
  1228. * it assumes the name starts with 'on'
  1229. */
  1230. static const char *const htmlScriptAttributes[] = {
  1231. "onclick",
  1232. "ondblclick",
  1233. "onmousedown",
  1234. "onmouseup",
  1235. "onmouseover",
  1236. "onmousemove",
  1237. "onmouseout",
  1238. "onkeypress",
  1239. "onkeydown",
  1240. "onkeyup",
  1241. "onload",
  1242. "onunload",
  1243. "onfocus",
  1244. "onblur",
  1245. "onsubmit",
  1246. "onreset",
  1247. "onchange",
  1248. "onselect"
  1249. };
  1250. /*
  1251. * This table is used by the htmlparser to know what to do with
  1252. * broken html pages. By assigning different priorities to different
  1253. * elements the parser can decide how to handle extra endtags.
  1254. * Endtags are only allowed to close elements with lower or equal
  1255. * priority.
  1256. */
  1257. typedef struct {
  1258. const char *name;
  1259. int priority;
  1260. } elementPriority;
  1261. static const elementPriority htmlEndPriority[] = {
  1262. {"div", 150},
  1263. {"td", 160},
  1264. {"th", 160},
  1265. {"tr", 170},
  1266. {"thead", 180},
  1267. {"tbody", 180},
  1268. {"tfoot", 180},
  1269. {"table", 190},
  1270. {"head", 200},
  1271. {"body", 200},
  1272. {"html", 220},
  1273. {NULL, 100} /* Default priority */
  1274. };
  1275. /************************************************************************
  1276. * *
  1277. * functions to handle HTML specific data *
  1278. * *
  1279. ************************************************************************/
  1280. /**
  1281. * htmlInitAutoClose:
  1282. *
  1283. * DEPRECATED: This is a no-op.
  1284. */
  1285. void
  1286. htmlInitAutoClose(void) {
  1287. }
  1288. static int
  1289. htmlCompareTags(const void *key, const void *member) {
  1290. const xmlChar *tag = (const xmlChar *) key;
  1291. const htmlElemDesc *desc = (const htmlElemDesc *) member;
  1292. return(xmlStrcasecmp(tag, BAD_CAST desc->name));
  1293. }
  1294. /**
  1295. * htmlTagLookup:
  1296. * @tag: The tag name in lowercase
  1297. *
  1298. * Lookup the HTML tag in the ElementTable
  1299. *
  1300. * Returns the related htmlElemDescPtr or NULL if not found.
  1301. */
  1302. const htmlElemDesc *
  1303. htmlTagLookup(const xmlChar *tag) {
  1304. if (tag == NULL)
  1305. return(NULL);
  1306. return((const htmlElemDesc *) bsearch(tag, html40ElementTable,
  1307. sizeof(html40ElementTable) / sizeof(htmlElemDesc),
  1308. sizeof(htmlElemDesc), htmlCompareTags));
  1309. }
  1310. /**
  1311. * htmlGetEndPriority:
  1312. * @name: The name of the element to look up the priority for.
  1313. *
  1314. * Return value: The "endtag" priority.
  1315. **/
  1316. static int
  1317. htmlGetEndPriority (const xmlChar *name) {
  1318. int i = 0;
  1319. while ((htmlEndPriority[i].name != NULL) &&
  1320. (!xmlStrEqual((const xmlChar *)htmlEndPriority[i].name, name)))
  1321. i++;
  1322. return(htmlEndPriority[i].priority);
  1323. }
  1324. static int
  1325. htmlCompareStartClose(const void *vkey, const void *member) {
  1326. const htmlStartCloseEntry *key = (const htmlStartCloseEntry *) vkey;
  1327. const htmlStartCloseEntry *entry = (const htmlStartCloseEntry *) member;
  1328. int ret;
  1329. ret = strcmp(key->oldTag, entry->oldTag);
  1330. if (ret == 0)
  1331. ret = strcmp(key->newTag, entry->newTag);
  1332. return(ret);
  1333. }
  1334. /**
  1335. * htmlCheckAutoClose:
  1336. * @newtag: The new tag name
  1337. * @oldtag: The old tag name
  1338. *
  1339. * Checks whether the new tag is one of the registered valid tags for
  1340. * closing old.
  1341. *
  1342. * Returns 0 if no, 1 if yes.
  1343. */
  1344. static int
  1345. htmlCheckAutoClose(const xmlChar * newtag, const xmlChar * oldtag)
  1346. {
  1347. htmlStartCloseEntry key;
  1348. void *res;
  1349. key.oldTag = (const char *) oldtag;
  1350. key.newTag = (const char *) newtag;
  1351. res = bsearch(&key, htmlStartClose,
  1352. sizeof(htmlStartClose) / sizeof(htmlStartCloseEntry),
  1353. sizeof(htmlStartCloseEntry), htmlCompareStartClose);
  1354. return(res != NULL);
  1355. }
  1356. /**
  1357. * htmlAutoCloseOnClose:
  1358. * @ctxt: an HTML parser context
  1359. * @newtag: The new tag name
  1360. * @force: force the tag closure
  1361. *
  1362. * The HTML DTD allows an ending tag to implicitly close other tags.
  1363. */
  1364. static void
  1365. htmlAutoCloseOnClose(htmlParserCtxtPtr ctxt, const xmlChar * newtag)
  1366. {
  1367. const htmlElemDesc *info;
  1368. int i, priority;
  1369. priority = htmlGetEndPriority(newtag);
  1370. for (i = (ctxt->nameNr - 1); i >= 0; i--) {
  1371. if (xmlStrEqual(newtag, ctxt->nameTab[i]))
  1372. break;
  1373. /*
  1374. * A misplaced endtag can only close elements with lower
  1375. * or equal priority, so if we find an element with higher
  1376. * priority before we find an element with
  1377. * matching name, we just ignore this endtag
  1378. */
  1379. if (htmlGetEndPriority(ctxt->nameTab[i]) > priority)
  1380. return;
  1381. }
  1382. if (i < 0)
  1383. return;
  1384. while (!xmlStrEqual(newtag, ctxt->name)) {
  1385. info = htmlTagLookup(ctxt->name);
  1386. if ((info != NULL) && (info->endTag == 3)) {
  1387. htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
  1388. "Opening and ending tag mismatch: %s and %s\n",
  1389. newtag, ctxt->name);
  1390. }
  1391. if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
  1392. ctxt->sax->endElement(ctxt->userData, ctxt->name);
  1393. htmlnamePop(ctxt);
  1394. }
  1395. }
  1396. /**
  1397. * htmlAutoCloseOnEnd:
  1398. * @ctxt: an HTML parser context
  1399. *
  1400. * Close all remaining tags at the end of the stream
  1401. */
  1402. static void
  1403. htmlAutoCloseOnEnd(htmlParserCtxtPtr ctxt)
  1404. {
  1405. int i;
  1406. if (ctxt->nameNr == 0)
  1407. return;
  1408. for (i = (ctxt->nameNr - 1); i >= 0; i--) {
  1409. if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
  1410. ctxt->sax->endElement(ctxt->userData, ctxt->name);
  1411. htmlnamePop(ctxt);
  1412. }
  1413. }
  1414. /**
  1415. * htmlAutoClose:
  1416. * @ctxt: an HTML parser context
  1417. * @newtag: The new tag name or NULL
  1418. *
  1419. * The HTML DTD allows a tag to implicitly close other tags.
  1420. * The list is kept in htmlStartClose array. This function is
  1421. * called when a new tag has been detected and generates the
  1422. * appropriates closes if possible/needed.
  1423. * If newtag is NULL this mean we are at the end of the resource
  1424. * and we should check
  1425. */
  1426. static void
  1427. htmlAutoClose(htmlParserCtxtPtr ctxt, const xmlChar * newtag)
  1428. {
  1429. if (newtag == NULL)
  1430. return;
  1431. while ((ctxt->name != NULL) &&
  1432. (htmlCheckAutoClose(newtag, ctxt->name))) {
  1433. if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
  1434. ctxt->sax->endElement(ctxt->userData, ctxt->name);
  1435. htmlnamePop(ctxt);
  1436. }
  1437. }
  1438. /**
  1439. * htmlAutoCloseTag:
  1440. * @doc: the HTML document
  1441. * @name: The tag name
  1442. * @elem: the HTML element
  1443. *
  1444. * The HTML DTD allows a tag to implicitly close other tags.
  1445. * The list is kept in htmlStartClose array. This function checks
  1446. * if the element or one of it's children would autoclose the
  1447. * given tag.
  1448. *
  1449. * Returns 1 if autoclose, 0 otherwise
  1450. */
  1451. int
  1452. htmlAutoCloseTag(htmlDocPtr doc, const xmlChar *name, htmlNodePtr elem) {
  1453. htmlNodePtr child;
  1454. if (elem == NULL) return(1);
  1455. if (xmlStrEqual(name, elem->name)) return(0);
  1456. if (htmlCheckAutoClose(elem->name, name)) return(1);
  1457. child = elem->children;
  1458. while (child != NULL) {
  1459. if (htmlAutoCloseTag(doc, name, child)) return(1);
  1460. child = child->next;
  1461. }
  1462. return(0);
  1463. }
  1464. /**
  1465. * htmlIsAutoClosed:
  1466. * @doc: the HTML document
  1467. * @elem: the HTML element
  1468. *
  1469. * The HTML DTD allows a tag to implicitly close other tags.
  1470. * The list is kept in htmlStartClose array. This function checks
  1471. * if a tag is autoclosed by one of it's child
  1472. *
  1473. * Returns 1 if autoclosed, 0 otherwise
  1474. */
  1475. int
  1476. htmlIsAutoClosed(htmlDocPtr doc, htmlNodePtr elem) {
  1477. htmlNodePtr child;
  1478. if (elem == NULL) return(1);
  1479. child = elem->children;
  1480. while (child != NULL) {
  1481. if (htmlAutoCloseTag(doc, elem->name, child)) return(1);
  1482. child = child->next;
  1483. }
  1484. return(0);
  1485. }
  1486. /**
  1487. * htmlCheckImplied:
  1488. * @ctxt: an HTML parser context
  1489. * @newtag: The new tag name
  1490. *
  1491. * The HTML DTD allows a tag to exists only implicitly
  1492. * called when a new tag has been detected and generates the
  1493. * appropriates implicit tags if missing
  1494. */
  1495. static void
  1496. htmlCheckImplied(htmlParserCtxtPtr ctxt, const xmlChar *newtag) {
  1497. int i;
  1498. if (ctxt->options & HTML_PARSE_NOIMPLIED)
  1499. return;
  1500. if (!htmlOmittedDefaultValue)
  1501. return;
  1502. if (xmlStrEqual(newtag, BAD_CAST"html"))
  1503. return;
  1504. if (ctxt->nameNr <= 0) {
  1505. htmlnamePush(ctxt, BAD_CAST"html");
  1506. if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
  1507. ctxt->sax->startElement(ctxt->userData, BAD_CAST"html", NULL);
  1508. }
  1509. if ((xmlStrEqual(newtag, BAD_CAST"body")) || (xmlStrEqual(newtag, BAD_CAST"head")))
  1510. return;
  1511. if ((ctxt->nameNr <= 1) &&
  1512. ((xmlStrEqual(newtag, BAD_CAST"script")) ||
  1513. (xmlStrEqual(newtag, BAD_CAST"style")) ||
  1514. (xmlStrEqual(newtag, BAD_CAST"meta")) ||
  1515. (xmlStrEqual(newtag, BAD_CAST"link")) ||
  1516. (xmlStrEqual(newtag, BAD_CAST"title")) ||
  1517. (xmlStrEqual(newtag, BAD_CAST"base")))) {
  1518. if (ctxt->html >= 3) {
  1519. /* we already saw or generated an <head> before */
  1520. return;
  1521. }
  1522. /*
  1523. * dropped OBJECT ... i you put it first BODY will be
  1524. * assumed !
  1525. */
  1526. htmlnamePush(ctxt, BAD_CAST"head");
  1527. if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
  1528. ctxt->sax->startElement(ctxt->userData, BAD_CAST"head", NULL);
  1529. } else if ((!xmlStrEqual(newtag, BAD_CAST"noframes")) &&
  1530. (!xmlStrEqual(newtag, BAD_CAST"frame")) &&
  1531. (!xmlStrEqual(newtag, BAD_CAST"frameset"))) {
  1532. if (ctxt->html >= 10) {
  1533. /* we already saw or generated a <body> before */
  1534. return;
  1535. }
  1536. for (i = 0;i < ctxt->nameNr;i++) {
  1537. if (xmlStrEqual(ctxt->nameTab[i], BAD_CAST"body")) {
  1538. return;
  1539. }
  1540. if (xmlStrEqual(ctxt->nameTab[i], BAD_CAST"head")) {
  1541. return;
  1542. }
  1543. }
  1544. htmlnamePush(ctxt, BAD_CAST"body");
  1545. if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
  1546. ctxt->sax->startElement(ctxt->userData, BAD_CAST"body", NULL);
  1547. }
  1548. }
  1549. /**
  1550. * htmlCheckParagraph
  1551. * @ctxt: an HTML parser context
  1552. *
  1553. * Check whether a p element need to be implied before inserting
  1554. * characters in the current element.
  1555. *
  1556. * Returns 1 if a paragraph has been inserted, 0 if not and -1
  1557. * in case of error.
  1558. */
  1559. static int
  1560. htmlCheckParagraph(htmlParserCtxtPtr ctxt) {
  1561. const xmlChar *tag;
  1562. int i;
  1563. if (ctxt == NULL)
  1564. return(-1);
  1565. tag = ctxt->name;
  1566. if (tag == NULL) {
  1567. htmlAutoClose(ctxt, BAD_CAST"p");
  1568. htmlCheckImplied(ctxt, BAD_CAST"p");
  1569. htmlnamePush(ctxt, BAD_CAST"p");
  1570. if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
  1571. ctxt->sax->startElement(ctxt->userData, BAD_CAST"p", NULL);
  1572. return(1);
  1573. }
  1574. if (!htmlOmittedDefaultValue)
  1575. return(0);
  1576. for (i = 0; htmlNoContentElements[i] != NULL; i++) {
  1577. if (xmlStrEqual(tag, BAD_CAST htmlNoContentElements[i])) {
  1578. htmlAutoClose(ctxt, BAD_CAST"p");
  1579. htmlCheckImplied(ctxt, BAD_CAST"p");
  1580. htmlnamePush(ctxt, BAD_CAST"p");
  1581. if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL))
  1582. ctxt->sax->startElement(ctxt->userData, BAD_CAST"p", NULL);
  1583. return(1);
  1584. }
  1585. }
  1586. return(0);
  1587. }
  1588. /**
  1589. * htmlIsScriptAttribute:
  1590. * @name: an attribute name
  1591. *
  1592. * Check if an attribute is of content type Script
  1593. *
  1594. * Returns 1 is the attribute is a script 0 otherwise
  1595. */
  1596. int
  1597. htmlIsScriptAttribute(const xmlChar *name) {
  1598. unsigned int i;
  1599. if (name == NULL)
  1600. return(0);
  1601. /*
  1602. * all script attributes start with 'on'
  1603. */
  1604. if ((name[0] != 'o') || (name[1] != 'n'))
  1605. return(0);
  1606. for (i = 0;
  1607. i < sizeof(htmlScriptAttributes)/sizeof(htmlScriptAttributes[0]);
  1608. i++) {
  1609. if (xmlStrEqual(name, (const xmlChar *) htmlScriptAttributes[i]))
  1610. return(1);
  1611. }
  1612. return(0);
  1613. }
  1614. /************************************************************************
  1615. * *
  1616. * The list of HTML predefined entities *
  1617. * *
  1618. ************************************************************************/
  1619. static const htmlEntityDesc html40EntitiesTable[] = {
  1620. /*
  1621. * the 4 absolute ones, plus apostrophe.
  1622. */
  1623. { 34, "quot", "quotation mark = APL quote, U+0022 ISOnum" },
  1624. { 38, "amp", "ampersand, U+0026 ISOnum" },
  1625. { 39, "apos", "single quote" },
  1626. { 60, "lt", "less-than sign, U+003C ISOnum" },
  1627. { 62, "gt", "greater-than sign, U+003E ISOnum" },
  1628. /*
  1629. * A bunch still in the 128-255 range
  1630. * Replacing them depend really on the charset used.
  1631. */
  1632. { 160, "nbsp", "no-break space = non-breaking space, U+00A0 ISOnum" },
  1633. { 161, "iexcl","inverted exclamation mark, U+00A1 ISOnum" },
  1634. { 162, "cent", "cent sign, U+00A2 ISOnum" },
  1635. { 163, "pound","pound sign, U+00A3 ISOnum" },
  1636. { 164, "curren","currency sign, U+00A4 ISOnum" },
  1637. { 165, "yen", "yen sign = yuan sign, U+00A5 ISOnum" },
  1638. { 166, "brvbar","broken bar = broken vertical bar, U+00A6 ISOnum" },
  1639. { 167, "sect", "section sign, U+00A7 ISOnum" },
  1640. { 168, "uml", "diaeresis = spacing diaeresis, U+00A8 ISOdia" },
  1641. { 169, "copy", "copyright sign, U+00A9 ISOnum" },
  1642. { 170, "ordf", "feminine ordinal indicator, U+00AA ISOnum" },
  1643. { 171, "laquo","left-pointing double angle quotation mark = left pointing guillemet, U+00AB ISOnum" },
  1644. { 172, "not", "not sign, U+00AC ISOnum" },
  1645. { 173, "shy", "soft hyphen = discretionary hyphen, U+00AD ISOnum" },
  1646. { 174, "reg", "registered sign = registered trade mark sign, U+00AE ISOnum" },
  1647. { 175, "macr", "macron = spacing macron = overline = APL overbar, U+00AF ISOdia" },
  1648. { 176, "deg", "degree sign, U+00B0 ISOnum" },
  1649. { 177, "plusmn","plus-minus sign = plus-or-minus sign, U+00B1 ISOnum" },
  1650. { 178, "sup2", "superscript two = superscript digit two = squared, U+00B2 ISOnum" },
  1651. { 179, "sup3", "superscript three = superscript digit three = cubed, U+00B3 ISOnum" },
  1652. { 180, "acute","acute accent = spacing acute, U+00B4 ISOdia" },
  1653. { 181, "micro","micro sign, U+00B5 ISOnum" },
  1654. { 182, "para", "pilcrow sign = paragraph sign, U+00B6 ISOnum" },
  1655. { 183, "middot","middle dot = Georgian comma Greek middle dot, U+00B7 ISOnum" },
  1656. { 184, "cedil","cedilla = spacing cedilla, U+00B8 ISOdia" },
  1657. { 185, "sup1", "superscript one = superscript digit one, U+00B9 ISOnum" },
  1658. { 186, "ordm", "masculine ordinal indicator, U+00BA ISOnum" },
  1659. { 187, "raquo","right-pointing double angle quotation mark right pointing guillemet, U+00BB ISOnum" },
  1660. { 188, "frac14","vulgar fraction one quarter = fraction one quarter, U+00BC ISOnum" },
  1661. { 189, "frac12","vulgar fraction one half = fraction one half, U+00BD ISOnum" },
  1662. { 190, "frac34","vulgar fraction three quarters = fraction three quarters, U+00BE ISOnum" },
  1663. { 191, "iquest","inverted question mark = turned question mark, U+00BF ISOnum" },
  1664. { 192, "Agrave","latin capital letter A with grave = latin capital letter A grave, U+00C0 ISOlat1" },
  1665. { 193, "Aacute","latin capital letter A with acute, U+00C1 ISOlat1" },
  1666. { 194, "Acirc","latin capital letter A with circumflex, U+00C2 ISOlat1" },
  1667. { 195, "Atilde","latin capital letter A with tilde, U+00C3 ISOlat1" },
  1668. { 196, "Auml", "latin capital letter A with diaeresis, U+00C4 ISOlat1" },
  1669. { 197, "Aring","latin capital letter A with ring above = latin capital letter A ring, U+00C5 ISOlat1" },
  1670. { 198, "AElig","latin capital letter AE = latin capital ligature AE, U+00C6 ISOlat1" },
  1671. { 199, "Ccedil","latin capital letter C with cedilla, U+00C7 ISOlat1" },
  1672. { 200, "Egrave","latin capital letter E with grave, U+00C8 ISOlat1" },
  1673. { 201, "Eacute","latin capital letter E with acute, U+00C9 ISOlat1" },
  1674. { 202, "Ecirc","latin capital letter E with circumflex, U+00CA ISOlat1" },
  1675. { 203, "Euml", "latin capital letter E with diaeresis, U+00CB ISOlat1" },
  1676. { 204, "Igrave","latin capital letter I with grave, U+00CC ISOlat1" },
  1677. { 205, "Iacute","latin capital letter I with acute, U+00CD ISOlat1" },
  1678. { 206, "Icirc","latin capital letter I with circumflex, U+00CE ISOlat1" },
  1679. { 207, "Iuml", "latin capital letter I with diaeresis, U+00CF ISOlat1" },
  1680. { 208, "ETH", "latin capital letter ETH, U+00D0 ISOlat1" },
  1681. { 209, "Ntilde","latin capital letter N with tilde, U+00D1 ISOlat1" },
  1682. { 210, "Ograve","latin capital letter O with grave, U+00D2 ISOlat1" },
  1683. { 211, "Oacute","latin capital letter O with acute, U+00D3 ISOlat1" },
  1684. { 212, "Ocirc","latin capital letter O with circumflex, U+00D4 ISOlat1" },
  1685. { 213, "Otilde","latin capital letter O with tilde, U+00D5 ISOlat1" },
  1686. { 214, "Ouml", "latin capital letter O with diaeresis, U+00D6 ISOlat1" },
  1687. { 215, "times","multiplication sign, U+00D7 ISOnum" },
  1688. { 216, "Oslash","latin capital letter O with stroke latin capital letter O slash, U+00D8 ISOlat1" },
  1689. { 217, "Ugrave","latin capital letter U with grave, U+00D9 ISOlat1" },
  1690. { 218, "Uacute","latin capital letter U with acute, U+00DA ISOlat1" },
  1691. { 219, "Ucirc","latin capital letter U with circumflex, U+00DB ISOlat1" },
  1692. { 220, "Uuml", "latin capital letter U with diaeresis, U+00DC ISOlat1" },
  1693. { 221, "Yacute","latin capital letter Y with acute, U+00DD ISOlat1" },
  1694. { 222, "THORN","latin capital letter THORN, U+00DE ISOlat1" },
  1695. { 223, "szlig","latin small letter sharp s = ess-zed, U+00DF ISOlat1" },
  1696. { 224, "agrave","latin small letter a with grave = latin small letter a grave, U+00E0 ISOlat1" },
  1697. { 225, "aacute","latin small letter a with acute, U+00E1 ISOlat1" },
  1698. { 226, "acirc","latin small letter a with circumflex, U+00E2 ISOlat1" },
  1699. { 227, "atilde","latin small letter a with tilde, U+00E3 ISOlat1" },
  1700. { 228, "auml", "latin small letter a with diaeresis, U+00E4 ISOlat1" },
  1701. { 229, "aring","latin small letter a with ring above = latin small letter a ring, U+00E5 ISOlat1" },
  1702. { 230, "aelig","latin small letter ae = latin small ligature ae, U+00E6 ISOlat1" },
  1703. { 231, "ccedil","latin small letter c with cedilla, U+00E7 ISOlat1" },
  1704. { 232, "egrave","latin small letter e with grave, U+00E8 ISOlat1" },
  1705. { 233, "eacute","latin small letter e with acute, U+00E9 ISOlat1" },
  1706. { 234, "ecirc","latin small letter e with circumflex, U+00EA ISOlat1" },
  1707. { 235, "euml", "latin small letter e with diaeresis, U+00EB ISOlat1" },
  1708. { 236, "igrave","latin small letter i with grave, U+00EC ISOlat1" },
  1709. { 237, "iacute","latin small letter i with acute, U+00ED ISOlat1" },
  1710. { 238, "icirc","latin small letter i with circumflex, U+00EE ISOlat1" },
  1711. { 239, "iuml", "latin small letter i with diaeresis, U+00EF ISOlat1" },
  1712. { 240, "eth", "latin small letter eth, U+00F0 ISOlat1" },
  1713. { 241, "ntilde","latin small letter n with tilde, U+00F1 ISOlat1" },
  1714. { 242, "ograve","latin small letter o with grave, U+00F2 ISOlat1" },
  1715. { 243, "oacute","latin small letter o with acute, U+00F3 ISOlat1" },
  1716. { 244, "ocirc","latin small letter o with circumflex, U+00F4 ISOlat1" },
  1717. { 245, "otilde","latin small letter o with tilde, U+00F5 ISOlat1" },
  1718. { 246, "ouml", "latin small letter o with diaeresis, U+00F6 ISOlat1" },
  1719. { 247, "divide","division sign, U+00F7 ISOnum" },
  1720. { 248, "oslash","latin small letter o with stroke, = latin small letter o slash, U+00F8 ISOlat1" },
  1721. { 249, "ugrave","latin small letter u with grave, U+00F9 ISOlat1" },
  1722. { 250, "uacute","latin small letter u with acute, U+00FA ISOlat1" },
  1723. { 251, "ucirc","latin small letter u with circumflex, U+00FB ISOlat1" },
  1724. { 252, "uuml", "latin small letter u with diaeresis, U+00FC ISOlat1" },
  1725. { 253, "yacute","latin small letter y with acute, U+00FD ISOlat1" },
  1726. { 254, "thorn","latin small letter thorn with, U+00FE ISOlat1" },
  1727. { 255, "yuml", "latin small letter y with diaeresis, U+00FF ISOlat1" },
  1728. { 338, "OElig","latin capital ligature OE, U+0152 ISOlat2" },
  1729. { 339, "oelig","latin small ligature oe, U+0153 ISOlat2" },
  1730. { 352, "Scaron","latin capital letter S with caron, U+0160 ISOlat2" },
  1731. { 353, "scaron","latin small letter s with caron, U+0161 ISOlat2" },
  1732. { 376, "Yuml", "latin capital letter Y with diaeresis, U+0178 ISOlat2" },
  1733. /*
  1734. * Anything below should really be kept as entities references
  1735. */
  1736. { 402, "fnof", "latin small f with hook = function = florin, U+0192 ISOtech" },
  1737. { 710, "circ", "modifier letter circumflex accent, U+02C6 ISOpub" },
  1738. { 732, "tilde","small tilde, U+02DC ISOdia" },
  1739. { 913, "Alpha","greek capital letter alpha, U+0391" },
  1740. { 914, "Beta", "greek capital letter beta, U+0392" },
  1741. { 915, "Gamma","greek capital letter gamma, U+0393 ISOgrk3" },
  1742. { 916, "Delta","greek capital letter delta, U+0394 ISOgrk3" },
  1743. { 917, "Epsilon","greek capital letter epsilon, U+0395" },
  1744. { 918, "Zeta", "greek capital letter zeta, U+0396" },
  1745. { 919, "Eta", "greek capital letter eta, U+0397" },
  1746. { 920, "Theta","greek capital letter theta, U+0398 ISOgrk3" },
  1747. { 921, "Iota", "greek capital letter iota, U+0399" },
  1748. { 922, "Kappa","greek capital letter kappa, U+039A" },
  1749. { 923, "Lambda", "greek capital letter lambda, U+039B ISOgrk3" },
  1750. { 924, "Mu", "greek capital letter mu, U+039C" },
  1751. { 925, "Nu", "greek capital letter nu, U+039D" },
  1752. { 926, "Xi", "greek capital letter xi, U+039E ISOgrk3" },
  1753. { 927, "Omicron","greek capital letter omicron, U+039F" },
  1754. { 928, "Pi", "greek capital letter pi, U+03A0 ISOgrk3" },
  1755. { 929, "Rho", "greek capital letter rho, U+03A1" },
  1756. { 931, "Sigma","greek capital letter sigma, U+03A3 ISOgrk3" },
  1757. { 932, "Tau", "greek capital letter tau, U+03A4" },
  1758. { 933, "Upsilon","greek capital letter upsilon, U+03A5 ISOgrk3" },
  1759. { 934, "Phi", "greek capital letter phi, U+03A6 ISOgrk3" },
  1760. { 935, "Chi", "greek capital letter chi, U+03A7" },
  1761. { 936, "Psi", "greek capital letter psi, U+03A8 ISOgrk3" },
  1762. { 937, "Omega","greek capital letter omega, U+03A9 ISOgrk3" },
  1763. { 945, "alpha","greek small letter alpha, U+03B1 ISOgrk3" },
  1764. { 946, "beta", "greek small letter beta, U+03B2 ISOgrk3" },
  1765. { 947, "gamma","greek small letter gamma, U+03B3 ISOgrk3" },
  1766. { 948, "delta","greek small letter delta, U+03B4 ISOgrk3" },
  1767. { 949, "epsilon","greek small letter epsilon, U+03B5 ISOgrk3" },
  1768. { 950, "zeta", "greek small letter zeta, U+03B6 ISOgrk3" },
  1769. { 951, "eta", "greek small letter eta, U+03B7 ISOgrk3" },
  1770. { 952, "theta","greek small letter theta, U+03B8 ISOgrk3" },
  1771. { 953, "iota", "greek small letter iota, U+03B9 ISOgrk3" },
  1772. { 954, "kappa","greek small letter kappa, U+03BA ISOgrk3" },
  1773. { 955, "lambda","greek small letter lambda, U+03BB ISOgrk3" },
  1774. { 956, "mu", "greek small letter mu, U+03BC ISOgrk3" },
  1775. { 957, "nu", "greek small letter nu, U+03BD ISOgrk3" },
  1776. { 958, "xi", "greek small letter xi, U+03BE ISOgrk3" },
  1777. { 959, "omicron","greek small letter omicron, U+03BF NEW" },
  1778. { 960, "pi", "greek small letter pi, U+03C0 ISOgrk3" },
  1779. { 961, "rho", "greek small letter rho, U+03C1 ISOgrk3" },
  1780. { 962, "sigmaf","greek small letter final sigma, U+03C2 ISOgrk3" },
  1781. { 963, "sigma","greek small letter sigma, U+03C3 ISOgrk3" },
  1782. { 964, "tau", "greek small letter tau, U+03C4 ISOgrk3" },
  1783. { 965, "upsilon","greek small letter upsilon, U+03C5 ISOgrk3" },
  1784. { 966, "phi", "greek small letter phi, U+03C6 ISOgrk3" },
  1785. { 967, "chi", "greek small letter chi, U+03C7 ISOgrk3" },
  1786. { 968, "psi", "greek small letter psi, U+03C8 ISOgrk3" },
  1787. { 969, "omega","greek small letter omega, U+03C9 ISOgrk3" },
  1788. { 977, "thetasym","greek small letter theta symbol, U+03D1 NEW" },
  1789. { 978, "upsih","greek upsilon with hook symbol, U+03D2 NEW" },
  1790. { 982, "piv", "greek pi symbol, U+03D6 ISOgrk3" },
  1791. { 8194, "ensp", "en space, U+2002 ISOpub" },
  1792. { 8195, "emsp", "em space, U+2003 ISOpub" },
  1793. { 8201, "thinsp","thin space, U+2009 ISOpub" },
  1794. { 8204, "zwnj", "zero width non-joiner, U+200C NEW RFC 2070" },
  1795. { 8205, "zwj", "zero width joiner, U+200D NEW RFC 2070" },
  1796. { 8206, "lrm", "left-to-right mark, U+200E NEW RFC 2070" },
  1797. { 8207, "rlm", "right-to-left mark, U+200F NEW RFC 2070" },
  1798. { 8211, "ndash","en dash, U+2013 ISOpub" },
  1799. { 8212, "mdash","em dash, U+2014 ISOpub" },
  1800. { 8216, "lsquo","left single quotation mark, U+2018 ISOnum" },
  1801. { 8217, "rsquo","right single quotation mark, U+2019 ISOnum" },
  1802. { 8218, "sbquo","single low-9 quotation mark, U+201A NEW" },
  1803. { 8220, "ldquo","left double quotation mark, U+201C ISOnum" },
  1804. { 8221, "rdquo","right double quotation mark, U+201D ISOnum" },
  1805. { 8222, "bdquo","double low-9 quotation mark, U+201E NEW" },
  1806. { 8224, "dagger","dagger, U+2020 ISOpub" },
  1807. { 8225, "Dagger","double dagger, U+2021 ISOpub" },
  1808. { 8226, "bull", "bullet = black small circle, U+2022 ISOpub" },
  1809. { 8230, "hellip","horizontal ellipsis = three dot leader, U+2026 ISOpub" },
  1810. { 8240, "permil","per mille sign, U+2030 ISOtech" },
  1811. { 8242, "prime","prime = minutes = feet, U+2032 ISOtech" },
  1812. { 8243, "Prime","double prime = seconds = inches, U+2033 ISOtech" },
  1813. { 8249, "lsaquo","single left-pointing angle quotation mark, U+2039 ISO proposed" },
  1814. { 8250, "rsaquo","single right-pointing angle quotation mark, U+203A ISO proposed" },
  1815. { 8254, "oline","overline = spacing overscore, U+203E NEW" },
  1816. { 8260, "frasl","fraction slash, U+2044 NEW" },
  1817. { 8364, "euro", "euro sign, U+20AC NEW" },
  1818. { 8465, "image","blackletter capital I = imaginary part, U+2111 ISOamso" },
  1819. { 8472, "weierp","script capital P = power set = Weierstrass p, U+2118 ISOamso" },
  1820. { 8476, "real", "blackletter capital R = real part symbol, U+211C ISOamso" },
  1821. { 8482, "trade","trade mark sign, U+2122 ISOnum" },
  1822. { 8501, "alefsym","alef symbol = first transfinite cardinal, U+2135 NEW" },
  1823. { 8592, "larr", "leftwards arrow, U+2190 ISOnum" },
  1824. { 8593, "uarr", "upwards arrow, U+2191 ISOnum" },
  1825. { 8594, "rarr", "rightwards arrow, U+2192 ISOnum" },
  1826. { 8595, "darr", "downwards arrow, U+2193 ISOnum" },
  1827. { 8596, "harr", "left right arrow, U+2194 ISOamsa" },
  1828. { 8629, "crarr","downwards arrow with corner leftwards = carriage return, U+21B5 NEW" },
  1829. { 8656, "lArr", "leftwards double arrow, U+21D0 ISOtech" },
  1830. { 8657, "uArr", "upwards double arrow, U+21D1 ISOamsa" },
  1831. { 8658, "rArr", "rightwards double arrow, U+21D2 ISOtech" },
  1832. { 8659, "dArr", "downwards double arrow, U+21D3 ISOamsa" },
  1833. { 8660, "hArr", "left right double arrow, U+21D4 ISOamsa" },
  1834. { 8704, "forall","for all, U+2200 ISOtech" },
  1835. { 8706, "part", "partial differential, U+2202 ISOtech" },
  1836. { 8707, "exist","there exists, U+2203 ISOtech" },
  1837. { 8709, "empty","empty set = null set = diameter, U+2205 ISOamso" },
  1838. { 8711, "nabla","nabla = backward difference, U+2207 ISOtech" },
  1839. { 8712, "isin", "element of, U+2208 ISOtech" },
  1840. { 8713, "notin","not an element of, U+2209 ISOtech" },
  1841. { 8715, "ni", "contains as member, U+220B ISOtech" },
  1842. { 8719, "prod", "n-ary product = product sign, U+220F ISOamsb" },
  1843. { 8721, "sum", "n-ary summation, U+2211 ISOamsb" },
  1844. { 8722, "minus","minus sign, U+2212 ISOtech" },
  1845. { 8727, "lowast","asterisk operator, U+2217 ISOtech" },
  1846. { 8730, "radic","square root = radical sign, U+221A ISOtech" },
  1847. { 8733, "prop", "proportional to, U+221D ISOtech" },
  1848. { 8734, "infin","infinity, U+221E ISOtech" },
  1849. { 8736, "ang", "angle, U+2220 ISOamso" },
  1850. { 8743, "and", "logical and = wedge, U+2227 ISOtech" },
  1851. { 8744, "or", "logical or = vee, U+2228 ISOtech" },
  1852. { 8745, "cap", "intersection = cap, U+2229 ISOtech" },
  1853. { 8746, "cup", "union = cup, U+222A ISOtech" },
  1854. { 8747, "int", "integral, U+222B ISOtech" },
  1855. { 8756, "there4","therefore, U+2234 ISOtech" },
  1856. { 8764, "sim", "tilde operator = varies with = similar to, U+223C ISOtech" },
  1857. { 8773, "cong", "approximately equal to, U+2245 ISOtech" },
  1858. { 8776, "asymp","almost equal to = asymptotic to, U+2248 ISOamsr" },
  1859. { 8800, "ne", "not equal to, U+2260 ISOtech" },
  1860. { 8801, "equiv","identical to, U+2261 ISOtech" },
  1861. { 8804, "le", "less-than or equal to, U+2264 ISOtech" },
  1862. { 8805, "ge", "greater-than or equal to, U+2265 ISOtech" },
  1863. { 8834, "sub", "subset of, U+2282 ISOtech" },
  1864. { 8835, "sup", "superset of, U+2283 ISOtech" },
  1865. { 8836, "nsub", "not a subset of, U+2284 ISOamsn" },
  1866. { 8838, "sube", "subset of or equal to, U+2286 ISOtech" },
  1867. { 8839, "supe", "superset of or equal to, U+2287 ISOtech" },
  1868. { 8853, "oplus","circled plus = direct sum, U+2295 ISOamsb" },
  1869. { 8855, "otimes","circled times = vector product, U+2297 ISOamsb" },
  1870. { 8869, "perp", "up tack = orthogonal to = perpendicular, U+22A5 ISOtech" },
  1871. { 8901, "sdot", "dot operator, U+22C5 ISOamsb" },
  1872. { 8968, "lceil","left ceiling = apl upstile, U+2308 ISOamsc" },
  1873. { 8969, "rceil","right ceiling, U+2309 ISOamsc" },
  1874. { 8970, "lfloor","left floor = apl downstile, U+230A ISOamsc" },
  1875. { 8971, "rfloor","right floor, U+230B ISOamsc" },
  1876. { 9001, "lang", "left-pointing angle bracket = bra, U+2329 ISOtech" },
  1877. { 9002, "rang", "right-pointing angle bracket = ket, U+232A ISOtech" },
  1878. { 9674, "loz", "lozenge, U+25CA ISOpub" },
  1879. { 9824, "spades","black spade suit, U+2660 ISOpub" },
  1880. { 9827, "clubs","black club suit = shamrock, U+2663 ISOpub" },
  1881. { 9829, "hearts","black heart suit = valentine, U+2665 ISOpub" },
  1882. { 9830, "diams","black diamond suit, U+2666 ISOpub" },
  1883. };
  1884. /************************************************************************
  1885. * *
  1886. * Commodity functions to handle entities *
  1887. * *
  1888. ************************************************************************/
  1889. /*
  1890. * Macro used to grow the current buffer.
  1891. */
  1892. #define growBuffer(buffer) { \
  1893. xmlChar *tmp; \
  1894. buffer##_size *= 2; \
  1895. tmp = (xmlChar *) xmlRealloc(buffer, buffer##_size); \
  1896. if (tmp == NULL) { \
  1897. htmlErrMemory(ctxt, "growing buffer\n"); \
  1898. xmlFree(buffer); \
  1899. return(NULL); \
  1900. } \
  1901. buffer = tmp; \
  1902. }
  1903. /**
  1904. * htmlEntityLookup:
  1905. * @name: the entity name
  1906. *
  1907. * Lookup the given entity in EntitiesTable
  1908. *
  1909. * TODO: the linear scan is really ugly, an hash table is really needed.
  1910. *
  1911. * Returns the associated htmlEntityDescPtr if found, NULL otherwise.
  1912. */
  1913. const htmlEntityDesc *
  1914. htmlEntityLookup(const xmlChar *name) {
  1915. unsigned int i;
  1916. for (i = 0;i < (sizeof(html40EntitiesTable)/
  1917. sizeof(html40EntitiesTable[0]));i++) {
  1918. if (xmlStrEqual(name, BAD_CAST html40EntitiesTable[i].name)) {
  1919. return((htmlEntityDescPtr) &html40EntitiesTable[i]);
  1920. }
  1921. }
  1922. return(NULL);
  1923. }
  1924. /**
  1925. * htmlEntityValueLookup:
  1926. * @value: the entity's unicode value
  1927. *
  1928. * Lookup the given entity in EntitiesTable
  1929. *
  1930. * TODO: the linear scan is really ugly, an hash table is really needed.
  1931. *
  1932. * Returns the associated htmlEntityDescPtr if found, NULL otherwise.
  1933. */
  1934. const htmlEntityDesc *
  1935. htmlEntityValueLookup(unsigned int value) {
  1936. unsigned int i;
  1937. for (i = 0;i < (sizeof(html40EntitiesTable)/
  1938. sizeof(html40EntitiesTable[0]));i++) {
  1939. if (html40EntitiesTable[i].value >= value) {
  1940. if (html40EntitiesTable[i].value > value)
  1941. break;
  1942. return((htmlEntityDescPtr) &html40EntitiesTable[i]);
  1943. }
  1944. }
  1945. return(NULL);
  1946. }
  1947. /**
  1948. * UTF8ToHtml:
  1949. * @out: a pointer to an array of bytes to store the result
  1950. * @outlen: the length of @out
  1951. * @in: a pointer to an array of UTF-8 chars
  1952. * @inlen: the length of @in
  1953. *
  1954. * Take a block of UTF-8 chars in and try to convert it to an ASCII
  1955. * plus HTML entities block of chars out.
  1956. *
  1957. * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise
  1958. * The value of @inlen after return is the number of octets consumed
  1959. * as the return value is positive, else unpredictable.
  1960. * The value of @outlen after return is the number of octets consumed.
  1961. */
  1962. int
  1963. UTF8ToHtml(unsigned char* out, int *outlen,
  1964. const unsigned char* in, int *inlen) {
  1965. const unsigned char* processed = in;
  1966. const unsigned char* outend;
  1967. const unsigned char* outstart = out;
  1968. const unsigned char* instart = in;
  1969. const unsigned char* inend;
  1970. unsigned int c, d;
  1971. int trailing;
  1972. if ((out == NULL) || (outlen == NULL) || (inlen == NULL)) return(-1);
  1973. if (in == NULL) {
  1974. /*
  1975. * initialization nothing to do
  1976. */
  1977. *outlen = 0;
  1978. *inlen = 0;
  1979. return(0);
  1980. }
  1981. inend = in + (*inlen);
  1982. outend = out + (*outlen);
  1983. while (in < inend) {
  1984. d = *in++;
  1985. if (d < 0x80) { c= d; trailing= 0; }
  1986. else if (d < 0xC0) {
  1987. /* trailing byte in leading position */
  1988. *outlen = out - outstart;
  1989. *inlen = processed - instart;
  1990. return(-2);
  1991. } else if (d < 0xE0) { c= d & 0x1F; trailing= 1; }
  1992. else if (d < 0xF0) { c= d & 0x0F; trailing= 2; }
  1993. else if (d < 0xF8) { c= d & 0x07; trailing= 3; }
  1994. else {
  1995. /* no chance for this in Ascii */
  1996. *outlen = out - outstart;
  1997. *inlen = processed - instart;
  1998. return(-2);
  1999. }
  2000. if (inend - in < trailing) {
  2001. break;
  2002. }
  2003. for ( ; trailing; trailing--) {
  2004. if ((in >= inend) || (((d= *in++) & 0xC0) != 0x80))
  2005. break;
  2006. c <<= 6;
  2007. c |= d & 0x3F;
  2008. }
  2009. /* assertion: c is a single UTF-4 value */
  2010. if (c < 0x80) {
  2011. if (out + 1 >= outend)
  2012. break;
  2013. *out++ = c;
  2014. } else {
  2015. int len;
  2016. const htmlEntityDesc * ent;
  2017. const char *cp;
  2018. char nbuf[16];
  2019. /*
  2020. * Try to lookup a predefined HTML entity for it
  2021. */
  2022. ent = htmlEntityValueLookup(c);
  2023. if (ent == NULL) {
  2024. snprintf(nbuf, sizeof(nbuf), "#%u", c);
  2025. cp = nbuf;
  2026. }
  2027. else
  2028. cp = ent->name;
  2029. len = strlen(cp);
  2030. if (out + 2 + len >= outend)
  2031. break;
  2032. *out++ = '&';
  2033. memcpy(out, cp, len);
  2034. out += len;
  2035. *out++ = ';';
  2036. }
  2037. processed = in;
  2038. }
  2039. *outlen = out - outstart;
  2040. *inlen = processed - instart;
  2041. return(0);
  2042. }
  2043. /**
  2044. * htmlEncodeEntities:
  2045. * @out: a pointer to an array of bytes to store the result
  2046. * @outlen: the length of @out
  2047. * @in: a pointer to an array of UTF-8 chars
  2048. * @inlen: the length of @in
  2049. * @quoteChar: the quote character to escape (' or ") or zero.
  2050. *
  2051. * Take a block of UTF-8 chars in and try to convert it to an ASCII
  2052. * plus HTML entities block of chars out.
  2053. *
  2054. * Returns 0 if success, -2 if the transcoding fails, or -1 otherwise
  2055. * The value of @inlen after return is the number of octets consumed
  2056. * as the return value is positive, else unpredictable.
  2057. * The value of @outlen after return is the number of octets consumed.
  2058. */
  2059. int
  2060. htmlEncodeEntities(unsigned char* out, int *outlen,
  2061. const unsigned char* in, int *inlen, int quoteChar) {
  2062. const unsigned char* processed = in;
  2063. const unsigned char* outend;
  2064. const unsigned char* outstart = out;
  2065. const unsigned char* instart = in;
  2066. const unsigned char* inend;
  2067. unsigned int c, d;
  2068. int trailing;
  2069. if ((out == NULL) || (outlen == NULL) || (inlen == NULL) || (in == NULL))
  2070. return(-1);
  2071. outend = out + (*outlen);
  2072. inend = in + (*inlen);
  2073. while (in < inend) {
  2074. d = *in++;
  2075. if (d < 0x80) { c= d; trailing= 0; }
  2076. else if (d < 0xC0) {
  2077. /* trailing byte in leading position */
  2078. *outlen = out - outstart;
  2079. *inlen = processed - instart;
  2080. return(-2);
  2081. } else if (d < 0xE0) { c= d & 0x1F; trailing= 1; }
  2082. else if (d < 0xF0) { c= d & 0x0F; trailing= 2; }
  2083. else if (d < 0xF8) { c= d & 0x07; trailing= 3; }
  2084. else {
  2085. /* no chance for this in Ascii */
  2086. *outlen = out - outstart;
  2087. *inlen = processed - instart;
  2088. return(-2);
  2089. }
  2090. if (inend - in < trailing)
  2091. break;
  2092. while (trailing--) {
  2093. if (((d= *in++) & 0xC0) != 0x80) {
  2094. *outlen = out - outstart;
  2095. *inlen = processed - instart;
  2096. return(-2);
  2097. }
  2098. c <<= 6;
  2099. c |= d & 0x3F;
  2100. }
  2101. /* assertion: c is a single UTF-4 value */
  2102. if ((c < 0x80) && (c != (unsigned int) quoteChar) &&
  2103. (c != '&') && (c != '<') && (c != '>')) {
  2104. if (out >= outend)
  2105. break;
  2106. *out++ = c;
  2107. } else {
  2108. const htmlEntityDesc * ent;
  2109. const char *cp;
  2110. char nbuf[16];
  2111. int len;
  2112. /*
  2113. * Try to lookup a predefined HTML entity for it
  2114. */
  2115. ent = htmlEntityValueLookup(c);
  2116. if (ent == NULL) {
  2117. snprintf(nbuf, sizeof(nbuf), "#%u", c);
  2118. cp = nbuf;
  2119. }
  2120. else
  2121. cp = ent->name;
  2122. len = strlen(cp);
  2123. if (outend - out < len + 2)
  2124. break;
  2125. *out++ = '&';
  2126. memcpy(out, cp, len);
  2127. out += len;
  2128. *out++ = ';';
  2129. }
  2130. processed = in;
  2131. }
  2132. *outlen = out - outstart;
  2133. *inlen = processed - instart;
  2134. return(0);
  2135. }
  2136. /************************************************************************
  2137. * *
  2138. * Commodity functions to handle streams *
  2139. * *
  2140. ************************************************************************/
  2141. #ifdef LIBXML_PUSH_ENABLED
  2142. /**
  2143. * htmlNewInputStream:
  2144. * @ctxt: an HTML parser context
  2145. *
  2146. * Create a new input stream structure
  2147. * Returns the new input stream or NULL
  2148. */
  2149. static htmlParserInputPtr
  2150. htmlNewInputStream(htmlParserCtxtPtr ctxt) {
  2151. htmlParserInputPtr input;
  2152. input = (xmlParserInputPtr) xmlMalloc(sizeof(htmlParserInput));
  2153. if (input == NULL) {
  2154. htmlErrMemory(ctxt, "couldn't allocate a new input stream\n");
  2155. return(NULL);
  2156. }
  2157. memset(input, 0, sizeof(htmlParserInput));
  2158. input->filename = NULL;
  2159. input->directory = NULL;
  2160. input->base = NULL;
  2161. input->cur = NULL;
  2162. input->buf = NULL;
  2163. input->line = 1;
  2164. input->col = 1;
  2165. input->buf = NULL;
  2166. input->free = NULL;
  2167. input->version = NULL;
  2168. input->consumed = 0;
  2169. input->length = 0;
  2170. return(input);
  2171. }
  2172. #endif
  2173. /************************************************************************
  2174. * *
  2175. * Commodity functions, cleanup needed ? *
  2176. * *
  2177. ************************************************************************/
  2178. /*
  2179. * all tags allowing pc data from the html 4.01 loose dtd
  2180. * NOTE: it might be more appropriate to integrate this information
  2181. * into the html40ElementTable array but I don't want to risk any
  2182. * binary incompatibility
  2183. */
  2184. static const char *allowPCData[] = {
  2185. "a", "abbr", "acronym", "address", "applet", "b", "bdo", "big",
  2186. "blockquote", "body", "button", "caption", "center", "cite", "code",
  2187. "dd", "del", "dfn", "div", "dt", "em", "font", "form", "h1", "h2",
  2188. "h3", "h4", "h5", "h6", "i", "iframe", "ins", "kbd", "label", "legend",
  2189. "li", "noframes", "noscript", "object", "p", "pre", "q", "s", "samp",
  2190. "small", "span", "strike", "strong", "td", "th", "tt", "u", "var"
  2191. };
  2192. /**
  2193. * areBlanks:
  2194. * @ctxt: an HTML parser context
  2195. * @str: a xmlChar *
  2196. * @len: the size of @str
  2197. *
  2198. * Is this a sequence of blank chars that one can ignore ?
  2199. *
  2200. * Returns 1 if ignorable 0 otherwise.
  2201. */
  2202. static int areBlanks(htmlParserCtxtPtr ctxt, const xmlChar *str, int len) {
  2203. unsigned int i;
  2204. int j;
  2205. xmlNodePtr lastChild;
  2206. xmlDtdPtr dtd;
  2207. for (j = 0;j < len;j++)
  2208. if (!(IS_BLANK_CH(str[j]))) return(0);
  2209. if (CUR == 0) return(1);
  2210. if (CUR != '<') return(0);
  2211. if (ctxt->name == NULL)
  2212. return(1);
  2213. if (xmlStrEqual(ctxt->name, BAD_CAST"html"))
  2214. return(1);
  2215. if (xmlStrEqual(ctxt->name, BAD_CAST"head"))
  2216. return(1);
  2217. /* Only strip CDATA children of the body tag for strict HTML DTDs */
  2218. if (xmlStrEqual(ctxt->name, BAD_CAST "body") && ctxt->myDoc != NULL) {
  2219. dtd = xmlGetIntSubset(ctxt->myDoc);
  2220. if (dtd != NULL && dtd->ExternalID != NULL) {
  2221. if (!xmlStrcasecmp(dtd->ExternalID, BAD_CAST "-//W3C//DTD HTML 4.01//EN") ||
  2222. !xmlStrcasecmp(dtd->ExternalID, BAD_CAST "-//W3C//DTD HTML 4//EN"))
  2223. return(1);
  2224. }
  2225. }
  2226. if (ctxt->node == NULL) return(0);
  2227. lastChild = xmlGetLastChild(ctxt->node);
  2228. while ((lastChild) && (lastChild->type == XML_COMMENT_NODE))
  2229. lastChild = lastChild->prev;
  2230. if (lastChild == NULL) {
  2231. if ((ctxt->node->type != XML_ELEMENT_NODE) &&
  2232. (ctxt->node->content != NULL)) return(0);
  2233. /* keep ws in constructs like ...<b> </b>...
  2234. for all tags "b" allowing PCDATA */
  2235. for ( i = 0; i < sizeof(allowPCData)/sizeof(allowPCData[0]); i++ ) {
  2236. if ( xmlStrEqual(ctxt->name, BAD_CAST allowPCData[i]) ) {
  2237. return(0);
  2238. }
  2239. }
  2240. } else if (xmlNodeIsText(lastChild)) {
  2241. return(0);
  2242. } else {
  2243. /* keep ws in constructs like <p><b>xy</b> <i>z</i><p>
  2244. for all tags "p" allowing PCDATA */
  2245. for ( i = 0; i < sizeof(allowPCData)/sizeof(allowPCData[0]); i++ ) {
  2246. if ( xmlStrEqual(lastChild->name, BAD_CAST allowPCData[i]) ) {
  2247. return(0);
  2248. }
  2249. }
  2250. }
  2251. return(1);
  2252. }
  2253. /**
  2254. * htmlNewDocNoDtD:
  2255. * @URI: URI for the dtd, or NULL
  2256. * @ExternalID: the external ID of the DTD, or NULL
  2257. *
  2258. * Creates a new HTML document without a DTD node if @URI and @ExternalID
  2259. * are NULL
  2260. *
  2261. * Returns a new document, do not initialize the DTD if not provided
  2262. */
  2263. htmlDocPtr
  2264. htmlNewDocNoDtD(const xmlChar *URI, const xmlChar *ExternalID) {
  2265. xmlDocPtr cur;
  2266. /*
  2267. * Allocate a new document and fill the fields.
  2268. */
  2269. cur = (xmlDocPtr) xmlMalloc(sizeof(xmlDoc));
  2270. if (cur == NULL) {
  2271. htmlErrMemory(NULL, "HTML document creation failed\n");
  2272. return(NULL);
  2273. }
  2274. memset(cur, 0, sizeof(xmlDoc));
  2275. cur->type = XML_HTML_DOCUMENT_NODE;
  2276. cur->version = NULL;
  2277. cur->intSubset = NULL;
  2278. cur->doc = cur;
  2279. cur->name = NULL;
  2280. cur->children = NULL;
  2281. cur->extSubset = NULL;
  2282. cur->oldNs = NULL;
  2283. cur->encoding = NULL;
  2284. cur->standalone = 1;
  2285. cur->compression = 0;
  2286. cur->ids = NULL;
  2287. cur->refs = NULL;
  2288. cur->_private = NULL;
  2289. cur->charset = XML_CHAR_ENCODING_UTF8;
  2290. cur->properties = XML_DOC_HTML | XML_DOC_USERBUILT;
  2291. if ((ExternalID != NULL) ||
  2292. (URI != NULL))
  2293. xmlCreateIntSubset(cur, BAD_CAST "html", ExternalID, URI);
  2294. if ((__xmlRegisterCallbacks) && (xmlRegisterNodeDefaultValue))
  2295. xmlRegisterNodeDefaultValue((xmlNodePtr)cur);
  2296. return(cur);
  2297. }
  2298. /**
  2299. * htmlNewDoc:
  2300. * @URI: URI for the dtd, or NULL
  2301. * @ExternalID: the external ID of the DTD, or NULL
  2302. *
  2303. * Creates a new HTML document
  2304. *
  2305. * Returns a new document
  2306. */
  2307. htmlDocPtr
  2308. htmlNewDoc(const xmlChar *URI, const xmlChar *ExternalID) {
  2309. if ((URI == NULL) && (ExternalID == NULL))
  2310. return(htmlNewDocNoDtD(
  2311. BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd",
  2312. BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN"));
  2313. return(htmlNewDocNoDtD(URI, ExternalID));
  2314. }
  2315. /************************************************************************
  2316. * *
  2317. * The parser itself *
  2318. * Relates to http://www.w3.org/TR/html40 *
  2319. * *
  2320. ************************************************************************/
  2321. /************************************************************************
  2322. * *
  2323. * The parser itself *
  2324. * *
  2325. ************************************************************************/
  2326. static const xmlChar * htmlParseNameComplex(xmlParserCtxtPtr ctxt);
  2327. static void
  2328. htmlSkipBogusComment(htmlParserCtxtPtr ctxt) {
  2329. int c;
  2330. htmlParseErr(ctxt, XML_HTML_INCORRECTLY_OPENED_COMMENT,
  2331. "Incorrectly opened comment\n", NULL, NULL);
  2332. do {
  2333. c = CUR;
  2334. if (c == 0)
  2335. break;
  2336. NEXT;
  2337. } while (c != '>');
  2338. }
  2339. /**
  2340. * htmlParseHTMLName:
  2341. * @ctxt: an HTML parser context
  2342. *
  2343. * parse an HTML tag or attribute name, note that we convert it to lowercase
  2344. * since HTML names are not case-sensitive.
  2345. *
  2346. * Returns the Tag Name parsed or NULL
  2347. */
  2348. static const xmlChar *
  2349. htmlParseHTMLName(htmlParserCtxtPtr ctxt) {
  2350. const xmlChar *ret;
  2351. int i = 0;
  2352. xmlChar loc[HTML_PARSER_BUFFER_SIZE];
  2353. if (!IS_ASCII_LETTER(CUR) && (CUR != '_') &&
  2354. (CUR != ':') && (CUR != '.')) return(NULL);
  2355. while ((i < HTML_PARSER_BUFFER_SIZE) &&
  2356. ((IS_ASCII_LETTER(CUR)) || (IS_ASCII_DIGIT(CUR)) ||
  2357. (CUR == ':') || (CUR == '-') || (CUR == '_') ||
  2358. (CUR == '.'))) {
  2359. if ((CUR >= 'A') && (CUR <= 'Z')) loc[i] = CUR + 0x20;
  2360. else loc[i] = CUR;
  2361. i++;
  2362. NEXT;
  2363. }
  2364. ret = xmlDictLookup(ctxt->dict, loc, i);
  2365. if (ret == NULL)
  2366. htmlErrMemory(ctxt, NULL);
  2367. return(ret);
  2368. }
  2369. /**
  2370. * htmlParseHTMLName_nonInvasive:
  2371. * @ctxt: an HTML parser context
  2372. *
  2373. * parse an HTML tag or attribute name, note that we convert it to lowercase
  2374. * since HTML names are not case-sensitive, this doesn't consume the data
  2375. * from the stream, it's a look-ahead
  2376. *
  2377. * Returns the Tag Name parsed or NULL
  2378. */
  2379. static const xmlChar *
  2380. htmlParseHTMLName_nonInvasive(htmlParserCtxtPtr ctxt) {
  2381. int i = 0;
  2382. xmlChar loc[HTML_PARSER_BUFFER_SIZE];
  2383. if (!IS_ASCII_LETTER(NXT(1)) && (NXT(1) != '_') &&
  2384. (NXT(1) != ':')) return(NULL);
  2385. while ((i < HTML_PARSER_BUFFER_SIZE) &&
  2386. ((IS_ASCII_LETTER(NXT(1+i))) || (IS_ASCII_DIGIT(NXT(1+i))) ||
  2387. (NXT(1+i) == ':') || (NXT(1+i) == '-') || (NXT(1+i) == '_'))) {
  2388. if ((NXT(1+i) >= 'A') && (NXT(1+i) <= 'Z')) loc[i] = NXT(1+i) + 0x20;
  2389. else loc[i] = NXT(1+i);
  2390. i++;
  2391. }
  2392. return(xmlDictLookup(ctxt->dict, loc, i));
  2393. }
  2394. /**
  2395. * htmlParseName:
  2396. * @ctxt: an HTML parser context
  2397. *
  2398. * parse an HTML name, this routine is case sensitive.
  2399. *
  2400. * Returns the Name parsed or NULL
  2401. */
  2402. static const xmlChar *
  2403. htmlParseName(htmlParserCtxtPtr ctxt) {
  2404. const xmlChar *in;
  2405. const xmlChar *ret;
  2406. int count = 0;
  2407. GROW;
  2408. /*
  2409. * Accelerator for simple ASCII names
  2410. */
  2411. in = ctxt->input->cur;
  2412. if (((*in >= 0x61) && (*in <= 0x7A)) ||
  2413. ((*in >= 0x41) && (*in <= 0x5A)) ||
  2414. (*in == '_') || (*in == ':')) {
  2415. in++;
  2416. while (((*in >= 0x61) && (*in <= 0x7A)) ||
  2417. ((*in >= 0x41) && (*in <= 0x5A)) ||
  2418. ((*in >= 0x30) && (*in <= 0x39)) ||
  2419. (*in == '_') || (*in == '-') ||
  2420. (*in == ':') || (*in == '.'))
  2421. in++;
  2422. if (in == ctxt->input->end)
  2423. return(NULL);
  2424. if ((*in > 0) && (*in < 0x80)) {
  2425. count = in - ctxt->input->cur;
  2426. ret = xmlDictLookup(ctxt->dict, ctxt->input->cur, count);
  2427. ctxt->input->cur = in;
  2428. ctxt->input->col += count;
  2429. return(ret);
  2430. }
  2431. }
  2432. return(htmlParseNameComplex(ctxt));
  2433. }
  2434. static const xmlChar *
  2435. htmlParseNameComplex(xmlParserCtxtPtr ctxt) {
  2436. int len = 0, l;
  2437. int c;
  2438. int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
  2439. XML_MAX_TEXT_LENGTH :
  2440. XML_MAX_NAME_LENGTH;
  2441. const xmlChar *base = ctxt->input->base;
  2442. /*
  2443. * Handler for more complex cases
  2444. */
  2445. c = CUR_CHAR(l);
  2446. if ((c == ' ') || (c == '>') || (c == '/') || /* accelerators */
  2447. (!IS_LETTER(c) && (c != '_') &&
  2448. (c != ':'))) {
  2449. return(NULL);
  2450. }
  2451. while ((c != ' ') && (c != '>') && (c != '/') && /* test bigname.xml */
  2452. ((IS_LETTER(c)) || (IS_DIGIT(c)) ||
  2453. (c == '.') || (c == '-') ||
  2454. (c == '_') || (c == ':') ||
  2455. (IS_COMBINING(c)) ||
  2456. (IS_EXTENDER(c)))) {
  2457. len += l;
  2458. if (len > maxLength) {
  2459. htmlParseErr(ctxt, XML_ERR_NAME_TOO_LONG, "name too long", NULL, NULL);
  2460. return(NULL);
  2461. }
  2462. NEXTL(l);
  2463. c = CUR_CHAR(l);
  2464. if (ctxt->input->base != base) {
  2465. /*
  2466. * We changed encoding from an unknown encoding
  2467. * Input buffer changed location, so we better start again
  2468. */
  2469. return(htmlParseNameComplex(ctxt));
  2470. }
  2471. }
  2472. if (ctxt->instate == XML_PARSER_EOF)
  2473. return(NULL);
  2474. if (ctxt->input->cur - ctxt->input->base < len) {
  2475. /* Sanity check */
  2476. htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
  2477. "unexpected change of input buffer", NULL, NULL);
  2478. return (NULL);
  2479. }
  2480. return(xmlDictLookup(ctxt->dict, ctxt->input->cur - len, len));
  2481. }
  2482. /**
  2483. * htmlParseHTMLAttribute:
  2484. * @ctxt: an HTML parser context
  2485. * @stop: a char stop value
  2486. *
  2487. * parse an HTML attribute value till the stop (quote), if
  2488. * stop is 0 then it stops at the first space
  2489. *
  2490. * Returns the attribute parsed or NULL
  2491. */
  2492. static xmlChar *
  2493. htmlParseHTMLAttribute(htmlParserCtxtPtr ctxt, const xmlChar stop) {
  2494. xmlChar *buffer = NULL;
  2495. int buffer_size = 0;
  2496. int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
  2497. XML_MAX_HUGE_LENGTH :
  2498. XML_MAX_TEXT_LENGTH;
  2499. xmlChar *out = NULL;
  2500. const xmlChar *name = NULL;
  2501. const xmlChar *cur = NULL;
  2502. const htmlEntityDesc * ent;
  2503. /*
  2504. * allocate a translation buffer.
  2505. */
  2506. buffer_size = HTML_PARSER_BUFFER_SIZE;
  2507. buffer = (xmlChar *) xmlMallocAtomic(buffer_size);
  2508. if (buffer == NULL) {
  2509. htmlErrMemory(ctxt, "buffer allocation failed\n");
  2510. return(NULL);
  2511. }
  2512. out = buffer;
  2513. /*
  2514. * Ok loop until we reach one of the ending chars
  2515. */
  2516. while ((CUR != 0) && (CUR != stop)) {
  2517. if ((stop == 0) && (CUR == '>')) break;
  2518. if ((stop == 0) && (IS_BLANK_CH(CUR))) break;
  2519. if (CUR == '&') {
  2520. if (NXT(1) == '#') {
  2521. unsigned int c;
  2522. int bits;
  2523. c = htmlParseCharRef(ctxt);
  2524. if (c < 0x80)
  2525. { *out++ = c; bits= -6; }
  2526. else if (c < 0x800)
  2527. { *out++ =((c >> 6) & 0x1F) | 0xC0; bits= 0; }
  2528. else if (c < 0x10000)
  2529. { *out++ =((c >> 12) & 0x0F) | 0xE0; bits= 6; }
  2530. else
  2531. { *out++ =((c >> 18) & 0x07) | 0xF0; bits= 12; }
  2532. for ( ; bits >= 0; bits-= 6) {
  2533. *out++ = ((c >> bits) & 0x3F) | 0x80;
  2534. }
  2535. if (out - buffer > buffer_size - 100) {
  2536. int indx = out - buffer;
  2537. growBuffer(buffer);
  2538. out = &buffer[indx];
  2539. }
  2540. } else {
  2541. ent = htmlParseEntityRef(ctxt, &name);
  2542. if (name == NULL) {
  2543. *out++ = '&';
  2544. if (out - buffer > buffer_size - 100) {
  2545. int indx = out - buffer;
  2546. growBuffer(buffer);
  2547. out = &buffer[indx];
  2548. }
  2549. } else if (ent == NULL) {
  2550. *out++ = '&';
  2551. cur = name;
  2552. while (*cur != 0) {
  2553. if (out - buffer > buffer_size - 100) {
  2554. int indx = out - buffer;
  2555. growBuffer(buffer);
  2556. out = &buffer[indx];
  2557. }
  2558. *out++ = *cur++;
  2559. }
  2560. } else {
  2561. unsigned int c;
  2562. int bits;
  2563. if (out - buffer > buffer_size - 100) {
  2564. int indx = out - buffer;
  2565. growBuffer(buffer);
  2566. out = &buffer[indx];
  2567. }
  2568. c = ent->value;
  2569. if (c < 0x80)
  2570. { *out++ = c; bits= -6; }
  2571. else if (c < 0x800)
  2572. { *out++ =((c >> 6) & 0x1F) | 0xC0; bits= 0; }
  2573. else if (c < 0x10000)
  2574. { *out++ =((c >> 12) & 0x0F) | 0xE0; bits= 6; }
  2575. else
  2576. { *out++ =((c >> 18) & 0x07) | 0xF0; bits= 12; }
  2577. for ( ; bits >= 0; bits-= 6) {
  2578. *out++ = ((c >> bits) & 0x3F) | 0x80;
  2579. }
  2580. }
  2581. }
  2582. } else {
  2583. unsigned int c;
  2584. int bits, l;
  2585. if (out - buffer > buffer_size - 100) {
  2586. int indx = out - buffer;
  2587. growBuffer(buffer);
  2588. out = &buffer[indx];
  2589. }
  2590. c = CUR_CHAR(l);
  2591. if (ctxt->instate == XML_PARSER_EOF) {
  2592. xmlFree(buffer);
  2593. return(NULL);
  2594. }
  2595. if (c < 0x80)
  2596. { *out++ = c; bits= -6; }
  2597. else if (c < 0x800)
  2598. { *out++ =((c >> 6) & 0x1F) | 0xC0; bits= 0; }
  2599. else if (c < 0x10000)
  2600. { *out++ =((c >> 12) & 0x0F) | 0xE0; bits= 6; }
  2601. else
  2602. { *out++ =((c >> 18) & 0x07) | 0xF0; bits= 12; }
  2603. for ( ; bits >= 0; bits-= 6) {
  2604. *out++ = ((c >> bits) & 0x3F) | 0x80;
  2605. }
  2606. NEXTL(l);
  2607. }
  2608. if (out - buffer > maxLength) {
  2609. htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
  2610. "attribute value too long\n", NULL, NULL);
  2611. xmlFree(buffer);
  2612. return(NULL);
  2613. }
  2614. }
  2615. *out = 0;
  2616. return(buffer);
  2617. }
  2618. /**
  2619. * htmlParseEntityRef:
  2620. * @ctxt: an HTML parser context
  2621. * @str: location to store the entity name
  2622. *
  2623. * DEPRECATED: Internal function, don't use.
  2624. *
  2625. * parse an HTML ENTITY references
  2626. *
  2627. * [68] EntityRef ::= '&' Name ';'
  2628. *
  2629. * Returns the associated htmlEntityDescPtr if found, or NULL otherwise,
  2630. * if non-NULL *str will have to be freed by the caller.
  2631. */
  2632. const htmlEntityDesc *
  2633. htmlParseEntityRef(htmlParserCtxtPtr ctxt, const xmlChar **str) {
  2634. const xmlChar *name;
  2635. const htmlEntityDesc * ent = NULL;
  2636. if (str != NULL) *str = NULL;
  2637. if ((ctxt == NULL) || (ctxt->input == NULL)) return(NULL);
  2638. if (CUR == '&') {
  2639. NEXT;
  2640. name = htmlParseName(ctxt);
  2641. if (name == NULL) {
  2642. htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
  2643. "htmlParseEntityRef: no name\n", NULL, NULL);
  2644. } else {
  2645. GROW;
  2646. if (CUR == ';') {
  2647. if (str != NULL)
  2648. *str = name;
  2649. /*
  2650. * Lookup the entity in the table.
  2651. */
  2652. ent = htmlEntityLookup(name);
  2653. if (ent != NULL) /* OK that's ugly !!! */
  2654. NEXT;
  2655. } else {
  2656. htmlParseErr(ctxt, XML_ERR_ENTITYREF_SEMICOL_MISSING,
  2657. "htmlParseEntityRef: expecting ';'\n",
  2658. NULL, NULL);
  2659. if (str != NULL)
  2660. *str = name;
  2661. }
  2662. }
  2663. }
  2664. return(ent);
  2665. }
  2666. /**
  2667. * htmlParseAttValue:
  2668. * @ctxt: an HTML parser context
  2669. *
  2670. * parse a value for an attribute
  2671. * Note: the parser won't do substitution of entities here, this
  2672. * will be handled later in xmlStringGetNodeList, unless it was
  2673. * asked for ctxt->replaceEntities != 0
  2674. *
  2675. * Returns the AttValue parsed or NULL.
  2676. */
  2677. static xmlChar *
  2678. htmlParseAttValue(htmlParserCtxtPtr ctxt) {
  2679. xmlChar *ret = NULL;
  2680. if (CUR == '"') {
  2681. NEXT;
  2682. ret = htmlParseHTMLAttribute(ctxt, '"');
  2683. if (CUR != '"') {
  2684. htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
  2685. "AttValue: \" expected\n", NULL, NULL);
  2686. } else
  2687. NEXT;
  2688. } else if (CUR == '\'') {
  2689. NEXT;
  2690. ret = htmlParseHTMLAttribute(ctxt, '\'');
  2691. if (CUR != '\'') {
  2692. htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_NOT_FINISHED,
  2693. "AttValue: ' expected\n", NULL, NULL);
  2694. } else
  2695. NEXT;
  2696. } else {
  2697. /*
  2698. * That's an HTMLism, the attribute value may not be quoted
  2699. */
  2700. ret = htmlParseHTMLAttribute(ctxt, 0);
  2701. if (ret == NULL) {
  2702. htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_WITHOUT_VALUE,
  2703. "AttValue: no value found\n", NULL, NULL);
  2704. }
  2705. }
  2706. return(ret);
  2707. }
  2708. /**
  2709. * htmlParseSystemLiteral:
  2710. * @ctxt: an HTML parser context
  2711. *
  2712. * parse an HTML Literal
  2713. *
  2714. * [11] SystemLiteral ::= ('"' [^"]* '"') | ("'" [^']* "'")
  2715. *
  2716. * Returns the SystemLiteral parsed or NULL
  2717. */
  2718. static xmlChar *
  2719. htmlParseSystemLiteral(htmlParserCtxtPtr ctxt) {
  2720. size_t len = 0, startPosition = 0;
  2721. int err = 0;
  2722. int quote;
  2723. xmlChar *ret = NULL;
  2724. if ((CUR != '"') && (CUR != '\'')) {
  2725. htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_STARTED,
  2726. "SystemLiteral \" or ' expected\n", NULL, NULL);
  2727. return(NULL);
  2728. }
  2729. quote = CUR;
  2730. NEXT;
  2731. if (CUR_PTR < BASE_PTR)
  2732. return(ret);
  2733. startPosition = CUR_PTR - BASE_PTR;
  2734. while ((CUR != 0) && (CUR != quote)) {
  2735. /* TODO: Handle UTF-8 */
  2736. if (!IS_CHAR_CH(CUR)) {
  2737. htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
  2738. "Invalid char in SystemLiteral 0x%X\n", CUR);
  2739. err = 1;
  2740. }
  2741. NEXT;
  2742. len++;
  2743. }
  2744. if (CUR != quote) {
  2745. htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED,
  2746. "Unfinished SystemLiteral\n", NULL, NULL);
  2747. } else {
  2748. if (err == 0)
  2749. ret = xmlStrndup((BASE_PTR+startPosition), len);
  2750. NEXT;
  2751. }
  2752. return(ret);
  2753. }
  2754. /**
  2755. * htmlParsePubidLiteral:
  2756. * @ctxt: an HTML parser context
  2757. *
  2758. * parse an HTML public literal
  2759. *
  2760. * [12] PubidLiteral ::= '"' PubidChar* '"' | "'" (PubidChar - "'")* "'"
  2761. *
  2762. * Returns the PubidLiteral parsed or NULL.
  2763. */
  2764. static xmlChar *
  2765. htmlParsePubidLiteral(htmlParserCtxtPtr ctxt) {
  2766. size_t len = 0, startPosition = 0;
  2767. int err = 0;
  2768. int quote;
  2769. xmlChar *ret = NULL;
  2770. if ((CUR != '"') && (CUR != '\'')) {
  2771. htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_STARTED,
  2772. "PubidLiteral \" or ' expected\n", NULL, NULL);
  2773. return(NULL);
  2774. }
  2775. quote = CUR;
  2776. NEXT;
  2777. /*
  2778. * Name ::= (Letter | '_') (NameChar)*
  2779. */
  2780. if (CUR_PTR < BASE_PTR)
  2781. return(ret);
  2782. startPosition = CUR_PTR - BASE_PTR;
  2783. while ((CUR != 0) && (CUR != quote)) {
  2784. if (!IS_PUBIDCHAR_CH(CUR)) {
  2785. htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
  2786. "Invalid char in PubidLiteral 0x%X\n", CUR);
  2787. err = 1;
  2788. }
  2789. len++;
  2790. NEXT;
  2791. }
  2792. if (CUR != quote) {
  2793. htmlParseErr(ctxt, XML_ERR_LITERAL_NOT_FINISHED,
  2794. "Unfinished PubidLiteral\n", NULL, NULL);
  2795. } else {
  2796. if (err == 0)
  2797. ret = xmlStrndup((BASE_PTR + startPosition), len);
  2798. NEXT;
  2799. }
  2800. return(ret);
  2801. }
  2802. /**
  2803. * htmlParseScript:
  2804. * @ctxt: an HTML parser context
  2805. *
  2806. * parse the content of an HTML SCRIPT or STYLE element
  2807. * http://www.w3.org/TR/html4/sgml/dtd.html#Script
  2808. * http://www.w3.org/TR/html4/sgml/dtd.html#StyleSheet
  2809. * http://www.w3.org/TR/html4/types.html#type-script
  2810. * http://www.w3.org/TR/html4/types.html#h-6.15
  2811. * http://www.w3.org/TR/html4/appendix/notes.html#h-B.3.2.1
  2812. *
  2813. * Script data ( %Script; in the DTD) can be the content of the SCRIPT
  2814. * element and the value of intrinsic event attributes. User agents must
  2815. * not evaluate script data as HTML markup but instead must pass it on as
  2816. * data to a script engine.
  2817. * NOTES:
  2818. * - The content is passed like CDATA
  2819. * - the attributes for style and scripting "onXXX" are also described
  2820. * as CDATA but SGML allows entities references in attributes so their
  2821. * processing is identical as other attributes
  2822. */
  2823. static void
  2824. htmlParseScript(htmlParserCtxtPtr ctxt) {
  2825. xmlChar buf[HTML_PARSER_BIG_BUFFER_SIZE + 5];
  2826. int nbchar = 0;
  2827. int cur,l;
  2828. cur = CUR_CHAR(l);
  2829. while (cur != 0) {
  2830. if ((cur == '<') && (NXT(1) == '/')) {
  2831. /*
  2832. * One should break here, the specification is clear:
  2833. * Authors should therefore escape "</" within the content.
  2834. * Escape mechanisms are specific to each scripting or
  2835. * style sheet language.
  2836. *
  2837. * In recovery mode, only break if end tag match the
  2838. * current tag, effectively ignoring all tags inside the
  2839. * script/style block and treating the entire block as
  2840. * CDATA.
  2841. */
  2842. if (ctxt->recovery) {
  2843. if (xmlStrncasecmp(ctxt->name, ctxt->input->cur+2,
  2844. xmlStrlen(ctxt->name)) == 0)
  2845. {
  2846. break; /* while */
  2847. } else {
  2848. htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
  2849. "Element %s embeds close tag\n",
  2850. ctxt->name, NULL);
  2851. }
  2852. } else {
  2853. if (((NXT(2) >= 'A') && (NXT(2) <= 'Z')) ||
  2854. ((NXT(2) >= 'a') && (NXT(2) <= 'z')))
  2855. {
  2856. break; /* while */
  2857. }
  2858. }
  2859. }
  2860. if (IS_CHAR(cur)) {
  2861. COPY_BUF(l,buf,nbchar,cur);
  2862. } else {
  2863. htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
  2864. "Invalid char in CDATA 0x%X\n", cur);
  2865. }
  2866. NEXTL(l);
  2867. if (nbchar >= HTML_PARSER_BIG_BUFFER_SIZE) {
  2868. buf[nbchar] = 0;
  2869. if (ctxt->sax->cdataBlock!= NULL) {
  2870. /*
  2871. * Insert as CDATA, which is the same as HTML_PRESERVE_NODE
  2872. */
  2873. ctxt->sax->cdataBlock(ctxt->userData, buf, nbchar);
  2874. } else if (ctxt->sax->characters != NULL) {
  2875. ctxt->sax->characters(ctxt->userData, buf, nbchar);
  2876. }
  2877. nbchar = 0;
  2878. SHRINK;
  2879. }
  2880. cur = CUR_CHAR(l);
  2881. }
  2882. if (ctxt->instate == XML_PARSER_EOF)
  2883. return;
  2884. if ((nbchar != 0) && (ctxt->sax != NULL) && (!ctxt->disableSAX)) {
  2885. buf[nbchar] = 0;
  2886. if (ctxt->sax->cdataBlock!= NULL) {
  2887. /*
  2888. * Insert as CDATA, which is the same as HTML_PRESERVE_NODE
  2889. */
  2890. ctxt->sax->cdataBlock(ctxt->userData, buf, nbchar);
  2891. } else if (ctxt->sax->characters != NULL) {
  2892. ctxt->sax->characters(ctxt->userData, buf, nbchar);
  2893. }
  2894. }
  2895. }
  2896. /**
  2897. * htmlParseCharDataInternal:
  2898. * @ctxt: an HTML parser context
  2899. * @readahead: optional read ahead character in ascii range
  2900. *
  2901. * parse a CharData section.
  2902. * if we are within a CDATA section ']]>' marks an end of section.
  2903. *
  2904. * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
  2905. */
  2906. static void
  2907. htmlParseCharDataInternal(htmlParserCtxtPtr ctxt, int readahead) {
  2908. xmlChar buf[HTML_PARSER_BIG_BUFFER_SIZE + 6];
  2909. int nbchar = 0;
  2910. int cur, l;
  2911. if (readahead)
  2912. buf[nbchar++] = readahead;
  2913. cur = CUR_CHAR(l);
  2914. while (((cur != '<') || (ctxt->token == '<')) &&
  2915. ((cur != '&') || (ctxt->token == '&')) &&
  2916. (cur != 0)) {
  2917. if (!(IS_CHAR(cur))) {
  2918. htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
  2919. "Invalid char in CDATA 0x%X\n", cur);
  2920. } else {
  2921. COPY_BUF(l,buf,nbchar,cur);
  2922. }
  2923. NEXTL(l);
  2924. if (nbchar >= HTML_PARSER_BIG_BUFFER_SIZE) {
  2925. buf[nbchar] = 0;
  2926. /*
  2927. * Ok the segment is to be consumed as chars.
  2928. */
  2929. if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
  2930. if (areBlanks(ctxt, buf, nbchar)) {
  2931. if (ctxt->keepBlanks) {
  2932. if (ctxt->sax->characters != NULL)
  2933. ctxt->sax->characters(ctxt->userData, buf, nbchar);
  2934. } else {
  2935. if (ctxt->sax->ignorableWhitespace != NULL)
  2936. ctxt->sax->ignorableWhitespace(ctxt->userData,
  2937. buf, nbchar);
  2938. }
  2939. } else {
  2940. htmlCheckParagraph(ctxt);
  2941. if (ctxt->sax->characters != NULL)
  2942. ctxt->sax->characters(ctxt->userData, buf, nbchar);
  2943. }
  2944. }
  2945. nbchar = 0;
  2946. SHRINK;
  2947. }
  2948. cur = CUR_CHAR(l);
  2949. }
  2950. if (ctxt->instate == XML_PARSER_EOF)
  2951. return;
  2952. if (nbchar != 0) {
  2953. buf[nbchar] = 0;
  2954. /*
  2955. * Ok the segment is to be consumed as chars.
  2956. */
  2957. if ((ctxt->sax != NULL) && (!ctxt->disableSAX)) {
  2958. if (areBlanks(ctxt, buf, nbchar)) {
  2959. if (ctxt->keepBlanks) {
  2960. if (ctxt->sax->characters != NULL)
  2961. ctxt->sax->characters(ctxt->userData, buf, nbchar);
  2962. } else {
  2963. if (ctxt->sax->ignorableWhitespace != NULL)
  2964. ctxt->sax->ignorableWhitespace(ctxt->userData,
  2965. buf, nbchar);
  2966. }
  2967. } else {
  2968. htmlCheckParagraph(ctxt);
  2969. if (ctxt->sax->characters != NULL)
  2970. ctxt->sax->characters(ctxt->userData, buf, nbchar);
  2971. }
  2972. }
  2973. }
  2974. }
  2975. /**
  2976. * htmlParseCharData:
  2977. * @ctxt: an HTML parser context
  2978. *
  2979. * parse a CharData section.
  2980. * if we are within a CDATA section ']]>' marks an end of section.
  2981. *
  2982. * [14] CharData ::= [^<&]* - ([^<&]* ']]>' [^<&]*)
  2983. */
  2984. static void
  2985. htmlParseCharData(htmlParserCtxtPtr ctxt) {
  2986. htmlParseCharDataInternal(ctxt, 0);
  2987. }
  2988. /**
  2989. * htmlParseExternalID:
  2990. * @ctxt: an HTML parser context
  2991. * @publicID: a xmlChar** receiving PubidLiteral
  2992. *
  2993. * Parse an External ID or a Public ID
  2994. *
  2995. * [75] ExternalID ::= 'SYSTEM' S SystemLiteral
  2996. * | 'PUBLIC' S PubidLiteral S SystemLiteral
  2997. *
  2998. * [83] PublicID ::= 'PUBLIC' S PubidLiteral
  2999. *
  3000. * Returns the function returns SystemLiteral and in the second
  3001. * case publicID receives PubidLiteral, is strict is off
  3002. * it is possible to return NULL and have publicID set.
  3003. */
  3004. static xmlChar *
  3005. htmlParseExternalID(htmlParserCtxtPtr ctxt, xmlChar **publicID) {
  3006. xmlChar *URI = NULL;
  3007. if ((UPPER == 'S') && (UPP(1) == 'Y') &&
  3008. (UPP(2) == 'S') && (UPP(3) == 'T') &&
  3009. (UPP(4) == 'E') && (UPP(5) == 'M')) {
  3010. SKIP(6);
  3011. if (!IS_BLANK_CH(CUR)) {
  3012. htmlParseErr(ctxt, XML_ERR_SPACE_REQUIRED,
  3013. "Space required after 'SYSTEM'\n", NULL, NULL);
  3014. }
  3015. SKIP_BLANKS;
  3016. URI = htmlParseSystemLiteral(ctxt);
  3017. if (URI == NULL) {
  3018. htmlParseErr(ctxt, XML_ERR_URI_REQUIRED,
  3019. "htmlParseExternalID: SYSTEM, no URI\n", NULL, NULL);
  3020. }
  3021. } else if ((UPPER == 'P') && (UPP(1) == 'U') &&
  3022. (UPP(2) == 'B') && (UPP(3) == 'L') &&
  3023. (UPP(4) == 'I') && (UPP(5) == 'C')) {
  3024. SKIP(6);
  3025. if (!IS_BLANK_CH(CUR)) {
  3026. htmlParseErr(ctxt, XML_ERR_SPACE_REQUIRED,
  3027. "Space required after 'PUBLIC'\n", NULL, NULL);
  3028. }
  3029. SKIP_BLANKS;
  3030. *publicID = htmlParsePubidLiteral(ctxt);
  3031. if (*publicID == NULL) {
  3032. htmlParseErr(ctxt, XML_ERR_PUBID_REQUIRED,
  3033. "htmlParseExternalID: PUBLIC, no Public Identifier\n",
  3034. NULL, NULL);
  3035. }
  3036. SKIP_BLANKS;
  3037. if ((CUR == '"') || (CUR == '\'')) {
  3038. URI = htmlParseSystemLiteral(ctxt);
  3039. }
  3040. }
  3041. return(URI);
  3042. }
  3043. /**
  3044. * xmlParsePI:
  3045. * @ctxt: an XML parser context
  3046. *
  3047. * parse an XML Processing Instruction.
  3048. *
  3049. * [16] PI ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'
  3050. */
  3051. static void
  3052. htmlParsePI(htmlParserCtxtPtr ctxt) {
  3053. xmlChar *buf = NULL;
  3054. int len = 0;
  3055. int size = HTML_PARSER_BUFFER_SIZE;
  3056. int cur, l;
  3057. int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
  3058. XML_MAX_HUGE_LENGTH :
  3059. XML_MAX_TEXT_LENGTH;
  3060. const xmlChar *target;
  3061. xmlParserInputState state;
  3062. if ((RAW == '<') && (NXT(1) == '?')) {
  3063. state = ctxt->instate;
  3064. ctxt->instate = XML_PARSER_PI;
  3065. /*
  3066. * this is a Processing Instruction.
  3067. */
  3068. SKIP(2);
  3069. /*
  3070. * Parse the target name and check for special support like
  3071. * namespace.
  3072. */
  3073. target = htmlParseName(ctxt);
  3074. if (target != NULL) {
  3075. if (RAW == '>') {
  3076. SKIP(1);
  3077. /*
  3078. * SAX: PI detected.
  3079. */
  3080. if ((ctxt->sax) && (!ctxt->disableSAX) &&
  3081. (ctxt->sax->processingInstruction != NULL))
  3082. ctxt->sax->processingInstruction(ctxt->userData,
  3083. target, NULL);
  3084. ctxt->instate = state;
  3085. return;
  3086. }
  3087. buf = (xmlChar *) xmlMallocAtomic(size);
  3088. if (buf == NULL) {
  3089. htmlErrMemory(ctxt, NULL);
  3090. ctxt->instate = state;
  3091. return;
  3092. }
  3093. cur = CUR;
  3094. if (!IS_BLANK(cur)) {
  3095. htmlParseErr(ctxt, XML_ERR_SPACE_REQUIRED,
  3096. "ParsePI: PI %s space expected\n", target, NULL);
  3097. }
  3098. SKIP_BLANKS;
  3099. cur = CUR_CHAR(l);
  3100. while ((cur != 0) && (cur != '>')) {
  3101. if (len + 5 >= size) {
  3102. xmlChar *tmp;
  3103. size *= 2;
  3104. tmp = (xmlChar *) xmlRealloc(buf, size);
  3105. if (tmp == NULL) {
  3106. htmlErrMemory(ctxt, NULL);
  3107. xmlFree(buf);
  3108. ctxt->instate = state;
  3109. return;
  3110. }
  3111. buf = tmp;
  3112. }
  3113. if (IS_CHAR(cur)) {
  3114. COPY_BUF(l,buf,len,cur);
  3115. } else {
  3116. htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
  3117. "Invalid char in processing instruction "
  3118. "0x%X\n", cur);
  3119. }
  3120. if (len > maxLength) {
  3121. htmlParseErr(ctxt, XML_ERR_PI_NOT_FINISHED,
  3122. "PI %s too long", target, NULL);
  3123. xmlFree(buf);
  3124. ctxt->instate = state;
  3125. return;
  3126. }
  3127. NEXTL(l);
  3128. cur = CUR_CHAR(l);
  3129. }
  3130. buf[len] = 0;
  3131. if (ctxt->instate == XML_PARSER_EOF) {
  3132. xmlFree(buf);
  3133. return;
  3134. }
  3135. if (cur != '>') {
  3136. htmlParseErr(ctxt, XML_ERR_PI_NOT_FINISHED,
  3137. "ParsePI: PI %s never end ...\n", target, NULL);
  3138. } else {
  3139. SKIP(1);
  3140. /*
  3141. * SAX: PI detected.
  3142. */
  3143. if ((ctxt->sax) && (!ctxt->disableSAX) &&
  3144. (ctxt->sax->processingInstruction != NULL))
  3145. ctxt->sax->processingInstruction(ctxt->userData,
  3146. target, buf);
  3147. }
  3148. xmlFree(buf);
  3149. } else {
  3150. htmlParseErr(ctxt, XML_ERR_PI_NOT_STARTED,
  3151. "PI is not started correctly", NULL, NULL);
  3152. }
  3153. ctxt->instate = state;
  3154. }
  3155. }
  3156. /**
  3157. * htmlParseComment:
  3158. * @ctxt: an HTML parser context
  3159. *
  3160. * Parse an XML (SGML) comment <!-- .... -->
  3161. *
  3162. * [15] Comment ::= '<!--' ((Char - '-') | ('-' (Char - '-')))* '-->'
  3163. */
  3164. static void
  3165. htmlParseComment(htmlParserCtxtPtr ctxt) {
  3166. xmlChar *buf = NULL;
  3167. int len;
  3168. int size = HTML_PARSER_BUFFER_SIZE;
  3169. int q, ql;
  3170. int r, rl;
  3171. int cur, l;
  3172. int next, nl;
  3173. int maxLength = (ctxt->options & XML_PARSE_HUGE) ?
  3174. XML_MAX_HUGE_LENGTH :
  3175. XML_MAX_TEXT_LENGTH;
  3176. xmlParserInputState state;
  3177. /*
  3178. * Check that there is a comment right here.
  3179. */
  3180. if ((RAW != '<') || (NXT(1) != '!') ||
  3181. (NXT(2) != '-') || (NXT(3) != '-')) return;
  3182. state = ctxt->instate;
  3183. ctxt->instate = XML_PARSER_COMMENT;
  3184. SKIP(4);
  3185. buf = (xmlChar *) xmlMallocAtomic(size);
  3186. if (buf == NULL) {
  3187. htmlErrMemory(ctxt, "buffer allocation failed\n");
  3188. ctxt->instate = state;
  3189. return;
  3190. }
  3191. len = 0;
  3192. buf[len] = 0;
  3193. q = CUR_CHAR(ql);
  3194. if (q == 0)
  3195. goto unfinished;
  3196. if (q == '>') {
  3197. htmlParseErr(ctxt, XML_ERR_COMMENT_ABRUPTLY_ENDED, "Comment abruptly ended", NULL, NULL);
  3198. cur = '>';
  3199. goto finished;
  3200. }
  3201. NEXTL(ql);
  3202. r = CUR_CHAR(rl);
  3203. if (r == 0)
  3204. goto unfinished;
  3205. if (q == '-' && r == '>') {
  3206. htmlParseErr(ctxt, XML_ERR_COMMENT_ABRUPTLY_ENDED, "Comment abruptly ended", NULL, NULL);
  3207. cur = '>';
  3208. goto finished;
  3209. }
  3210. NEXTL(rl);
  3211. cur = CUR_CHAR(l);
  3212. while ((cur != 0) &&
  3213. ((cur != '>') ||
  3214. (r != '-') || (q != '-'))) {
  3215. NEXTL(l);
  3216. next = CUR_CHAR(nl);
  3217. if ((q == '-') && (r == '-') && (cur == '!') && (next == '>')) {
  3218. htmlParseErr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
  3219. "Comment incorrectly closed by '--!>'", NULL, NULL);
  3220. cur = '>';
  3221. break;
  3222. }
  3223. if (len + 5 >= size) {
  3224. xmlChar *tmp;
  3225. size *= 2;
  3226. tmp = (xmlChar *) xmlRealloc(buf, size);
  3227. if (tmp == NULL) {
  3228. xmlFree(buf);
  3229. htmlErrMemory(ctxt, "growing buffer failed\n");
  3230. ctxt->instate = state;
  3231. return;
  3232. }
  3233. buf = tmp;
  3234. }
  3235. if (IS_CHAR(q)) {
  3236. COPY_BUF(ql,buf,len,q);
  3237. } else {
  3238. htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
  3239. "Invalid char in comment 0x%X\n", q);
  3240. }
  3241. if (len > maxLength) {
  3242. htmlParseErr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
  3243. "comment too long", NULL, NULL);
  3244. xmlFree(buf);
  3245. ctxt->instate = state;
  3246. return;
  3247. }
  3248. q = r;
  3249. ql = rl;
  3250. r = cur;
  3251. rl = l;
  3252. cur = next;
  3253. l = nl;
  3254. }
  3255. finished:
  3256. buf[len] = 0;
  3257. if (ctxt->instate == XML_PARSER_EOF) {
  3258. xmlFree(buf);
  3259. return;
  3260. }
  3261. if (cur == '>') {
  3262. NEXT;
  3263. if ((ctxt->sax != NULL) && (ctxt->sax->comment != NULL) &&
  3264. (!ctxt->disableSAX))
  3265. ctxt->sax->comment(ctxt->userData, buf);
  3266. xmlFree(buf);
  3267. ctxt->instate = state;
  3268. return;
  3269. }
  3270. unfinished:
  3271. htmlParseErr(ctxt, XML_ERR_COMMENT_NOT_FINISHED,
  3272. "Comment not terminated \n<!--%.50s\n", buf, NULL);
  3273. xmlFree(buf);
  3274. }
  3275. /**
  3276. * htmlParseCharRef:
  3277. * @ctxt: an HTML parser context
  3278. *
  3279. * DEPRECATED: Internal function, don't use.
  3280. *
  3281. * parse Reference declarations
  3282. *
  3283. * [66] CharRef ::= '&#' [0-9]+ ';' |
  3284. * '&#x' [0-9a-fA-F]+ ';'
  3285. *
  3286. * Returns the value parsed (as an int)
  3287. */
  3288. int
  3289. htmlParseCharRef(htmlParserCtxtPtr ctxt) {
  3290. int val = 0;
  3291. if ((ctxt == NULL) || (ctxt->input == NULL)) {
  3292. htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
  3293. "htmlParseCharRef: context error\n",
  3294. NULL, NULL);
  3295. return(0);
  3296. }
  3297. if ((CUR == '&') && (NXT(1) == '#') &&
  3298. ((NXT(2) == 'x') || NXT(2) == 'X')) {
  3299. SKIP(3);
  3300. while (CUR != ';') {
  3301. if ((CUR >= '0') && (CUR <= '9')) {
  3302. if (val < 0x110000)
  3303. val = val * 16 + (CUR - '0');
  3304. } else if ((CUR >= 'a') && (CUR <= 'f')) {
  3305. if (val < 0x110000)
  3306. val = val * 16 + (CUR - 'a') + 10;
  3307. } else if ((CUR >= 'A') && (CUR <= 'F')) {
  3308. if (val < 0x110000)
  3309. val = val * 16 + (CUR - 'A') + 10;
  3310. } else {
  3311. htmlParseErr(ctxt, XML_ERR_INVALID_HEX_CHARREF,
  3312. "htmlParseCharRef: missing semicolon\n",
  3313. NULL, NULL);
  3314. break;
  3315. }
  3316. NEXT;
  3317. }
  3318. if (CUR == ';')
  3319. NEXT;
  3320. } else if ((CUR == '&') && (NXT(1) == '#')) {
  3321. SKIP(2);
  3322. while (CUR != ';') {
  3323. if ((CUR >= '0') && (CUR <= '9')) {
  3324. if (val < 0x110000)
  3325. val = val * 10 + (CUR - '0');
  3326. } else {
  3327. htmlParseErr(ctxt, XML_ERR_INVALID_DEC_CHARREF,
  3328. "htmlParseCharRef: missing semicolon\n",
  3329. NULL, NULL);
  3330. break;
  3331. }
  3332. NEXT;
  3333. }
  3334. if (CUR == ';')
  3335. NEXT;
  3336. } else {
  3337. htmlParseErr(ctxt, XML_ERR_INVALID_CHARREF,
  3338. "htmlParseCharRef: invalid value\n", NULL, NULL);
  3339. }
  3340. /*
  3341. * Check the value IS_CHAR ...
  3342. */
  3343. if (IS_CHAR(val)) {
  3344. return(val);
  3345. } else if (val >= 0x110000) {
  3346. htmlParseErr(ctxt, XML_ERR_INVALID_CHAR,
  3347. "htmlParseCharRef: value too large\n", NULL, NULL);
  3348. } else {
  3349. htmlParseErrInt(ctxt, XML_ERR_INVALID_CHAR,
  3350. "htmlParseCharRef: invalid xmlChar value %d\n",
  3351. val);
  3352. }
  3353. return(0);
  3354. }
  3355. /**
  3356. * htmlParseDocTypeDecl:
  3357. * @ctxt: an HTML parser context
  3358. *
  3359. * parse a DOCTYPE declaration
  3360. *
  3361. * [28] doctypedecl ::= '<!DOCTYPE' S Name (S ExternalID)? S?
  3362. * ('[' (markupdecl | PEReference | S)* ']' S?)? '>'
  3363. */
  3364. static void
  3365. htmlParseDocTypeDecl(htmlParserCtxtPtr ctxt) {
  3366. const xmlChar *name;
  3367. xmlChar *ExternalID = NULL;
  3368. xmlChar *URI = NULL;
  3369. /*
  3370. * We know that '<!DOCTYPE' has been detected.
  3371. */
  3372. SKIP(9);
  3373. SKIP_BLANKS;
  3374. /*
  3375. * Parse the DOCTYPE name.
  3376. */
  3377. name = htmlParseName(ctxt);
  3378. if (name == NULL) {
  3379. htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
  3380. "htmlParseDocTypeDecl : no DOCTYPE name !\n",
  3381. NULL, NULL);
  3382. }
  3383. /*
  3384. * Check that upper(name) == "HTML" !!!!!!!!!!!!!
  3385. */
  3386. SKIP_BLANKS;
  3387. /*
  3388. * Check for SystemID and ExternalID
  3389. */
  3390. URI = htmlParseExternalID(ctxt, &ExternalID);
  3391. SKIP_BLANKS;
  3392. /*
  3393. * We should be at the end of the DOCTYPE declaration.
  3394. */
  3395. if (CUR != '>') {
  3396. htmlParseErr(ctxt, XML_ERR_DOCTYPE_NOT_FINISHED,
  3397. "DOCTYPE improperly terminated\n", NULL, NULL);
  3398. /* Ignore bogus content */
  3399. while ((CUR != 0) && (CUR != '>') &&
  3400. (ctxt->instate != XML_PARSER_EOF))
  3401. NEXT;
  3402. }
  3403. if (CUR == '>')
  3404. NEXT;
  3405. /*
  3406. * Create or update the document accordingly to the DOCTYPE
  3407. */
  3408. if ((ctxt->sax != NULL) && (ctxt->sax->internalSubset != NULL) &&
  3409. (!ctxt->disableSAX))
  3410. ctxt->sax->internalSubset(ctxt->userData, name, ExternalID, URI);
  3411. /*
  3412. * Cleanup, since we don't use all those identifiers
  3413. */
  3414. if (URI != NULL) xmlFree(URI);
  3415. if (ExternalID != NULL) xmlFree(ExternalID);
  3416. }
  3417. /**
  3418. * htmlParseAttribute:
  3419. * @ctxt: an HTML parser context
  3420. * @value: a xmlChar ** used to store the value of the attribute
  3421. *
  3422. * parse an attribute
  3423. *
  3424. * [41] Attribute ::= Name Eq AttValue
  3425. *
  3426. * [25] Eq ::= S? '=' S?
  3427. *
  3428. * With namespace:
  3429. *
  3430. * [NS 11] Attribute ::= QName Eq AttValue
  3431. *
  3432. * Also the case QName == xmlns:??? is handled independently as a namespace
  3433. * definition.
  3434. *
  3435. * Returns the attribute name, and the value in *value.
  3436. */
  3437. static const xmlChar *
  3438. htmlParseAttribute(htmlParserCtxtPtr ctxt, xmlChar **value) {
  3439. const xmlChar *name;
  3440. xmlChar *val = NULL;
  3441. *value = NULL;
  3442. name = htmlParseHTMLName(ctxt);
  3443. if (name == NULL) {
  3444. htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
  3445. "error parsing attribute name\n", NULL, NULL);
  3446. return(NULL);
  3447. }
  3448. /*
  3449. * read the value
  3450. */
  3451. SKIP_BLANKS;
  3452. if (CUR == '=') {
  3453. NEXT;
  3454. SKIP_BLANKS;
  3455. val = htmlParseAttValue(ctxt);
  3456. }
  3457. *value = val;
  3458. return(name);
  3459. }
  3460. /**
  3461. * htmlCheckEncoding:
  3462. * @ctxt: an HTML parser context
  3463. * @attvalue: the attribute value
  3464. *
  3465. * Checks an http-equiv attribute from a Meta tag to detect
  3466. * the encoding
  3467. * If a new encoding is detected the parser is switched to decode
  3468. * it and pass UTF8
  3469. */
  3470. static void
  3471. htmlCheckEncoding(htmlParserCtxtPtr ctxt, const xmlChar *attvalue) {
  3472. const xmlChar *encoding;
  3473. if (!attvalue)
  3474. return;
  3475. encoding = xmlStrcasestr(attvalue, BAD_CAST"charset");
  3476. if (encoding != NULL) {
  3477. encoding += 7;
  3478. }
  3479. /*
  3480. * skip blank
  3481. */
  3482. if (encoding && IS_BLANK_CH(*encoding))
  3483. encoding = xmlStrcasestr(attvalue, BAD_CAST"=");
  3484. if (encoding && *encoding == '=') {
  3485. encoding ++;
  3486. xmlSetDeclaredEncoding(ctxt, xmlStrdup(encoding));
  3487. }
  3488. }
  3489. /**
  3490. * htmlCheckMeta:
  3491. * @ctxt: an HTML parser context
  3492. * @atts: the attributes values
  3493. *
  3494. * Checks an attributes from a Meta tag
  3495. */
  3496. static void
  3497. htmlCheckMeta(htmlParserCtxtPtr ctxt, const xmlChar **atts) {
  3498. int i;
  3499. const xmlChar *att, *value;
  3500. int http = 0;
  3501. const xmlChar *content = NULL;
  3502. if ((ctxt == NULL) || (atts == NULL))
  3503. return;
  3504. i = 0;
  3505. att = atts[i++];
  3506. while (att != NULL) {
  3507. value = atts[i++];
  3508. if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"http-equiv"))
  3509. && (!xmlStrcasecmp(value, BAD_CAST"Content-Type")))
  3510. http = 1;
  3511. else if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"charset")))
  3512. xmlSetDeclaredEncoding(ctxt, xmlStrdup(value));
  3513. else if ((value != NULL) && (!xmlStrcasecmp(att, BAD_CAST"content")))
  3514. content = value;
  3515. att = atts[i++];
  3516. }
  3517. if ((http) && (content != NULL))
  3518. htmlCheckEncoding(ctxt, content);
  3519. }
  3520. /**
  3521. * htmlParseStartTag:
  3522. * @ctxt: an HTML parser context
  3523. *
  3524. * parse a start of tag either for rule element or
  3525. * EmptyElement. In both case we don't parse the tag closing chars.
  3526. *
  3527. * [40] STag ::= '<' Name (S Attribute)* S? '>'
  3528. *
  3529. * [44] EmptyElemTag ::= '<' Name (S Attribute)* S? '/>'
  3530. *
  3531. * With namespace:
  3532. *
  3533. * [NS 8] STag ::= '<' QName (S Attribute)* S? '>'
  3534. *
  3535. * [NS 10] EmptyElement ::= '<' QName (S Attribute)* S? '/>'
  3536. *
  3537. * Returns 0 in case of success, -1 in case of error and 1 if discarded
  3538. */
  3539. static int
  3540. htmlParseStartTag(htmlParserCtxtPtr ctxt) {
  3541. const xmlChar *name;
  3542. const xmlChar *attname;
  3543. xmlChar *attvalue;
  3544. const xmlChar **atts;
  3545. int nbatts = 0;
  3546. int maxatts;
  3547. int meta = 0;
  3548. int i;
  3549. int discardtag = 0;
  3550. if ((ctxt == NULL) || (ctxt->input == NULL)) {
  3551. htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
  3552. "htmlParseStartTag: context error\n", NULL, NULL);
  3553. return -1;
  3554. }
  3555. if (ctxt->instate == XML_PARSER_EOF)
  3556. return(-1);
  3557. if (CUR != '<') return -1;
  3558. NEXT;
  3559. atts = ctxt->atts;
  3560. maxatts = ctxt->maxatts;
  3561. GROW;
  3562. name = htmlParseHTMLName(ctxt);
  3563. if (name == NULL) {
  3564. htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
  3565. "htmlParseStartTag: invalid element name\n",
  3566. NULL, NULL);
  3567. /* Dump the bogus tag like browsers do */
  3568. while ((CUR != 0) && (CUR != '>') &&
  3569. (ctxt->instate != XML_PARSER_EOF))
  3570. NEXT;
  3571. return -1;
  3572. }
  3573. if (xmlStrEqual(name, BAD_CAST"meta"))
  3574. meta = 1;
  3575. /*
  3576. * Check for auto-closure of HTML elements.
  3577. */
  3578. htmlAutoClose(ctxt, name);
  3579. /*
  3580. * Check for implied HTML elements.
  3581. */
  3582. htmlCheckImplied(ctxt, name);
  3583. /*
  3584. * Avoid html at any level > 0, head at any level != 1
  3585. * or any attempt to recurse body
  3586. */
  3587. if ((ctxt->nameNr > 0) && (xmlStrEqual(name, BAD_CAST"html"))) {
  3588. htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
  3589. "htmlParseStartTag: misplaced <html> tag\n",
  3590. name, NULL);
  3591. discardtag = 1;
  3592. ctxt->depth++;
  3593. }
  3594. if ((ctxt->nameNr != 1) &&
  3595. (xmlStrEqual(name, BAD_CAST"head"))) {
  3596. htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
  3597. "htmlParseStartTag: misplaced <head> tag\n",
  3598. name, NULL);
  3599. discardtag = 1;
  3600. ctxt->depth++;
  3601. }
  3602. if (xmlStrEqual(name, BAD_CAST"body")) {
  3603. int indx;
  3604. for (indx = 0;indx < ctxt->nameNr;indx++) {
  3605. if (xmlStrEqual(ctxt->nameTab[indx], BAD_CAST"body")) {
  3606. htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
  3607. "htmlParseStartTag: misplaced <body> tag\n",
  3608. name, NULL);
  3609. discardtag = 1;
  3610. ctxt->depth++;
  3611. }
  3612. }
  3613. }
  3614. /*
  3615. * Now parse the attributes, it ends up with the ending
  3616. *
  3617. * (S Attribute)* S?
  3618. */
  3619. SKIP_BLANKS;
  3620. while ((CUR != 0) &&
  3621. (CUR != '>') &&
  3622. ((CUR != '/') || (NXT(1) != '>')) &&
  3623. (ctxt->instate != XML_PARSER_EOF)) {
  3624. GROW;
  3625. attname = htmlParseAttribute(ctxt, &attvalue);
  3626. if (attname != NULL) {
  3627. /*
  3628. * Well formedness requires at most one declaration of an attribute
  3629. */
  3630. for (i = 0; i < nbatts;i += 2) {
  3631. if (xmlStrEqual(atts[i], attname)) {
  3632. htmlParseErr(ctxt, XML_ERR_ATTRIBUTE_REDEFINED,
  3633. "Attribute %s redefined\n", attname, NULL);
  3634. if (attvalue != NULL)
  3635. xmlFree(attvalue);
  3636. goto failed;
  3637. }
  3638. }
  3639. /*
  3640. * Add the pair to atts
  3641. */
  3642. if (atts == NULL) {
  3643. maxatts = 22; /* allow for 10 attrs by default */
  3644. atts = (const xmlChar **)
  3645. xmlMalloc(maxatts * sizeof(xmlChar *));
  3646. if (atts == NULL) {
  3647. htmlErrMemory(ctxt, NULL);
  3648. if (attvalue != NULL)
  3649. xmlFree(attvalue);
  3650. goto failed;
  3651. }
  3652. ctxt->atts = atts;
  3653. ctxt->maxatts = maxatts;
  3654. } else if (nbatts + 4 > maxatts) {
  3655. const xmlChar **n;
  3656. maxatts *= 2;
  3657. n = (const xmlChar **) xmlRealloc((void *) atts,
  3658. maxatts * sizeof(const xmlChar *));
  3659. if (n == NULL) {
  3660. htmlErrMemory(ctxt, NULL);
  3661. if (attvalue != NULL)
  3662. xmlFree(attvalue);
  3663. goto failed;
  3664. }
  3665. atts = n;
  3666. ctxt->atts = atts;
  3667. ctxt->maxatts = maxatts;
  3668. }
  3669. atts[nbatts++] = attname;
  3670. atts[nbatts++] = attvalue;
  3671. atts[nbatts] = NULL;
  3672. atts[nbatts + 1] = NULL;
  3673. }
  3674. else {
  3675. if (attvalue != NULL)
  3676. xmlFree(attvalue);
  3677. /* Dump the bogus attribute string up to the next blank or
  3678. * the end of the tag. */
  3679. while ((CUR != 0) &&
  3680. !(IS_BLANK_CH(CUR)) && (CUR != '>') &&
  3681. ((CUR != '/') || (NXT(1) != '>')) &&
  3682. (ctxt->instate != XML_PARSER_EOF))
  3683. NEXT;
  3684. }
  3685. failed:
  3686. SKIP_BLANKS;
  3687. }
  3688. /*
  3689. * Handle specific association to the META tag
  3690. */
  3691. if (meta && (nbatts != 0))
  3692. htmlCheckMeta(ctxt, atts);
  3693. /*
  3694. * SAX: Start of Element !
  3695. */
  3696. if (!discardtag) {
  3697. htmlnamePush(ctxt, name);
  3698. if ((ctxt->sax != NULL) && (ctxt->sax->startElement != NULL)) {
  3699. if (nbatts != 0)
  3700. ctxt->sax->startElement(ctxt->userData, name, atts);
  3701. else
  3702. ctxt->sax->startElement(ctxt->userData, name, NULL);
  3703. }
  3704. }
  3705. if (atts != NULL) {
  3706. for (i = 1;i < nbatts;i += 2) {
  3707. if (atts[i] != NULL)
  3708. xmlFree((xmlChar *) atts[i]);
  3709. }
  3710. }
  3711. return(discardtag);
  3712. }
  3713. /**
  3714. * htmlParseEndTag:
  3715. * @ctxt: an HTML parser context
  3716. *
  3717. * parse an end of tag
  3718. *
  3719. * [42] ETag ::= '</' Name S? '>'
  3720. *
  3721. * With namespace
  3722. *
  3723. * [NS 9] ETag ::= '</' QName S? '>'
  3724. *
  3725. * Returns 1 if the current level should be closed.
  3726. */
  3727. static int
  3728. htmlParseEndTag(htmlParserCtxtPtr ctxt)
  3729. {
  3730. const xmlChar *name;
  3731. const xmlChar *oldname;
  3732. int i, ret;
  3733. if ((CUR != '<') || (NXT(1) != '/')) {
  3734. htmlParseErr(ctxt, XML_ERR_LTSLASH_REQUIRED,
  3735. "htmlParseEndTag: '</' not found\n", NULL, NULL);
  3736. return (0);
  3737. }
  3738. SKIP(2);
  3739. name = htmlParseHTMLName(ctxt);
  3740. if (name == NULL)
  3741. return (0);
  3742. /*
  3743. * We should definitely be at the ending "S? '>'" part
  3744. */
  3745. SKIP_BLANKS;
  3746. if (CUR != '>') {
  3747. htmlParseErr(ctxt, XML_ERR_GT_REQUIRED,
  3748. "End tag : expected '>'\n", NULL, NULL);
  3749. /* Skip to next '>' */
  3750. while ((CUR != 0) && (CUR != '>'))
  3751. NEXT;
  3752. }
  3753. if (CUR == '>')
  3754. NEXT;
  3755. /*
  3756. * if we ignored misplaced tags in htmlParseStartTag don't pop them
  3757. * out now.
  3758. */
  3759. if ((ctxt->depth > 0) &&
  3760. (xmlStrEqual(name, BAD_CAST "html") ||
  3761. xmlStrEqual(name, BAD_CAST "body") ||
  3762. xmlStrEqual(name, BAD_CAST "head"))) {
  3763. ctxt->depth--;
  3764. return (0);
  3765. }
  3766. /*
  3767. * If the name read is not one of the element in the parsing stack
  3768. * then return, it's just an error.
  3769. */
  3770. for (i = (ctxt->nameNr - 1); i >= 0; i--) {
  3771. if (xmlStrEqual(name, ctxt->nameTab[i]))
  3772. break;
  3773. }
  3774. if (i < 0) {
  3775. htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
  3776. "Unexpected end tag : %s\n", name, NULL);
  3777. return (0);
  3778. }
  3779. /*
  3780. * Check for auto-closure of HTML elements.
  3781. */
  3782. htmlAutoCloseOnClose(ctxt, name);
  3783. /*
  3784. * Well formedness constraints, opening and closing must match.
  3785. * With the exception that the autoclose may have popped stuff out
  3786. * of the stack.
  3787. */
  3788. if ((ctxt->name != NULL) && (!xmlStrEqual(ctxt->name, name))) {
  3789. htmlParseErr(ctxt, XML_ERR_TAG_NAME_MISMATCH,
  3790. "Opening and ending tag mismatch: %s and %s\n",
  3791. name, ctxt->name);
  3792. }
  3793. /*
  3794. * SAX: End of Tag
  3795. */
  3796. oldname = ctxt->name;
  3797. if ((oldname != NULL) && (xmlStrEqual(oldname, name))) {
  3798. if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
  3799. ctxt->sax->endElement(ctxt->userData, name);
  3800. htmlNodeInfoPop(ctxt);
  3801. htmlnamePop(ctxt);
  3802. ret = 1;
  3803. } else {
  3804. ret = 0;
  3805. }
  3806. return (ret);
  3807. }
  3808. /**
  3809. * htmlParseReference:
  3810. * @ctxt: an HTML parser context
  3811. *
  3812. * parse and handle entity references in content,
  3813. * this will end-up in a call to character() since this is either a
  3814. * CharRef, or a predefined entity.
  3815. */
  3816. static void
  3817. htmlParseReference(htmlParserCtxtPtr ctxt) {
  3818. const htmlEntityDesc * ent;
  3819. xmlChar out[6];
  3820. const xmlChar *name;
  3821. if (CUR != '&') return;
  3822. if (NXT(1) == '#') {
  3823. unsigned int c;
  3824. int bits, i = 0;
  3825. c = htmlParseCharRef(ctxt);
  3826. if (c == 0)
  3827. return;
  3828. if (c < 0x80) { out[i++]= c; bits= -6; }
  3829. else if (c < 0x800) { out[i++]=((c >> 6) & 0x1F) | 0xC0; bits= 0; }
  3830. else if (c < 0x10000) { out[i++]=((c >> 12) & 0x0F) | 0xE0; bits= 6; }
  3831. else { out[i++]=((c >> 18) & 0x07) | 0xF0; bits= 12; }
  3832. for ( ; bits >= 0; bits-= 6) {
  3833. out[i++]= ((c >> bits) & 0x3F) | 0x80;
  3834. }
  3835. out[i] = 0;
  3836. htmlCheckParagraph(ctxt);
  3837. if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
  3838. ctxt->sax->characters(ctxt->userData, out, i);
  3839. } else {
  3840. ent = htmlParseEntityRef(ctxt, &name);
  3841. if (name == NULL) {
  3842. htmlCheckParagraph(ctxt);
  3843. if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
  3844. ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
  3845. return;
  3846. }
  3847. if ((ent == NULL) || !(ent->value > 0)) {
  3848. htmlCheckParagraph(ctxt);
  3849. if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL)) {
  3850. ctxt->sax->characters(ctxt->userData, BAD_CAST "&", 1);
  3851. ctxt->sax->characters(ctxt->userData, name, xmlStrlen(name));
  3852. /* ctxt->sax->characters(ctxt->userData, BAD_CAST ";", 1); */
  3853. }
  3854. } else {
  3855. unsigned int c;
  3856. int bits, i = 0;
  3857. c = ent->value;
  3858. if (c < 0x80)
  3859. { out[i++]= c; bits= -6; }
  3860. else if (c < 0x800)
  3861. { out[i++]=((c >> 6) & 0x1F) | 0xC0; bits= 0; }
  3862. else if (c < 0x10000)
  3863. { out[i++]=((c >> 12) & 0x0F) | 0xE0; bits= 6; }
  3864. else
  3865. { out[i++]=((c >> 18) & 0x07) | 0xF0; bits= 12; }
  3866. for ( ; bits >= 0; bits-= 6) {
  3867. out[i++]= ((c >> bits) & 0x3F) | 0x80;
  3868. }
  3869. out[i] = 0;
  3870. htmlCheckParagraph(ctxt);
  3871. if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
  3872. ctxt->sax->characters(ctxt->userData, out, i);
  3873. }
  3874. }
  3875. }
  3876. /**
  3877. * htmlParseContent:
  3878. * @ctxt: an HTML parser context
  3879. *
  3880. * Parse a content: comment, sub-element, reference or text.
  3881. * Kept for compatibility with old code
  3882. */
  3883. static void
  3884. htmlParseContent(htmlParserCtxtPtr ctxt) {
  3885. xmlChar *currentNode;
  3886. int depth;
  3887. const xmlChar *name;
  3888. currentNode = xmlStrdup(ctxt->name);
  3889. depth = ctxt->nameNr;
  3890. while (1) {
  3891. GROW;
  3892. if (ctxt->instate == XML_PARSER_EOF)
  3893. break;
  3894. /*
  3895. * Our tag or one of it's parent or children is ending.
  3896. */
  3897. if ((CUR == '<') && (NXT(1) == '/')) {
  3898. if (htmlParseEndTag(ctxt) &&
  3899. ((currentNode != NULL) || (ctxt->nameNr == 0))) {
  3900. if (currentNode != NULL)
  3901. xmlFree(currentNode);
  3902. return;
  3903. }
  3904. continue; /* while */
  3905. }
  3906. else if ((CUR == '<') &&
  3907. ((IS_ASCII_LETTER(NXT(1))) ||
  3908. (NXT(1) == '_') || (NXT(1) == ':'))) {
  3909. name = htmlParseHTMLName_nonInvasive(ctxt);
  3910. if (name == NULL) {
  3911. htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
  3912. "htmlParseStartTag: invalid element name\n",
  3913. NULL, NULL);
  3914. /* Dump the bogus tag like browsers do */
  3915. while ((CUR != 0) && (CUR != '>'))
  3916. NEXT;
  3917. if (currentNode != NULL)
  3918. xmlFree(currentNode);
  3919. return;
  3920. }
  3921. if (ctxt->name != NULL) {
  3922. if (htmlCheckAutoClose(name, ctxt->name) == 1) {
  3923. htmlAutoClose(ctxt, name);
  3924. continue;
  3925. }
  3926. }
  3927. }
  3928. /*
  3929. * Has this node been popped out during parsing of
  3930. * the next element
  3931. */
  3932. if ((ctxt->nameNr > 0) && (depth >= ctxt->nameNr) &&
  3933. (!xmlStrEqual(currentNode, ctxt->name)))
  3934. {
  3935. if (currentNode != NULL) xmlFree(currentNode);
  3936. return;
  3937. }
  3938. if ((CUR != 0) && ((xmlStrEqual(currentNode, BAD_CAST"script")) ||
  3939. (xmlStrEqual(currentNode, BAD_CAST"style")))) {
  3940. /*
  3941. * Handle SCRIPT/STYLE separately
  3942. */
  3943. htmlParseScript(ctxt);
  3944. }
  3945. else if ((CUR == '<') && (NXT(1) == '!')) {
  3946. /*
  3947. * Sometimes DOCTYPE arrives in the middle of the document
  3948. */
  3949. if ((UPP(2) == 'D') && (UPP(3) == 'O') &&
  3950. (UPP(4) == 'C') && (UPP(5) == 'T') &&
  3951. (UPP(6) == 'Y') && (UPP(7) == 'P') &&
  3952. (UPP(8) == 'E')) {
  3953. htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
  3954. "Misplaced DOCTYPE declaration\n",
  3955. BAD_CAST "DOCTYPE" , NULL);
  3956. htmlParseDocTypeDecl(ctxt);
  3957. }
  3958. /*
  3959. * First case : a comment
  3960. */
  3961. else if ((NXT(2) == '-') && (NXT(3) == '-')) {
  3962. htmlParseComment(ctxt);
  3963. }
  3964. else {
  3965. htmlSkipBogusComment(ctxt);
  3966. }
  3967. }
  3968. /*
  3969. * Second case : a Processing Instruction.
  3970. */
  3971. else if ((CUR == '<') && (NXT(1) == '?')) {
  3972. htmlParsePI(ctxt);
  3973. }
  3974. /*
  3975. * Third case : a sub-element.
  3976. */
  3977. else if ((CUR == '<') && IS_ASCII_LETTER(NXT(1))) {
  3978. htmlParseElement(ctxt);
  3979. }
  3980. else if (CUR == '<') {
  3981. if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
  3982. (ctxt->sax->characters != NULL))
  3983. ctxt->sax->characters(ctxt->userData, BAD_CAST "<", 1);
  3984. NEXT;
  3985. }
  3986. /*
  3987. * Fourth case : a reference. If if has not been resolved,
  3988. * parsing returns it's Name, create the node
  3989. */
  3990. else if (CUR == '&') {
  3991. htmlParseReference(ctxt);
  3992. }
  3993. /*
  3994. * Fifth case : end of the resource
  3995. */
  3996. else if (CUR == 0) {
  3997. htmlAutoCloseOnEnd(ctxt);
  3998. break;
  3999. }
  4000. /*
  4001. * Last case, text. Note that References are handled directly.
  4002. */
  4003. else {
  4004. htmlParseCharData(ctxt);
  4005. }
  4006. SHRINK;
  4007. GROW;
  4008. }
  4009. if (currentNode != NULL) xmlFree(currentNode);
  4010. }
  4011. /**
  4012. * htmlParseElement:
  4013. * @ctxt: an HTML parser context
  4014. *
  4015. * DEPRECATED: Internal function, don't use.
  4016. *
  4017. * parse an HTML element, this is highly recursive
  4018. * this is kept for compatibility with previous code versions
  4019. *
  4020. * [39] element ::= EmptyElemTag | STag content ETag
  4021. *
  4022. * [41] Attribute ::= Name Eq AttValue
  4023. */
  4024. void
  4025. htmlParseElement(htmlParserCtxtPtr ctxt) {
  4026. const xmlChar *name;
  4027. xmlChar *currentNode = NULL;
  4028. const htmlElemDesc * info;
  4029. htmlParserNodeInfo node_info;
  4030. int failed;
  4031. int depth;
  4032. const xmlChar *oldptr;
  4033. if ((ctxt == NULL) || (ctxt->input == NULL)) {
  4034. htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
  4035. "htmlParseElement: context error\n", NULL, NULL);
  4036. return;
  4037. }
  4038. if (ctxt->instate == XML_PARSER_EOF)
  4039. return;
  4040. /* Capture start position */
  4041. if (ctxt->record_info) {
  4042. node_info.begin_pos = ctxt->input->consumed +
  4043. (CUR_PTR - ctxt->input->base);
  4044. node_info.begin_line = ctxt->input->line;
  4045. }
  4046. failed = htmlParseStartTag(ctxt);
  4047. name = ctxt->name;
  4048. if ((failed == -1) || (name == NULL)) {
  4049. if (CUR == '>')
  4050. NEXT;
  4051. return;
  4052. }
  4053. /*
  4054. * Lookup the info for that element.
  4055. */
  4056. info = htmlTagLookup(name);
  4057. if (info == NULL) {
  4058. htmlParseErr(ctxt, XML_HTML_UNKNOWN_TAG,
  4059. "Tag %s invalid\n", name, NULL);
  4060. }
  4061. /*
  4062. * Check for an Empty Element labeled the XML/SGML way
  4063. */
  4064. if ((CUR == '/') && (NXT(1) == '>')) {
  4065. SKIP(2);
  4066. if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
  4067. ctxt->sax->endElement(ctxt->userData, name);
  4068. htmlnamePop(ctxt);
  4069. return;
  4070. }
  4071. if (CUR == '>') {
  4072. NEXT;
  4073. } else {
  4074. htmlParseErr(ctxt, XML_ERR_GT_REQUIRED,
  4075. "Couldn't find end of Start Tag %s\n", name, NULL);
  4076. /*
  4077. * end of parsing of this node.
  4078. */
  4079. if (xmlStrEqual(name, ctxt->name)) {
  4080. nodePop(ctxt);
  4081. htmlnamePop(ctxt);
  4082. }
  4083. /*
  4084. * Capture end position and add node
  4085. */
  4086. if (ctxt->record_info) {
  4087. node_info.end_pos = ctxt->input->consumed +
  4088. (CUR_PTR - ctxt->input->base);
  4089. node_info.end_line = ctxt->input->line;
  4090. node_info.node = ctxt->node;
  4091. xmlParserAddNodeInfo(ctxt, &node_info);
  4092. }
  4093. return;
  4094. }
  4095. /*
  4096. * Check for an Empty Element from DTD definition
  4097. */
  4098. if ((info != NULL) && (info->empty)) {
  4099. if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
  4100. ctxt->sax->endElement(ctxt->userData, name);
  4101. htmlnamePop(ctxt);
  4102. return;
  4103. }
  4104. /*
  4105. * Parse the content of the element:
  4106. */
  4107. currentNode = xmlStrdup(ctxt->name);
  4108. depth = ctxt->nameNr;
  4109. while (CUR != 0) {
  4110. oldptr = ctxt->input->cur;
  4111. htmlParseContent(ctxt);
  4112. if (oldptr==ctxt->input->cur) break;
  4113. if (ctxt->nameNr < depth) break;
  4114. }
  4115. /*
  4116. * Capture end position and add node
  4117. */
  4118. if ( currentNode != NULL && ctxt->record_info ) {
  4119. node_info.end_pos = ctxt->input->consumed +
  4120. (CUR_PTR - ctxt->input->base);
  4121. node_info.end_line = ctxt->input->line;
  4122. node_info.node = ctxt->node;
  4123. xmlParserAddNodeInfo(ctxt, &node_info);
  4124. }
  4125. if (CUR == 0) {
  4126. htmlAutoCloseOnEnd(ctxt);
  4127. }
  4128. if (currentNode != NULL)
  4129. xmlFree(currentNode);
  4130. }
  4131. static void
  4132. htmlParserFinishElementParsing(htmlParserCtxtPtr ctxt) {
  4133. /*
  4134. * Capture end position and add node
  4135. */
  4136. if ( ctxt->node != NULL && ctxt->record_info ) {
  4137. ctxt->nodeInfo->end_pos = ctxt->input->consumed +
  4138. (CUR_PTR - ctxt->input->base);
  4139. ctxt->nodeInfo->end_line = ctxt->input->line;
  4140. ctxt->nodeInfo->node = ctxt->node;
  4141. xmlParserAddNodeInfo(ctxt, ctxt->nodeInfo);
  4142. htmlNodeInfoPop(ctxt);
  4143. }
  4144. if (CUR == 0) {
  4145. htmlAutoCloseOnEnd(ctxt);
  4146. }
  4147. }
  4148. /**
  4149. * htmlParseElementInternal:
  4150. * @ctxt: an HTML parser context
  4151. *
  4152. * parse an HTML element, new version, non recursive
  4153. *
  4154. * [39] element ::= EmptyElemTag | STag content ETag
  4155. *
  4156. * [41] Attribute ::= Name Eq AttValue
  4157. */
  4158. static void
  4159. htmlParseElementInternal(htmlParserCtxtPtr ctxt) {
  4160. const xmlChar *name;
  4161. const htmlElemDesc * info;
  4162. htmlParserNodeInfo node_info = { NULL, 0, 0, 0, 0 };
  4163. int failed;
  4164. if ((ctxt == NULL) || (ctxt->input == NULL)) {
  4165. htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
  4166. "htmlParseElementInternal: context error\n", NULL, NULL);
  4167. return;
  4168. }
  4169. if (ctxt->instate == XML_PARSER_EOF)
  4170. return;
  4171. /* Capture start position */
  4172. if (ctxt->record_info) {
  4173. node_info.begin_pos = ctxt->input->consumed +
  4174. (CUR_PTR - ctxt->input->base);
  4175. node_info.begin_line = ctxt->input->line;
  4176. }
  4177. failed = htmlParseStartTag(ctxt);
  4178. name = ctxt->name;
  4179. if ((failed == -1) || (name == NULL)) {
  4180. if (CUR == '>')
  4181. NEXT;
  4182. return;
  4183. }
  4184. /*
  4185. * Lookup the info for that element.
  4186. */
  4187. info = htmlTagLookup(name);
  4188. if (info == NULL) {
  4189. htmlParseErr(ctxt, XML_HTML_UNKNOWN_TAG,
  4190. "Tag %s invalid\n", name, NULL);
  4191. }
  4192. /*
  4193. * Check for an Empty Element labeled the XML/SGML way
  4194. */
  4195. if ((CUR == '/') && (NXT(1) == '>')) {
  4196. SKIP(2);
  4197. if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
  4198. ctxt->sax->endElement(ctxt->userData, name);
  4199. htmlnamePop(ctxt);
  4200. return;
  4201. }
  4202. if (CUR == '>') {
  4203. NEXT;
  4204. } else {
  4205. htmlParseErr(ctxt, XML_ERR_GT_REQUIRED,
  4206. "Couldn't find end of Start Tag %s\n", name, NULL);
  4207. /*
  4208. * end of parsing of this node.
  4209. */
  4210. if (xmlStrEqual(name, ctxt->name)) {
  4211. nodePop(ctxt);
  4212. htmlnamePop(ctxt);
  4213. }
  4214. if (ctxt->record_info)
  4215. htmlNodeInfoPush(ctxt, &node_info);
  4216. htmlParserFinishElementParsing(ctxt);
  4217. return;
  4218. }
  4219. /*
  4220. * Check for an Empty Element from DTD definition
  4221. */
  4222. if ((info != NULL) && (info->empty)) {
  4223. if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
  4224. ctxt->sax->endElement(ctxt->userData, name);
  4225. htmlnamePop(ctxt);
  4226. return;
  4227. }
  4228. if (ctxt->record_info)
  4229. htmlNodeInfoPush(ctxt, &node_info);
  4230. }
  4231. /**
  4232. * htmlParseContentInternal:
  4233. * @ctxt: an HTML parser context
  4234. *
  4235. * Parse a content: comment, sub-element, reference or text.
  4236. * New version for non recursive htmlParseElementInternal
  4237. */
  4238. static void
  4239. htmlParseContentInternal(htmlParserCtxtPtr ctxt) {
  4240. xmlChar *currentNode;
  4241. int depth;
  4242. const xmlChar *name;
  4243. depth = ctxt->nameNr;
  4244. if (depth <= 0) {
  4245. currentNode = NULL;
  4246. } else {
  4247. currentNode = xmlStrdup(ctxt->name);
  4248. if (currentNode == NULL) {
  4249. htmlErrMemory(ctxt, NULL);
  4250. return;
  4251. }
  4252. }
  4253. while (1) {
  4254. GROW;
  4255. if (ctxt->instate == XML_PARSER_EOF)
  4256. break;
  4257. /*
  4258. * Our tag or one of it's parent or children is ending.
  4259. */
  4260. if ((CUR == '<') && (NXT(1) == '/')) {
  4261. if (htmlParseEndTag(ctxt) &&
  4262. ((currentNode != NULL) || (ctxt->nameNr == 0))) {
  4263. if (currentNode != NULL)
  4264. xmlFree(currentNode);
  4265. depth = ctxt->nameNr;
  4266. if (depth <= 0) {
  4267. currentNode = NULL;
  4268. } else {
  4269. currentNode = xmlStrdup(ctxt->name);
  4270. if (currentNode == NULL) {
  4271. htmlErrMemory(ctxt, NULL);
  4272. break;
  4273. }
  4274. }
  4275. }
  4276. continue; /* while */
  4277. }
  4278. else if ((CUR == '<') &&
  4279. ((IS_ASCII_LETTER(NXT(1))) ||
  4280. (NXT(1) == '_') || (NXT(1) == ':'))) {
  4281. name = htmlParseHTMLName_nonInvasive(ctxt);
  4282. if (name == NULL) {
  4283. htmlParseErr(ctxt, XML_ERR_NAME_REQUIRED,
  4284. "htmlParseStartTag: invalid element name\n",
  4285. NULL, NULL);
  4286. /* Dump the bogus tag like browsers do */
  4287. while ((CUR == 0) && (CUR != '>'))
  4288. NEXT;
  4289. htmlParserFinishElementParsing(ctxt);
  4290. if (currentNode != NULL)
  4291. xmlFree(currentNode);
  4292. currentNode = xmlStrdup(ctxt->name);
  4293. if (currentNode == NULL) {
  4294. htmlErrMemory(ctxt, NULL);
  4295. break;
  4296. }
  4297. depth = ctxt->nameNr;
  4298. continue;
  4299. }
  4300. if (ctxt->name != NULL) {
  4301. if (htmlCheckAutoClose(name, ctxt->name) == 1) {
  4302. htmlAutoClose(ctxt, name);
  4303. continue;
  4304. }
  4305. }
  4306. }
  4307. /*
  4308. * Has this node been popped out during parsing of
  4309. * the next element
  4310. */
  4311. if ((ctxt->nameNr > 0) && (depth >= ctxt->nameNr) &&
  4312. (!xmlStrEqual(currentNode, ctxt->name)))
  4313. {
  4314. htmlParserFinishElementParsing(ctxt);
  4315. if (currentNode != NULL) xmlFree(currentNode);
  4316. currentNode = xmlStrdup(ctxt->name);
  4317. if (currentNode == NULL) {
  4318. htmlErrMemory(ctxt, NULL);
  4319. break;
  4320. }
  4321. depth = ctxt->nameNr;
  4322. continue;
  4323. }
  4324. if ((CUR != 0) && ((xmlStrEqual(currentNode, BAD_CAST"script")) ||
  4325. (xmlStrEqual(currentNode, BAD_CAST"style")))) {
  4326. /*
  4327. * Handle SCRIPT/STYLE separately
  4328. */
  4329. htmlParseScript(ctxt);
  4330. }
  4331. else if ((CUR == '<') && (NXT(1) == '!')) {
  4332. /*
  4333. * Sometimes DOCTYPE arrives in the middle of the document
  4334. */
  4335. if ((UPP(2) == 'D') && (UPP(3) == 'O') &&
  4336. (UPP(4) == 'C') && (UPP(5) == 'T') &&
  4337. (UPP(6) == 'Y') && (UPP(7) == 'P') &&
  4338. (UPP(8) == 'E')) {
  4339. htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
  4340. "Misplaced DOCTYPE declaration\n",
  4341. BAD_CAST "DOCTYPE" , NULL);
  4342. htmlParseDocTypeDecl(ctxt);
  4343. }
  4344. /*
  4345. * First case : a comment
  4346. */
  4347. else if ((NXT(2) == '-') && (NXT(3) == '-')) {
  4348. htmlParseComment(ctxt);
  4349. }
  4350. else {
  4351. htmlSkipBogusComment(ctxt);
  4352. }
  4353. }
  4354. /*
  4355. * Second case : a Processing Instruction.
  4356. */
  4357. else if ((CUR == '<') && (NXT(1) == '?')) {
  4358. htmlParsePI(ctxt);
  4359. }
  4360. /*
  4361. * Third case : a sub-element.
  4362. */
  4363. else if ((CUR == '<') && IS_ASCII_LETTER(NXT(1))) {
  4364. htmlParseElementInternal(ctxt);
  4365. if (currentNode != NULL) xmlFree(currentNode);
  4366. currentNode = xmlStrdup(ctxt->name);
  4367. if (currentNode == NULL) {
  4368. htmlErrMemory(ctxt, NULL);
  4369. break;
  4370. }
  4371. depth = ctxt->nameNr;
  4372. }
  4373. else if (CUR == '<') {
  4374. if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
  4375. (ctxt->sax->characters != NULL))
  4376. ctxt->sax->characters(ctxt->userData, BAD_CAST "<", 1);
  4377. NEXT;
  4378. }
  4379. /*
  4380. * Fourth case : a reference. If if has not been resolved,
  4381. * parsing returns it's Name, create the node
  4382. */
  4383. else if (CUR == '&') {
  4384. htmlParseReference(ctxt);
  4385. }
  4386. /*
  4387. * Fifth case : end of the resource
  4388. */
  4389. else if (CUR == 0) {
  4390. htmlAutoCloseOnEnd(ctxt);
  4391. break;
  4392. }
  4393. /*
  4394. * Last case, text. Note that References are handled directly.
  4395. */
  4396. else {
  4397. htmlParseCharData(ctxt);
  4398. }
  4399. SHRINK;
  4400. GROW;
  4401. }
  4402. if (currentNode != NULL) xmlFree(currentNode);
  4403. }
  4404. /**
  4405. * htmlParseContent:
  4406. * @ctxt: an HTML parser context
  4407. *
  4408. * Parse a content: comment, sub-element, reference or text.
  4409. * This is the entry point when called from parser.c
  4410. */
  4411. void
  4412. __htmlParseContent(void *ctxt) {
  4413. if (ctxt != NULL)
  4414. htmlParseContentInternal((htmlParserCtxtPtr) ctxt);
  4415. }
  4416. /**
  4417. * htmlParseDocument:
  4418. * @ctxt: an HTML parser context
  4419. *
  4420. * parse an HTML document (and build a tree if using the standard SAX
  4421. * interface).
  4422. *
  4423. * Returns 0, -1 in case of error. the parser context is augmented
  4424. * as a result of the parsing.
  4425. */
  4426. int
  4427. htmlParseDocument(htmlParserCtxtPtr ctxt) {
  4428. xmlDtdPtr dtd;
  4429. xmlInitParser();
  4430. if ((ctxt == NULL) || (ctxt->input == NULL)) {
  4431. htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
  4432. "htmlParseDocument: context error\n", NULL, NULL);
  4433. return(XML_ERR_INTERNAL_ERROR);
  4434. }
  4435. /*
  4436. * SAX: beginning of the document processing.
  4437. */
  4438. if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
  4439. ctxt->sax->setDocumentLocator(ctxt->userData, &xmlDefaultSAXLocator);
  4440. xmlDetectEncoding(ctxt);
  4441. /*
  4442. * This is wrong but matches long-standing behavior. In most cases,
  4443. * a document starting with an XML declaration will specify UTF-8.
  4444. */
  4445. if (((ctxt->input->flags & XML_INPUT_HAS_ENCODING) == 0) &&
  4446. (xmlStrncmp(ctxt->input->cur, BAD_CAST "<?xm", 4) == 0))
  4447. xmlSwitchEncoding(ctxt, XML_CHAR_ENCODING_UTF8);
  4448. /*
  4449. * Wipe out everything which is before the first '<'
  4450. */
  4451. SKIP_BLANKS;
  4452. if (CUR == 0) {
  4453. htmlParseErr(ctxt, XML_ERR_DOCUMENT_EMPTY,
  4454. "Document is empty\n", NULL, NULL);
  4455. }
  4456. if ((ctxt->sax) && (ctxt->sax->startDocument) && (!ctxt->disableSAX))
  4457. ctxt->sax->startDocument(ctxt->userData);
  4458. /*
  4459. * Parse possible comments and PIs before any content
  4460. */
  4461. while (((CUR == '<') && (NXT(1) == '!') &&
  4462. (NXT(2) == '-') && (NXT(3) == '-')) ||
  4463. ((CUR == '<') && (NXT(1) == '?'))) {
  4464. htmlParseComment(ctxt);
  4465. htmlParsePI(ctxt);
  4466. SKIP_BLANKS;
  4467. }
  4468. /*
  4469. * Then possibly doc type declaration(s) and more Misc
  4470. * (doctypedecl Misc*)?
  4471. */
  4472. if ((CUR == '<') && (NXT(1) == '!') &&
  4473. (UPP(2) == 'D') && (UPP(3) == 'O') &&
  4474. (UPP(4) == 'C') && (UPP(5) == 'T') &&
  4475. (UPP(6) == 'Y') && (UPP(7) == 'P') &&
  4476. (UPP(8) == 'E')) {
  4477. htmlParseDocTypeDecl(ctxt);
  4478. }
  4479. SKIP_BLANKS;
  4480. /*
  4481. * Parse possible comments and PIs before any content
  4482. */
  4483. while (((CUR == '<') && (NXT(1) == '!') &&
  4484. (NXT(2) == '-') && (NXT(3) == '-')) ||
  4485. ((CUR == '<') && (NXT(1) == '?'))) {
  4486. htmlParseComment(ctxt);
  4487. htmlParsePI(ctxt);
  4488. SKIP_BLANKS;
  4489. }
  4490. /*
  4491. * Time to start parsing the tree itself
  4492. */
  4493. htmlParseContentInternal(ctxt);
  4494. /*
  4495. * autoclose
  4496. */
  4497. if (CUR == 0)
  4498. htmlAutoCloseOnEnd(ctxt);
  4499. /*
  4500. * SAX: end of the document processing.
  4501. */
  4502. if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
  4503. ctxt->sax->endDocument(ctxt->userData);
  4504. if ((!(ctxt->options & HTML_PARSE_NODEFDTD)) && (ctxt->myDoc != NULL)) {
  4505. dtd = xmlGetIntSubset(ctxt->myDoc);
  4506. if (dtd == NULL)
  4507. ctxt->myDoc->intSubset =
  4508. xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "html",
  4509. BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN",
  4510. BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd");
  4511. }
  4512. if (! ctxt->wellFormed) return(-1);
  4513. return(0);
  4514. }
  4515. /************************************************************************
  4516. * *
  4517. * Parser contexts handling *
  4518. * *
  4519. ************************************************************************/
  4520. /**
  4521. * htmlInitParserCtxt:
  4522. * @ctxt: an HTML parser context
  4523. * @sax: SAX handler
  4524. * @userData: user data
  4525. *
  4526. * Initialize a parser context
  4527. *
  4528. * Returns 0 in case of success and -1 in case of error
  4529. */
  4530. static int
  4531. htmlInitParserCtxt(htmlParserCtxtPtr ctxt, const htmlSAXHandler *sax,
  4532. void *userData)
  4533. {
  4534. if (ctxt == NULL) return(-1);
  4535. memset(ctxt, 0, sizeof(htmlParserCtxt));
  4536. ctxt->dict = xmlDictCreate();
  4537. if (ctxt->dict == NULL) {
  4538. htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
  4539. return(-1);
  4540. }
  4541. if (ctxt->sax == NULL)
  4542. ctxt->sax = (htmlSAXHandler *) xmlMalloc(sizeof(htmlSAXHandler));
  4543. if (ctxt->sax == NULL) {
  4544. htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
  4545. return(-1);
  4546. }
  4547. if (sax == NULL) {
  4548. memset(ctxt->sax, 0, sizeof(htmlSAXHandler));
  4549. xmlSAX2InitHtmlDefaultSAXHandler(ctxt->sax);
  4550. ctxt->userData = ctxt;
  4551. } else {
  4552. memcpy(ctxt->sax, sax, sizeof(htmlSAXHandler));
  4553. ctxt->userData = userData ? userData : ctxt;
  4554. }
  4555. /* Allocate the Input stack */
  4556. ctxt->inputTab = (htmlParserInputPtr *)
  4557. xmlMalloc(5 * sizeof(htmlParserInputPtr));
  4558. if (ctxt->inputTab == NULL) {
  4559. htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
  4560. ctxt->inputNr = 0;
  4561. ctxt->inputMax = 0;
  4562. ctxt->input = NULL;
  4563. return(-1);
  4564. }
  4565. ctxt->inputNr = 0;
  4566. ctxt->inputMax = 5;
  4567. ctxt->input = NULL;
  4568. ctxt->version = NULL;
  4569. ctxt->encoding = NULL;
  4570. ctxt->standalone = -1;
  4571. ctxt->instate = XML_PARSER_START;
  4572. /* Allocate the Node stack */
  4573. ctxt->nodeTab = (htmlNodePtr *) xmlMalloc(10 * sizeof(htmlNodePtr));
  4574. if (ctxt->nodeTab == NULL) {
  4575. htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
  4576. ctxt->nodeNr = 0;
  4577. ctxt->nodeMax = 0;
  4578. ctxt->node = NULL;
  4579. ctxt->inputNr = 0;
  4580. ctxt->inputMax = 0;
  4581. ctxt->input = NULL;
  4582. return(-1);
  4583. }
  4584. ctxt->nodeNr = 0;
  4585. ctxt->nodeMax = 10;
  4586. ctxt->node = NULL;
  4587. /* Allocate the Name stack */
  4588. ctxt->nameTab = (const xmlChar **) xmlMalloc(10 * sizeof(xmlChar *));
  4589. if (ctxt->nameTab == NULL) {
  4590. htmlErrMemory(NULL, "htmlInitParserCtxt: out of memory\n");
  4591. ctxt->nameNr = 0;
  4592. ctxt->nameMax = 0;
  4593. ctxt->name = NULL;
  4594. ctxt->nodeNr = 0;
  4595. ctxt->nodeMax = 0;
  4596. ctxt->node = NULL;
  4597. ctxt->inputNr = 0;
  4598. ctxt->inputMax = 0;
  4599. ctxt->input = NULL;
  4600. return(-1);
  4601. }
  4602. ctxt->nameNr = 0;
  4603. ctxt->nameMax = 10;
  4604. ctxt->name = NULL;
  4605. ctxt->nodeInfoTab = NULL;
  4606. ctxt->nodeInfoNr = 0;
  4607. ctxt->nodeInfoMax = 0;
  4608. ctxt->myDoc = NULL;
  4609. ctxt->wellFormed = 1;
  4610. ctxt->replaceEntities = 0;
  4611. ctxt->linenumbers = xmlLineNumbersDefaultValue;
  4612. ctxt->keepBlanks = xmlKeepBlanksDefaultValue;
  4613. ctxt->html = 1;
  4614. ctxt->vctxt.flags = XML_VCTXT_USE_PCTXT;
  4615. ctxt->vctxt.userData = ctxt;
  4616. ctxt->vctxt.error = xmlParserValidityError;
  4617. ctxt->vctxt.warning = xmlParserValidityWarning;
  4618. ctxt->record_info = 0;
  4619. ctxt->validate = 0;
  4620. ctxt->checkIndex = 0;
  4621. ctxt->catalogs = NULL;
  4622. xmlInitNodeInfoSeq(&ctxt->node_seq);
  4623. return(0);
  4624. }
  4625. /**
  4626. * htmlFreeParserCtxt:
  4627. * @ctxt: an HTML parser context
  4628. *
  4629. * Free all the memory used by a parser context. However the parsed
  4630. * document in ctxt->myDoc is not freed.
  4631. */
  4632. void
  4633. htmlFreeParserCtxt(htmlParserCtxtPtr ctxt)
  4634. {
  4635. xmlFreeParserCtxt(ctxt);
  4636. }
  4637. /**
  4638. * htmlNewParserCtxt:
  4639. *
  4640. * Allocate and initialize a new parser context.
  4641. *
  4642. * Returns the htmlParserCtxtPtr or NULL in case of allocation error
  4643. */
  4644. htmlParserCtxtPtr
  4645. htmlNewParserCtxt(void)
  4646. {
  4647. return(htmlNewSAXParserCtxt(NULL, NULL));
  4648. }
  4649. /**
  4650. * htmlNewSAXParserCtxt:
  4651. * @sax: SAX handler
  4652. * @userData: user data
  4653. *
  4654. * Allocate and initialize a new SAX parser context. If userData is NULL,
  4655. * the parser context will be passed as user data.
  4656. *
  4657. * Returns the htmlParserCtxtPtr or NULL in case of allocation error
  4658. */
  4659. htmlParserCtxtPtr
  4660. htmlNewSAXParserCtxt(const htmlSAXHandler *sax, void *userData)
  4661. {
  4662. xmlParserCtxtPtr ctxt;
  4663. ctxt = (xmlParserCtxtPtr) xmlMalloc(sizeof(xmlParserCtxt));
  4664. if (ctxt == NULL) {
  4665. htmlErrMemory(NULL, "NewParserCtxt: out of memory\n");
  4666. return(NULL);
  4667. }
  4668. memset(ctxt, 0, sizeof(xmlParserCtxt));
  4669. if (htmlInitParserCtxt(ctxt, sax, userData) < 0) {
  4670. htmlFreeParserCtxt(ctxt);
  4671. return(NULL);
  4672. }
  4673. return(ctxt);
  4674. }
  4675. /**
  4676. * htmlCreateMemoryParserCtxt:
  4677. * @buffer: a pointer to a char array
  4678. * @size: the size of the array
  4679. *
  4680. * Create a parser context for an HTML in-memory document.
  4681. *
  4682. * Returns the new parser context or NULL
  4683. */
  4684. htmlParserCtxtPtr
  4685. htmlCreateMemoryParserCtxt(const char *buffer, int size) {
  4686. xmlParserCtxtPtr ctxt;
  4687. xmlParserInputPtr input;
  4688. xmlParserInputBufferPtr buf;
  4689. if (buffer == NULL)
  4690. return(NULL);
  4691. if (size <= 0)
  4692. return(NULL);
  4693. ctxt = htmlNewParserCtxt();
  4694. if (ctxt == NULL)
  4695. return(NULL);
  4696. buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE);
  4697. if (buf == NULL) {
  4698. xmlFreeParserCtxt(ctxt);
  4699. return(NULL);
  4700. }
  4701. input = xmlNewInputStream(ctxt);
  4702. if (input == NULL) {
  4703. xmlFreeParserInputBuffer(buf);
  4704. xmlFreeParserCtxt(ctxt);
  4705. return(NULL);
  4706. }
  4707. input->filename = NULL;
  4708. input->buf = buf;
  4709. xmlBufResetInput(buf->buffer, input);
  4710. inputPush(ctxt, input);
  4711. return(ctxt);
  4712. }
  4713. /**
  4714. * htmlCreateDocParserCtxt:
  4715. * @str: a pointer to an array of xmlChar
  4716. * @encoding: a free form C string describing the HTML document encoding, or NULL
  4717. *
  4718. * Create a parser context for an HTML document.
  4719. *
  4720. * TODO: check the need to add encoding handling there
  4721. *
  4722. * Returns the new parser context or NULL
  4723. */
  4724. static htmlParserCtxtPtr
  4725. htmlCreateDocParserCtxt(const xmlChar *str, const char *encoding) {
  4726. xmlParserCtxtPtr ctxt;
  4727. xmlParserInputPtr input;
  4728. xmlParserInputBufferPtr buf;
  4729. if (str == NULL)
  4730. return(NULL);
  4731. ctxt = htmlNewParserCtxt();
  4732. if (ctxt == NULL)
  4733. return(NULL);
  4734. buf = xmlParserInputBufferCreateString(str);
  4735. if (buf == NULL) {
  4736. xmlFreeParserCtxt(ctxt);
  4737. return(NULL);
  4738. }
  4739. input = xmlNewInputStream(ctxt);
  4740. if (input == NULL) {
  4741. xmlFreeParserInputBuffer(buf);
  4742. xmlFreeParserCtxt(ctxt);
  4743. return(NULL);
  4744. }
  4745. input->filename = NULL;
  4746. input->buf = buf;
  4747. xmlBufResetInput(buf->buffer, input);
  4748. inputPush(ctxt, input);
  4749. if (encoding != NULL) {
  4750. xmlCharEncoding enc;
  4751. xmlCharEncodingHandlerPtr handler;
  4752. enc = xmlParseCharEncoding(encoding);
  4753. /*
  4754. * registered set of known encodings
  4755. */
  4756. if (enc != XML_CHAR_ENCODING_ERROR) {
  4757. xmlSwitchEncoding(ctxt, enc);
  4758. if (ctxt->errNo == XML_ERR_UNSUPPORTED_ENCODING) {
  4759. htmlParseErr(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
  4760. "Unsupported encoding %s\n",
  4761. (const xmlChar *) encoding, NULL);
  4762. }
  4763. } else {
  4764. /*
  4765. * fallback for unknown encodings
  4766. */
  4767. handler = xmlFindCharEncodingHandler((const char *) encoding);
  4768. if (handler != NULL) {
  4769. xmlSwitchToEncoding(ctxt, handler);
  4770. } else {
  4771. htmlParseErr(ctxt, XML_ERR_UNSUPPORTED_ENCODING,
  4772. "Unsupported encoding %s\n",
  4773. (const xmlChar *) encoding, NULL);
  4774. }
  4775. }
  4776. }
  4777. return(ctxt);
  4778. }
  4779. #ifdef LIBXML_PUSH_ENABLED
  4780. /************************************************************************
  4781. * *
  4782. * Progressive parsing interfaces *
  4783. * *
  4784. ************************************************************************/
  4785. /**
  4786. * htmlParseLookupSequence:
  4787. * @ctxt: an HTML parser context
  4788. * @first: the first char to lookup
  4789. * @next: the next char to lookup or zero
  4790. * @third: the next char to lookup or zero
  4791. * @ignoreattrval: skip over attribute values
  4792. *
  4793. * Try to find if a sequence (first, next, third) or just (first next) or
  4794. * (first) is available in the input stream.
  4795. * This function has a side effect of (possibly) incrementing ctxt->checkIndex
  4796. * to avoid rescanning sequences of bytes, it DOES change the state of the
  4797. * parser, do not use liberally.
  4798. * This is basically similar to xmlParseLookupSequence()
  4799. *
  4800. * Returns the index to the current parsing point if the full sequence
  4801. * is available, -1 otherwise.
  4802. */
  4803. static int
  4804. htmlParseLookupSequence(htmlParserCtxtPtr ctxt, xmlChar first,
  4805. xmlChar next, xmlChar third, int ignoreattrval)
  4806. {
  4807. size_t base, len;
  4808. htmlParserInputPtr in;
  4809. const xmlChar *buf;
  4810. int quote;
  4811. in = ctxt->input;
  4812. if (in == NULL)
  4813. return (-1);
  4814. base = ctxt->checkIndex;
  4815. quote = ctxt->endCheckState;
  4816. buf = in->cur;
  4817. len = in->end - in->cur;
  4818. /* take into account the sequence length */
  4819. if (third)
  4820. len -= 2;
  4821. else if (next)
  4822. len--;
  4823. for (; base < len; base++) {
  4824. if (base >= INT_MAX / 2) {
  4825. ctxt->checkIndex = 0;
  4826. ctxt->endCheckState = 0;
  4827. return (base - 2);
  4828. }
  4829. if (ignoreattrval) {
  4830. if (quote) {
  4831. if (buf[base] == quote)
  4832. quote = 0;
  4833. continue;
  4834. }
  4835. if (buf[base] == '"' || buf[base] == '\'') {
  4836. quote = buf[base];
  4837. continue;
  4838. }
  4839. }
  4840. if (buf[base] == first) {
  4841. if (third != 0) {
  4842. if ((buf[base + 1] != next) || (buf[base + 2] != third))
  4843. continue;
  4844. } else if (next != 0) {
  4845. if (buf[base + 1] != next)
  4846. continue;
  4847. }
  4848. ctxt->checkIndex = 0;
  4849. ctxt->endCheckState = 0;
  4850. return (base);
  4851. }
  4852. }
  4853. ctxt->checkIndex = base;
  4854. ctxt->endCheckState = quote;
  4855. return (-1);
  4856. }
  4857. /**
  4858. * htmlParseLookupCommentEnd:
  4859. * @ctxt: an HTML parser context
  4860. *
  4861. * Try to find a comment end tag in the input stream
  4862. * The search includes "-->" as well as WHATWG-recommended incorrectly-closed tags.
  4863. * (See https://html.spec.whatwg.org/multipage/parsing.html#parse-error-incorrectly-closed-comment)
  4864. * This function has a side effect of (possibly) incrementing ctxt->checkIndex
  4865. * to avoid rescanning sequences of bytes, it DOES change the state of the
  4866. * parser, do not use liberally.
  4867. * This wraps to htmlParseLookupSequence()
  4868. *
  4869. * Returns the index to the current parsing point if the full sequence is available, -1 otherwise.
  4870. */
  4871. static int
  4872. htmlParseLookupCommentEnd(htmlParserCtxtPtr ctxt)
  4873. {
  4874. int mark = 0;
  4875. int offset;
  4876. while (1) {
  4877. mark = htmlParseLookupSequence(ctxt, '-', '-', 0, 0);
  4878. if (mark < 0)
  4879. break;
  4880. if ((NXT(mark+2) == '>') ||
  4881. ((NXT(mark+2) == '!') && (NXT(mark+3) == '>'))) {
  4882. ctxt->checkIndex = 0;
  4883. break;
  4884. }
  4885. offset = (NXT(mark+2) == '!') ? 3 : 2;
  4886. if (mark + offset >= ctxt->input->end - ctxt->input->cur) {
  4887. ctxt->checkIndex = mark;
  4888. return(-1);
  4889. }
  4890. ctxt->checkIndex = mark + 1;
  4891. }
  4892. return mark;
  4893. }
  4894. /**
  4895. * htmlParseTryOrFinish:
  4896. * @ctxt: an HTML parser context
  4897. * @terminate: last chunk indicator
  4898. *
  4899. * Try to progress on parsing
  4900. *
  4901. * Returns zero if no parsing was possible
  4902. */
  4903. static int
  4904. htmlParseTryOrFinish(htmlParserCtxtPtr ctxt, int terminate) {
  4905. int ret = 0;
  4906. htmlParserInputPtr in;
  4907. ptrdiff_t avail = 0;
  4908. xmlChar cur, next;
  4909. htmlParserNodeInfo node_info;
  4910. while (1) {
  4911. in = ctxt->input;
  4912. if (in == NULL) break;
  4913. avail = in->end - in->cur;
  4914. if ((avail == 0) && (terminate)) {
  4915. htmlAutoCloseOnEnd(ctxt);
  4916. if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) {
  4917. /*
  4918. * SAX: end of the document processing.
  4919. */
  4920. ctxt->instate = XML_PARSER_EOF;
  4921. if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
  4922. ctxt->sax->endDocument(ctxt->userData);
  4923. }
  4924. }
  4925. if (avail < 1)
  4926. goto done;
  4927. /*
  4928. * This is done to make progress and avoid an infinite loop
  4929. * if a parsing attempt was aborted by hitting a NUL byte. After
  4930. * changing htmlCurrentChar, this probably isn't necessary anymore.
  4931. * We should consider removing this check.
  4932. */
  4933. cur = in->cur[0];
  4934. if (cur == 0) {
  4935. SKIP(1);
  4936. continue;
  4937. }
  4938. switch (ctxt->instate) {
  4939. case XML_PARSER_EOF:
  4940. /*
  4941. * Document parsing is done !
  4942. */
  4943. goto done;
  4944. case XML_PARSER_START:
  4945. /*
  4946. * This is wrong but matches long-standing behavior. In most
  4947. * cases, a document starting with an XML declaration will
  4948. * specify UTF-8.
  4949. */
  4950. if (((ctxt->input->flags & XML_INPUT_HAS_ENCODING) == 0) &&
  4951. (xmlStrncmp(ctxt->input->cur, BAD_CAST "<?xm", 4) == 0)) {
  4952. xmlSwitchEncoding(ctxt, XML_CHAR_ENCODING_UTF8);
  4953. }
  4954. /*
  4955. * Very first chars read from the document flow.
  4956. */
  4957. cur = in->cur[0];
  4958. if (IS_BLANK_CH(cur)) {
  4959. SKIP_BLANKS;
  4960. avail = in->end - in->cur;
  4961. }
  4962. if ((ctxt->sax) && (ctxt->sax->setDocumentLocator))
  4963. ctxt->sax->setDocumentLocator(ctxt->userData,
  4964. &xmlDefaultSAXLocator);
  4965. if ((ctxt->sax) && (ctxt->sax->startDocument) &&
  4966. (!ctxt->disableSAX))
  4967. ctxt->sax->startDocument(ctxt->userData);
  4968. if (ctxt->instate == XML_PARSER_EOF)
  4969. goto done;
  4970. cur = in->cur[0];
  4971. next = in->cur[1];
  4972. if ((cur == '<') && (next == '!') &&
  4973. (UPP(2) == 'D') && (UPP(3) == 'O') &&
  4974. (UPP(4) == 'C') && (UPP(5) == 'T') &&
  4975. (UPP(6) == 'Y') && (UPP(7) == 'P') &&
  4976. (UPP(8) == 'E')) {
  4977. if ((!terminate) &&
  4978. (htmlParseLookupSequence(ctxt, '>', 0, 0, 1) < 0))
  4979. goto done;
  4980. htmlParseDocTypeDecl(ctxt);
  4981. if (ctxt->instate == XML_PARSER_EOF)
  4982. goto done;
  4983. ctxt->instate = XML_PARSER_PROLOG;
  4984. } else {
  4985. ctxt->instate = XML_PARSER_MISC;
  4986. }
  4987. break;
  4988. case XML_PARSER_MISC:
  4989. SKIP_BLANKS;
  4990. avail = in->end - in->cur;
  4991. /*
  4992. * no chars in buffer
  4993. */
  4994. if (avail < 1)
  4995. goto done;
  4996. /*
  4997. * not enough chars in buffer
  4998. */
  4999. if (avail < 2) {
  5000. if (!terminate)
  5001. goto done;
  5002. else
  5003. next = ' ';
  5004. } else {
  5005. next = in->cur[1];
  5006. }
  5007. cur = in->cur[0];
  5008. if ((cur == '<') && (next == '!') &&
  5009. (in->cur[2] == '-') && (in->cur[3] == '-')) {
  5010. if ((!terminate) && (htmlParseLookupCommentEnd(ctxt) < 0))
  5011. goto done;
  5012. htmlParseComment(ctxt);
  5013. if (ctxt->instate == XML_PARSER_EOF)
  5014. goto done;
  5015. ctxt->instate = XML_PARSER_MISC;
  5016. } else if ((cur == '<') && (next == '?')) {
  5017. if ((!terminate) &&
  5018. (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
  5019. goto done;
  5020. htmlParsePI(ctxt);
  5021. if (ctxt->instate == XML_PARSER_EOF)
  5022. goto done;
  5023. ctxt->instate = XML_PARSER_MISC;
  5024. } else if ((cur == '<') && (next == '!') &&
  5025. (UPP(2) == 'D') && (UPP(3) == 'O') &&
  5026. (UPP(4) == 'C') && (UPP(5) == 'T') &&
  5027. (UPP(6) == 'Y') && (UPP(7) == 'P') &&
  5028. (UPP(8) == 'E')) {
  5029. if ((!terminate) &&
  5030. (htmlParseLookupSequence(ctxt, '>', 0, 0, 1) < 0))
  5031. goto done;
  5032. htmlParseDocTypeDecl(ctxt);
  5033. if (ctxt->instate == XML_PARSER_EOF)
  5034. goto done;
  5035. ctxt->instate = XML_PARSER_PROLOG;
  5036. } else if ((cur == '<') && (next == '!') &&
  5037. (avail < 9)) {
  5038. goto done;
  5039. } else {
  5040. ctxt->instate = XML_PARSER_CONTENT;
  5041. }
  5042. break;
  5043. case XML_PARSER_PROLOG:
  5044. SKIP_BLANKS;
  5045. avail = in->end - in->cur;
  5046. if (avail < 2)
  5047. goto done;
  5048. cur = in->cur[0];
  5049. next = in->cur[1];
  5050. if ((cur == '<') && (next == '!') &&
  5051. (in->cur[2] == '-') && (in->cur[3] == '-')) {
  5052. if ((!terminate) && (htmlParseLookupCommentEnd(ctxt) < 0))
  5053. goto done;
  5054. htmlParseComment(ctxt);
  5055. if (ctxt->instate == XML_PARSER_EOF)
  5056. goto done;
  5057. ctxt->instate = XML_PARSER_PROLOG;
  5058. } else if ((cur == '<') && (next == '?')) {
  5059. if ((!terminate) &&
  5060. (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
  5061. goto done;
  5062. htmlParsePI(ctxt);
  5063. if (ctxt->instate == XML_PARSER_EOF)
  5064. goto done;
  5065. ctxt->instate = XML_PARSER_PROLOG;
  5066. } else if ((cur == '<') && (next == '!') &&
  5067. (avail < 4)) {
  5068. goto done;
  5069. } else {
  5070. ctxt->instate = XML_PARSER_CONTENT;
  5071. }
  5072. break;
  5073. case XML_PARSER_EPILOG:
  5074. avail = in->end - in->cur;
  5075. if (avail < 1)
  5076. goto done;
  5077. cur = in->cur[0];
  5078. if (IS_BLANK_CH(cur)) {
  5079. htmlParseCharData(ctxt);
  5080. goto done;
  5081. }
  5082. if (avail < 2)
  5083. goto done;
  5084. next = in->cur[1];
  5085. if ((cur == '<') && (next == '!') &&
  5086. (in->cur[2] == '-') && (in->cur[3] == '-')) {
  5087. if ((!terminate) && (htmlParseLookupCommentEnd(ctxt) < 0))
  5088. goto done;
  5089. htmlParseComment(ctxt);
  5090. if (ctxt->instate == XML_PARSER_EOF)
  5091. goto done;
  5092. ctxt->instate = XML_PARSER_EPILOG;
  5093. } else if ((cur == '<') && (next == '?')) {
  5094. if ((!terminate) &&
  5095. (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
  5096. goto done;
  5097. htmlParsePI(ctxt);
  5098. if (ctxt->instate == XML_PARSER_EOF)
  5099. goto done;
  5100. ctxt->instate = XML_PARSER_EPILOG;
  5101. } else if ((cur == '<') && (next == '!') &&
  5102. (avail < 4)) {
  5103. goto done;
  5104. } else {
  5105. ctxt->errNo = XML_ERR_DOCUMENT_END;
  5106. ctxt->wellFormed = 0;
  5107. ctxt->instate = XML_PARSER_EOF;
  5108. if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
  5109. ctxt->sax->endDocument(ctxt->userData);
  5110. goto done;
  5111. }
  5112. break;
  5113. case XML_PARSER_START_TAG: {
  5114. const xmlChar *name;
  5115. int failed;
  5116. const htmlElemDesc * info;
  5117. /*
  5118. * no chars in buffer
  5119. */
  5120. if (avail < 1)
  5121. goto done;
  5122. /*
  5123. * not enough chars in buffer
  5124. */
  5125. if (avail < 2) {
  5126. if (!terminate)
  5127. goto done;
  5128. else
  5129. next = ' ';
  5130. } else {
  5131. next = in->cur[1];
  5132. }
  5133. cur = in->cur[0];
  5134. if (cur != '<') {
  5135. ctxt->instate = XML_PARSER_CONTENT;
  5136. break;
  5137. }
  5138. if (next == '/') {
  5139. ctxt->instate = XML_PARSER_END_TAG;
  5140. ctxt->checkIndex = 0;
  5141. break;
  5142. }
  5143. if ((!terminate) &&
  5144. (htmlParseLookupSequence(ctxt, '>', 0, 0, 1) < 0))
  5145. goto done;
  5146. /* Capture start position */
  5147. if (ctxt->record_info) {
  5148. node_info.begin_pos = ctxt->input->consumed +
  5149. (CUR_PTR - ctxt->input->base);
  5150. node_info.begin_line = ctxt->input->line;
  5151. }
  5152. failed = htmlParseStartTag(ctxt);
  5153. name = ctxt->name;
  5154. if ((failed == -1) ||
  5155. (name == NULL)) {
  5156. if (CUR == '>')
  5157. NEXT;
  5158. break;
  5159. }
  5160. /*
  5161. * Lookup the info for that element.
  5162. */
  5163. info = htmlTagLookup(name);
  5164. if (info == NULL) {
  5165. htmlParseErr(ctxt, XML_HTML_UNKNOWN_TAG,
  5166. "Tag %s invalid\n", name, NULL);
  5167. }
  5168. /*
  5169. * Check for an Empty Element labeled the XML/SGML way
  5170. */
  5171. if ((CUR == '/') && (NXT(1) == '>')) {
  5172. SKIP(2);
  5173. if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
  5174. ctxt->sax->endElement(ctxt->userData, name);
  5175. htmlnamePop(ctxt);
  5176. if (ctxt->instate == XML_PARSER_EOF)
  5177. goto done;
  5178. ctxt->instate = XML_PARSER_CONTENT;
  5179. break;
  5180. }
  5181. if (CUR == '>') {
  5182. NEXT;
  5183. } else {
  5184. htmlParseErr(ctxt, XML_ERR_GT_REQUIRED,
  5185. "Couldn't find end of Start Tag %s\n",
  5186. name, NULL);
  5187. /*
  5188. * end of parsing of this node.
  5189. */
  5190. if (xmlStrEqual(name, ctxt->name)) {
  5191. nodePop(ctxt);
  5192. htmlnamePop(ctxt);
  5193. }
  5194. if (ctxt->record_info)
  5195. htmlNodeInfoPush(ctxt, &node_info);
  5196. if (ctxt->instate == XML_PARSER_EOF)
  5197. goto done;
  5198. ctxt->instate = XML_PARSER_CONTENT;
  5199. break;
  5200. }
  5201. /*
  5202. * Check for an Empty Element from DTD definition
  5203. */
  5204. if ((info != NULL) && (info->empty)) {
  5205. if ((ctxt->sax != NULL) && (ctxt->sax->endElement != NULL))
  5206. ctxt->sax->endElement(ctxt->userData, name);
  5207. htmlnamePop(ctxt);
  5208. }
  5209. if (ctxt->record_info)
  5210. htmlNodeInfoPush(ctxt, &node_info);
  5211. if (ctxt->instate == XML_PARSER_EOF)
  5212. goto done;
  5213. ctxt->instate = XML_PARSER_CONTENT;
  5214. break;
  5215. }
  5216. case XML_PARSER_CONTENT: {
  5217. xmlChar chr[2] = { 0, 0 };
  5218. /*
  5219. * Handle preparsed entities and charRef
  5220. */
  5221. if (ctxt->token != 0) {
  5222. chr[0] = ctxt->token;
  5223. htmlCheckParagraph(ctxt);
  5224. if ((ctxt->sax != NULL) && (ctxt->sax->characters != NULL))
  5225. ctxt->sax->characters(ctxt->userData, chr, 1);
  5226. ctxt->token = 0;
  5227. ctxt->checkIndex = 0;
  5228. }
  5229. if ((avail == 1) && (terminate)) {
  5230. cur = in->cur[0];
  5231. if ((cur != '<') && (cur != '&')) {
  5232. if (ctxt->sax != NULL) {
  5233. chr[0] = cur;
  5234. if (IS_BLANK_CH(cur)) {
  5235. if (ctxt->keepBlanks) {
  5236. if (ctxt->sax->characters != NULL)
  5237. ctxt->sax->characters(
  5238. ctxt->userData, chr, 1);
  5239. } else {
  5240. if (ctxt->sax->ignorableWhitespace != NULL)
  5241. ctxt->sax->ignorableWhitespace(
  5242. ctxt->userData, chr, 1);
  5243. }
  5244. } else {
  5245. htmlCheckParagraph(ctxt);
  5246. if (ctxt->sax->characters != NULL)
  5247. ctxt->sax->characters(
  5248. ctxt->userData, chr, 1);
  5249. }
  5250. }
  5251. ctxt->token = 0;
  5252. ctxt->checkIndex = 0;
  5253. in->cur++;
  5254. break;
  5255. }
  5256. }
  5257. if (avail < 2)
  5258. goto done;
  5259. cur = in->cur[0];
  5260. next = in->cur[1];
  5261. if ((xmlStrEqual(ctxt->name, BAD_CAST"script")) ||
  5262. (xmlStrEqual(ctxt->name, BAD_CAST"style"))) {
  5263. /*
  5264. * Handle SCRIPT/STYLE separately
  5265. */
  5266. if (!terminate) {
  5267. int idx;
  5268. xmlChar val;
  5269. idx = htmlParseLookupSequence(ctxt, '<', '/', 0, 0);
  5270. if (idx < 0)
  5271. goto done;
  5272. val = in->cur[idx + 2];
  5273. if (val == 0) { /* bad cut of input */
  5274. /*
  5275. * FIXME: htmlParseScript checks for additional
  5276. * characters after '</'.
  5277. */
  5278. ctxt->checkIndex = idx;
  5279. goto done;
  5280. }
  5281. }
  5282. htmlParseScript(ctxt);
  5283. if (ctxt->instate == XML_PARSER_EOF)
  5284. goto done;
  5285. if ((cur == '<') && (next == '/')) {
  5286. ctxt->instate = XML_PARSER_END_TAG;
  5287. ctxt->checkIndex = 0;
  5288. break;
  5289. }
  5290. } else if ((cur == '<') && (next == '!')) {
  5291. if (avail < 4)
  5292. goto done;
  5293. /*
  5294. * Sometimes DOCTYPE arrives in the middle of the document
  5295. */
  5296. if ((UPP(2) == 'D') && (UPP(3) == 'O') &&
  5297. (UPP(4) == 'C') && (UPP(5) == 'T') &&
  5298. (UPP(6) == 'Y') && (UPP(7) == 'P') &&
  5299. (UPP(8) == 'E')) {
  5300. if ((!terminate) &&
  5301. (htmlParseLookupSequence(ctxt, '>', 0, 0, 1) < 0))
  5302. goto done;
  5303. htmlParseErr(ctxt, XML_HTML_STRUCURE_ERROR,
  5304. "Misplaced DOCTYPE declaration\n",
  5305. BAD_CAST "DOCTYPE" , NULL);
  5306. htmlParseDocTypeDecl(ctxt);
  5307. } else if ((in->cur[2] == '-') && (in->cur[3] == '-')) {
  5308. if ((!terminate) &&
  5309. (htmlParseLookupCommentEnd(ctxt) < 0))
  5310. goto done;
  5311. htmlParseComment(ctxt);
  5312. if (ctxt->instate == XML_PARSER_EOF)
  5313. goto done;
  5314. ctxt->instate = XML_PARSER_CONTENT;
  5315. } else {
  5316. if ((!terminate) &&
  5317. (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
  5318. goto done;
  5319. htmlSkipBogusComment(ctxt);
  5320. }
  5321. } else if ((cur == '<') && (next == '?')) {
  5322. if ((!terminate) &&
  5323. (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
  5324. goto done;
  5325. htmlParsePI(ctxt);
  5326. if (ctxt->instate == XML_PARSER_EOF)
  5327. goto done;
  5328. ctxt->instate = XML_PARSER_CONTENT;
  5329. } else if ((cur == '<') && (next == '/')) {
  5330. ctxt->instate = XML_PARSER_END_TAG;
  5331. ctxt->checkIndex = 0;
  5332. break;
  5333. } else if ((cur == '<') && IS_ASCII_LETTER(next)) {
  5334. if ((!terminate) && (next == 0))
  5335. goto done;
  5336. ctxt->instate = XML_PARSER_START_TAG;
  5337. ctxt->checkIndex = 0;
  5338. break;
  5339. } else if (cur == '<') {
  5340. if ((ctxt->sax != NULL) && (!ctxt->disableSAX) &&
  5341. (ctxt->sax->characters != NULL))
  5342. ctxt->sax->characters(ctxt->userData,
  5343. BAD_CAST "<", 1);
  5344. NEXT;
  5345. } else {
  5346. /*
  5347. * check that the text sequence is complete
  5348. * before handing out the data to the parser
  5349. * to avoid problems with erroneous end of
  5350. * data detection.
  5351. */
  5352. if ((!terminate) &&
  5353. (htmlParseLookupSequence(ctxt, '<', 0, 0, 0) < 0))
  5354. goto done;
  5355. ctxt->checkIndex = 0;
  5356. while ((ctxt->instate != XML_PARSER_EOF) &&
  5357. (cur != '<') && (in->cur < in->end)) {
  5358. if (cur == '&') {
  5359. htmlParseReference(ctxt);
  5360. } else {
  5361. htmlParseCharData(ctxt);
  5362. }
  5363. cur = in->cur[0];
  5364. }
  5365. }
  5366. break;
  5367. }
  5368. case XML_PARSER_END_TAG:
  5369. if (avail < 2)
  5370. goto done;
  5371. if ((!terminate) &&
  5372. (htmlParseLookupSequence(ctxt, '>', 0, 0, 0) < 0))
  5373. goto done;
  5374. htmlParseEndTag(ctxt);
  5375. if (ctxt->instate == XML_PARSER_EOF)
  5376. goto done;
  5377. if (ctxt->nameNr == 0) {
  5378. ctxt->instate = XML_PARSER_EPILOG;
  5379. } else {
  5380. ctxt->instate = XML_PARSER_CONTENT;
  5381. }
  5382. ctxt->checkIndex = 0;
  5383. break;
  5384. default:
  5385. htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
  5386. "HPP: internal error\n", NULL, NULL);
  5387. ctxt->instate = XML_PARSER_EOF;
  5388. break;
  5389. }
  5390. }
  5391. done:
  5392. if ((avail == 0) && (terminate)) {
  5393. htmlAutoCloseOnEnd(ctxt);
  5394. if ((ctxt->nameNr == 0) && (ctxt->instate != XML_PARSER_EOF)) {
  5395. /*
  5396. * SAX: end of the document processing.
  5397. */
  5398. ctxt->instate = XML_PARSER_EOF;
  5399. if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
  5400. ctxt->sax->endDocument(ctxt->userData);
  5401. }
  5402. }
  5403. if ((!(ctxt->options & HTML_PARSE_NODEFDTD)) && (ctxt->myDoc != NULL) &&
  5404. ((terminate) || (ctxt->instate == XML_PARSER_EOF) ||
  5405. (ctxt->instate == XML_PARSER_EPILOG))) {
  5406. xmlDtdPtr dtd;
  5407. dtd = xmlGetIntSubset(ctxt->myDoc);
  5408. if (dtd == NULL)
  5409. ctxt->myDoc->intSubset =
  5410. xmlCreateIntSubset(ctxt->myDoc, BAD_CAST "html",
  5411. BAD_CAST "-//W3C//DTD HTML 4.0 Transitional//EN",
  5412. BAD_CAST "http://www.w3.org/TR/REC-html40/loose.dtd");
  5413. }
  5414. return(ret);
  5415. }
  5416. /**
  5417. * htmlParseChunk:
  5418. * @ctxt: an HTML parser context
  5419. * @chunk: an char array
  5420. * @size: the size in byte of the chunk
  5421. * @terminate: last chunk indicator
  5422. *
  5423. * Parse a Chunk of memory
  5424. *
  5425. * Returns zero if no error, the xmlParserErrors otherwise.
  5426. */
  5427. int
  5428. htmlParseChunk(htmlParserCtxtPtr ctxt, const char *chunk, int size,
  5429. int terminate) {
  5430. if ((ctxt == NULL) || (ctxt->input == NULL)) {
  5431. htmlParseErr(ctxt, XML_ERR_INTERNAL_ERROR,
  5432. "htmlParseChunk: context error\n", NULL, NULL);
  5433. return(XML_ERR_INTERNAL_ERROR);
  5434. }
  5435. if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
  5436. (ctxt->input->buf != NULL) && (ctxt->instate != XML_PARSER_EOF)) {
  5437. size_t pos = ctxt->input->cur - ctxt->input->base;
  5438. int res;
  5439. res = xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
  5440. xmlBufUpdateInput(ctxt->input->buf->buffer, ctxt->input, pos);
  5441. if (res < 0) {
  5442. htmlParseErr(ctxt, ctxt->input->buf->error,
  5443. "xmlParserInputBufferPush failed", NULL, NULL);
  5444. xmlHaltParser(ctxt);
  5445. return (ctxt->errNo);
  5446. }
  5447. }
  5448. htmlParseTryOrFinish(ctxt, terminate);
  5449. if (terminate) {
  5450. if ((ctxt->instate != XML_PARSER_EOF) &&
  5451. (ctxt->instate != XML_PARSER_EPILOG) &&
  5452. (ctxt->instate != XML_PARSER_MISC)) {
  5453. ctxt->errNo = XML_ERR_DOCUMENT_END;
  5454. ctxt->wellFormed = 0;
  5455. }
  5456. if (ctxt->instate != XML_PARSER_EOF) {
  5457. if ((ctxt->sax) && (ctxt->sax->endDocument != NULL))
  5458. ctxt->sax->endDocument(ctxt->userData);
  5459. }
  5460. ctxt->instate = XML_PARSER_EOF;
  5461. }
  5462. return((xmlParserErrors) ctxt->errNo);
  5463. }
  5464. /************************************************************************
  5465. * *
  5466. * User entry points *
  5467. * *
  5468. ************************************************************************/
  5469. /**
  5470. * htmlCreatePushParserCtxt:
  5471. * @sax: a SAX handler
  5472. * @user_data: The user data returned on SAX callbacks
  5473. * @chunk: a pointer to an array of chars
  5474. * @size: number of chars in the array
  5475. * @filename: an optional file name or URI
  5476. * @enc: an optional encoding
  5477. *
  5478. * Create a parser context for using the HTML parser in push mode
  5479. * The value of @filename is used for fetching external entities
  5480. * and error/warning reports.
  5481. *
  5482. * Returns the new parser context or NULL
  5483. */
  5484. htmlParserCtxtPtr
  5485. htmlCreatePushParserCtxt(htmlSAXHandlerPtr sax, void *user_data,
  5486. const char *chunk, int size, const char *filename,
  5487. xmlCharEncoding enc) {
  5488. htmlParserCtxtPtr ctxt;
  5489. htmlParserInputPtr inputStream;
  5490. xmlParserInputBufferPtr buf;
  5491. xmlInitParser();
  5492. buf = xmlAllocParserInputBuffer(XML_CHAR_ENCODING_NONE);
  5493. if (buf == NULL) return(NULL);
  5494. ctxt = htmlNewSAXParserCtxt(sax, user_data);
  5495. if (ctxt == NULL) {
  5496. xmlFreeParserInputBuffer(buf);
  5497. return(NULL);
  5498. }
  5499. if (filename == NULL) {
  5500. ctxt->directory = NULL;
  5501. } else {
  5502. ctxt->directory = xmlParserGetDirectory(filename);
  5503. }
  5504. inputStream = htmlNewInputStream(ctxt);
  5505. if (inputStream == NULL) {
  5506. xmlFreeParserCtxt(ctxt);
  5507. xmlFreeParserInputBuffer(buf);
  5508. return(NULL);
  5509. }
  5510. if (filename == NULL)
  5511. inputStream->filename = NULL;
  5512. else
  5513. inputStream->filename = (char *)
  5514. xmlCanonicPath((const xmlChar *) filename);
  5515. inputStream->buf = buf;
  5516. xmlBufResetInput(buf->buffer, inputStream);
  5517. inputPush(ctxt, inputStream);
  5518. if (enc != XML_CHAR_ENCODING_NONE)
  5519. xmlSwitchEncoding(ctxt, enc);
  5520. if ((size > 0) && (chunk != NULL) && (ctxt->input != NULL) &&
  5521. (ctxt->input->buf != NULL)) {
  5522. size_t pos = ctxt->input->cur - ctxt->input->base;
  5523. int res;
  5524. res = xmlParserInputBufferPush(ctxt->input->buf, size, chunk);
  5525. xmlBufUpdateInput(ctxt->input->buf->buffer, ctxt->input, pos);
  5526. if (res < 0) {
  5527. htmlParseErr(ctxt, ctxt->input->buf->error,
  5528. "xmlParserInputBufferPush failed\n", NULL, NULL);
  5529. xmlHaltParser(ctxt);
  5530. }
  5531. }
  5532. ctxt->progressive = 1;
  5533. return(ctxt);
  5534. }
  5535. #endif /* LIBXML_PUSH_ENABLED */
  5536. /**
  5537. * htmlSAXParseDoc:
  5538. * @cur: a pointer to an array of xmlChar
  5539. * @encoding: a free form C string describing the HTML document encoding, or NULL
  5540. * @sax: the SAX handler block
  5541. * @userData: if using SAX, this pointer will be provided on callbacks.
  5542. *
  5543. * DEPRECATED: Use htmlNewSAXParserCtxt and htmlCtxtReadDoc.
  5544. *
  5545. * Parse an HTML in-memory document. If sax is not NULL, use the SAX callbacks
  5546. * to handle parse events. If sax is NULL, fallback to the default DOM
  5547. * behavior and return a tree.
  5548. *
  5549. * Returns the resulting document tree unless SAX is NULL or the document is
  5550. * not well formed.
  5551. */
  5552. htmlDocPtr
  5553. htmlSAXParseDoc(const xmlChar *cur, const char *encoding,
  5554. htmlSAXHandlerPtr sax, void *userData) {
  5555. htmlDocPtr ret;
  5556. htmlParserCtxtPtr ctxt;
  5557. xmlInitParser();
  5558. if (cur == NULL) return(NULL);
  5559. ctxt = htmlCreateDocParserCtxt(cur, encoding);
  5560. if (ctxt == NULL) return(NULL);
  5561. if (sax != NULL) {
  5562. if (ctxt->sax != NULL) xmlFree (ctxt->sax);
  5563. ctxt->sax = sax;
  5564. ctxt->userData = userData;
  5565. }
  5566. htmlParseDocument(ctxt);
  5567. ret = ctxt->myDoc;
  5568. if (sax != NULL) {
  5569. ctxt->sax = NULL;
  5570. ctxt->userData = NULL;
  5571. }
  5572. htmlFreeParserCtxt(ctxt);
  5573. return(ret);
  5574. }
  5575. /**
  5576. * htmlParseDoc:
  5577. * @cur: a pointer to an array of xmlChar
  5578. * @encoding: a free form C string describing the HTML document encoding, or NULL
  5579. *
  5580. * parse an HTML in-memory document and build a tree.
  5581. *
  5582. * Returns the resulting document tree
  5583. */
  5584. htmlDocPtr
  5585. htmlParseDoc(const xmlChar *cur, const char *encoding) {
  5586. return(htmlSAXParseDoc(cur, encoding, NULL, NULL));
  5587. }
  5588. /**
  5589. * htmlCreateFileParserCtxt:
  5590. * @filename: the filename
  5591. * @encoding: a free form C string describing the HTML document encoding, or NULL
  5592. *
  5593. * Create a parser context for a file content.
  5594. * Automatic support for ZLIB/Compress compressed document is provided
  5595. * by default if found at compile-time.
  5596. *
  5597. * Returns the new parser context or NULL
  5598. */
  5599. htmlParserCtxtPtr
  5600. htmlCreateFileParserCtxt(const char *filename, const char *encoding)
  5601. {
  5602. htmlParserCtxtPtr ctxt;
  5603. htmlParserInputPtr inputStream;
  5604. char *canonicFilename;
  5605. if (filename == NULL)
  5606. return(NULL);
  5607. ctxt = htmlNewParserCtxt();
  5608. if (ctxt == NULL) {
  5609. return(NULL);
  5610. }
  5611. canonicFilename = (char *) xmlCanonicPath((const xmlChar *) filename);
  5612. if (canonicFilename == NULL) {
  5613. xmlFreeParserCtxt(ctxt);
  5614. return(NULL);
  5615. }
  5616. inputStream = xmlLoadExternalEntity(canonicFilename, NULL, ctxt);
  5617. xmlFree(canonicFilename);
  5618. if (inputStream == NULL) {
  5619. xmlFreeParserCtxt(ctxt);
  5620. return(NULL);
  5621. }
  5622. inputPush(ctxt, inputStream);
  5623. /* set encoding */
  5624. if (encoding) {
  5625. xmlCharEncodingHandlerPtr hdlr;
  5626. hdlr = xmlFindCharEncodingHandler(encoding);
  5627. if (hdlr != NULL) {
  5628. xmlSwitchToEncoding(ctxt, hdlr);
  5629. }
  5630. }
  5631. return(ctxt);
  5632. }
  5633. /**
  5634. * htmlSAXParseFile:
  5635. * @filename: the filename
  5636. * @encoding: a free form C string describing the HTML document encoding, or NULL
  5637. * @sax: the SAX handler block
  5638. * @userData: if using SAX, this pointer will be provided on callbacks.
  5639. *
  5640. * DEPRECATED: Use htmlNewSAXParserCtxt and htmlCtxtReadFile.
  5641. *
  5642. * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
  5643. * compressed document is provided by default if found at compile-time.
  5644. * It use the given SAX function block to handle the parsing callback.
  5645. * If sax is NULL, fallback to the default DOM tree building routines.
  5646. *
  5647. * Returns the resulting document tree unless SAX is NULL or the document is
  5648. * not well formed.
  5649. */
  5650. htmlDocPtr
  5651. htmlSAXParseFile(const char *filename, const char *encoding, htmlSAXHandlerPtr sax,
  5652. void *userData) {
  5653. htmlDocPtr ret;
  5654. htmlParserCtxtPtr ctxt;
  5655. htmlSAXHandlerPtr oldsax = NULL;
  5656. xmlInitParser();
  5657. ctxt = htmlCreateFileParserCtxt(filename, encoding);
  5658. if (ctxt == NULL) return(NULL);
  5659. if (sax != NULL) {
  5660. oldsax = ctxt->sax;
  5661. ctxt->sax = sax;
  5662. ctxt->userData = userData;
  5663. }
  5664. htmlParseDocument(ctxt);
  5665. ret = ctxt->myDoc;
  5666. if (sax != NULL) {
  5667. ctxt->sax = oldsax;
  5668. ctxt->userData = NULL;
  5669. }
  5670. htmlFreeParserCtxt(ctxt);
  5671. return(ret);
  5672. }
  5673. /**
  5674. * htmlParseFile:
  5675. * @filename: the filename
  5676. * @encoding: a free form C string describing the HTML document encoding, or NULL
  5677. *
  5678. * parse an HTML file and build a tree. Automatic support for ZLIB/Compress
  5679. * compressed document is provided by default if found at compile-time.
  5680. *
  5681. * Returns the resulting document tree
  5682. */
  5683. htmlDocPtr
  5684. htmlParseFile(const char *filename, const char *encoding) {
  5685. return(htmlSAXParseFile(filename, encoding, NULL, NULL));
  5686. }
  5687. /**
  5688. * htmlHandleOmittedElem:
  5689. * @val: int 0 or 1
  5690. *
  5691. * Set and return the previous value for handling HTML omitted tags.
  5692. *
  5693. * Returns the last value for 0 for no handling, 1 for auto insertion.
  5694. */
  5695. int
  5696. htmlHandleOmittedElem(int val) {
  5697. int old = htmlOmittedDefaultValue;
  5698. htmlOmittedDefaultValue = val;
  5699. return(old);
  5700. }
  5701. /**
  5702. * htmlElementAllowedHere:
  5703. * @parent: HTML parent element
  5704. * @elt: HTML element
  5705. *
  5706. * Checks whether an HTML element may be a direct child of a parent element.
  5707. * Note - doesn't check for deprecated elements
  5708. *
  5709. * Returns 1 if allowed; 0 otherwise.
  5710. */
  5711. int
  5712. htmlElementAllowedHere(const htmlElemDesc* parent, const xmlChar* elt) {
  5713. const char** p ;
  5714. if ( ! elt || ! parent || ! parent->subelts )
  5715. return 0 ;
  5716. for ( p = parent->subelts; *p; ++p )
  5717. if ( !xmlStrcmp((const xmlChar *)*p, elt) )
  5718. return 1 ;
  5719. return 0 ;
  5720. }
  5721. /**
  5722. * htmlElementStatusHere:
  5723. * @parent: HTML parent element
  5724. * @elt: HTML element
  5725. *
  5726. * Checks whether an HTML element may be a direct child of a parent element.
  5727. * and if so whether it is valid or deprecated.
  5728. *
  5729. * Returns one of HTML_VALID, HTML_DEPRECATED, HTML_INVALID
  5730. */
  5731. htmlStatus
  5732. htmlElementStatusHere(const htmlElemDesc* parent, const htmlElemDesc* elt) {
  5733. if ( ! parent || ! elt )
  5734. return HTML_INVALID ;
  5735. if ( ! htmlElementAllowedHere(parent, (const xmlChar*) elt->name ) )
  5736. return HTML_INVALID ;
  5737. return ( elt->dtd == 0 ) ? HTML_VALID : HTML_DEPRECATED ;
  5738. }
  5739. /**
  5740. * htmlAttrAllowed:
  5741. * @elt: HTML element
  5742. * @attr: HTML attribute
  5743. * @legacy: whether to allow deprecated attributes
  5744. *
  5745. * Checks whether an attribute is valid for an element
  5746. * Has full knowledge of Required and Deprecated attributes
  5747. *
  5748. * Returns one of HTML_REQUIRED, HTML_VALID, HTML_DEPRECATED, HTML_INVALID
  5749. */
  5750. htmlStatus
  5751. htmlAttrAllowed(const htmlElemDesc* elt, const xmlChar* attr, int legacy) {
  5752. const char** p ;
  5753. if ( !elt || ! attr )
  5754. return HTML_INVALID ;
  5755. if ( elt->attrs_req )
  5756. for ( p = elt->attrs_req; *p; ++p)
  5757. if ( !xmlStrcmp((const xmlChar*)*p, attr) )
  5758. return HTML_REQUIRED ;
  5759. if ( elt->attrs_opt )
  5760. for ( p = elt->attrs_opt; *p; ++p)
  5761. if ( !xmlStrcmp((const xmlChar*)*p, attr) )
  5762. return HTML_VALID ;
  5763. if ( legacy && elt->attrs_depr )
  5764. for ( p = elt->attrs_depr; *p; ++p)
  5765. if ( !xmlStrcmp((const xmlChar*)*p, attr) )
  5766. return HTML_DEPRECATED ;
  5767. return HTML_INVALID ;
  5768. }
  5769. /**
  5770. * htmlNodeStatus:
  5771. * @node: an htmlNodePtr in a tree
  5772. * @legacy: whether to allow deprecated elements (YES is faster here
  5773. * for Element nodes)
  5774. *
  5775. * Checks whether the tree node is valid. Experimental (the author
  5776. * only uses the HTML enhancements in a SAX parser)
  5777. *
  5778. * Return: for Element nodes, a return from htmlElementAllowedHere (if
  5779. * legacy allowed) or htmlElementStatusHere (otherwise).
  5780. * for Attribute nodes, a return from htmlAttrAllowed
  5781. * for other nodes, HTML_NA (no checks performed)
  5782. */
  5783. htmlStatus
  5784. htmlNodeStatus(const htmlNodePtr node, int legacy) {
  5785. if ( ! node )
  5786. return HTML_INVALID ;
  5787. switch ( node->type ) {
  5788. case XML_ELEMENT_NODE:
  5789. return legacy
  5790. ? ( htmlElementAllowedHere (
  5791. htmlTagLookup(node->parent->name) , node->name
  5792. ) ? HTML_VALID : HTML_INVALID )
  5793. : htmlElementStatusHere(
  5794. htmlTagLookup(node->parent->name) ,
  5795. htmlTagLookup(node->name) )
  5796. ;
  5797. case XML_ATTRIBUTE_NODE:
  5798. return htmlAttrAllowed(
  5799. htmlTagLookup(node->parent->name) , node->name, legacy) ;
  5800. default: return HTML_NA ;
  5801. }
  5802. }
  5803. /************************************************************************
  5804. * *
  5805. * New set (2.6.0) of simpler and more flexible APIs *
  5806. * *
  5807. ************************************************************************/
  5808. /**
  5809. * DICT_FREE:
  5810. * @str: a string
  5811. *
  5812. * Free a string if it is not owned by the "dict" dictionary in the
  5813. * current scope
  5814. */
  5815. #define DICT_FREE(str) \
  5816. if ((str) && ((!dict) || \
  5817. (xmlDictOwns(dict, (const xmlChar *)(str)) == 0))) \
  5818. xmlFree((char *)(str));
  5819. /**
  5820. * htmlCtxtReset:
  5821. * @ctxt: an HTML parser context
  5822. *
  5823. * Reset a parser context
  5824. */
  5825. void
  5826. htmlCtxtReset(htmlParserCtxtPtr ctxt)
  5827. {
  5828. xmlParserInputPtr input;
  5829. xmlDictPtr dict;
  5830. if (ctxt == NULL)
  5831. return;
  5832. xmlInitParser();
  5833. dict = ctxt->dict;
  5834. while ((input = inputPop(ctxt)) != NULL) { /* Non consuming */
  5835. xmlFreeInputStream(input);
  5836. }
  5837. ctxt->inputNr = 0;
  5838. ctxt->input = NULL;
  5839. ctxt->spaceNr = 0;
  5840. if (ctxt->spaceTab != NULL) {
  5841. ctxt->spaceTab[0] = -1;
  5842. ctxt->space = &ctxt->spaceTab[0];
  5843. } else {
  5844. ctxt->space = NULL;
  5845. }
  5846. ctxt->nodeNr = 0;
  5847. ctxt->node = NULL;
  5848. ctxt->nameNr = 0;
  5849. ctxt->name = NULL;
  5850. ctxt->nsNr = 0;
  5851. DICT_FREE(ctxt->version);
  5852. ctxt->version = NULL;
  5853. DICT_FREE(ctxt->encoding);
  5854. ctxt->encoding = NULL;
  5855. DICT_FREE(ctxt->directory);
  5856. ctxt->directory = NULL;
  5857. DICT_FREE(ctxt->extSubURI);
  5858. ctxt->extSubURI = NULL;
  5859. DICT_FREE(ctxt->extSubSystem);
  5860. ctxt->extSubSystem = NULL;
  5861. if (ctxt->myDoc != NULL)
  5862. xmlFreeDoc(ctxt->myDoc);
  5863. ctxt->myDoc = NULL;
  5864. ctxt->standalone = -1;
  5865. ctxt->hasExternalSubset = 0;
  5866. ctxt->hasPErefs = 0;
  5867. ctxt->html = 1;
  5868. ctxt->external = 0;
  5869. ctxt->instate = XML_PARSER_START;
  5870. ctxt->token = 0;
  5871. ctxt->wellFormed = 1;
  5872. ctxt->nsWellFormed = 1;
  5873. ctxt->disableSAX = 0;
  5874. ctxt->valid = 1;
  5875. ctxt->vctxt.userData = ctxt;
  5876. ctxt->vctxt.flags = XML_VCTXT_USE_PCTXT;
  5877. ctxt->vctxt.error = xmlParserValidityError;
  5878. ctxt->vctxt.warning = xmlParserValidityWarning;
  5879. ctxt->record_info = 0;
  5880. ctxt->checkIndex = 0;
  5881. ctxt->endCheckState = 0;
  5882. ctxt->inSubset = 0;
  5883. ctxt->errNo = XML_ERR_OK;
  5884. ctxt->depth = 0;
  5885. ctxt->catalogs = NULL;
  5886. xmlInitNodeInfoSeq(&ctxt->node_seq);
  5887. if (ctxt->attsDefault != NULL) {
  5888. xmlHashFree(ctxt->attsDefault, xmlHashDefaultDeallocator);
  5889. ctxt->attsDefault = NULL;
  5890. }
  5891. if (ctxt->attsSpecial != NULL) {
  5892. xmlHashFree(ctxt->attsSpecial, NULL);
  5893. ctxt->attsSpecial = NULL;
  5894. }
  5895. ctxt->nbErrors = 0;
  5896. ctxt->nbWarnings = 0;
  5897. if (ctxt->lastError.code != XML_ERR_OK)
  5898. xmlResetError(&ctxt->lastError);
  5899. }
  5900. /**
  5901. * htmlCtxtUseOptions:
  5902. * @ctxt: an HTML parser context
  5903. * @options: a combination of htmlParserOption(s)
  5904. *
  5905. * Applies the options to the parser context
  5906. *
  5907. * Returns 0 in case of success, the set of unknown or unimplemented options
  5908. * in case of error.
  5909. */
  5910. int
  5911. htmlCtxtUseOptions(htmlParserCtxtPtr ctxt, int options)
  5912. {
  5913. if (ctxt == NULL)
  5914. return(-1);
  5915. if (options & HTML_PARSE_NOWARNING) {
  5916. ctxt->sax->warning = NULL;
  5917. ctxt->vctxt.warning = NULL;
  5918. options -= XML_PARSE_NOWARNING;
  5919. ctxt->options |= XML_PARSE_NOWARNING;
  5920. }
  5921. if (options & HTML_PARSE_NOERROR) {
  5922. ctxt->sax->error = NULL;
  5923. ctxt->vctxt.error = NULL;
  5924. ctxt->sax->fatalError = NULL;
  5925. options -= XML_PARSE_NOERROR;
  5926. ctxt->options |= XML_PARSE_NOERROR;
  5927. }
  5928. if (options & HTML_PARSE_PEDANTIC) {
  5929. ctxt->pedantic = 1;
  5930. options -= XML_PARSE_PEDANTIC;
  5931. ctxt->options |= XML_PARSE_PEDANTIC;
  5932. } else
  5933. ctxt->pedantic = 0;
  5934. if (options & XML_PARSE_NOBLANKS) {
  5935. ctxt->keepBlanks = 0;
  5936. ctxt->sax->ignorableWhitespace = xmlSAX2IgnorableWhitespace;
  5937. options -= XML_PARSE_NOBLANKS;
  5938. ctxt->options |= XML_PARSE_NOBLANKS;
  5939. } else
  5940. ctxt->keepBlanks = 1;
  5941. if (options & HTML_PARSE_RECOVER) {
  5942. ctxt->recovery = 1;
  5943. options -= HTML_PARSE_RECOVER;
  5944. } else
  5945. ctxt->recovery = 0;
  5946. if (options & HTML_PARSE_COMPACT) {
  5947. ctxt->options |= HTML_PARSE_COMPACT;
  5948. options -= HTML_PARSE_COMPACT;
  5949. }
  5950. if (options & XML_PARSE_HUGE) {
  5951. ctxt->options |= XML_PARSE_HUGE;
  5952. options -= XML_PARSE_HUGE;
  5953. }
  5954. if (options & HTML_PARSE_NODEFDTD) {
  5955. ctxt->options |= HTML_PARSE_NODEFDTD;
  5956. options -= HTML_PARSE_NODEFDTD;
  5957. }
  5958. if (options & HTML_PARSE_IGNORE_ENC) {
  5959. ctxt->options |= HTML_PARSE_IGNORE_ENC;
  5960. options -= HTML_PARSE_IGNORE_ENC;
  5961. }
  5962. if (options & HTML_PARSE_NOIMPLIED) {
  5963. ctxt->options |= HTML_PARSE_NOIMPLIED;
  5964. options -= HTML_PARSE_NOIMPLIED;
  5965. }
  5966. ctxt->dictNames = 0;
  5967. ctxt->linenumbers = 1;
  5968. return (options);
  5969. }
  5970. /**
  5971. * htmlDoRead:
  5972. * @ctxt: an HTML parser context
  5973. * @URL: the base URL to use for the document
  5974. * @encoding: the document encoding, or NULL
  5975. * @options: a combination of htmlParserOption(s)
  5976. * @reuse: keep the context for reuse
  5977. *
  5978. * Common front-end for the htmlRead functions
  5979. *
  5980. * Returns the resulting document tree or NULL
  5981. */
  5982. static htmlDocPtr
  5983. htmlDoRead(htmlParserCtxtPtr ctxt, const char *URL, const char *encoding,
  5984. int options, int reuse)
  5985. {
  5986. htmlDocPtr ret;
  5987. htmlCtxtUseOptions(ctxt, options);
  5988. ctxt->html = 1;
  5989. if (encoding != NULL) {
  5990. xmlCharEncodingHandlerPtr hdlr;
  5991. hdlr = xmlFindCharEncodingHandler(encoding);
  5992. if (hdlr != NULL) {
  5993. xmlSwitchToEncoding(ctxt, hdlr);
  5994. }
  5995. }
  5996. if ((URL != NULL) && (ctxt->input != NULL) &&
  5997. (ctxt->input->filename == NULL))
  5998. ctxt->input->filename = (char *) xmlStrdup((const xmlChar *) URL);
  5999. htmlParseDocument(ctxt);
  6000. ret = ctxt->myDoc;
  6001. ctxt->myDoc = NULL;
  6002. if (!reuse) {
  6003. if ((ctxt->dictNames) &&
  6004. (ret != NULL) &&
  6005. (ret->dict == ctxt->dict))
  6006. ctxt->dict = NULL;
  6007. xmlFreeParserCtxt(ctxt);
  6008. }
  6009. return (ret);
  6010. }
  6011. /**
  6012. * htmlReadDoc:
  6013. * @cur: a pointer to a zero terminated string
  6014. * @URL: the base URL to use for the document
  6015. * @encoding: the document encoding, or NULL
  6016. * @options: a combination of htmlParserOption(s)
  6017. *
  6018. * parse an XML in-memory document and build a tree.
  6019. *
  6020. * Returns the resulting document tree
  6021. */
  6022. htmlDocPtr
  6023. htmlReadDoc(const xmlChar * cur, const char *URL, const char *encoding, int options)
  6024. {
  6025. htmlParserCtxtPtr ctxt;
  6026. if (cur == NULL)
  6027. return (NULL);
  6028. xmlInitParser();
  6029. ctxt = htmlCreateDocParserCtxt(cur, NULL);
  6030. if (ctxt == NULL)
  6031. return (NULL);
  6032. return (htmlDoRead(ctxt, URL, encoding, options, 0));
  6033. }
  6034. /**
  6035. * htmlReadFile:
  6036. * @filename: a file or URL
  6037. * @encoding: the document encoding, or NULL
  6038. * @options: a combination of htmlParserOption(s)
  6039. *
  6040. * parse an XML file from the filesystem or the network.
  6041. *
  6042. * Returns the resulting document tree
  6043. */
  6044. htmlDocPtr
  6045. htmlReadFile(const char *filename, const char *encoding, int options)
  6046. {
  6047. htmlParserCtxtPtr ctxt;
  6048. xmlInitParser();
  6049. ctxt = htmlCreateFileParserCtxt(filename, encoding);
  6050. if (ctxt == NULL)
  6051. return (NULL);
  6052. return (htmlDoRead(ctxt, NULL, NULL, options, 0));
  6053. }
  6054. /**
  6055. * htmlReadMemory:
  6056. * @buffer: a pointer to a char array
  6057. * @size: the size of the array
  6058. * @URL: the base URL to use for the document
  6059. * @encoding: the document encoding, or NULL
  6060. * @options: a combination of htmlParserOption(s)
  6061. *
  6062. * parse an XML in-memory document and build a tree.
  6063. *
  6064. * Returns the resulting document tree
  6065. */
  6066. htmlDocPtr
  6067. htmlReadMemory(const char *buffer, int size, const char *URL, const char *encoding, int options)
  6068. {
  6069. htmlParserCtxtPtr ctxt;
  6070. xmlInitParser();
  6071. ctxt = htmlCreateMemoryParserCtxt(buffer, size);
  6072. if (ctxt == NULL)
  6073. return (NULL);
  6074. return (htmlDoRead(ctxt, URL, encoding, options, 0));
  6075. }
  6076. /**
  6077. * htmlReadFd:
  6078. * @fd: an open file descriptor
  6079. * @URL: the base URL to use for the document
  6080. * @encoding: the document encoding, or NULL
  6081. * @options: a combination of htmlParserOption(s)
  6082. *
  6083. * parse an HTML from a file descriptor and build a tree.
  6084. * NOTE that the file descriptor will not be closed when the
  6085. * reader is closed or reset.
  6086. *
  6087. * Returns the resulting document tree
  6088. */
  6089. htmlDocPtr
  6090. htmlReadFd(int fd, const char *URL, const char *encoding, int options)
  6091. {
  6092. htmlParserCtxtPtr ctxt;
  6093. xmlParserInputBufferPtr input;
  6094. htmlParserInputPtr stream;
  6095. if (fd < 0)
  6096. return (NULL);
  6097. xmlInitParser();
  6098. input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
  6099. if (input == NULL)
  6100. return (NULL);
  6101. input->closecallback = NULL;
  6102. ctxt = htmlNewParserCtxt();
  6103. if (ctxt == NULL) {
  6104. xmlFreeParserInputBuffer(input);
  6105. return (NULL);
  6106. }
  6107. stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
  6108. if (stream == NULL) {
  6109. xmlFreeParserInputBuffer(input);
  6110. htmlFreeParserCtxt(ctxt);
  6111. return (NULL);
  6112. }
  6113. inputPush(ctxt, stream);
  6114. return (htmlDoRead(ctxt, URL, encoding, options, 0));
  6115. }
  6116. /**
  6117. * htmlReadIO:
  6118. * @ioread: an I/O read function
  6119. * @ioclose: an I/O close function
  6120. * @ioctx: an I/O handler
  6121. * @URL: the base URL to use for the document
  6122. * @encoding: the document encoding, or NULL
  6123. * @options: a combination of htmlParserOption(s)
  6124. *
  6125. * parse an HTML document from I/O functions and source and build a tree.
  6126. *
  6127. * Returns the resulting document tree
  6128. */
  6129. htmlDocPtr
  6130. htmlReadIO(xmlInputReadCallback ioread, xmlInputCloseCallback ioclose,
  6131. void *ioctx, const char *URL, const char *encoding, int options)
  6132. {
  6133. htmlParserCtxtPtr ctxt;
  6134. xmlParserInputBufferPtr input;
  6135. xmlParserInputPtr stream;
  6136. if (ioread == NULL)
  6137. return (NULL);
  6138. xmlInitParser();
  6139. input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
  6140. XML_CHAR_ENCODING_NONE);
  6141. if (input == NULL) {
  6142. if (ioclose != NULL)
  6143. ioclose(ioctx);
  6144. return (NULL);
  6145. }
  6146. ctxt = htmlNewParserCtxt();
  6147. if (ctxt == NULL) {
  6148. xmlFreeParserInputBuffer(input);
  6149. return (NULL);
  6150. }
  6151. stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
  6152. if (stream == NULL) {
  6153. xmlFreeParserInputBuffer(input);
  6154. xmlFreeParserCtxt(ctxt);
  6155. return (NULL);
  6156. }
  6157. inputPush(ctxt, stream);
  6158. return (htmlDoRead(ctxt, URL, encoding, options, 0));
  6159. }
  6160. /**
  6161. * htmlCtxtReadDoc:
  6162. * @ctxt: an HTML parser context
  6163. * @str: a pointer to a zero terminated string
  6164. * @URL: the base URL to use for the document
  6165. * @encoding: the document encoding, or NULL
  6166. * @options: a combination of htmlParserOption(s)
  6167. *
  6168. * parse an XML in-memory document and build a tree.
  6169. * This reuses the existing @ctxt parser context
  6170. *
  6171. * Returns the resulting document tree
  6172. */
  6173. htmlDocPtr
  6174. htmlCtxtReadDoc(htmlParserCtxtPtr ctxt, const xmlChar *str,
  6175. const char *URL, const char *encoding, int options)
  6176. {
  6177. xmlParserInputBufferPtr input;
  6178. xmlParserInputPtr stream;
  6179. if (ctxt == NULL)
  6180. return (NULL);
  6181. if (str == NULL)
  6182. return (NULL);
  6183. xmlInitParser();
  6184. htmlCtxtReset(ctxt);
  6185. input = xmlParserInputBufferCreateString(str);
  6186. if (input == NULL) {
  6187. return(NULL);
  6188. }
  6189. stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
  6190. if (stream == NULL) {
  6191. xmlFreeParserInputBuffer(input);
  6192. return(NULL);
  6193. }
  6194. inputPush(ctxt, stream);
  6195. return (htmlDoRead(ctxt, URL, encoding, options, 1));
  6196. }
  6197. /**
  6198. * htmlCtxtReadFile:
  6199. * @ctxt: an HTML parser context
  6200. * @filename: a file or URL
  6201. * @encoding: the document encoding, or NULL
  6202. * @options: a combination of htmlParserOption(s)
  6203. *
  6204. * parse an XML file from the filesystem or the network.
  6205. * This reuses the existing @ctxt parser context
  6206. *
  6207. * Returns the resulting document tree
  6208. */
  6209. htmlDocPtr
  6210. htmlCtxtReadFile(htmlParserCtxtPtr ctxt, const char *filename,
  6211. const char *encoding, int options)
  6212. {
  6213. xmlParserInputPtr stream;
  6214. if (filename == NULL)
  6215. return (NULL);
  6216. if (ctxt == NULL)
  6217. return (NULL);
  6218. xmlInitParser();
  6219. htmlCtxtReset(ctxt);
  6220. stream = xmlLoadExternalEntity(filename, NULL, ctxt);
  6221. if (stream == NULL) {
  6222. return (NULL);
  6223. }
  6224. inputPush(ctxt, stream);
  6225. return (htmlDoRead(ctxt, NULL, encoding, options, 1));
  6226. }
  6227. /**
  6228. * htmlCtxtReadMemory:
  6229. * @ctxt: an HTML parser context
  6230. * @buffer: a pointer to a char array
  6231. * @size: the size of the array
  6232. * @URL: the base URL to use for the document
  6233. * @encoding: the document encoding, or NULL
  6234. * @options: a combination of htmlParserOption(s)
  6235. *
  6236. * parse an XML in-memory document and build a tree.
  6237. * This reuses the existing @ctxt parser context
  6238. *
  6239. * Returns the resulting document tree
  6240. */
  6241. htmlDocPtr
  6242. htmlCtxtReadMemory(htmlParserCtxtPtr ctxt, const char *buffer, int size,
  6243. const char *URL, const char *encoding, int options)
  6244. {
  6245. xmlParserInputBufferPtr input;
  6246. xmlParserInputPtr stream;
  6247. if (ctxt == NULL)
  6248. return (NULL);
  6249. if (buffer == NULL)
  6250. return (NULL);
  6251. xmlInitParser();
  6252. htmlCtxtReset(ctxt);
  6253. input = xmlParserInputBufferCreateStatic(buffer, size,
  6254. XML_CHAR_ENCODING_NONE);
  6255. if (input == NULL) {
  6256. return(NULL);
  6257. }
  6258. stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
  6259. if (stream == NULL) {
  6260. xmlFreeParserInputBuffer(input);
  6261. return(NULL);
  6262. }
  6263. inputPush(ctxt, stream);
  6264. return (htmlDoRead(ctxt, URL, encoding, options, 1));
  6265. }
  6266. /**
  6267. * htmlCtxtReadFd:
  6268. * @ctxt: an HTML parser context
  6269. * @fd: an open file descriptor
  6270. * @URL: the base URL to use for the document
  6271. * @encoding: the document encoding, or NULL
  6272. * @options: a combination of htmlParserOption(s)
  6273. *
  6274. * parse an XML from a file descriptor and build a tree.
  6275. * This reuses the existing @ctxt parser context
  6276. *
  6277. * Returns the resulting document tree
  6278. */
  6279. htmlDocPtr
  6280. htmlCtxtReadFd(htmlParserCtxtPtr ctxt, int fd,
  6281. const char *URL, const char *encoding, int options)
  6282. {
  6283. xmlParserInputBufferPtr input;
  6284. xmlParserInputPtr stream;
  6285. if (fd < 0)
  6286. return (NULL);
  6287. if (ctxt == NULL)
  6288. return (NULL);
  6289. xmlInitParser();
  6290. htmlCtxtReset(ctxt);
  6291. input = xmlParserInputBufferCreateFd(fd, XML_CHAR_ENCODING_NONE);
  6292. if (input == NULL)
  6293. return (NULL);
  6294. stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
  6295. if (stream == NULL) {
  6296. xmlFreeParserInputBuffer(input);
  6297. return (NULL);
  6298. }
  6299. inputPush(ctxt, stream);
  6300. return (htmlDoRead(ctxt, URL, encoding, options, 1));
  6301. }
  6302. /**
  6303. * htmlCtxtReadIO:
  6304. * @ctxt: an HTML parser context
  6305. * @ioread: an I/O read function
  6306. * @ioclose: an I/O close function
  6307. * @ioctx: an I/O handler
  6308. * @URL: the base URL to use for the document
  6309. * @encoding: the document encoding, or NULL
  6310. * @options: a combination of htmlParserOption(s)
  6311. *
  6312. * parse an HTML document from I/O functions and source and build a tree.
  6313. * This reuses the existing @ctxt parser context
  6314. *
  6315. * Returns the resulting document tree
  6316. */
  6317. htmlDocPtr
  6318. htmlCtxtReadIO(htmlParserCtxtPtr ctxt, xmlInputReadCallback ioread,
  6319. xmlInputCloseCallback ioclose, void *ioctx,
  6320. const char *URL,
  6321. const char *encoding, int options)
  6322. {
  6323. xmlParserInputBufferPtr input;
  6324. xmlParserInputPtr stream;
  6325. if (ioread == NULL)
  6326. return (NULL);
  6327. if (ctxt == NULL)
  6328. return (NULL);
  6329. xmlInitParser();
  6330. htmlCtxtReset(ctxt);
  6331. input = xmlParserInputBufferCreateIO(ioread, ioclose, ioctx,
  6332. XML_CHAR_ENCODING_NONE);
  6333. if (input == NULL) {
  6334. if (ioclose != NULL)
  6335. ioclose(ioctx);
  6336. return (NULL);
  6337. }
  6338. stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE);
  6339. if (stream == NULL) {
  6340. xmlFreeParserInputBuffer(input);
  6341. return (NULL);
  6342. }
  6343. inputPush(ctxt, stream);
  6344. return (htmlDoRead(ctxt, URL, encoding, options, 1));
  6345. }
  6346. #endif /* LIBXML_HTML_ENABLED */